Reputation: 1616
After building the model from Embedding RecommenderNet model, how to save it, link to the doc is https://keras.io/examples/structured_data/collaborative_filtering_movielens/
class RecommenderNet(keras.Model):
def __init__(self, num_users, num_movies, embedding_size, **kwargs):
super(RecommenderNet, self).__init__(**kwargs)
self.num_users = num_users
self.num_movies = num_movies
self.embedding_size = embedding_size
self.user_embedding = layers.Embedding(
num_users,
embedding_size,
embeddings_initializer="he_normal",
embeddings_regularizer=keras.regularizers.l2(1e-6),
)
self.user_bias = layers.Embedding(num_users, 1)
self.movie_embedding = layers.Embedding(
num_movies,
embedding_size,
embeddings_initializer="he_normal",
embeddings_regularizer=keras.regularizers.l2(1e-6),
)
self.movie_bias = layers.Embedding(num_movies, 1)
def call(self, inputs):
user_vector = self.user_embedding(inputs[:, 0])
user_bias = self.user_bias(inputs[:, 0])
movie_vector = self.movie_embedding(inputs[:, 1])
movie_bias = self.movie_bias(inputs[:, 1])
dot_user_movie = tf.tensordot(user_vector, movie_vector, 2)
# Add all the components (including bias)
x = dot_user_movie + user_bias + movie_bias
# The sigmoid activation forces the rating to between 0 and 1
return tf.nn.sigmoid(x)
model = RecommenderNet(num_users, num_movies, EMBEDDING_SIZE)
model.compile(
loss=tf.keras.losses.BinaryCrossentropy(), optimizer=keras.optimizers.Adam(lr=0.001)
)
history = model.fit(
x=x_train,
y=y_train,
batch_size=64,
epochs=5,
verbose=1,
validation_data=(x_val, y_val),
)
tried these
model.save('model.h5py')
tf.keras.models.save_model(model, overwrite=True, include_optimizer=True, save_format='h5')
Both throws
NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or
a Sequential model.It does not work for subclassed models, because such models are defined via the body of
a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel
format (by setting save_format="tf") or using `save_weights`.
model type is main.RecommenderNet
Upvotes: 0
Views: 1324
Reputation: 1616
Actually recreating the model with
keras.models.load_model('path_to_my_model')
didn't work for me First we have to save_weights from the built model
model.save_weights('model_weights', save_format='tf')
Then we have to initiate a new instance for the subclass Model then compile and train_on_batch with one record and load_weights of built model
loaded_model = RecommenderNet(num_users, num_movies, EMBEDDING_SIZE)
loaded_model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=keras.optimizers.Adam(lr=0.001))
loaded_model.train_on_batch(x_train[:1], y_train[:1])
loaded_model.load_weights('model_weights')
This work perfectly in TensorFlow==2.2.0
Upvotes: 1
Reputation: 1616
I upgraded tensorflow 1.14.0 to 2.2.0, save_model worked
tf.keras.models.save_model(model,'./saved_model')
But with the load_model, after loading the model, while prediction
loaded_model = tf.keras.models.load_model('./saved_model')
I get value error
rates = loaded_model.predict(user_product_array).flatten()
ValueError: Python inputs incompatible with input_signature:
inputs: (
Tensor("IteratorGetNext:0", shape=(None, 2), dtype=int32))
input_signature: (
TensorSpec(shape=(None, 2), dtype=tf.int64, name='input_1'))
May I know the issue here?
Upvotes: 0
Reputation: 1769
So as the error says, you can use save_format="tf"
since your model is not Functional
or Sequential
:
model.save('model.py')
tf.keras.models.save_model(model, overwrite=True, include_optimizer=True, save_format='tf')
Also as seen in the documentation you can use:
keras_model_path = "/tmp/keras_save"
model.save(keras_model_path)
Upvotes: 1