浅析PyTorch中nn.Linear的使用


Posted in Python onAugust 18, 2019

查看源码

Linear 的初始化部分:

class Linear(Module):
 ...
 __constants__ = ['bias']
 
 def __init__(self, in_features, out_features, bias=True):
   super(Linear, self).__init__()
   self.in_features = in_features
   self.out_features = out_features
   self.weight = Parameter(torch.Tensor(out_features, in_features))
   if bias:
     self.bias = Parameter(torch.Tensor(out_features))
   else:
     self.register_parameter('bias', None)
   self.reset_parameters()
 ...

需要实现的内容:

浅析PyTorch中nn.Linear的使用

计算步骤:

@weak_script_method
  def forward(self, input):
    return F.linear(input, self.weight, self.bias)

返回的是:input * weight + bias

对于 weight

weight: the learnable weights of the module of shape
  :math:`(\text{out\_features}, \text{in\_features})`. The values are
  initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
  :math:`k = \frac{1}{\text{in\_features}}`

对于 bias

bias:  the learnable bias of the module of shape :math:`(\text{out\_features})`.
    If :attr:`bias` is ``True``, the values are initialized from
    :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
    :math:`k = \frac{1}{\text{in\_features}}`

实例展示

举个例子:

>>> import torch
>>> nn1 = torch.nn.Linear(100, 50)
>>> input1 = torch.randn(140, 100)
>>> output1 = nn1(input1)
>>> output1.size()
torch.Size([140, 50])

张量的大小由 140 x 100 变成了 140 x 50

执行的操作是:

[140,100]×[100,50]=[140,50]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python控制台英汉汉英电子词典
Apr 23 Python
跟老齐学Python之通过Python连接数据库
Oct 28 Python
Python与Redis的连接教程
Apr 22 Python
Python实现简单的文本相似度分析操作详解
Jun 16 Python
Python变量作用域LEGB用法解析
Feb 04 Python
django3.02模板中的超链接配置实例代码
Feb 04 Python
Python class的继承方法代码实例
Feb 14 Python
基于django micro搭建网站实现加水印功能
May 22 Python
python识别验证码的思路及解决方案
Sep 13 Python
利用Python实现自动扫雷小脚本
Dec 17 Python
python设置 matplotlib 正确显示中文的四种方式
May 10 Python
python自动计算图像数据集的RGB均值
Jun 18 Python
Pytorch实现GoogLeNet的方法
Aug 18 #Python
PyTorch之图像和Tensor填充的实例
Aug 18 #Python
Pytorch Tensor的索引与切片例子
Aug 18 #Python
在PyTorch中Tensor的查找和筛选例子
Aug 18 #Python
对Pytorch神经网络初始化kaiming分布详解
Aug 18 #Python
pytorch中的embedding词向量的使用方法
Aug 18 #Python
Pytorch加载部分预训练模型的参数实例
Aug 18 #Python
You might like
php 变量未定义等错误的解决方法
2011/01/12 PHP
POST一个JSON格式的数据给Restful服务实例详解
2017/04/07 PHP
PHP使用gearman进行异步的邮件或短信发送操作详解
2020/02/27 PHP
JavaScript-世界上误解最深的语言分析
2007/08/12 Javascript
HTML Dom与Css控制方法
2010/10/25 Javascript
jquery解决客户端跨域访问问题
2015/01/06 Javascript
JS控制弹出新页面窗口位置和大小的方法
2015/03/02 Javascript
基于JavaScript实现移动端TAB触屏切换效果
2015/10/20 Javascript
Ubuntu系统下Angularjs开发环境安装
2016/09/01 Javascript
js获取地址栏中传递的参数(两种方法)
2017/02/08 Javascript
jquery处理checkbox(复选框)是否被选中实例代码
2017/06/12 jQuery
Javascript快速实现浏览器系统通知
2017/08/26 Javascript
Angular使用Md5加密的解决方法
2017/09/16 Javascript
Postman模拟发送带token的请求方法
2018/03/31 Javascript
JS伪继承prototype实现方法示例
2018/06/20 Javascript
NodeJs实现简单的爬虫功能案例分析
2018/12/05 NodeJs
vue路由--网站导航功能详解
2019/03/29 Javascript
vue+elementUI实现表单和图片上传及验证功能示例
2019/05/14 Javascript
jQuery实现的鼠标拖动画矩形框示例【可兼容IE8】
2019/05/17 jQuery
在实例中重学JavaScript事件循环
2020/12/03 Javascript
[29:23]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场1
2014/05/23 DOTA
[53:49]LGD vs Fnatic 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python中转换角度为弧度的radians()方法
2015/05/18 Python
python获取mp3文件信息的方法
2015/06/15 Python
Python面向对象类继承和组合实例分析
2018/05/28 Python
解决pycharm无法识别本地site-packages的问题
2018/10/13 Python
python实现栅栏加解密 支持密钥加密
2019/03/20 Python
Django框架视图介绍与使用详解
2019/07/18 Python
python实现银行管理系统
2019/10/25 Python
Python笔记之观察者模式
2019/11/20 Python
利用python实现AR教程
2019/11/20 Python
python用分数表示矩阵的方法实例
2021/01/11 Python
大学旷课检讨书
2014/01/28 职场文书
婚礼家长致辞
2015/07/27 职场文书
python中Matplotlib绘制直线的实例代码
2021/07/04 Python
一文搞懂Java中的注解和反射
2022/06/21 Java/Android