You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasicRNNCell.cs 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using Tensorflow.Keras.Engine;
  15. using static Tensorflow.Binding;
  16. namespace Tensorflow
  17. {
  18. public class BasicRnnCell : LayerRnnCell
  19. {
  20. int _num_units;
  21. Func<Tensor, string, Tensor> _activation;
  22. public override int state_size => _num_units;
  23. public override int output_size => _num_units;
  24. public VariableV1 _kernel;
  25. string _WEIGHTS_VARIABLE_NAME = "kernel";
  26. public VariableV1 _bias;
  27. string _BIAS_VARIABLE_NAME = "bias";
  28. public BasicRnnCell(int num_units,
  29. Func<Tensor, string, Tensor> activation = null,
  30. bool? reuse = null,
  31. string name = null,
  32. TF_DataType dtype = TF_DataType.DtInvalid) : base(_reuse: reuse,
  33. name: name,
  34. dtype: dtype)
  35. {
  36. // Inputs must be 2-dimensional.
  37. input_spec = new InputSpec(ndim: 2);
  38. _num_units = num_units;
  39. if (activation == null)
  40. _activation = math_ops.tanh;
  41. else
  42. _activation = activation;
  43. }
  44. protected override void build(TensorShape inputs_shape)
  45. {
  46. var input_depth = inputs_shape.dims[inputs_shape.ndim - 1];
  47. _kernel = add_weight(
  48. _WEIGHTS_VARIABLE_NAME,
  49. shape: new[] { input_depth + _num_units, _num_units });
  50. _bias = add_weight(
  51. _BIAS_VARIABLE_NAME,
  52. shape: new[] { _num_units },
  53. initializer: tf.zeros_initializer);
  54. built = true;
  55. }
  56. protected override (Tensor, Tensor) call(Tensor inputs, Tensor training = null, Tensor state = null)
  57. {
  58. // Most basic RNN: output = new_state = act(W * input + U * state + B).
  59. var concat = array_ops.concat(new[] { inputs, state }, 1);
  60. var gate_inputs = math_ops.matmul(concat, _kernel as RefVariable);
  61. return (inputs, inputs);
  62. }
  63. }
  64. }