Keras串联

时间:2018-09-28 15:56:40

标签: python machine-learning keras keras-layer

我正在使用Keras,但出现此错误。我该如何解决?

这是我的代码:

cbow_words = Sequential()
cbow_words.add(Embedding(input_dim=V1, output_dim=dim, input_length=window_size*2 ,embeddings_constraint=non_neg()))#modifié
cbow_words.add(Lambda(lambda x: K.mean(x, axis=1), output_shape=(dim,)))
cbow_words.add(Dense(V1, activation='softmax'))

cbow_texts = Sequential()
cbow_texts.add(Embedding(input_dim=V2, output_dim=dim, input_length=1,embeddings_constraint=non_neg()))
cbow_texts.add(Lambda(lambda x: K.mean(x, axis=1), output_shape=(dim,)))
cbow_texts.add(Dense(V2, activation='softmax'))

cbow=Concatenate([cbow_words,cbow_texts])

cbow.compile(loss=loss, optimizer=optimizers.Adadelta(lr=lr, rho=0.95, epsilon=None, decay=0.0))

我遇到了这个问题:

-------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-40-b94a3567fc00> in <module>()
     11 cbow=Concatenate([cbow_words,cbow_texts])
     12 
---> 13 cbow.compile(loss=loss, optimizer=optimizers.Adadelta(lr=lr, rho=0.95, epsilon=None, decay=0.0))

AttributeError: 'Concatenate' object has no attribute 'compile'

1 个答案:

答案 0 :(得分:1)

您正在将Sequential模型与功能组件混合在一起。 Concatenate将张量作为输入,而不是Sequential模型。

由于有两个输入,因此建议您使用functional API,在您的情况下,它会导致结构大致如下:

from keras.models import Model
from keras.layers import Input, Dense, concatenate

words_in = Input((10,))
words = Dense(10, activation='softmax')(words_in)

texts_in = Input((10,))
texts = Dense(10, activation='softmax')(texts_in)

concat = concatenate([words, texts])

cbow = Model(inputs=[words_in, texts_in], output=concat)

cbow.compile(loss="categorical_crossentropy", optimizer="adagrad")