浅析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 20 Python
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
Dec 14 Python
python 日期操作类代码
May 05 Python
python 实现矩阵上下/左右翻转,转置的示例
Jan 23 Python
python集合是否可变总结
Jun 20 Python
基于torch.where和布尔索引的速度比较
Jan 02 Python
python中如何使用insert函数
Jan 09 Python
Python获取二维数组的行列数的2种方法
Feb 11 Python
python实例化对象的具体方法
Jun 17 Python
django haystack实现全文检索的示例代码
Jun 24 Python
Python爬虫简单运用爬取代理IP的实现
Dec 01 Python
python index() 与 rindex() 方法的使用示例详解
Dec 24 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/03/04 日漫
3
2006/10/09 PHP
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
2011/08/09 PHP
php xml常用函数的集合(比较详细)
2013/06/06 PHP
mysql_connect localhost和127.0.0.1的区别(网络层阐述)
2015/03/26 PHP
Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】
2019/07/24 PHP
使用Json比用string返回数据更友好,也更面向对象一些
2011/09/13 Javascript
JavaScript定时显示广告代码分享
2015/03/02 Javascript
jQuery实现右侧显示可向左滑动展示的深色QQ客服效果代码
2015/10/23 Javascript
js实现图片上传并正常显示
2015/12/19 Javascript
JavaScript基础知识点归纳(推荐)
2016/07/09 Javascript
BootStrap的select2既可以查询又可以输入的实现代码
2017/02/17 Javascript
JavaScript插件Tab选项卡效果
2017/11/14 Javascript
Express进阶之log4js实用入门指南
2018/02/10 Javascript
Javascript三种字符串连接方式及性能比较
2019/05/28 Javascript
Echarts实现单条折线可拖拽效果
2019/12/19 Javascript
JavaScript 禁止用户保存图片的实现代码
2020/04/28 Javascript
Vue 样式切换及三元判断样式关联操作
2020/08/09 Javascript
Javascript confirm多种使用方法解析
2020/09/25 Javascript
python正则匹配查询港澳通行证办理进度示例分享
2013/12/27 Python
Python实现删除文件但保留指定文件
2015/06/21 Python
Python中集合的内建函数和内建方法学习教程
2015/08/19 Python
使用实现pandas读取csv文件指定的前几行
2018/04/20 Python
python命名空间(namespace)简单介绍
2019/08/10 Python
大家都说好用的Python命令行库click的使用
2019/11/07 Python
Python __slots__的使用方法
2020/11/15 Python
Opencv 图片的OCR识别的实战示例
2021/03/02 Python
Django项目在pycharm新建的步骤方法
2021/03/02 Python
美国保健品专家:Life Extension
2018/05/04 全球购物
潘多拉珠宝美国官方网站:Pandora US
2020/06/18 全球购物
见习期自我鉴定
2014/01/31 职场文书
电子银行营销方案
2014/02/22 职场文书
2019朋友新婚祝福语精选
2019/10/10 职场文书
Python装饰器详细介绍
2022/03/25 Python
python在package下继续嵌套一个package
2022/04/14 Python
Win10 Anaconda安装python-pcl
2022/04/29 Servers