我有两种不同类型的数据(图像体积和坐标),我想在图像体积数据上使用卷积神经网络,然后在这之后我想附加一些额外的信息(即坐标量)。
独立地,这应该为我的函数创建一个非常可靠的预测器。我如何使用 Keras 实现它。
我在网上找到的唯一答案要么模棱两可,要么正在使用我必须使用的已弃用方法。但我真的很想使用当前的 API 来实现它,这样我就可以更轻松地保存模型供以后使用。
model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv3D(64, (3, 3, 3), activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
print(model.output_shape)
# The additional data (the coordinates x,y,z)
extra = Sequential()
extra.add(Activation('sigmoid', input_shape=(3,)))
print(extra.output_shape)
merged = Concatenate([model, extra])
# New model should encompass the outputs of the convolutional network and the coordinates that have been merged.
# But how?
new_model = Sequential()
new_model.add(Dense(128, activation='relu'))
new_model.add(Dropout(0.8))
new_model.add(Dense(32, activation='sigmoid'))
new_model.add(Dense(num_classes, activation='softmax'))
new_model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
最佳答案
顺序模型不适合创建带有分支的模型。
您可以将两个独立的模型作为顺序模型,就像您所做的那样,但是从Concatenate
开始,您应该开始使用功能模型 API。
想法是获取两个模型的输出张量,并将它们馈送到其他层以获得新的输出张量。
因此,考虑到您有 model
和 extra
:
mergedOutput = Concatenate()([model.output, extra.output])
这个 mergetOutput
是一个张量。您可以使用此张量创建模型的最后一部分,也可以独立创建最后一部分,然后在此张量上调用它。如果您想单独训练每个模型(似乎不是您的情况),则第二种方法可能会很好。
现在,将新模型创建为函数式 API 模型:
out = Dense(128, activation='relu')(mergetOutput)
out = Dropout(0.8)(out)
out = Dense(32, activation='sigmoid')(out)
out = Dense(num_classes, activation='softmax')(out)
new_model = Model(
[model.input, extra.input], #model with two input tensors
out #and one output tensor
)
一种更简单的方法是采用您已经创建的所有三个模型并使用它们创建一个组合模型:
model = Sequential() #your first model
extra = Sequential() #your second model
new_model = Sequential() #all these three exactly as you did
#in this case, you just need to add an input shape to new_model, compatible with the concatenated output of the previous models.
new_model.add(FirstNewModelLayer(...,input_shape=(someValue,)))
像这样加入他们:
mergedOutput = Concatenate()([model.output, extra.output])
finalOutput = new_model(mergedOutput)
fullModel = Model([model.input,extra.input],finalOutput)
关于python - 在 Keras 中合并两个不同的深度学习模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47035367/