如何使用张量流的ncf模型进行预测?

时间:2020-03-12 01:56:01

标签: python tensorflow neural-network recommendation-engine tensorflow-model-garden

嗨,我是张量流和神经网络的新手。试图了解tensorflow官方模型仓库中的ncf recommendation model

我的理解是,您将使用输入层和学习层来构建模型。然后,创建一批数据以训练模型,然后使用测试数据评估模型。这是在此file中完成的。

但是,我很难理解输入层。

它显示在代码中

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?您是否应该提供模型返回的用户列表的用户列表?

2 个答案:

答案 0 :(得分:0)

我以前没有使用过ncf模型,但是您输入的训练数据是1个具有256个特征的样本,而不是256个具有1个特征的样本。只需翻转您的numpy数组,确保要素矩阵为2D,并且要素的数量是第一维。

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

...其他。 (嗯,标签应该保持一维不变)

类似地,在预测输入中确保特征矩阵为2D:

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

答案 1 :(得分:0)

如果您不熟悉 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 版本 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

enter image description here