浅析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 15 Python
Python实现Sqlite将字段当做索引进行查询的方法
Jul 21 Python
Python爬虫获取整个站点中的所有外部链接代码示例
Dec 26 Python
Python中实现最小二乘法思路及实现代码
Jan 04 Python
python将字典内容存入mysql实例代码
Jan 18 Python
python 递归深度优先搜索与广度优先搜索算法模拟实现
Oct 22 Python
对Python模块导入时全局变量__all__的作用详解
Jan 11 Python
tensorflow之变量初始化(tf.Variable)使用详解
Feb 06 Python
如何解决安装python3.6.1失败
Jul 01 Python
一个入门级python爬虫教程详解
Jan 27 Python
Python办公自动化之教你如何用Python将任意文件转为PDF格式
Jun 28 Python
Python字符串格式化方式
Apr 07 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
2020年4月新番动漫目录 官方宣布4月播出的作品一览
2020/03/08 日漫
php中根据变量的类型 选择echo或dump
2012/07/05 PHP
php读取文件内容的方法汇总
2015/01/24 PHP
php中钩子(hook)的原理与简单应用demo示例
2019/09/03 PHP
js购物车实现思路及代码(个人感觉不错)
2013/12/23 Javascript
jQuery实现网页抖动的菜单抖动效果
2015/08/07 Javascript
ES6学习教程之对象的扩展详解
2017/05/02 Javascript
详解如何在angular2中获取节点
2017/11/23 Javascript
Node中使用ES6语法的基础教程
2018/01/05 Javascript
Vue.js 通过jQuery ajax获取数据实现更新后重新渲染页面的方法
2018/08/09 jQuery
使用JavaScript保存文本文件到本地的两种方法
2019/01/22 Javascript
详解从vue-loader源码分析CSS Scoped的实现
2019/09/23 Javascript
webpack的 rquire.context用法实现工程自动化的方法
2020/02/07 Javascript
Element实现表格嵌套、多个表格共用一个表头的方法
2020/05/09 Javascript
python实现的一只从百度开始不断搜索的小爬虫
2013/08/13 Python
Python基于pygame实现的font游戏字体(附源码)
2015/11/11 Python
浅析Python中的for 循环
2016/06/09 Python
pyenv命令管理多个Python版本
2017/03/26 Python
pandas删除指定行详解
2019/04/04 Python
python使用pygame模块实现坦克大战游戏
2020/03/25 Python
python实现图片九宫格分割
2021/03/07 Python
selenium+python实现基本自动化测试的示例代码
2021/01/27 Python
CSS3.0实现霓虹灯按钮动画特效的示例代码
2021/01/12 HTML / CSS
HTML5应用之文件上传
2016/12/30 HTML / CSS
世界上最大的在线旅行社新加坡网站:Expedia新加坡
2016/08/25 全球购物
One.com挪威:北欧成长最快的网络托管公司
2016/11/19 全球购物
New Balance英国官方网站:始于1906年,百年慢跑品牌
2016/12/07 全球购物
哈曼俄罗斯官方网上商店:Harman.club
2020/07/24 全球购物
家长对孩子评语
2014/01/30 职场文书
自我介绍演讲稿范文
2014/08/21 职场文书
2015年七一建党节演讲稿
2015/03/19 职场文书
2015年征兵工作总结
2015/07/23 职场文书
会议承办单位欢迎词
2015/09/30 职场文书
学习弘扬焦裕禄精神心得体会
2016/01/23 职场文书
Pytest allure 命令行参数的使用
2021/04/18 Python
Python中tkinter的用户登录管理的实现
2021/04/22 Python