【Pytorch】nn.Relu and F.relu

在这里插入图片描述


文章目录


torch.nn.Relu and torch.nn.functional.relu 结果一致,不同点如下

1 nn.Relu

不同的是 nn.Relu 作为一层结构,必须添加到 nn.Module 容器中才能使用

使用方式如下,定义时不接输入,定义好后,使用 Relu()(input) 进行参数传递

class BasicBlock(nn.Module):
    expansion = 1
    def __init__(self):
        super(BasicBlock, self).__init__()
        self.relu = nn.ReLU()
        ...
    def forward(self, x):
        x = self.relu(x)

2 F.relu

F.relu 是函数,可以直接在 forward 中调用,使用 relu(input) 进行参数传递,具体方式如下

import torch.nn.functional as F
class BasicBlock(nn.Module):
    expansion = 1
    def __init__(self):
        super(BasicBlock, self).__init__()
        ...
    def forward(self, x):
        x = F.relu(x)

注意,torch.nn 中的代码实现基本都会调用 torch.nn.functional 中的方法

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值