【问题标题】:why before embedding, have to make the item be sequential starting at zero为什么在嵌入之前,必须使项目从零开始连续
【发布时间】:2020-02-21 15:22:21
【问题描述】:

我从这个博尔格 Deep Learning With Keras: Recommender Systems 学习协同过滤。

教程很好,代码运行良好。 Here is my code.

作者说,有一件事让我很困惑,

用户/电影字段目前是非连续整数,表示该实体的某个唯一 ID。我们需要它们从零开始按顺序用于建模(稍后您会看到原因)。

user_enc = LabelEncoder()
ratings['user'] = user_enc.fit_transform(ratings['userId'].values)
n_users = ratings['user'].nunique()

但他似乎没有提及原因,我不需要这样做。有人可以为我解释一下吗?

【问题讨论】:

    标签: python-3.x tensorflow neural-network embedding collaborative-filtering


    【解决方案1】:

    假定嵌入是连续的。

    Embedding的第一个输入是输入维度。 因此,如果输入超出输入维度,则忽略该值。 Embedding 假设输入中的最大值是输入维度 -1(从 0 开始)。

    https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding?hl=ja

    例如,以下代码将仅为输入 [4,3] 生成嵌入,并且将跳过输入 [7, 8],因为输入维度为 5。

    我觉得用tensorflow解释比较清楚;

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Embedding
    
    model = Sequential()
    model.add(Embedding(5, 1, input_length=2))
    input_array = np.array([[4,3], [7,8]])
    model.compile('rmsprop', 'mse')
    output_array = model.predict(input_array)
    

    您可以将输入维度增加到 9,然后您将获得两个输入的嵌入。

    您可以将原始数据集中的输入维度增加到最大数 + 1,但这并不有效。 它实际上类似于 one-hot 编码,顺序数据可以节省大量内存。

    【讨论】:

    • “将正整数(索引)转换为固定大小的密集向量。”谢谢。
    猜你喜欢
    • 2014-04-19
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2010-10-15
    • 2015-05-31
    相关资源
    最近更新 更多