From e92aa44c1d0387e6496730e7a4c056e0e00ca8e1 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 17 Oct 2020 21:39:19 -0500 Subject: [PATCH] rename call to call_fn. --- .../Keras/ArgsDefinition/TensorFlowOpLayerArgs.cs | 4 +++- src/TensorFlowNET.Core/Keras/BackendImpl.cs | 4 ++-- src/TensorFlowNET.Core/Keras/Engine/Flatten.cs | 2 +- src/TensorFlowNET.Core/Keras/Engine/Functional.cs | 8 ++++++-- src/TensorFlowNET.Core/Keras/Engine/Layer.Apply.cs | 2 +- .../Keras/Engine/Layer.FunctionalConstructionCall.cs | 2 +- src/TensorFlowNET.Core/Keras/Engine/Layer.cs | 2 +- src/TensorFlowNET.Core/Keras/Engine/Node.cs | 2 -- src/TensorFlowNET.Core/Keras/Engine/TensorFlowOpLayer.cs | 4 ++-- src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/Convolutional.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/Dense.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/Dropout.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/Embedding.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/LSTM.cs | 4 ++-- src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/Rescaling.cs | 2 +- src/TensorFlowNET.Core/Keras/Layers/ZeroPadding2D.cs | 2 +- src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs | 7 ++++--- src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs | 2 +- src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs | 2 +- src/TensorFlowNET.Core/Sessions/_FetchHandler.cs | 3 +++ src/TensorFlowNET.Core/Tensorflow.Binding.csproj | 8 ++------ 23 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/TensorFlowOpLayerArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/TensorFlowOpLayerArgs.cs index 743a47d3..80bcff88 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/TensorFlowOpLayerArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/TensorFlowOpLayerArgs.cs @@ -1,4 +1,5 @@ -using System; +using NumSharp; +using System; using System.Collections.Generic; using System.Text; @@ -7,5 +8,6 @@ namespace Tensorflow.Keras.ArgsDefinition public class TensorFlowOpLayerArgs : LayerArgs { public NodeDef NodeDef { get; set; } + public Dictionary Constants { get; set; } } } diff --git a/src/TensorFlowNET.Core/Keras/BackendImpl.cs b/src/TensorFlowNET.Core/Keras/BackendImpl.cs index b765a48e..b88b1bb7 100644 --- a/src/TensorFlowNET.Core/Keras/BackendImpl.cs +++ b/src/TensorFlowNET.Core/Keras/BackendImpl.cs @@ -160,9 +160,9 @@ namespace Tensorflow.Keras /// /// /// - public Tensor eval_in_eager_or_function(Tensor outputs) + public NDArray eval_in_eager_or_function(Tensor outputs) { - throw new NotImplementedException(""); + return outputs.eval(); } public class _DummyEagerGraph diff --git a/src/TensorFlowNET.Core/Keras/Engine/Flatten.cs b/src/TensorFlowNET.Core/Keras/Engine/Flatten.cs index e6c2d9b0..c1163e15 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Flatten.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Flatten.cs @@ -21,7 +21,7 @@ namespace Tensorflow.Keras.Engine _channels_first = args.DataFormat == "channels_first"; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { if (_channels_first) { diff --git a/src/TensorFlowNET.Core/Keras/Engine/Functional.cs b/src/TensorFlowNET.Core/Keras/Engine/Functional.cs index 2b977dbc..af8e9114 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Functional.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Functional.cs @@ -69,10 +69,14 @@ namespace Tensorflow.Keras.Engine } } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { - return base.call(inputs, state, is_training); + return run_internal_graph(inputs, state, is_training); } + Tensors run_internal_graph(Tensors inputs, Tensor state = null, bool is_training = false) + { + throw new NotImplementedException(""); + } } } diff --git a/src/TensorFlowNET.Core/Keras/Engine/Layer.Apply.cs b/src/TensorFlowNET.Core/Keras/Engine/Layer.Apply.cs index b115e201..9513b26c 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Layer.Apply.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Layer.Apply.cs @@ -46,7 +46,7 @@ namespace Tensorflow.Keras.Engine if (!built) MaybeBuild(inputs); - outputs = call(inputs, state: state, is_training: is_training); + outputs = call_fn(inputs, state: state, is_training: is_training); outputs = _set_connectivity_metadata_(inputs, outputs); _handle_activity_regularization(inputs, outputs); diff --git a/src/TensorFlowNET.Core/Keras/Engine/Layer.FunctionalConstructionCall.cs b/src/TensorFlowNET.Core/Keras/Engine/Layer.FunctionalConstructionCall.cs index af24b6d4..14d61c31 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Layer.FunctionalConstructionCall.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Layer.FunctionalConstructionCall.cs @@ -42,7 +42,7 @@ namespace Tensorflow.Keras.Engine if (!dynamic) throw new NotImplementedException(""); - outputs = call(inputs); + outputs = call_fn(inputs); outputs = _set_connectivity_metadata_(inputs, outputs); _handle_activity_regularization(inputs, outputs); diff --git a/src/TensorFlowNET.Core/Keras/Engine/Layer.cs b/src/TensorFlowNET.Core/Keras/Engine/Layer.cs index 84e9e750..c88a2263 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Layer.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Layer.cs @@ -162,7 +162,7 @@ namespace Tensorflow.Keras.Engine /// /// /// - protected virtual Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected virtual Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { throw new NotImplementedException(""); } diff --git a/src/TensorFlowNET.Core/Keras/Engine/Node.cs b/src/TensorFlowNET.Core/Keras/Engine/Node.cs index 6c29850b..0ae84ac8 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Node.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Node.cs @@ -52,8 +52,6 @@ namespace Tensorflow.Keras.Engine layer.InboundNodes.Add(this); foreach (var kt in kerasInputs) { - if (kt.KerasHistory == null) - continue; var inbound_layer = kt.KerasHistory.layer; if (inbound_layer != null) inbound_layer.OutboundNodes.Add(this); diff --git a/src/TensorFlowNET.Core/Keras/Engine/TensorFlowOpLayer.cs b/src/TensorFlowNET.Core/Keras/Engine/TensorFlowOpLayer.cs index 8e9725fa..70a1458f 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/TensorFlowOpLayer.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/TensorFlowOpLayer.cs @@ -23,9 +23,9 @@ namespace Tensorflow.Keras.Engine built = true; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { - return base.call(inputs, state, is_training); + return base.call_fn(inputs, state, is_training); } } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs b/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs index c452d485..0f855915 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs @@ -119,7 +119,7 @@ namespace Tensorflow.Keras.Layers built = true; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { Tensor outputs = null; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Convolutional.cs b/src/TensorFlowNET.Core/Keras/Layers/Convolutional.cs index 43739c7e..7b358d75 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Convolutional.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Convolutional.cs @@ -98,7 +98,7 @@ namespace Tensorflow.Keras.Layers built = true; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool training = false) { var outputs = _convolution_op.Apply(inputs, kernel); if (use_bias) diff --git a/src/TensorFlowNET.Core/Keras/Layers/Dense.cs b/src/TensorFlowNET.Core/Keras/Layers/Dense.cs index 845cca2f..2bdda94c 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Dense.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Dense.cs @@ -65,7 +65,7 @@ namespace Tensorflow.Keras.Layers built = true; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool training = false) { Tensor outputs = null; var rank = inputs.rank; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Dropout.cs b/src/TensorFlowNET.Core/Keras/Layers/Dropout.cs index 057dc2c7..cd53a7a2 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Dropout.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Dropout.cs @@ -18,7 +18,7 @@ namespace Tensorflow.Keras.Layers this.args = args; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { var output = tf_utils.smart_cond(is_training, () => tf.nn.dropout(inputs, diff --git a/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs b/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs index 47752081..fddafbc9 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs @@ -62,7 +62,7 @@ namespace Tensorflow.Keras.Layers built = true; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { var dtype = inputs.dtype; if (dtype != tf.int32 && dtype != tf.int64) diff --git a/src/TensorFlowNET.Core/Keras/Layers/LSTM.cs b/src/TensorFlowNET.Core/Keras/Layers/LSTM.cs index d61f3faa..2ef11ed8 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/LSTM.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/LSTM.cs @@ -29,9 +29,9 @@ namespace Tensorflow.Keras.Layers .ToArray(); } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { - return base.call(inputs, state: state, is_training: is_training); + return base.call_fn(inputs, state: state, is_training: is_training); } } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs b/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs index 559fc982..1cccb598 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs @@ -36,7 +36,7 @@ namespace Tensorflow.Keras.Layers input_spec = new InputSpec(ndim: 4); } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { int[] pool_shape; int[] strides; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Rescaling.cs b/src/TensorFlowNET.Core/Keras/Layers/Rescaling.cs index 98352275..112f427e 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Rescaling.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Rescaling.cs @@ -20,7 +20,7 @@ namespace Tensorflow.Keras.Layers this.args = args; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { scale = math_ops.cast(args.Scale, args.DType); offset = math_ops.cast(args.Offset, args.DType); diff --git a/src/TensorFlowNET.Core/Keras/Layers/ZeroPadding2D.cs b/src/TensorFlowNET.Core/Keras/Layers/ZeroPadding2D.cs index 7e6d06a8..5b738426 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/ZeroPadding2D.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/ZeroPadding2D.cs @@ -29,7 +29,7 @@ namespace Tensorflow.Keras.Layers this.input_spec = new InputSpec(ndim: 4); } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { return tf.keras.backend.spatial_2d_padding(inputs, padding: padding, diff --git a/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs b/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs index ab246464..22ec2d47 100644 --- a/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs +++ b/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs @@ -14,6 +14,7 @@ limitations under the License. ******************************************************************************/ +using NumSharp; using System; using System.Collections.Generic; using System.Linq; @@ -135,7 +136,7 @@ namespace Tensorflow.Keras.Utils if (!processed_ops.Contains(op)) { var layer_inputs = new List(); - + var constants = new Dictionary(); foreach (var (i, op_input) in enumerate(op.inputs._inputs)) { if (uses_keras_history(op_input)) @@ -144,8 +145,7 @@ namespace Tensorflow.Keras.Utils { tf_with(ops.init_scope(), delegate { - - + constants[i] = tf.keras.backend.eval_in_eager_or_function(op_input); }); } } @@ -155,6 +155,7 @@ namespace Tensorflow.Keras.Utils var op_layer = new TensorFlowOpLayer(new TensorFlowOpLayerArgs { NodeDef = op.node_def, + Constants = constants, Name = op.name }); created_layers.Add(op_layer); diff --git a/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs b/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs index 9e3e3591..84293b72 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs @@ -74,7 +74,7 @@ namespace Tensorflow /// /// /// - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { var one = constant_op.constant(1, dtype: dtypes.int32); // Parameters of gates are concatenated into one multiply for efficiency. diff --git a/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs b/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs index 7bf27e8e..23de35dc 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs @@ -67,7 +67,7 @@ namespace Tensorflow built = true; } - protected override Tensors call(Tensors inputs, Tensor state = null, bool is_training = false) + protected override Tensors call_fn(Tensors inputs, Tensor state = null, bool is_training = false) { // Most basic RNN: output = new_state = act(W * input + U * state + B). var concat = array_ops.concat(new Tensor[] { inputs, state }, 1); diff --git a/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs b/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs index 84cae247..c8f0becf 100644 --- a/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs +++ b/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs @@ -124,6 +124,9 @@ namespace Tensorflow case NPTypeCode.Double: full_values.Add(value.GetValue(0)); break; + case NPTypeCode.Boolean: + full_values.Add(value.GetValue(0)); + break; /*case "String": full_values.Add(value.Data()[0]); break;*/ diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index 29a33837..e1b9bd86 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -72,6 +72,8 @@ https://tensorflownet.readthedocs.io + + @@ -84,10 +86,4 @@ https://tensorflownet.readthedocs.io - - - - PreserveNewest - -