在搭建pytorch神经网络时有疑惑:为什么别人的forward方法是callable的呢?
from torch import nn
import torch
class Mnn(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
output = input + 1
return output
mnn = Mnn()
x = torch.tensor(1.0)
output = mnn(x)
print(output)
可以看到结果是不会报错的:
F:\Anaconda3\envs\pytorch\python.exe F:\PycharmProjects\study1\nn_module.py
tensor(2.)
Process finished with exit code 0
原因:扒出父类Module的原稿: