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

Error loading or saving model #1267

Open
svarogaden opened this issue Jul 11, 2024 · 0 comments
Open

Error loading or saving model #1267

svarogaden opened this issue Jul 11, 2024 · 0 comments

Comments

@svarogaden
Copy link

Description

I create a model and save it, but when loading it I get an error.
System.NullReferenceException: "Object reference not set to an instance of an object."

` public Sequential CreateModelLSTMWithBatchNormalization()
{
Sequential model = KerasApi.keras.Sequential();

        model.add(KerasApi.keras.layers.Input(shape: new Shape(51, 3))); 
        model.add(KerasApi.keras.layers.LSTM(256, return_sequences: true));
        model.add(KerasApi.keras.layers.BatchNormalization());
        model.add(KerasApi.keras.layers.Dropout(0.3f)); 

        model.add(KerasApi.keras.layers.LSTM(128, return_sequences: true));
        model.add(KerasApi.keras.layers.BatchNormalization());
        model.add(KerasApi.keras.layers.Dropout(0.3f)); 

        model.add(KerasApi.keras.layers.LSTM(64));
        model.add(KerasApi.keras.layers.BatchNormalization());
        model.add(KerasApi.keras.layers.Dropout(0.3f)); 

        model.add(KerasApi.keras.layers.Dense(128, activation: "relu"));
        model.add(KerasApi.keras.layers.BatchNormalization());
        model.add(KerasApi.keras.layers.Dense(52, activation: "softmax"));
        var optimizer = new Adam(learning_rate: 0.001f);
        model.compile(optimizer: optimizer, loss: new Tensorflow.Keras.Losses.CategoricalCrossentropy(), metrics: new string[] { "accuracy" });

        model.summary();

        return model;
    }



    if (Directory.Exists(modelPath))
    {        
            model = (Sequential)KerasApi.keras.models.load_model(modelPath);               
   }
  else
{
      model = CreateModelLSTMWithBatchNormalization();
}

           public void TrainModelLSTM(NDArray X, NDArray Y, int epochs = 50, int batchSize = 52)
    {
        try
        {
            model.fit(X, Y, epochs: epochs, batch_size: batchSize);
            model.save(modelPath, save_format: "tf");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
            Console.WriteLine(ex.StackTrace);
        }

    }

error

`

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

1 participant