[转载]Pytorch中nn.Linear module的理解
[转载]Pytorch中nn.Linear module的理解
本文转载并援引全文纯粹是为了构建和分类自己的知识,方便自己未来的查找,没啥其他意思。
这个模块要实现的公式是:y=xAT+*b
来源:https://blog.csdn.net/u012936765/article/details/52671156
Linear 是module的子类,是参数化module的一种,与其名称一样,表示着一种线性变换。
创建
parent 的init函数
Linear的创建需要两个参数,inputSize 和 outputSize
inputSize:输入节点数
outputSize:输出节点数
所以Linear 有7个字段:weight : Tensor , outputSize ×× inputSize
bias: Tensor ,outputSize
gradWeight: Tensor , outputSize ×× inputSize
gradBias: Tensor ,outputSize
gradInput: Tensor
output: Tensor
_type: output:type()
例子
module = nn.Linear(10, 5)
1
Forward Pass
————————————————
版权声明:本文为CSDN博主「bubbleoooooo」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012936765/article/details/52671156
这篇文章有一个很好的例子:
import torch
x = torch.randn(128, 20) # 输入的维度是(128,20)
m = torch.nn.Linear(20, 30) # 20,30是指维度
output = m(x)
print('m.weight.shape:\n ', m.weight.shape)
print('m.bias.shape:\n', m.bias.shape)
print('output.shape:\n', output.shape)
# ans = torch.mm(input,torch.t(m.weight))+m.bias 等价于下面的
ans = torch.mm(x, m.weight.t()) + m.bias
print('ans.shape:\n', ans.shape)
print(torch.equal(ans, output))
输出是:
m.weight.shape:
torch.Size([30, 20])
m.bias.shape:
torch.Size([30])
output.shape:
torch.Size([128, 30])
ans.shape:
torch.Size([128, 30])
True
注意它输入的是一个128*20的二维tensor,经过一个线性变换后变成了128*30的.如果输入换成了:
x = torch.randn(20, 128) # 输入的维度是(20,128)
m = torch.nn.Linear(20, 30) # 20,30是指维度
output = m(x)
就会报错了。因为公式是y=xAT+b。由上面的输出我们可以看到,A的维度是3020,转置之后是20*30,所以应该和X的列数对应。一般的:linear的输入和输出值的都是列数,把输入换成:
x = torch.randn(20, 20) # 输入的维度是(20,20)
m = torch.nn.Linear(20, 30) # 20,30是指维度
output = m(x)
输出之后就会发现,改变的依然是列数。
[转载]Pytorch中nn.Linear module的理解的更多相关文章
- Pytorch中nn.Conv2d的用法
Pytorch中nn.Conv2d的用法 nn.Conv2d是二维卷积方法,相对应的还有一维卷积方法nn.Conv1d,常用于文本数据的处理,而nn.Conv2d一般用于二维图像. 先看一下接口定义: ...
- [转载]PyTorch中permute的用法
[转载]PyTorch中permute的用法 来源:https://blog.csdn.net/york1996/article/details/81876886 permute(dims) 将ten ...
- Pytorch中nn.Dropout2d的作用
Pytorch中nn.Dropout2d的作用 首先,关于Dropout方法,这篇博文有详细的介绍.简单来说, 我们在前向传播的时候,让某个神经元的激活值以一定的概率p停止工作,这样可以使模型泛化性更 ...
- torch.nn.Linear()函数的理解
import torch x = torch.randn(128, 20) # 输入的维度是(128,20)m = torch.nn.Linear(20, 30) # 20,30是指维度output ...
- pytorch中的Linear Layer(线性层)
LINEAR LAYERS Linear Examples: >>> m = nn.Linear(20, 30) >>> input = torch.randn(1 ...
- pytorch 中的重要模块化接口nn.Module
torch.nn 是专门为神经网络设计的模块化接口,nn构建于autgrad之上,可以用来定义和运行神经网络 nn.Module 是nn中重要的类,包含网络各层的定义,以及forward方法 对于自己 ...
- PyTorch 中,nn 与 nn.functional 有什么区别?
作者:infiniteft链接:https://www.zhihu.com/question/66782101/answer/579393790来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...
- Pytorch中Module,Parameter和Buffer的区别
下文都将torch.nn简写成nn Module: 就是我们常用的torch.nn.Module类,你定义的所有网络结构都必须继承这个类. Buffer: buffer和parameter相对,就是指 ...
- 小白学习之pytorch框架(3)-模型训练三要素+torch.nn.Linear()
模型训练的三要素:数据处理.损失函数.优化算法 数据处理(模块torch.utils.data) 从线性回归的的简洁实现-初始化模型参数(模块torch.nn.init)开始 from torc ...
随机推荐
- 黑马lavarel教程---9、缓存操作
黑马lavarel教程---9.缓存操作 一.总结 一句话总结: legend2项目中自己写的哪些文件操作都可以通过这里的缓存实现,简单方便 1.lavarel中如何使用后端主流的缓存如 Memcac ...
- 阿里云 docker image 加速
使用的国内网络下载docker image太困难了,简直龟速,于是上网查看如何加速docker image的下载,没想到网上还真有,看来现在自己的知识圈子太小了,还需要多接触新的知识.找到第一个atu ...
- Android跨进程通信Messenger
一.概述 我们可以在客户端发送一个Message给服务端,在服务端的handler中会接收到客户端的消息,然后进行对应的处理,处理完成后,再将结果等数据封装成Message,发送给客户端,客户端的ha ...
- Faster R-CNN论文阅读摘要
论文链接: https://arxiv.org/pdf/1506.01497.pdf 代码下载: https://github.com/ShaoqingRen/faster_rcnn (MATLAB) ...
- LC 351. Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- mybatis映射文件祥解(StudentMapper.xml)
1)以下是StudentMapper.xml文件,提倡放在与实体同目录下,文件名任意 <?xml version="1.0" encoding="UTF-8&quo ...
- Linux环境下warning: no newline at end of file
今天在Windows下VS2012写了一个程序, 然后放在Linux系统下进行编译.Linux下使用的编译器是CC,结果,一编译出现了很多诸如下面的警告信息,似乎每一个.cpp和.h文件都有. Ite ...
- AOP获取方法注解实现动态切换数据源
AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...
- centos7最小安装怎么安装防火墙
CentOS 7.0默认使用的是firewall作为防火墙,需要事先关闭. 关闭firewall: 1 2 3 systemctl stop firewalld.service systemctl d ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-4.微信授权一键登录开发之授权URL获取
笔记 4.微信授权一键登录开发之授权URL获取 简介:获取微信开放平台扫码连url地址 1.增加结果工具类,JsonData; 增加application.properties配置 ...