温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nn.Module接口怎么在pytorch中使用

发布时间:2021-03-09 16:39:17 来源:亿速云 阅读:193 作者:Leah 栏目:开发技术

nn.Module接口怎么在pytorch中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

torch.nn 是专门为神经网络设计的模块化接口,nn构建于autgrad之上,可以用来定义和运行神经网络
nn.Module 是nn中重要的类,包含网络各层的定义,以及forward方法

查看源码

初始化部分:

def __init__(self):   self._backend = thnn_backend   self._parameters = OrderedDict()   self._buffers = OrderedDict()   self._backward_hooks = OrderedDict()   self._forward_hooks = OrderedDict()   self._forward_pre_hooks = OrderedDict()   self._state_dict_hooks = OrderedDict()   self._load_state_dict_pre_hooks = OrderedDict()   self._modules = OrderedDict()   self.training = True

属性解释:

  • _parameters:字典,保存用户直接设置的 Parameter

  • _modules:子 module,即子类构造函数中的内容

  • _buffers:缓存

  • _backward_hooks与_forward_hooks:钩子技术,用来提取中间变量

  • training:判断值来决定前向传播策略

方法定义:

def forward(self, *input):  raise NotImplementedError

没有实际内容,用于被子类的 forward() 方法覆盖

且 forward 方法在 __call__ 方法中被调用:

def __call__(self, *input, **kwargs):  for hook in self._forward_pre_hooks.values():     hook(self, input)   if torch._C._get_tracing_state():     result = self._slow_forward(*input, **kwargs)   else:     result = self.forward(*input, **kwargs)   ...   ...

对于自己定义的网络,需要注意以下几点:

1)需要继承nn.Module类,并实现forward方法,只要在nn.Module的子类中定义forward方法,backward函数就会被自动实现(利用autograd机制)
2)一般把网络中可学习参数的层放在构造函数中__init__(),没有可学习参数的层如Relu层可以放在构造函数中,也可以不放在构造函数中(在forward函数中使用nn.Functional)
3)在forward中可以使用任何Variable支持的函数,在整个pytorch构建的图中,是Variable在流动,也可以使用for,print,log等
4)基于nn.Module构建的模型中,只支持mini-batch的Variable的输入方式,如,N*C*H*W

代码示例:

class LeNet(nn.Module):   def __init__(self):     # nn.Module的子类函数必须在构造函数中执行父类的构造函数     super(LeNet, self).__init__() # 等价与nn.Module.__init__()     # nn.Conv2d返回的是一个Conv2d class的一个对象,该类中包含forward函数的实现     # 当调用self.conv1(input)的时候,就会调用该类的forward函数     self.conv1 = nn.Conv2d(1, 6, (5, 5)) # output (N, C_{out}, H_{out}, W_{out})`     self.conv2 = nn.Conv2d(6, 16, (5, 5))     self.fc1 = nn.Linear(256, 120)     self.fc2 = nn.Linear(120, 84)     self.fc3 = nn.Linear(84, 10)   def forward(self, x):     # F.max_pool2d的返回值是一个Variable, input:(10,1,28,28) ouput:(10, 6, 12, 12)     x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))     # input:(10, 6, 12, 12)  output:(10,6,4,4)     x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))     # 固定样本个数,将其他维度的数据平铺,无论你是几通道,最终都会变成参数, output:(10, 256)     x = x.view(x.size()[0], -1)     # 全连接     x = F.relu(self.fc1(x))     x = F.relu(self.fc2(x))     x = F.relu(self.fc3(x))     # 返回值也是一个Variable对象     return x def output_name_and_params(net):   for name, parameters in net.named_parameters():     print('name: {}, param: {}'.format(name, parameters)) if __name__ == '__main__':   net = LeNet()   print('net: {}'.format(net))   params = net.parameters() # generator object   print('params: {}'.format(params))   output_name_and_params(net)   input_image = torch.FloatTensor(10, 1, 28, 28)   # 和tensorflow不一样,pytorch中模型的输入是一个Variable,而且是Variable在图中流动,不是Tensor。   # 这可以从forward中每一步的执行结果可以看出   input_image = Variable(input_image)   output = net(input_image)   print('output: {}'.format(output))   print('output.size: {}'.format(output.size()))

看完上述内容,你们掌握nn.Module接口怎么在pytorch中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI