pytorch系列 ----暂时就叫5的番外吧: nn.Modlue及nn.Linear 源码理解

先看一个列子:

import torch
from torch import nn

m = nn.Linear(20, 30)
input = torch.randn(128, 20)
output = m(input)

output.size()

out:

torch.Size([128, 30])

刚开始看这份代码是有点迷惑的,m是类对象,而直接像函数一样调用m,m(input)

重点

  • nn.Module 是所有神经网络单元(neural network modules)的基类
  • pytorch在nn.Module中,实现了__call__方法,而在__call__方法中调用了forward函数。

经过以上两点。上述代码就不难理解。

接下来看一下源码:
https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/module.py

在这里插入图片描述

再来看一下nn.Linear
https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html
主要看一下forward函数:
在这里插入图片描述

返回的是:
i n p u t ∗ w e i g h t + b i a s input * weight + bias inputweight+bias的线性函数

此时再看一下这一份代码:

import torch
from torch import nn

m = nn.Linear(20, 30)
input = torch.randn(128, 20)
output = m(input)

output.size()

首先创建类对象m,然后通过m(input)实际上调用__call__(input),然后__call__(input)调用
forward()函数,最后返回计算结果为:
[ 128 , 20 ] × [ 20 , 30 ] = [ 128 , 30 ] [128, 20] \times [20, 30] = [128, 30] [128,20]×[20,30]=[128,30]

所以自己创建多层神经网络模块时,只需要在实现__init__forward即可.

接下来看一个简单的三层神经网络的例子:

# define three layers
class simpleNet(nn.Module):

    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super().__init__()
        self.layer1 = nn.Linear(in_dim, n_hidden_1)
        self.layer2 = nn.Linear(n_hidden_1, n_hidden_2)
        self.layer3 = nn.Linear(n_hidden_2, out_dim)

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        return x

以下为各层神经元个数:
输入: in_dim
第一层: n_hidden_1
第二层:n_hidden_2
第三层(输出层):out_dim

  • 150
    点赞
  • 274
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值