繁体   English   中英

如何使用tensorflow的ncf模型进行预测?

[英]How to use tensorflow's ncf model to predict?

嗨,我是 tensorflow 和神经网络的新手。 试图了解 tensorflow 的官方模型 repo 中的ncf 推荐模型

我的理解是您构建了一个带有输入层和学习层的模型。 然后创建批量数据来训练模型,然后使用测试数据来评估模型。 这是在这个文件中完成的。

但是,我无法理解输入层。

它显示在代码中

user_input = tf.keras.layers.Input(
      shape=(1,), name=movielens.USER_COLUMN, dtype=tf.int32)

据我了解,您可以一次输入一个参数。

但是我只能使用以下虚拟数据来调用 predict_on_batch

user_input = np.full(shape=(256,),fill_value=1, dtype=np.int32)
item_input = np.full(shape=(256,),fill_value=1, dtype=np.int32)
valid_pt_mask_input = np.full(shape=(256,),fill_value=True, dtype=np.bool)
dup_mask_input = np.full(shape=(256,),fill_value=1, dtype=np.int32)
label_input = np.full(shape=(256,),fill_value=True, dtype=np.bool)
test_input_list = [user_input,item_input,valid_pt_mask_input,dup_mask_input,label_input]

tf.print(keras_model.predict_on_batch(test_input_list))

当我运行以下代码时:

    user_input = np.full(shape=(1,),fill_value=1, dtype=np.int32)
    item_input = np.full(shape=(1,),fill_value=1, dtype=np.int32)
    valid_pt_mask_input = np.full(shape=(1,),fill_value=True, dtype=np.bool)
    dup_mask_input = np.full(shape=(1,),fill_value=1, dtype=np.int32)
    label_input = np.full(shape=(1,),fill_value=True, dtype=np.bool)
    test_input_list = [user_input,item_input,valid_pt_mask_input,dup_mask_input,label_input]

    classes = _model.predict(test_input_list)
    tf.print(classes)

我收到此错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 256
     [[{{node model_1/metric_layer/StatefulPartitionedCall/StatefulPartitionedCall/Reshape_1}}]] [Op:__inference_predict_function_2828]

有人可以帮助我如何使用这个模型来预测单个输入吗? 还有为什么在进行预测时需要 user_id 和 item_id ? 不应该是你提供一个用户列表模型返回一个项目列表吗?

我之前没有使用过 ncf 模型,但看起来您将训练数据输入为 1 个具有 256 个特征的样本,而不是 256 个样本,每个样本具有 1 个特征。 只需翻转你的 numpy 数组,确保特征矩阵是二维的,并且特征的数量是第一维。

user_input = np.full(shape=(1,256),fill_value=1, dtype=np.int32)

...等其他人。 (好吧,标签应该保持一维,因为你有它们)

同样确保在您的预测输入中特征矩阵是二维的:

user_input = np.full(shape=(1,1),fill_value=1, dtype=np.int32)

如果您不熟悉 TensorFlow 和深度学习,这个推荐项目可能不是一个好的起点。 代码没有文档化,架构可能会令人困惑。

无论如何,为了回答您的问题,该模型不会采用单一输入进行预测。 查看代码,有 5 个输入(user_id、item_id、duplicate_mask、valid_pt_mask、label)但这实际上是用于训练。 如果只想做预测,其实只需要user_id和item_id就可以了。 该模型基于 user_id 和 item_id 交互进行预测,这就是您需要两者的原因。 但是,除非在进行预测时剪掉模型中不必要的部分,否则您不能直接这样做。 以下是在训练名为keras_model的模型对象后如何执行此操作的代码(我使用了 tf-model-official version 2.5.0 并且工作正常):

from tensorflow.keras import Model
import tensorflow as tf

inputUserIds = keras_model.input['user_id']
inputItemIds = keras_model.input['item_id']
# Cut off the unnecessary parts of the model for predictions.
# Specifically, we're removing the loss and metric layers in the architecture.
# Note: we are not training a new model, just taking the parts of the model we need.
outputs = keras_model.get_layer('rating').output
newModel = Model(inputs=[inputUserIds, inputItemIds], outputs=outputs)

## Make predictions for user 1 with items ids 1, 2, 3, 4, 5
# Make a user. Each row will be user_id 1. The shape of this tensor is (5,1)
userIds = tf.constant([1, 1, 1, 1, 1])[:, tf.newaxis]
# Make a tensor of items. Each row will be different item ids. The shape of this tensor is (5,1)
itemIds = tf.constant([1,2,3,4,5])[:, tf.newaxis]

# Make preds. This predicts for user id 1 and items ids 1,2,3,4,5.
preds = newModel.predict(x=[userIds, itemIds])

因此,如果您进行预测,您希望创建所有项目-用户组合,然后按降序对预测进行排序,同时跟踪每个用户的索引。 该用户的顶部项目将是该用户最有可能与之交互的模型预测,第二个项目将是第二个最有可能与之交互的模型预测,依此类推。

运行此 ncf_keras_main.py 文件时会创建一个名为“summaries”的文件夹。 如果您将 tensorboard 指向该文件夹,您可以在左上角的图形选项卡下探索模型架构。 它可能有助于更好地理解代码。 要运行 tensorboard,你打开一个终端并输入

tensorboard --logdir location_of_summaries_folder_here

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM