Reputation: 3502
I saved an MLP regression type algorithm with this type of code:
#define model
model = Sequential()
model.add(Dense(80, input_dim=2, kernel_initializer='normal', activation='relu'))
model.add(Dense(60, kernel_initializer='normal', activation='relu'))
model.add(Dense(40, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(10, kernel_initializer='normal', activation='relu'))
model.add(Dense(5, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.summary()
model.compile(loss='mse', optimizer='adam', metrics=[rmse])
# train model, test callback option
history = model.fit(X_train, Y_train, epochs=75, batch_size=1, verbose=2, callbacks=[callback])
#history = model.fit(X_train, Y_train, epochs=60, batch_size=1, verbose=2)
# plot metrics
plt.plot(history.history['rmse'])
plt.title('kW RSME Vs Epoch')
plt.show()
model.save('./saved_model/kwSummer')
But when I try to load the saved model:
model = tf.keras.models.load_model('./saved_model/kwSummer')
# Check its architecture
new_model.summary()
I get this error below when trying to load the model.. Would anyone have any ideas to try?
ValueError: Unable to restore custom object of type _tf_keras_metric currently. Please make sure that the layer implements `get_config`and `from_config` when saving. In addition, please use the `custom_objects` arg when calling `load_model()`.
I have been experimenting with using Python 3.7 to train the model and then IPython Anaconda Python 3.8 to load the model, would this have anything to do with the issue? Like 2 different versions of tensorflow?
EDIT, This is the entire script
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import backend
from datetime import datetime
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import seaborn as sns
import math
df = pd.read_csv('./colabData.csv', index_col='Date', parse_dates=True)
print(df.info())
# This function keeps the learning rate at 0.001
# and decreases it exponentially after that.
def scheduler(epoch):
if epoch < 1:
return 0.001
else:
return 0.001 * tf.math.exp(0.01 * (1 - epoch))
callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
#function to calculate RSME
def rmse(y_true, y_pred):
return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))
dfTrain = df.copy()
# split into input (X) and output (Y) variables
X = dfTrain.drop(['kW'],1)
Y = dfTrain['kW']
#define training & testing data set
offset = int(X.shape[0] * 0.8)
X_train, Y_train = X[:offset], Y[:offset]
X_test, Y_test = X[offset:], Y[offset:]
#define model
model = Sequential()
model.add(Dense(80, input_dim=2, kernel_initializer='normal', activation='relu'))
model.add(Dense(60, kernel_initializer='normal', activation='relu'))
model.add(Dense(40, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(10, kernel_initializer='normal', activation='relu'))
model.add(Dense(5, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.summary()
model.compile(loss='mse', optimizer='adam', metrics=[rmse])
# train model, test callback option
history = model.fit(X_train, Y_train, epochs=75, batch_size=1, verbose=2, callbacks=[callback])
#history = model.fit(X_train, Y_train, epochs=60, batch_size=1, verbose=2)
# plot metrics
plt.plot(history.history['rmse'])
plt.title('kW RSME Vs Epoch')
plt.show()
model.save('./saved_model/kwSummer')
print('[INFO] Saved model to drive')
Upvotes: 0
Views: 25494
Reputation: 17239
As you have a custom object, you have to load it with the custom_object
argument. It also informed you in the error log. Src.
In addition, please use the `custom_objects` arg when calling `load_model()`.
Try as follows
new_model = tf.keras.models.load_model('./saved_model/kwSummer', ,
custom_objects={"rmse": rmse})
Upvotes: 3
Reputation: 3
might I suggest running the code through google colab? this might help to see if the issue with the code or a compatibility issue. As google colab will ensure compatibility as it fixed a lot of the ML issues I had.
Upvotes: 0