Python 如何使用tensorflow将两个不同的CNN网络合并为一个网络
我有两个不同的CNN网络,如下所示:Python 如何使用tensorflow将两个不同的CNN网络合并为一个网络,python,tensorflow,Python,Tensorflow,我有两个不同的CNN网络,如下所示: class CNN_1(object): def __init__(self, max_input_right, max_input_left,list_ans,filter_sizes, embeddings,embedding_size): self.max_input_right = max_input_right self.max_input_left = max_input_left self.list_ans = list
class CNN_1(object):
def __init__(self, max_input_right, max_input_left,list_ans,filter_sizes, embeddings,embedding_size):
self.max_input_right = max_input_right
self.max_input_left = max_input_left
self.list_ans = list_ans
self.filter_sizes = filter_sizes
self.embeddings = embeddings
self.total_embedding_dim = embedding_size
def create_placeholder(self):
print('Create placeholders')
self.question = tf.placeholder(tf.int32,[None,self.max_input_left],name = 'input_question')
self.sample_set_1 = tf.placeholder(tf.int32, [None,self.max_input_right])
self.sample_set_2 = tf.placeholder(tf.int32, [None,self.max_input_right])
第二层CNN看起来也很相似,它们在其中有更多的功能来构建网络。现在我想建立第三个网络,合并这两个现有的网络
有人能建议如何使用tensorflow构建第三个网络吗?您可以使用Keras Functional API将它们合并到一个网络中。 下面是代码:
merged = Concatenate()([model_1, model_2])
model_final = Model(inputs=[model_1_input_shape, model_2_input_shape], outputs=[output])
model_final.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
如果要可视化最终模型,请执行以下操作:
from keras.utils import plot_model
plot_model(model_final, to_file='Summary.png')
在您自己的工作中,您可以使用任何优化器或Loss。希望它对您有所帮助。合并意味着什么?堆叠?平均值?这些CNN是预先训练好的吗?@Patwie合并意味着将它们集中在一起。是的,这些CNN都是预先训练过的,然后是复制品。堆叠是通过一个网络的输出而不是另一个网络的输入来完成的。并对加载过程进行了描述