是否可以在keras中添加外部输入到Merge图层?我有简单的嵌入,我想与外部值结合,但每次我尝试做我总是得到错误。有没有办法将外部输入添加到Keras图层?
models = []
inputs =Input(shape=(10,))
models.append(inputs)
for i in range(2):
model_s = Sequential()
model_s.add(Embedding(1115, 10, input_length=1, name='Hb_{}'.format(i)))
model_s.add(Reshape(target_shape=(10,)))
models.append(model_s)
model = Sequential()
model.add(Merge(models, mode='concat'))
model.add(Dense(1000, kernel_initializer='uniform'))
model.add(Activation('relu'))
model.add(Dense(500, kernel_initializer='uniform'))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.summary()
错误
Tensor' object has no attribute 'get_output_shape_at'
这是用于测试的小代码。
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
m={'Hb_0_input': np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9]),'Hb_1_input': np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9]), 'x': np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])}
y=np.array([0, 1, 0, 1, 0, 1, 0, 0, 0, 0])
model.fit(m, y)
答案 0 :(得分:2)
不确定您的意图是什么,但是您将模型和张量混合在同一列表models = [inputs, model1, model2]
中。这是错误的原因。
现在,我们不知道你有什么样的输入,所以我们无法进一步帮助,但假设一些事情(可能是错的),这段代码可以帮助你:
inputForEmbedding = Input((length,)) #where length seems to be 1
extraInput = Input(shapeOfTheExtraInput) #I don't know this shape
model1Out = Embedding(1115, 10, input_length=1 name='Hb_{}'.format(i))(inputForEmbedding)
model1Out = Reshape((10,))(model1Out)
....you must make the shapes compatible for `model1Out` and `extraInput`...
outputs = Concatenate(axis=...)([model1Out,extraInput])
outputs = Dense(1000, kernel_initializer='uniform')(outputs)
outputs = Activation('relu')(outputs)
outputs = Dense(500, kernel_initializer='uniform')(outputs)
outputs = Activation('relu')(outputs)
outputs = Dense(1)(outputs)
outputs = Activation('sigmoid')(outputs)
model = Model([inputForEmbedding, extraInput])
model.summary()