如何将列表输入到嵌入层?

我有三个输入,这些输入后来被拼接在一起。其中两个输入的形状是(1,),但第三个输入本身就是一个列表(每个列表有25个元素)。我试图将这三个输入到一个嵌入层中,但第三个输入(即列表)会产生一个错误:ValueError: total size of new array must be unchanged

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)]

回答:

因此,这是我给你的建议。这里是我的解决方案的描述。你可以看到,问题出在问号所在的地方。你需要在这个点上进行一些转换,以便能够拼接并得到[None, 150]的形状。

enter image description here顺便说一下,请注意我做了一些小的改动(这些改动不应该影响解决方案的正确性)。

  • 移除了l2正则化
  • 没有创建Adam优化器,而是将字符串'adam'传递给优化器参数
import tensorflow.keras.backend as Kdef 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')(user)    u = Reshape((n_factors,))(u)    movie = Input(shape=(1,))    m = Embedding(n_movies, n_factors, embeddings_initializer='he_normal')(movie)    m = Reshape((n_factors,))(m)    tags = Input(shape=(25,))    t = Embedding(500, n_factors)(tags)    t = Lambda(lambda x: K.mean(x, axis=1))(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)    model.compile(loss='mean_squared_error', optimizer='adam')    print(model.summary())    return model

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注