我正在尝试使用 Keras 来近似一个函数,该函数可以将五个输入平滑地映射到一个概率,但似乎已经达到了极限。这里提出了类似的问题(Keras 回归来近似十维函数的函数(目标:损失< 1e-7)),我发现那里提出的架构,即:
model = Sequential()
model.add(Dense(128,input_shape=(5,), activation='tanh'))
model.add(Dense(64,activation='tanh'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='adam', loss='mae')
给了我最好的结果,当批量大小为 1000 时,验证数据的最佳损失收敛到 7e-4 左右。添加或删除更多神经元或层似乎会降低准确性。 Dropout正则化也会降低准确性。我目前正在使用 1e7 个训练样本,需要两天的时间才能生成(因此希望近似这个函数)。我想将 mae 减少另一个数量级,有人有任何建议如何做到这一点吗?
我建议使用keras回调ReduceLROnPlateau,文档是这里和ModelCheckpoint,文档是这里。首先,将其设置为监控验证损失,如果在固定数量(耐心)的连续 epoch 后损失未能减少,则会将学习率降低一个因子(因子)。对于第二个,还监视验证损失并将验证损失最低的模型的权重保存到目录中。训练后加载权重并使用它们来评估或预测测试集。我的代码实现如下所示。
checkpoint=tf.keras.callbacks.ModelCheckpoint(filepath=save_loc, monitor='val_loss', verbose=1, save_best_only=True,
save_weights_only=True, mode='auto', save_freq='epoch', options=None)
lr_adjust=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=1, verbose=1, mode="auto",
min_delta=0.00001, cooldown=0, min_lr=0)
callbacks=[checkpoint, lr_adjust]
history = model.fit_generator( train_generator, epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,validation_data=validation_generator,
validation_steps=VALIDATION_STEPS, callbacks=callbacks)
model.load_weights(save_loc) # load the saved weights
# after this use the model to evaluate or predict on the test set.
# if you are satisfied with the results you can then save the entire model with
model.save(save_loc)