KERAS:CNN模型的预训练并将其用于CNN-LSTM模型之后

时间:2020-04-10 08:13:28

标签: tensorflow keras lstm cnn

我有这个cnn模型:

model = Sequential()
model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(n_rows,n_cols,1)))
model.add(Convolution2D(32, (3, 3), activation='relu'))
model.add(AveragePooling2D(pool_size=(1,3)))
#...
model.add(Flatten())

model.add(Dense(1024, activation='relu')) #needed?
model.add(Dense(3)) #default linear activation

训练

在我要使用此CNN的权重之后,将其用于CNN-LSTM模型中。

所以我有这样的东西:

#Load from previous CNN (pre-trained!) (up to Flatten()) and use TimeDistributed on them
model.add(LSTM(1024, activation='relu', return_sequences=True))
model.add(LSTM(1024, activation='relu'))
model.add(Dense(3))

这有意义吗?怎么可能呢? 非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以执行此操作。但是,为了成功实现此新模型,您需要使用Functional API(https://www.tensorflow.org/guide/keras/functional)来实现。

下面,我举一个示例,说明如何向预训练的模型中添加新图层。

# Initialize the base model
# Say that this is the cnn model which is pretrained
base_model = cnn_model(input_shape=(n_rows, n_cols, 1))
base_model.trainable = False

# Adding the first lstm layer
x = tf.keras.layers.LSTM(1024,activation='relu',return_sequences='True')(base_model.output)

# Adding the second lstm layer
x = tf.keras.layers.LSTM(1024, activation='relu',return_sequences='False')(x)

# Adding the output
output = tf.keras.layers.Dense(3,activation='linear')(x)

# Final model creation
model = Model(inputs=[base_model.input], outputs=[output])