我正在使用MovieLens数据集上的模型。我想在keras的点积中合并两个序列。但是我收到以下错误:
Layer dot_1 was called with an input that isn't a symbolic tensor. Received
type: <class 'keras.engine.sequential.Sequential'>. Full input:
[<keras.engine.sequential.Sequential object at 0x00000282DAFCC710>,
<keras.engine.sequential.Sequential object at 0x00000282DB172C18>]. All
inputs to the layer should be tensors.
下面的代码是模型的构建方式。错误来自以下行:
merged = dot([P, Q], axes = 1, normalize = True)
max_userid,max_movieid和K_FACTORS已经定义。有人可以帮我解决这个错误吗?
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Dense, Embedding, Reshape, Concatenate, dot
from keras import Input
from keras.optimizers import Adagrad
# Define model
# P is the embedding layer that creates an User by latent factors matrix.
# If the intput is a user_id, P returns the latent factor vector for that user.
P = Sequential()
P.add(Embedding(max_userid, K_FACTORS, input_length=1))
P.add(Reshape((K_FACTORS,)))
# Q is the embedding layer that creates a Movie by latent factors matrix.
# If the input is a movie_id, Q returns the latent factor vector for that movie.
Q = Sequential()
Q.add(Embedding(max_movieid, K_FACTORS, input_length=1))
Q.add(Reshape((K_FACTORS,)))
mergedModel = Sequential()
merged = dot([P, Q], axes = 1, normalize = True)
mergedModel.add(merged)
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
答案 0 :(得分:0)
Keras功能API提供了更灵活的定义方式 这样的模型。
from keras.layers import Input
input_1 = Input(shape=(1,))
input_2 = Input(shape=(1,))
P = Reshape((K_FACTORS,))(Embedding(max_userid, K_FACTORS, input_length=1)(input_1))
Q = Reshape((K_FACTORS,))(Embedding(max_userid, K_FACTORS, input_length=1)(input_2))
P_dot_Q = dot([P, Q], axes = 1, normalize = True)
model = Model(inputs=[input_1,input_2], outputs=P_dot_Q)
#print(model.summary())
#model.compile(loss = 'MSE', optimizer='adam',metrics = ['accuracy'])
#model.fit([np.array([1]), np.array([1])],[1])