[英]Tensorflow: How to sensibly merge two neural network layers into one
[英]How to merge two different CNN network as one network using 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_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。希望它對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.