torch.nn.Module.forward(*input)

参考链接: torch.nn.Module.forward(*input)

在这里插入图片描述
原文及翻译:

forward(*input)
方法: forward(*input)
    Defines the computation performed at every call.
    该方法定义了神经网络每次调用时都需要执行的前向传播计算.
    Should be overridden by all subclasses.
    所有的子类都必须要重写这个方法.

    Note  注意:

    Although the recipe for forward pass needs to be defined within 
    this function, one should call the Module instance afterwards 
    instead of this since the former takes care of running the 
    registered hooks while the latter silently ignores them.
    尽管在这个函数中定义了神经网络前向传递计算的具体流程和特定的细节,但是
    此后我们应该调用这个模块实例,而不是调用这个方法【
    	译者注: model = Model,之后应该调用model(input_data),
    		这里实际上执行的时model.__call__(input_data),
    		而不应该调用model.forward(input_data),因为前者(:model(input_data))会小心地处理并执行相关地注册登记好
    的钩子函数,然而后者(:model.forward(input_data))仅仅静默地忽略掉
    这些钩子函数.

在这里插入图片描述

在这里插入图片描述

Calling an instance of nn.Module with a set of arguments ends up calling a method
named forward with the same arguments. The forward method is what executes the
forward computation, while __call__ does other rather important chores before and
after calling forward. So, it is technically possible to call forward directly, and it will
produce the same output as __call__, but this should not be done from user code:

__call__函数会自动调用forward函数,而forward函数所做的就是执行前向传递的计算。
只不过__call__函数除了调用forward函数之外,还会在调用该函数之前和之后做一些必
要的准备工作。我们虽然也可以直接调用forward函数,并且返回和__call__一样地结
果,但是在用户代码中不应该这样做,尽管从技术上来说可以调用。

在这里插入图片描述

As we can see, there are a lot of hooks that won’t get called properly if we just 
use .forward(…) directly.
如果我们直接调用forward函数,那么就会有很多钩子函数hook不被正确地调用。
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值