keras 如何获得基于Transformer的推荐模型来预测新电影

fnatzsnv  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(198)

我尝试在https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/structured_data/ipynb/movielens_recommendations_transformers.ipynb中运行Behavior Sequence Transformer模型,以根据以前的电影评级预测下一部要观看的电影。
我已经让Colab notebook中的代码正确运行,但似乎无法从中获得任何有意义的预测。
下面是model.input_shape的输出:

{'user_id': (None, 1), 'sequence_movie_ids': (None, 3), 'target_movie_id': (None, 1), 'sequence_ratings': (None, 3), 'sex': (None, 1), 'age_group': (None, 1), 'occupation': (None, 1)}

下面是我用于预测的代码:

# Load the latest 3 movies you watched and gave them all a rating of 4
latest_movies = pd.DataFrame({
    'user_id': [0],
    'sequence_movie_ids': [[1, 2, 3]],
    'target_movie_id': [0],
    'sequence_ratings': [[4.0, 4.0, 4.0]],
    'sex': [0],
    'age_group': [0],
    'occupation': [0]
})

# Predict the next movie to watch
next_movie = model.predict(input_data)
print(next_movie)

但是,这会导致以下错误消息:

Node: 'model/Cast'
Cast int32 to string is not supported
     [[{{node model/Cast}}]] [Op:__inference_predict_function_40501]

我还尝试将input_data转换为Tensor:

# Prepare the input data
input_data = {
    'user_id': tf.convert_to_tensor(latest_movies['user_id'], dtype=tf.int32),
    'sequence_movie_ids': tf.convert_to_tensor(latest_movies['sequence_movie_ids'], dtype=tf.int32),
    'target_movie_id': tf.convert_to_tensor(latest_movies['target_movie_id'], dtype=tf.int32),
    'sequence_ratings': tf.convert_to_tensor(latest_movies['sequence_ratings'], dtype=tf.float32),
    'sex': tf.convert_to_tensor(latest_movies['sex'], dtype=tf.int32),
    'age_group': tf.convert_to_tensor(latest_movies['age_group'], dtype=tf.int32),
    'occupation': tf.convert_to_tensor(latest_movies['occupation'], dtype=tf.int32)
}

但这也失败了,并显示错误消息:

ValueError: setting an array element with a sequence.

我们如何让这个模型做出预测?
非常感谢

agyaoht7

agyaoht71#

我想明白了有一个名为' create_model_inputs() '的函数,它使我能够推断出输入数据的结构。
工作代码在下面。请注意,input_data中的值源自test_data.csv和train_data.csv文件,并且需要是字符串。

# Create the input dictionary. Get example values from the test_data.csv or train_data.csv files. 
input_data = {
    "user_id": np.array(["user_1"]),
    "sequence_movie_ids": np.array([["movie_531", "movie_532", "movie_533"]]),
    "target_movie_id": np.array(["movie_534"]),
    "sequence_ratings": np.array([[4.0, 1.0, 5.0]]),
    "sex": np.array(["M"]),
    "age_group": np.array(["group_35"]),
    "occupation": np.array(["occupation_1"])
}

# Predict the next movie to watch
next_movie = model.predict(input_data)
print(next_movie)

相关问题