浅析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聊天程序实例代码分享
Nov 18 Python
python批量修改文件名的实现代码
Sep 01 Python
Python中使用gzip模块压缩文件的简单教程
Apr 08 Python
python模仿网页版微信发送消息功能
Feb 24 Python
Python实现的当前时间多加一天、一小时、一分钟操作示例
May 21 Python
ubuntu 18.04搭建python环境(pycharm+anaconda)
Jun 14 Python
python适合人工智能的理由和优势
Jun 28 Python
深入浅析Python 函数注解与匿名函数
Feb 24 Python
python3用PyPDF2解析pdf文件,用正则匹配数据方式
May 12 Python
Jupyter notebook快速入门教程(推荐)
May 18 Python
Python爬虫Scrapy框架CrawlSpider原理及使用案例
Nov 20 Python
python ansible自动化运维工具执行流程
Jun 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
PHP 开源框架22个简单简介
2009/08/24 PHP
php实现比较两个字符串日期大小的方法
2015/05/12 PHP
[原创]php使用strpos判断字符串中数字类型子字符串出错的解决方法
2017/04/01 PHP
通过Javascript将数据导出到外部Excel文档的函数代码
2012/06/15 Javascript
javascript中数组的多种定义方法和常用函数简介
2014/05/09 Javascript
js利用prototype调用Array的slice方法示例
2014/06/09 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
JS控制层作圆周运动的方法
2016/06/20 Javascript
VUE axios上传图片到七牛的实例代码
2017/07/28 Javascript
js中数组常用方法总结(推荐)
2019/04/09 Javascript
jQuery实现弹幕特效
2019/11/29 jQuery
three.js 利用uv和ThreeBSP制作一个快递柜功能
2020/08/18 Javascript
JavaScript事件循环及宏任务微任务原理解析
2020/09/02 Javascript
在vue中实现某一些路由页面隐藏导航栏的功能操作
2020/09/21 Javascript
Flask SQLAlchemy一对一,一对多的使用方法实践
2013/02/10 Python
Python脚本实现格式化css文件
2015/04/08 Python
在Python中使用next()方法操作文件的教程
2015/05/24 Python
python将unicode转为str的方法
2017/06/21 Python
Python入门之后再看点什么好?
2018/03/05 Python
解决Pytorch 训练与测试时爆显存(out of memory)的问题
2019/08/20 Python
python基于opencv检测程序运行效率
2019/12/28 Python
python实现ftp文件传输功能
2020/03/20 Python
pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
2020/04/24 Python
Python自动化xpath实现自动抢票抢货
2020/09/19 Python
Python列表元素删除和remove()方法详解
2021/01/04 Python
美国儿童运动鞋和服装零售商:Kids Foot Locker
2017/08/05 全球购物
澳大利亚新奇小玩意网站:Yellow Octopus
2017/12/28 全球购物
潘多拉珠宝美国官方网站:Pandora US
2020/06/18 全球购物
办公室秘书岗位职责范本
2014/02/11 职场文书
商铺租赁意向书
2014/04/01 职场文书
作文批改评语大全
2014/04/23 职场文书
小学生学习保证书
2015/02/26 职场文书
企业党员岗位承诺书
2015/04/27 职场文书
公司车队管理制度
2015/08/04 职场文书
Java面试题冲刺第十七天--基础篇3
2021/08/07 面试题
源码安装apache脚本部署过程详解
2022/09/23 Servers