“合并”替换Keras / Tensorflow / Python3

时间:2018-10-18 06:48:16

标签: python tensorflow merge keras lstm

合并未与Keras / Tensorflow / Python3版本一起运行。在以前的版本中,Merge正在运行。 但是现在,它没有运行。所以我认为我应该将此代码转换为替换“ Merge”。新Keras / Tensorflow / Python3版本中缺少“合并”。

如何重写Merge(...,mode ='ave')和Merge(...,mode ='concat')吗?

我的代码部分在下面:

...

from keras.layers import Merge
...
left_branch = Sequential()
left_branch.add(Convolution3D...
...
right_branch = Sequential()
right_branch.add(Convolution3D...
...
merged = Merge([left_branch, right_branch], mode='ave')
lstm_branch = Sequential()
lstm_branch.add(Embedding(input_dim=7, output_dim=8, input_length=7))
lstm_branch.add(LSTM(output_dim=8, input_shape=(7, ) ))
lstm_branch.add(Dense(32, activation='relu'))

model = Sequential()
model.add(merged)
merged_2 = Merge([model, lstm_branch], mode='concat')
model_2 = Sequential()
model_2.add(merged_2)
model_2.add(Dense(nb_classes,init='normal'))
model_2.add(Activation('softmax'))
model_2.compile(loss='categorical_crossentropy', optimizer='RMSprop', metrics=['accuracy'])

1 个答案:

答案 0 :(得分:1)

合并已被连接和平均取代, 这是Keras新版本的更改。现在,您应该像这样实现它们

from keras.layers.merge import Concatenate, Average   

layer_merge = Concatenate()( [layer1, layer2] )
layer_avg   = Average()( [layer1, layer2] )

完整文档here