|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Tensorflow.Keras.ArgsDefinition; |
| 5 | +using Tensorflow.Common.Extensions; |
| 6 | +using Tensorflow.Common.Types; |
| 7 | +using Tensorflow.Keras.Saving; |
| 8 | + |
| 9 | + |
| 10 | +namespace Tensorflow.Keras.Layers |
| 11 | +{ |
| 12 | + public class GRU : RNN |
| 13 | + { |
| 14 | + GRUArgs _args; |
| 15 | + private static GRUCell _cell; |
| 16 | + |
| 17 | + bool _return_runtime; |
| 18 | + public GRUCell Cell { get => _cell; } |
| 19 | + public int units { get => _args.Units; } |
| 20 | + public Activation activation { get => _args.Activation; } |
| 21 | + public Activation recurrent_activation { get => _args.RecurrentActivation; } |
| 22 | + public bool use_bias { get => _args.UseBias; } |
| 23 | + public float dropout { get => _args.Dropout; } |
| 24 | + public float recurrent_dropout { get => _args.RecurrentDropout; } |
| 25 | + public IInitializer kernel_initializer { get => _args.KernelInitializer; } |
| 26 | + public IInitializer recurrent_initializer { get => _args.RecurrentInitializer; } |
| 27 | + public IInitializer bias_initializer { get => _args.BiasInitializer; } |
| 28 | + public int implementation { get => _args.Implementation; } |
| 29 | + public bool reset_after { get => _args.ResetAfter; } |
| 30 | + |
| 31 | + public GRU(GRUArgs args) : base(CreateCell(args), PreConstruct(args)) |
| 32 | + { |
| 33 | + _args = args; |
| 34 | + |
| 35 | + if (_args.Implementation == 0) |
| 36 | + { |
| 37 | + // Use the red output to act as a warning message that can also be used under the release version |
| 38 | + Console.ForegroundColor = ConsoleColor.Red; |
| 39 | + Console.WriteLine("Warning: `implementation=0` has been deprecated, "+ |
| 40 | + "and now defaults to `implementation=2`."+ |
| 41 | + "Please update your layer call."); |
| 42 | + Console.ResetColor(); |
| 43 | + } |
| 44 | + |
| 45 | + GRUCell cell = new GRUCell(new GRUCellArgs |
| 46 | + { |
| 47 | + Units = _args.Units, |
| 48 | + Activation = _args.Activation, |
| 49 | + RecurrentActivation = _args.RecurrentActivation, |
| 50 | + UseBias = _args.UseBias, |
| 51 | + Dropout = _args.Dropout, |
| 52 | + RecurrentDropout = _args.RecurrentDropout, |
| 53 | + KernelInitializer = _args.KernelInitializer, |
| 54 | + RecurrentInitializer = _args.RecurrentInitializer, |
| 55 | + BiasInitializer = _args.BiasInitializer, |
| 56 | + ResetAfter = _args.ResetAfter, |
| 57 | + Implementation = _args.Implementation |
| 58 | + }); |
| 59 | + _cell = cell; |
| 60 | + } |
| 61 | + |
| 62 | + protected override Tensors Call(Tensors inputs, Tensors initial_state = null, bool? training = null, IOptionalArgs? optional_args = null) |
| 63 | + { |
| 64 | + GRUOptionalArgs? gru_optional_args = optional_args as GRUOptionalArgs; |
| 65 | + if (optional_args is not null && gru_optional_args is null) |
| 66 | + { |
| 67 | + throw new ArgumentException("The type of optional args should be `GRUOptionalArgs`."); |
| 68 | + } |
| 69 | + Tensors? mask = gru_optional_args?.Mask; |
| 70 | + |
| 71 | + // Not support ragger input temporarily; |
| 72 | + int row_length = 0; |
| 73 | + bool is_ragged_input = false; |
| 74 | + |
| 75 | + _validate_args_if_ragged(is_ragged_input, mask); |
| 76 | + |
| 77 | + // GRU does not support constants.Ignore it during process. |
| 78 | + (inputs, initial_state, _) = this._process_inputs(inputs, initial_state, null); |
| 79 | + |
| 80 | + if (mask.Length > 1) |
| 81 | + { |
| 82 | + mask = mask[0]; |
| 83 | + } |
| 84 | + |
| 85 | + var input_shape = inputs.shape; |
| 86 | + var timesteps = _args.TimeMajor ? input_shape[0] : input_shape[1]; |
| 87 | + |
| 88 | + |
| 89 | + // TODO(Wanglongzhi2001), finish _could_use_gpu_kernel part |
| 90 | + Func<Tensors, Tensors, (Tensors, Tensors)> step = (cell_inputs, cell_states) => |
| 91 | + { |
| 92 | + var res = Cell.Apply(cell_inputs, cell_states, training is null ? true : training.Value); |
| 93 | + var (output, state) = res; |
| 94 | + return (output, state); |
| 95 | + }; |
| 96 | + |
| 97 | + var (last_output, outputs, states) = keras.backend.rnn( |
| 98 | + step, |
| 99 | + inputs, |
| 100 | + initial_state, |
| 101 | + constants: null, |
| 102 | + go_backwards: _args.GoBackwards, |
| 103 | + mask: mask, |
| 104 | + unroll: _args.Unroll, |
| 105 | + input_length: ops.convert_to_tensor(timesteps), |
| 106 | + time_major: _args.TimeMajor, |
| 107 | + zero_output_for_mask: base.Args.ZeroOutputForMask, |
| 108 | + return_all_outputs: _args.ReturnSequences |
| 109 | + ); |
| 110 | + |
| 111 | + Tensors output; |
| 112 | + if (_args.ReturnSequences) |
| 113 | + { |
| 114 | + output = outputs; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + output = last_output; |
| 119 | + } |
| 120 | + |
| 121 | + if (_args.ReturnState) |
| 122 | + { |
| 123 | + output = new Tensors { output, states }; |
| 124 | + } |
| 125 | + return output; |
| 126 | + } |
| 127 | + |
| 128 | + private static IRnnCell CreateCell(GRUArgs gruArgs) |
| 129 | + { |
| 130 | + return new GRUCell(new GRUCellArgs |
| 131 | + { |
| 132 | + Units = gruArgs.Units, |
| 133 | + Activation = gruArgs.Activation, |
| 134 | + RecurrentActivation = gruArgs.RecurrentActivation, |
| 135 | + UseBias = gruArgs.UseBias, |
| 136 | + Dropout = gruArgs.Dropout, |
| 137 | + RecurrentDropout = gruArgs.RecurrentDropout, |
| 138 | + KernelInitializer = gruArgs.KernelInitializer, |
| 139 | + RecurrentInitializer = gruArgs.RecurrentInitializer, |
| 140 | + BiasInitializer = gruArgs.BiasInitializer, |
| 141 | + ResetAfter = gruArgs.ResetAfter, |
| 142 | + Implementation = gruArgs.Implementation |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + private static RNNArgs PreConstruct(GRUArgs args) |
| 147 | + { |
| 148 | + return new RNNArgs |
| 149 | + { |
| 150 | + ReturnSequences = args.ReturnSequences, |
| 151 | + ReturnState = args.ReturnState, |
| 152 | + GoBackwards = args.GoBackwards, |
| 153 | + Stateful = args.Stateful, |
| 154 | + Unroll = args.Unroll, |
| 155 | + TimeMajor = args.TimeMajor, |
| 156 | + Units = args.Units, |
| 157 | + Activation = args.Activation, |
| 158 | + RecurrentActivation = args.RecurrentActivation, |
| 159 | + UseBias = args.UseBias, |
| 160 | + Dropout = args.Dropout, |
| 161 | + RecurrentDropout = args.RecurrentDropout, |
| 162 | + KernelInitializer = args.KernelInitializer, |
| 163 | + RecurrentInitializer = args.RecurrentInitializer, |
| 164 | + BiasInitializer = args.BiasInitializer |
| 165 | + }; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments