Skip to content

Commit b27ccca

Browse files
author
“Wanglongzhi2001”
committed
fix:fix the bug of load LSTM model
1 parent ed1a8d2 commit b27ccca

28 files changed

+71
-40
lines changed

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUCellArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Collections.Generic;
44
using System.Text;
55

6-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
6+
namespace Tensorflow.Keras.ArgsDefinition
77
{
88
public class GRUCellArgs : AutoSerializeLayerArgs
99
{

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
1+
namespace Tensorflow.Keras.ArgsDefinition
22
{
33
public class LSTMArgs : RNNArgs
44
{

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMCellArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Newtonsoft.Json;
22
using static Tensorflow.Binding;
33

4-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
4+
namespace Tensorflow.Keras.ArgsDefinition
55
{
66
// TODO: complete the implementation
77
public class LSTMCellArgs : AutoSerializeLayerArgs

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RNNArgs.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Newtonsoft.Json;
22
using System.Collections.Generic;
3-
using Tensorflow.Keras.Layers.Rnn;
3+
using Tensorflow.Keras.Layers;
44

5-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
5+
namespace Tensorflow.Keras.ArgsDefinition
66
{
77
// TODO(Rinne): add regularizers.
88
public class RNNArgs : AutoSerializeLayerArgs
@@ -23,16 +23,22 @@ public class RNNArgs : AutoSerializeLayerArgs
2323
public int? InputDim { get; set; }
2424
public int? InputLength { get; set; }
2525
// TODO: Add `num_constants` and `zero_output_for_mask`.
26-
26+
[JsonProperty("units")]
2727
public int Units { get; set; }
28+
[JsonProperty("activation")]
2829
public Activation Activation { get; set; }
30+
[JsonProperty("recurrent_activation")]
2931
public Activation RecurrentActivation { get; set; }
32+
[JsonProperty("use_bias")]
3033
public bool UseBias { get; set; } = true;
3134
public IInitializer KernelInitializer { get; set; }
3235
public IInitializer RecurrentInitializer { get; set; }
3336
public IInitializer BiasInitializer { get; set; }
37+
[JsonProperty("dropout")]
3438
public float Dropout { get; set; } = .0f;
39+
[JsonProperty("zero_output_for_mask")]
3540
public bool ZeroOutputForMask { get; set; } = false;
41+
[JsonProperty("recurrent_dropout")]
3642
public float RecurrentDropout { get; set; } = .0f;
3743
}
3844
}

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RnnOptionalArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Text;
44
using Tensorflow.Common.Types;
55

6-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
6+
namespace Tensorflow.Keras.ArgsDefinition
77
{
88
public class RnnOptionalArgs: IOptionalArgs
99
{

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
1+
namespace Tensorflow.Keras.ArgsDefinition
22
{
33
public class SimpleRNNArgs : RNNArgs
44
{

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNCellArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Newtonsoft.Json;
22

3-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
3+
namespace Tensorflow.Keras.ArgsDefinition
44
{
55
public class SimpleRNNCellArgs: AutoSerializeLayerArgs
66
{

src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/StackedRNNCellsArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
2-
using Tensorflow.Keras.Layers.Rnn;
2+
using Tensorflow.Keras.Layers;
33

4-
namespace Tensorflow.Keras.ArgsDefinition.Rnn
4+
namespace Tensorflow.Keras.ArgsDefinition
55
{
66
public class StackedRNNCellsArgs : LayerArgs
77
{

src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Tensorflow.Framework.Models;
33
using Tensorflow.Keras.Engine;
4-
using Tensorflow.Keras.Layers.Rnn;
4+
using Tensorflow.Keras.Layers;
55
using Tensorflow.NumPy;
66
using static Google.Protobuf.Reflection.FieldDescriptorProto.Types;
77

src/TensorFlowNET.Core/Keras/Layers/Rnn/IRnnCell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Text;
44
using Tensorflow.Common.Types;
55

6-
namespace Tensorflow.Keras.Layers.Rnn
6+
namespace Tensorflow.Keras.Layers
77
{
88
public interface IRnnCell: ILayer
99
{

src/TensorFlowNET.Core/Keras/Layers/Rnn/IStackedRnnCells.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
namespace Tensorflow.Keras.Layers.Rnn
5+
namespace Tensorflow.Keras.Layers
66
{
77
public interface IStackedRnnCells : IRnnCell
88
{

src/TensorFlowNET.Core/Operations/NnOps/RNNCell.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ limitations under the License.
1919
using Tensorflow.Common.Types;
2020
using Tensorflow.Keras;
2121
using Tensorflow.Keras.ArgsDefinition;
22-
using Tensorflow.Keras.ArgsDefinition.Rnn;
2322
using Tensorflow.Keras.Engine;
24-
using Tensorflow.Keras.Layers.Rnn;
23+
using Tensorflow.Keras.Layers;
2524
using Tensorflow.Keras.Saving;
2625
using Tensorflow.NumPy;
2726
using Tensorflow.Operations;

src/TensorFlowNET.Core/ops.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,9 @@ public static bool executing_eagerly_outside_functions()
571571
if (tf.Context.executing_eagerly())
572572
return true;
573573
else
574-
throw new NotImplementedException("");
574+
// TODO(Wanglongzhi2001), implement the false case
575+
return true;
576+
//throw new NotImplementedException("");
575577
}
576578

577579
public static bool inside_function()

src/TensorFlowNET.Keras/Layers/LayersApi.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
using Tensorflow.Framework.Models;
33
using Tensorflow.Keras.ArgsDefinition;
44
using Tensorflow.Keras.ArgsDefinition.Core;
5-
using Tensorflow.Keras.ArgsDefinition.Rnn;
65
using Tensorflow.Keras.Engine;
7-
using Tensorflow.Keras.Layers.Rnn;
6+
using Tensorflow.Keras.Layers;
87
using Tensorflow.NumPy;
98
using static Tensorflow.Binding;
109
using static Tensorflow.KerasApi;

src/TensorFlowNET.Keras/Layers/Rnn/DropoutRNNCellMixin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Tensorflow.Keras.Engine;
77
using Tensorflow.Keras.Utils;
88

9-
namespace Tensorflow.Keras.Layers.Rnn
9+
namespace Tensorflow.Keras.Layers
1010
{
1111
public abstract class DropoutRNNCellMixin: Layer, IRnnCell
1212
{

src/TensorFlowNET.Keras/Layers/Rnn/GRUCell.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
using System.Diagnostics;
44
using System.Text;
55
using Tensorflow.Keras.ArgsDefinition;
6-
using Tensorflow.Keras.ArgsDefinition.Rnn;
76
using Tensorflow.Common.Extensions;
87
using Tensorflow.Common.Types;
98
using Tensorflow.Keras.Saving;
109

11-
namespace Tensorflow.Keras.Layers.Rnn
10+
namespace Tensorflow.Keras.Layers
1211
{
1312
/// <summary>
1413
/// Cell class for the GRU layer.

src/TensorFlowNET.Keras/Layers/Rnn/LSTM.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Linq;
2-
using Tensorflow.Keras.ArgsDefinition.Rnn;
2+
using Tensorflow.Keras.ArgsDefinition;
33
using Tensorflow.Keras.Engine;
44
using Tensorflow.Common.Types;
55
using Tensorflow.Common.Extensions;
66

7-
namespace Tensorflow.Keras.Layers.Rnn
7+
namespace Tensorflow.Keras.Layers
88
{
99
/// <summary>
1010
/// Long Short-Term Memory layer - Hochreiter 1997.

src/TensorFlowNET.Keras/Layers/Rnn/LSTMCell.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System.Diagnostics;
44
using Tensorflow.Common.Extensions;
55
using Tensorflow.Common.Types;
6-
using Tensorflow.Keras.ArgsDefinition.Rnn;
6+
using Tensorflow.Keras.ArgsDefinition;
77
using Tensorflow.Keras.Engine;
88
using Tensorflow.Keras.Saving;
99
using Tensorflow.Keras.Utils;
1010

11-
namespace Tensorflow.Keras.Layers.Rnn
11+
namespace Tensorflow.Keras.Layers
1212
{
1313
/// <summary>
1414
/// Cell class for the LSTM layer.

src/TensorFlowNET.Keras/Layers/Rnn/RNN.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.Generic;
44
using System.Reflection;
55
using Tensorflow.Keras.ArgsDefinition;
6-
using Tensorflow.Keras.ArgsDefinition.Rnn;
76
using Tensorflow.Keras.Engine;
87
using Tensorflow.Keras.Saving;
98
using Tensorflow.Util;
@@ -14,7 +13,7 @@
1413
using System.Runtime.CompilerServices;
1514
// from tensorflow.python.distribute import distribution_strategy_context as ds_context;
1615

17-
namespace Tensorflow.Keras.Layers.Rnn
16+
namespace Tensorflow.Keras.Layers
1817
{
1918
/// <summary>
2019
/// Base class for recurrent layers.
@@ -185,6 +184,7 @@ private Tensors compute_mask(Tensors inputs, Tensors mask)
185184

186185
public override void build(KerasShapesWrapper input_shape)
187186
{
187+
_buildInputShape = input_shape;
188188
input_shape = new KerasShapesWrapper(input_shape.Shapes[0]);
189189

190190
InputSpec get_input_spec(Shape shape)

src/TensorFlowNET.Keras/Layers/Rnn/RnnBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Tensorflow.Keras.ArgsDefinition;
55
using Tensorflow.Keras.Engine;
66

7-
namespace Tensorflow.Keras.Layers.Rnn
7+
namespace Tensorflow.Keras.Layers
88
{
99
public abstract class RnnBase: Layer
1010
{

src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNN.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Data;
2-
using Tensorflow.Keras.ArgsDefinition.Rnn;
2+
using Tensorflow.Keras.ArgsDefinition;
33
using Tensorflow.Keras.Saving;
44
using Tensorflow.Operations.Activation;
55
using static HDF.PInvoke.H5Z;
66
using static Tensorflow.ApiDef.Types;
77

8-
namespace Tensorflow.Keras.Layers.Rnn
8+
namespace Tensorflow.Keras.Layers
99
{
1010
public class SimpleRNN : RNN
1111
{

src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNNCell.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4-
using Tensorflow.Keras.ArgsDefinition.Rnn;
4+
using Tensorflow.Keras.ArgsDefinition;
55
using Tensorflow.Keras.Engine;
66
using Tensorflow.Keras.Saving;
77
using Tensorflow.Common.Types;
88
using Tensorflow.Common.Extensions;
99
using Tensorflow.Keras.Utils;
1010
using Tensorflow.Graphs;
1111

12-
namespace Tensorflow.Keras.Layers.Rnn
12+
namespace Tensorflow.Keras.Layers
1313
{
1414
/// <summary>
1515
/// Cell class for SimpleRNN.

src/TensorFlowNET.Keras/Layers/Rnn/StackedRNNCells.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System.Linq;
44
using Tensorflow.Common.Extensions;
55
using Tensorflow.Common.Types;
6-
using Tensorflow.Keras.ArgsDefinition.Rnn;
6+
using Tensorflow.Keras.ArgsDefinition;
77
using Tensorflow.Keras.Engine;
88
using Tensorflow.Keras.Saving;
99
using Tensorflow.Keras.Utils;
1010

11-
namespace Tensorflow.Keras.Layers.Rnn
11+
namespace Tensorflow.Keras.Layers
1212
{
1313
public class StackedRNNCells : Layer, IRnnCell
1414
{

src/TensorFlowNET.Keras/Saving/KerasObjectLoader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Tensorflow.Keras.ArgsDefinition;
1414
using Tensorflow.Keras.Engine;
1515
using Tensorflow.Keras.Layers;
16-
using Tensorflow.Keras.Layers.Rnn;
1716
using Tensorflow.Keras.Losses;
1817
using Tensorflow.Keras.Metrics;
1918
using Tensorflow.Keras.Saving.SavedModel;

src/TensorFlowNET.Keras/Saving/SavedModel/serialized_attributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using Tensorflow.Keras.Engine;
6-
using Tensorflow.Keras.Layers.Rnn;
6+
using Tensorflow.Keras.Layers;
77
using Tensorflow.Keras.Metrics;
88
using Tensorflow.Train;
99

src/TensorFlowNET.Keras/Utils/RnnUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Diagnostics;
44
using System.Text;
55
using Tensorflow.Common.Types;
6-
using Tensorflow.Keras.Layers.Rnn;
6+
using Tensorflow.Keras.Layers;
77
using Tensorflow.Common.Extensions;
88

99
namespace Tensorflow.Keras.Utils

test/TensorFlowNET.Keras.UnitTest/Layers/Rnn.Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Threading.Tasks;
77
using Tensorflow.Common.Types;
88
using Tensorflow.Keras.Engine;
9-
using Tensorflow.Keras.Layers.Rnn;
9+
using Tensorflow.Keras.Layers;
1010
using Tensorflow.Keras.Saving;
1111
using Tensorflow.NumPy;
1212
using Tensorflow.Train;

test/TensorFlowNET.Keras.UnitTest/Model/ModelLoadTest.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Microsoft.VisualStudio.TestPlatform.Utilities;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using System.Linq;
4+
using Tensorflow.Keras.Engine;
35
using Tensorflow.Keras.Optimizers;
46
using Tensorflow.Keras.UnitTest.Helpers;
57
using Tensorflow.NumPy;
@@ -79,6 +81,31 @@ public void ModelWithSelfDefinedModule()
7981
model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size, num_epochs);
8082
}
8183

84+
[TestMethod]
85+
public void LSTMLoad()
86+
{
87+
var inputs = np.random.randn(10, 5, 3);
88+
var outputs = np.random.randn(10, 1);
89+
var model = keras.Sequential();
90+
model.add(keras.Input(shape: (5, 3)));
91+
var lstm = keras.layers.LSTM(32);
92+
93+
model.add(lstm);
94+
95+
model.add(keras.layers.Dense(1, keras.activations.Sigmoid));
96+
97+
model.compile(optimizer: keras.optimizers.Adam(),
98+
loss: keras.losses.MeanSquaredError(),
99+
new[] { "accuracy" });
100+
101+
var result = model.fit(inputs.numpy(), outputs.numpy(), batch_size: 10, epochs: 3, workers: 16, use_multiprocessing: true);
102+
103+
model.save("LSTM_Random");
104+
105+
var model_loaded = keras.models.load_model("LSTM_Random");
106+
model_loaded.summary();
107+
}
108+
82109
[Ignore]
83110
[TestMethod]
84111
public void VGG19()

0 commit comments

Comments
 (0)