在 for 循环中创建新的顺序模型(使用 Keras)



我想尝试使用不同的超参数训练我的模型,所以我设置了一系列嵌套的循环来迭代它们。

model = None
batch_generator = None
for sequence_length in all_sequence_length:
for label_periods in all_label_periods:
for num_layers in all_num_layers:
for num_units in all_num_units:
loadFiles()
createmodel()
trainmodel()

第一次迭代创建如下模型:

Layer (type)                 Output Shape              Param #
=================================================================
cu_dnnlstm_1 (CuDNNLSTM)     (None, 100, 75)           45300
_________________________________________________________________
dropout_1 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_2 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_2 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_3 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_3 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_4 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_4 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_5 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_5 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 228
=================================================================

然后,我调用model.fit_generator()来训练模型,并且执行良好。然后在下一次循环迭代中再次创建模型,摘要如下所示:

Layer (type)                 Output Shape              Param #
=================================================================
cu_dnnlstm_6 (CuDNNLSTM)     (None, 100, 75)           45300
_________________________________________________________________
dropout_6 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_7 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_7 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_8 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_8 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_9 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_9 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
cu_dnnlstm_10 (CuDNNLSTM)     (None, 100, 75)           45600
_________________________________________________________________
dropout_10 (Dropout)          (None, 100, 75)           0
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 228
=================================================================

您将看到图层 ID 已递增,这让我感到惊讶,因为我为模型变量创建了一个新的顺序模型,因此我本以为会有与第一个相同的摘要。

当我调用model.fit_generator()时,出现此错误:

InvalidArgumentError (参见上文的回溯(:必须输入一个值 对于占位符张量"cu_dnnlstm_1_input",具有 dtype 浮点数和形状 [?,100,74]

您将看到它需要cu_dnnlstm_1_input的输入,这是第一次迭代模型上的输入,而不是第二次模型上的cu_dnnlstm_6输入。 我用于创建模型的代码是在函数中完成的:

def createmodel():
global model
model = Sequential()
model.add( CuDNNLSTM(units=num_units, return_sequences=True, input_shape=(sequence_length, features_size) ) )
for _ in range(num_layers):
model.add( Dropout(dropout_rate) )
model.add( CuDNNLSTM(units=num_units, return_sequences=True) )
model.add( Dropout(dropout_rate) )
model.add( CuDNNLSTM(units=num_units, return_sequences=False) )
model.add( Dropout(dropout_rate) )
model.add( Dense(labels_size) )
model.compile(loss='mean_absolute_error', optimizer='adam')
model.summary()

模型使用以下函数进行训练:

def trainmodel():
global model
model.fit_generator(generator=batch_generator,
epochs=num_epochs,
steps_per_epoch=num_steps_per_epoch,
validation_data=validation_data_tuple,
callbacks=callbacks)

谁能发现我的"故意"错误?

我想这是因为 Keras 试图在同一张量流图上创建不同的模型。由于您的模型具有不同的体系结构,因此无法做到这一点。

尝试导入张量流:

import tensorflow as tf

并通过以下方式修改您的循环:

for sequence_length in all_sequence_length:
for label_periods in all_label_periods:
for num_layers in all_num_layers:
for num_units in all_num_units:
graph = tf.Graph()
with tf.Session(graph=graph):
loadFiles()
createmodel()
trainmodel()

相关内容

  • 没有找到相关文章

最新更新