Code Monkey home page Code Monkey logo

Comments (3)

Wanglongzhi2001 avatar Wanglongzhi2001 commented on September 24, 2024

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());

from tensorflow.net.

flakid avatar flakid commented on September 24, 2024

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.

from tensorflow.net.

Wanglongzhi2001 avatar Wanglongzhi2001 commented on September 24, 2024

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.

from tensorflow.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.