取消Pytorch警告

问题描述

在训练模型过程中,打印损失函数值的过程中如果出现很多如下警告信息,非常影响观感和对比。
在这里插入图片描述

警告信息为:UserWarning: size_average and reduce args will be deprecated,please use reduction=‘mean’ instead. warnings.warn(warning.format(ret))

为pytorch不同版本进行更新迭代时引起的警告,某些参数被取代了
更多细节参考Pytorch版本代码修正

解决方案

criterion = torch.nn.BCELoss(size_average=True)
改为:
criterion = torch.nn.BCELoss(reduction='mean')
criterion = torch.nn.BCELoss(size_average=False)
改为:
criterion = torch.nn.BCELoss(reduction='sum')

其它损失函数更改方法类似

更改后效果:没了警告看着就很舒适
在这里插入图片描述

注:此处如果使用如下代码来取消全文警告,仍然会打印警告信息。

import warnings
warnings.filterwarnings("ignore")

更多问题修正参考

更多pytorch不同版本进行更新迭代时引起的警告,在下面这个链接中总结了对应的修正方法。
Pytorch版本代码修正

猜你喜欢

转载自blog.csdn.net/weixin_45577864/article/details/119090269
今日推荐