浅析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正则表达式
Jan 15 Python
Python实现基本数据结构中队列的操作方法示例
Dec 04 Python
Sanic框架异常处理与中间件操作实例分析
Jul 16 Python
浅谈python连续赋值可能引发的错误
Nov 10 Python
利用python求积分的实例
Jul 03 Python
树莓派4B安装Tensorflow的方法步骤
Jul 16 Python
OpenCV Python实现图像指定区域裁剪
Mar 12 Python
Flask中sqlalchemy模块的实例用法
Aug 02 Python
python+django+selenium搭建简易自动化测试
Aug 19 Python
python获取百度热榜链接的实例方法
Aug 25 Python
详解pytorch tensor和ndarray转换相关总结
Sep 03 Python
在 Golang 中实现 Cache::remember 方法详解
Mar 30 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的可变变量名需要的注意的问题
2013/06/20 PHP
解析argc argv在php中的应用
2013/06/24 PHP
详解PHP编码转换函数应用技巧
2016/10/22 PHP
Ubuntu 16.04下安装PHP 7过程详解
2017/03/28 PHP
js程序中美元符号$是什么
2008/06/05 Javascript
EXTJS FORM HIDDEN TEXTFIELD 赋值 使用value不好用的问题
2011/04/16 Javascript
js改变img标签的src属性在IE下没反应的解决方法
2013/07/23 Javascript
JS获取计算机mac地址以及IP的实现方法
2014/01/08 Javascript
JavaScript的事件代理和委托实例分析
2015/03/25 Javascript
浅谈js多维数组和hash数组定义和使用
2016/07/27 Javascript
Bootstrap页面标题Page Header的实现方法
2017/03/22 Javascript
JavaScript实现的可变动态数字键盘控件方式实例代码
2017/07/15 Javascript
Three.js如何用轨迹球插件(trackball)增加对模型的交互功能详解
2017/09/25 Javascript
详解webpack-dev-server 设置反向代理解决跨域问题
2018/04/18 Javascript
让webpack+vue-cil项目不再自动打开浏览器的方法
2018/09/27 Javascript
vue地址栏直接输入路由无效问题的解决
2018/11/15 Javascript
详解puppeteer使用代理
2018/12/27 Javascript
基于elementUI实现图片预览组件的示例代码
2019/03/31 Javascript
详解小程序云开发数据库
2019/05/20 Javascript
构建Vue大型应用的10个最佳实践(小结)
2019/11/07 Javascript
通过实例了解Javascript柯里化流程
2020/03/03 Javascript
uni-app使用countdown插件实现倒计时
2020/11/01 Javascript
[40:19]2018完美盛典CS.GO表演赛
2018/12/17 DOTA
[48:18]DOTA2-DPC中国联赛 正赛 RNG vs Dynasty BO3 第二场 1月29日
2021/03/11 DOTA
CentOS 7下安装Python3.6 及遇到的问题小结
2018/11/08 Python
python中字符串的编码与解码详析
2020/12/03 Python
python链表类中获取元素实例方法
2021/02/23 Python
Mixbook加拿大:照片书,照片卡,剪贴簿,年历和日历
2017/02/21 全球购物
美国名牌香水折扣网站:Hottperfume
2021/02/10 全球购物
日本亚马逊官方网站:Amazon.co.jp
2020/04/14 全球购物
什么是属性访问器
2015/10/26 面试题
如何写好升职自荐信
2014/01/06 职场文书
工伤死亡理赔协议书
2014/10/20 职场文书
幼儿园感恩节活动总结
2015/03/24 职场文书
2016年政治理论学习心得体会
2016/01/25 职场文书
2016年幼儿园教研活动总结
2016/04/05 职场文书