【pytorch】详解vanilla(BCEWithLogitsLoss )、lsgan(nn.MSELoss)

有如下代码块

       self.gan_mode = gan_mode
        if gan_mode == 'lsgan':
            self.loss = nn.MSELoss()
        elif gan_mode == 'vanilla':
            self.loss = nn.BCEWithLogitsLoss()
        elif gan_mode in ['wgangp']:
            self.loss = None

vanilla 即为 BCEWithLogitsLoss 

参见:https://blog.csdn.net/qq_22210253/article/details/85222093


lsgan 在pytorch中为 nn.MSELoss(),均方误差

公式:

MSELOSS = \frac{1}{2n}\sum||y_{n}-x_{n}||^{2}

 其中y为target,x为模型输出值

示例:

import torch
import torch.nn as nn

output = torch.rand(2,2)
print(output)

 tensor([[0.1234, 0.8351],
        [0.9274, 0.8286]])

target = torch.FloatTensor([[0,1],[1,0]])
print(target)

tensor([[0., 1.],
        [1., 0.]])

利用nn.MSELoss()计算损失:

crit = nn.MSELoss()
cost = crit(input,target)

输出结果为:

利用公式手工验证计算:(注意除的数为2n,即为4)

MSELoss_handle = ((0-0.1234)*(0-0.1234) + (1-0.8351)*(1-0.8351))/4 + ((1-0.9274)*(1-0.9274) + (0-0.8286)*(0-0.8286))/4

输出结果为:

四舍五入之后结果一致!


wgangp 参见

https://blog.csdn.net/weixin_37993251/article/details/87120269

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值