【发布时间】:2020-03-24 21:19:41
【问题描述】:
我有三个稍后连接的输入。 其中两个输入的形状为 (1,),但第三个输入本身是一个列表(每个列表 25 个元素)。 我正在尝试将这 3 个输入到嵌入层。但是第三个(这是一个列表)会产生一个错误:ValueError:新数组的总大小必须保持不变
def rec(n_users, n_movies, n_factors, min_rating, max_rating):
user = Input(shape=(1,))
u = Embedding(n_users, n_factors, embeddings_initializer='he_normal',
embeddings_regularizer=l2(1e-6))(user)
u = Reshape((n_factors,))(u)
movie = Input(shape=(1,))
m = Embedding(n_movies, n_factors, embeddings_initializer='he_normal',
embeddings_regularizer=l2(1e-6))(movie)
m = Reshape((n_factors,))(m)
tags = Input(shape=(25,))
t = Embedding(500, n_factors)(tags)
t = Reshape((n_factors,))(t)
x = Concatenate()([u, m, t])
x = Dropout(0.05)(x)
x = Dense(10, kernel_initializer='he_normal')(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, kernel_initializer='he_normal')(x)
x = Activation('sigmoid')(x)
x = Lambda(lambda x: x * (max_rating - min_rating) + min_rating)(x)
model = Model(inputs=[user, movie, tags], outputs=x)
opt = Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=opt)
return model
X_train_array 如下所示:
[array([ 90, 291, 473, ..., 479, 5, 102], dtype=int64),
array([1829, 98, 1321, ..., 4601, 748, 4522], dtype=int64),
array([[ 0, 0, 0, ..., 401, 201, 100],
[ 0, 0, 0, ..., 235, 100, 385],
[ 0, 0, 0, ..., 439, 487, 385],
...,
[ 0, 0, 0, ..., 471, 235, 100],
[ 0, 0, 0, ..., 0, 0, 100],
[ 0, 0, 0, ..., 0, 0, 221]], dtype=int64)]
【问题讨论】:
-
这个模型在 TF 1.15 上编译得很好。你还有错误吗?如果是这样,你在哪一行代码得到这个?
-
@thushv89 t = Reshape((n_factors,))(t) 是行..说ValueError:新数组的总大小必须保持不变
-
我明白了。那么现在它是有道理的。对于
u和m,您只有一个输入,但对于t,它是25,所以很明显t不能重新整形为相同的大小u和m将被重新整形。你能告诉我每次重塑后你期望的输出大小吗? -
@thushv89 1,50 是我想要的..
-
我需要更多信息,
u和m将有[None, 50]输出,但问题是t。它将有一个[None, 25, 50]大小的输出。因此,如果您需要[None,1,50]输出,则需要在时间维度上减少(例如平均)t的嵌入。这就是你需要的。
标签: python-3.x machine-learning keras deep-learning