浅析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中关于时间和日期函数的常用计算总结(time和datatime)
Mar 08 Python
Python的加密模块md5、sha、crypt使用实例
Sep 28 Python
Python+django实现文件上传
Jan 17 Python
Python调用ctypes使用C函数printf的方法
Aug 23 Python
Python logging模块用法示例
Aug 28 Python
使用python实现unix2dos和dos2unix命令的例子
Aug 13 Python
Django通过json格式收集主机信息
May 29 Python
Python requests上传文件实现步骤
Sep 15 Python
10款最佳Python开发工具推荐,每一款都是神器
Oct 15 Python
python 怎样进行内存管理
Nov 10 Python
地图可视化神器kepler.gl python接口的使用方法
Dec 22 Python
Django扫码抽奖平台的配置过程详解
Jan 14 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
全国FM电台频率大全 - 27 陕西省
2020/03/11 无线电
连接到txt文本的超链接,不直接打开而是点击后下载的处理方法
2009/07/01 PHP
php FLEA中二叉树数组的遍历输出
2012/09/26 PHP
php数组合并array_merge()函数使用注意事项
2014/06/19 PHP
php判断手机浏览还是web浏览,并执行相应的动作简单实例
2016/07/28 PHP
phpMyAdmin无法登陆的解决方法
2017/04/27 PHP
国外的为初学者写的JavaScript教程
2008/06/09 Javascript
JavaScript prototype属性深入介绍
2012/11/27 Javascript
javascript中的缓动效果实现程序
2012/12/29 Javascript
JQuery获取或设置ckeditor的数据(示例代码)
2013/11/15 Javascript
js改变embed标签src值的方法
2015/04/10 Javascript
基于javascript实现浏览器滚动条快到底部时自动加载数据
2015/11/30 Javascript
easyui window refresh 刷新两次的解决方法(推荐)
2016/05/18 Javascript
Vue实现双向绑定的方法
2016/12/22 Javascript
Angular.js项目中使用gulp实现自动化构建以及压缩打包详解
2017/07/19 Javascript
微信小程序框架wepy之动态控制类名
2018/09/14 Javascript
详解jQuery-each()方法
2019/03/13 jQuery
[42:20]Secret vs Liquid 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
简洁的十分钟Python入门教程
2015/04/03 Python
解决在Python编辑器pycharm中程序run正常debug错误的问题
2019/01/17 Python
VSCode Python开发环境配置的详细步骤
2019/02/22 Python
Python3将数据保存为txt文件的方法
2019/09/12 Python
django admin 根据choice字段选择的不同来显示不同的页面方式
2020/05/13 Python
python--shutil移动文件到另一个路径的操作
2020/07/13 Python
django教程如何自学
2020/07/31 Python
Python3中FuzzyWuzzy库实例用法
2020/11/18 Python
bonprix荷兰网上商店:便宜的服装、鞋子和家居用品
2020/07/04 全球购物
自动化专业毕业生自荐信
2013/11/01 职场文书
员工年终演讲稿
2014/01/03 职场文书
竞选演讲稿范文大全
2014/05/12 职场文书
2014学习十八届四中全会精神思想汇报范文
2014/10/23 职场文书
2019年最新七夕唯美祝福语(60条)
2019/07/22 职场文书
Python数据清洗工具之Numpy的基本操作
2021/04/22 Python
python基础之文件操作
2021/10/24 Python
Win2008系统搭建DHCP服务器
2022/06/25 Servers
MySQL数据管理操作示例讲解
2022/12/24 MySQL