我试图连续训练不同的模型,而无需一直重新运行程序或更改代码,因此我可以让我的PC训练不同的模型
我使用for循环,同时从字典中获取不同的信息,以每次构建不同的模型,因此,每次调用de函数时,我都可以训练新模型,以测试不同设置的准确性,以了解其中的一个每种情况下最好的
def create_model(modeltoload):
model = Sequential()
previsores, alto, baixo, fechado, aberto = get_train_data(modeltoload)
if modeltoload['Type'] == 'LSTM':
if len(modeltoload['Layers']) == 1:
model.add(LSTM(units=modeltoload['Layers'][0], activation='tanh',
input_shape=(previsores.shape[1], modeltoload['Entry'])))
model.add(Dropout(0.3))
else:
model.add(LSTM(units=modeltoload['Layers'][0], activation='tanh', return_sequences=True,
input_shape=(previsores.shape[1], modeltoload['Entry'])))
model.add(Dropout(0.3))
for i in range(1, len(modeltoload['Layers'])):
if i == (len(modeltoload['Layers'])-1):
model.add(LSTM(units=modeltoload['Layers'][i], activation='tanh'))
else:
model.add(LSTM(units=modeltoload['Layers'][i], activation='tanh', return_sequences=True))
model.add(Dense(units=1, activation='relu'))
if modeltoload['Type'] == 'DENSE':
model.add(Dense(units=modeltoload['Layers'][0], activation='relu', input_dim=modeltoload['Entry']*5+1))
model.add(Dropout(0.1))
for i in range(1, len(modeltoload['Layers'])):
model.add(Dense(units=modeltoload['Layers'][i], activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(units=1, activation=modeltoload['Activation']))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_accuracy'])
return model
然后
def train_model(modeltoload):
previsores, classe, baixo, fechado, aberto = get_train_data(modeltoload)
model = create_model(modeltoload)
history1 = model.fit(previsores, classe, epochs=1000, batch_size=modeltoload['Batch'],
callbacks=[es, rlr, mcp, csv], shuffle='batch', verbose=2, validation_split=0.1)
k.clear_session()
del model
return history1
问题在于,当我开始第一次培训课程时,一切都变得像这样:
Training: DENSE/60N-14B-190E-tanh.h5
Train on 2575 samples, validate on 287 samples
Epoch 1/1000
Epoch 00001: loss improved from inf to 2.50127, saving model to DENSE/60N-14B-190E-tanh.h5
- 1s - loss: 2.5013 - binary_accuracy: 0.4711 - val_loss: 1.1434 - val_binary_accuracy: 0.5017
Epoch 2/1000
.
.
.
Epoch 307/1000
Epoch 00307: loss did not improve
- 0s - loss: 0.5200 - binary_accuracy: 0.7522 - val_loss: 0.8077 - val_binary_accuracy: 0.5401
Epoch 00307: early stopping
但是当创建第二个等模型时,损失不是从[inf]开始,而是从先前训练的最后一个值开始:
Training: DENSE/60N-14B-220E-tanh.h5
Train on 2548 samples, validate on 284 samples
Epoch 1/1000
Epoch 00001: loss did not improve
- 1s - loss: 1.3203 - binary_accuracy: 0.5063 - val_loss: 0.7724 - val_binary_accuracy: 0.5246
Epoch 2/1000
Epoch 00002: loss did not improve
- 0s - loss: 0.7366 - binary_accuracy: 0.4945 - val_loss: 0.7247 - val_binary_accuracy: 0.5000
即使使用
k.clear_session()
del model
似乎我正在加载有关最后训练的模型的一些先前信息。有人对此问题有见识吗?
答案 0 :(得分:0)
我从训练进度输出中假设您正在使用Keras的ModelCheckpoint回调。如果您对多个训练运行使用相同的ModelCheckpoint,则只有在丢失新模型比以前保存的模型有所改善的情况下,它才会保存新模型。
要解决此问题,只需在train_model
函数中每次生成ModelCheckpoint对象。