Pytorch-F函数(torch.nn.functional)和nn(torch.nn)的区别

本文探讨了PyTorch中torch.nn.functional(简称F)和torch.nn(简称nn)的区别。nn主要用于定义深度学习模型的类结构,而F包含各种实际操作的函数,如激活函数。nn.ReLu是在模型初始化时使用的类,F.relu则是直接对输入进行ReLU运算的函数。两者在使用场景上有明确的区分。
摘要由CSDN通过智能技术生成

torch.nn的实现去调用torch.nn.functional,实现方式是一致的。它们的区别是:
nn可以写在深度学习模型的初始化中,其是一个类;F函数不可以,它是一个实际的函数,其需要输入实际的input
例如nn.ReLu和F.relu,其代码如下。
代码:

class ReLU(Module):
    r"""Applies the rectified linear unit function element-wise:

    :math:`\text{ReLU}(x) = (x)^+ = \max(0, x)`

    Args:
        inplace: can optionally do the operation in-place. Default: ``False``

    Shape:
        - Input: :math:`(N, *)` where `*` means, any number of additional
          dimensions
        - Output: :math:`(N, *)`, same shape as the input

    .. image:: ../scripts/activation_images/ReLU.png

    Examples::

        >>> m = nn.ReLU()
        >>> input = torch.randn(2)
        >>> output = m(input)


      An implementation of CReLU - https://arxiv.org/abs/1603.05201

        >>> m = nn.ReLU()
  
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值