Слой dot_1 был вызван со входом, который не является символическим тензором. Все входы в слой должны быть тензорами - PullRequest
3 голосов
/ 10 марта 2019

Я использую модель в наборе данных MovieLens. Я хотел объединить две последовательности в точечное произведение керас. Однако я получил следующую ошибку:

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)

1 Ответ

1 голос
/ 10 марта 2019

Функциональный API Keras предоставляет более гибкий способ определения таких моделей.

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