参考链接: 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不被正确地调用。