很好的tuto,谢谢你,但是我有一个问题,保存模型重用它。我有这个错误
文件“C:\用户\guera\应用程序数据\本地\程序\Python\Python 310\lib\站点包\keras\实用程序\跟踪返回实用程序.py”,第67行,在错误处理程序中引发e.with_traceback(filtered_tb)来自无文件“C:\用户\guera\应用程序数据\本地\程序\Python\Python 310\lib\站点包\keras\保存\保存. py”,第142行,在保存模型中引发未实现错误(未实现错误:将模型保存为HDF5格式要求模型为函数模型或序列模型。它不适用于子类模型,因为此类模型是通过Python方法的主体定义的,无法安全地进行序列化。请考虑保存为Tensorflow SavedModel格式(通过设置save_format=“tf”)或使用save_weights
。
那么如何解决它们呢?
这是我的源代码:
import os
import pprint
import tempfile
from typing import Dict, Text
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import joblib
import tensorflow_recommenders as tfrs
from src.models.movie_lens import MovieLensModel
def content_based_filtering(user_id, table1, table2, user_id_label, target_label):
# Ratings data.
ratings = tfds.load(table1, split="train")
# Features of all the available movies.
movies = tfds.load(table2, split="train")
# Select the basic features.
ratings = ratings.map(lambda x: {
"{}".format(target_label): x[target_label],
"{}".format(user_id_label): x[user_id_label]
})
movies = movies.map(lambda x: x[target_label])
user_ids_vocabulary = tf.keras.layers.StringLookup(mask_token=None)
user_ids_vocabulary.adapt(ratings.map(lambda x: x[user_id_label]))
movie_titles_vocabulary = tf.keras.layers.StringLookup(mask_token=None)
movie_titles_vocabulary.adapt(movies)
# Define user and movie models.
user_model = tf.keras.Sequential([
user_ids_vocabulary,
tf.keras.layers.Embedding(user_ids_vocabulary.vocab_size(), 64)
])
movie_model = tf.keras.Sequential([
movie_titles_vocabulary,
tf.keras.layers.Embedding(movie_titles_vocabulary.vocab_size(), 64)
])
# Define your objectives.
task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(
movies.batch(128).map(movie_model)
)
)
# Create a retrieval model.
model = MovieLensModel(user_model, movie_model, task)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))
# Train for 3 epochs.
model.fit(ratings.batch(4096), epochs=3)
model.save('content_model.h5')
# Use brute-force search to set up retrieval using the trained representations.
index = tfrs.layers.factorized_top_k.BruteForce(model.user_model)
index.index_from_dataset(
movies.batch(100).map(lambda title: (title, model.movie_model(title))))
# Get some recommendations.
_, titles = index(np.array([str(user_id)]))
# print('Recommendation content based filtering\n')
return titles[0, :3].numpy()
影片镜头型号:
import os
import pprint
import tempfile
from typing import Dict, Text
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs
class MovieLensModel(tfrs.Model):
def __init__(self, user_model, movie_model, task):
super().__init__()
self.movie_model: tf.keras.Model = movie_model
self.user_model: tf.keras.Model = user_model
self.task: tf.keras.layers.Layer = task
def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
# We pick out the user features and pass them into the user model.
user_embeddings = self.user_model(features["user_id"])
# And pick out the movie features and pass them into the movie model,
# getting embeddings back.
positive_movie_embeddings = self.movie_model(features["movie_title"])
# The task computes the loss and the metrics.
return self.task(user_embeddings, positive_movie_embeddings)
图像错误:
2条答案
按热度按时间368yc8dk1#
谢谢你的帮助,你给予我的第二种方法正确地起作用了。
结果图像:
这个建议很有效,非常感谢
7vhp5slm2#
看看错误,它说:
将模型保存为HDF5格式要求模型为函数模型或序列模型。它不适用于子类模型,因为此类模型是通过Python方法的主体定义的,无法安全地进行序列化。请考虑保存为Tensorflow SavedModel格式(通过设置save_format=“tf”)或使用save_weights
您遇到此问题的原因是以下行:
您应该按照建议进行操作,即以不同的方式保存模型。您主要可以遵循两种方法。
方法1
请尝试将该行替换为:
并将其加载回:
方法2
可以首先保存权重,如下所示:
要重新加载它们,您需要示例化模型对象并加载其中的权重: