【问题标题】:Where can I see the source code for pytorch's MSELoss?在哪里可以看到 pytorch MSELoss 的源代码?
【发布时间】:2018-06-21 12:04:55
【问题描述】:

我使用 U-NET 网络来训练我的数据。 但是我需要修改它的损失函数,以减少低于1的像素损失,以减少负例对网络权重的影响。但是我在pycharm MSELOSS中打开了源码,看到这个:

class MSELoss(_Loss):
    r"""Creates a criterion that measures the mean squared error between
    `n` elements in the input `x` and target `y`:

    :math:`{loss}(x, y)  = 1/n \sum |x_i - y_i|^2`

    `x` and `y` arbitrary shapes with a total of `n` elements each.

    The sum operation still operates over all the elements, and divides by `n`.

    The division by `n` can be avoided if one sets the internal variable
    `size_average` to `False`.

    """
    pass

我得不到任何有用的东西。

【问题讨论】:

    标签: python deep-learning pytorch loss-function


    【解决方案1】:

    你去:https://github.com/pytorch/pytorch/blob/master/torch/nn/functional.py#L1423 但是,它调用 C api

    def mse_loss(input, target, size_average=True, reduce=True):
        """
        mse_loss(input, target, size_average=True, reduce=True) -> Variable
        Measures the element-wise mean squared error.
        See :class:`~torch.nn.MSELoss` for details.
        """
        return _pointwise_loss(lambda a, b: (a - b) ** 2, torch._C._nn.mse_loss,
    input, target, size_average, reduce)
    
    def own_mse_loss(input, target, size_average=True):
        L = (input - target) ** 2
        return torch.mean(L) if size_average else torch.sum(L)
    

    【讨论】:

    • 我可以使用 python 在 pytorch 中实现 MSEloss 吗?@Manuel Lagunas
    • 绝对是 应该不难。我在答案中添加了一些代码,以便您了解如何实现它。请记住,我没有测试/调试它:)
    猜你喜欢
    • 2016-04-04
    • 2010-09-20
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多