Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
flakid opened this issue Nov 26, 2023 · 3 comments
Open

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

flakid opened this issue Nov 26, 2023 · 3 comments

Comments

@flakid
Copy link

flakid commented Nov 26, 2023

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
Copy link
Collaborator

Wanglongzhi2001 commented Nov 26, 2023

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
Copy link
Author

flakid commented Nov 28, 2023

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants