浅析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中将阿拉伯数字转换成中文的实现代码
May 19 Python
Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
May 22 Python
Python使用Redis实现作业调度系统(超简单)
Mar 22 Python
Python中创建字典的几种方法总结(推荐)
Apr 27 Python
Django使用Mysql数据库已经存在的数据表方法
May 27 Python
python写一个随机点名软件的实例
Nov 28 Python
Python3 assert断言实现原理解析
Mar 02 Python
keras model.fit 解决validation_spilt=num 的问题
Jun 19 Python
用python实现前向分词最大匹配算法的示例代码
Aug 06 Python
python实现无边框进度条的实例代码
Dec 30 Python
用Python的绘图库(matplotlib)绘制小波能量谱
Apr 17 Python
Python绘画好看的星空图
Mar 17 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
dedecms中显示数字验证码的修改方法
2007/03/21 PHP
浅析PHP原理之变量分离/引用(Variables Separation)
2013/08/09 PHP
yii操作session实例简介
2014/07/31 PHP
从零开始学YII2框架(三)扩展插件yii2-gird
2014/08/20 PHP
浅析php工厂模式
2014/11/25 PHP
Joomla简单判断用户是否登录的方法
2016/05/04 PHP
PHP大文件切割上传功能实例分析
2019/07/01 PHP
解决FireFox下[使用event很麻烦]的问题
2006/11/26 Javascript
为jquery.ui.dialog 增加“在当前鼠标位置打开”的功能
2009/11/24 Javascript
js+xml生成级联下拉框代码
2012/07/24 Javascript
关于ExtJS4.1:快捷键支持的问题
2013/04/24 Javascript
Javascript之this关键字深入解析
2013/11/12 Javascript
无闪烁更新网页内容JS实现
2013/12/19 Javascript
JS实现为表格动态添加标题的方法
2015/03/31 Javascript
微信小程序 wxapp地图 map详解
2016/10/31 Javascript
addEventListener()与removeEventListener()解析
2017/04/20 Javascript
xmlplus组件设计系列之按钮(2)
2017/04/26 Javascript
详解Angular 4.x Injector
2017/05/04 Javascript
jQuery ajax读取本地json文件的实例
2017/10/31 jQuery
layDate日期控件使用方法详解
2018/11/15 Javascript
JS实现的贪吃蛇游戏完整实例
2019/01/18 Javascript
jQuery+PHP+Ajax实现动态数字统计展示功能
2019/12/25 jQuery
Element实现表格嵌套、多个表格共用一个表头的方法
2020/05/09 Javascript
JS实现鼠标移动拖尾
2020/12/27 Javascript
js中实现继承的五种方法
2021/01/25 Javascript
[05:24]TI9采访——教练
2019/08/24 DOTA
python读取二进制mnist实例详解
2017/05/31 Python
安装好Pycharm后如何配置Python解释器简易教程
2019/06/28 Python
对Pytorch中nn.ModuleList 和 nn.Sequential详解
2019/08/18 Python
Keras之自定义损失(loss)函数用法说明
2020/06/10 Python
Django后端分离 使用element-ui文件上传方式
2020/07/12 Python
Python列表元素删除和remove()方法详解
2021/01/04 Python
python自动打开浏览器下载zip并提取内容写入excel
2021/01/04 Python
党员公开承诺书
2014/03/25 职场文书
《草原的早晨》教学反思
2014/04/08 职场文书
2014年采购工作总结
2014/11/20 职场文书