Keras 是否可以使用两个输入的相同模型进行训练?
你好,我有个问题要问凯拉斯 目前我想实现一些网络 使用相同的cnn模型,并使用两幅图像作为cnn模型的输入 并利用cnn模型的两个结果,提供给稠密模型 比如说Keras 是否可以使用两个输入的相同模型进行训练?,keras,Keras,你好,我有个问题要问凯拉斯 目前我想实现一些网络 使用相同的cnn模型,并使用两幅图像作为cnn模型的输入 并利用cnn模型的两个结果,提供给稠密模型 比如说 def cnn_model(): input = Input(shape=(None, None, 3)) x = Conv2D(8, (3, 3), strides=(1, 1))(input) x = GlobalAvgPool2D()(x) model = Model(input, x) return
def cnn_model():
input = Input(shape=(None, None, 3))
x = Conv2D(8, (3, 3), strides=(1, 1))(input)
x = GlobalAvgPool2D()(x)
model = Model(input, x)
return model
def fc_model(cnn1, cnn2):
input_1 = cnn1.output
input_2 = cnn2.output
input = concatenate([input_1, input_2])
x = Dense(1, input_shape=(None, 16))(input)
x = Activation('sigmoid')(x)
model = Model([cnn1.input, cnn2.input], x)
return model
def main():
cnn1 = cnn_model()
cnn2 = cnn_model()
model = fc_model(cnn1, cnn2)
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(x=[image1, image2], y=[1.0, 1.0], batch_size=1, ecpochs=1)
我想实现类似这样的模型,并训练模型
但我收到如下错误消息:
'所有图层名称都应该是唯一的'
实际上我只想使用一个CNN模型作为特征提取器,最后使用两个特征预测一个浮点值为0.0~1.0
所以整个系统-->>
使用两幅图像,从同一CNN模型中提取特征,并将特征提供给稠密模型,得到一个浮点值
请帮我实施这个系统,以及如何进行培训
谢谢请参阅Keras文档中关于共享层的部分: 上面文档中的代码片段演示了这一点:
# This layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(64)
# When we reuse the same layer instance
# multiple times, the weights of the layer
# are also being reused
# (it is effectively *the same* layer)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)
# We can then concatenate the two vectors:
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)
# And add a logistic regression on top
predictions = Dense(1, activation='sigmoid')(merged_vector)
# We define a trainable model linking the
# tweet inputs to the predictions
model = Model(inputs=[tweet_a, tweet_b], outputs=predictions)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit([data_a, data_b], labels, epochs=10)
错误消息与您提供的代码不匹配,请提供生成真实错误消息的示例。