微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Keras将光滑的多维函数近似为1e-4的误差

如何解决使用Keras将光滑的多维函数近似为1e-4的误差

我正在尝试近似一个函数,该函数使用Keras将五个输入平滑地映射到单个概率,但是似乎已经达到极限。对于十维函数,这里(Keras Regression to approximate function (goal: loss < 1e-7))出现了类似的问题,我发现那里提出的体系结构是:

model = Sequential()

model.add(密集(128,input_shape =(5,),激活='tanh'))

model.add(Dense(64,activation ='tanh'))

model.add(Dense(1,activation ='Sigmoid'))

model.compile(optimizer ='adam',loss ='mae')

给我最好的结果,当批量为1000时,验证数据上的最佳损失达到7e-4左右。添加删除更多的神经元或层似乎降低了准确性。辍学正则化也会降低准确性。我目前正在使用1e7训练样本,该样本花了两天的时间才能生成(因此需要逼近该功能)。我想再减少一个数量级,有人对此有何建议?

解决方法

我建议使用利用keras回调ReduceLROnPlateau,文档在[here] [1]和ModelCheckpoint,文档在[here。] [2]。首先,将其设置为监控验证损失,如果在连续的固定次数(耐心)后损失未能减少,则学习速度将降低一个因数。对于第二个,还监视验证损失并将验证损失最小的模型的权重保存到目录中。训练后,加载权重,并使用它们来评估或预测测试集。我的代码实现如下所示。

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,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)


  [1]: https://keras.io/api/callbacks/reduce_lr_on_plateau/
  [2]: https://keras.io/api/callbacks/model_checkpoint/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。