SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.26k stars 523 forks source link

[Question]: Load LSTM Model twice causes error #1218

Open flakid opened 1 year ago

flakid commented 1 year ago

Description

Use example TrainLSTMWithMnist() in Rnn.Test.cs to train and save model. Then load exported model and predict TestData of Mnist.
Do this loading and prediction twice cause Tensorflow.InvalidArgumentError:“Matrix size-incompatible: In[0]: [32,50], In[1]: [28,200]”

Alternatives

No response

Wanglongzhi2001 commented 1 year ago

Hello, could you mind providing your version of TensorFlow.NET? And how do you save and load model (we recommend to use keras.models.load_model to load the whole model rather than save and load weight, because it has a more comprehensive implementation)? I use the following code to save model, then load it and predict it twice, and it works well. (The version of my TensorFlow.NET and TensorFlow.Keras is 0.110.4)

using static Tensorflow.KerasApi;
using Tensorflow;

var input = keras.Input((784));
var x = keras.layers.Reshape((28, 28)).Apply(input);
x = keras.layers.LSTM(50, return_sequences: true).Apply(x);
x = keras.layers.LSTM(100).Apply(x);
var output = keras.layers.Dense(10, activation: "softmax").Apply(x);

var model = keras.Model(input, output);
model.summary();
model.compile(keras.optimizers.Adam(), keras.losses.CategoricalCrossentropy(), new string[] { "accuracy" });

var data_loader = new MnistModelLoader();
var dataset = data_loader.LoadAsync(new ModelLoadSetting
{
    TrainDir = "mnist",
    OneHot = true,
    ValidationSize = 55000,
}).Result;

model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size: 16, epochs: 1);
model.save("./mnist_model");

// after training and saving model, comment the code above and uncomment the following code.
//var model = keras.models.load_model("./mnist_model");
//var input = tf.ones((8, 28, 28), dtype: TF_DataType.TF_FLOAT);
//var output = model.predict(input, 4);

//Console.WriteLine(output.numpy().ToString());
flakid commented 1 year ago

I use a version 0.150.0. Predict it twice I mean like this: model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size: 64, epochs: 1); (x_test, y_test) = (dataset.Test.Data, dataset.Test.Labels); var output = model.predict(x_test, , use_multiprocessing: true, workers: 8);

model.save("./mnist_model"); model.save_weights("./Weights");

keras.backend.clear_session(); model = keras.models.load_model("./mnist_model"); model.load_weights("./Weights"); output = model.predict(x_test, , use_multiprocessing: true, workers: 8);

keras.backend.clear_session(); model = keras.models.load_model("./mnist_model"); model.load_weights("./Weights"); output = model.predict(x_test, , use_multiprocessing: true, workers: 8);

Without the weights two predictions of same inputs do not match.

Wanglongzhi2001 commented 12 months ago

Hello, could you please try use version 0.110.4 for now? I run the code you provided above in version 0.110.4, it runs well, and it fails when in version 0.150.0.