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))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
4
5
6
7
8
9
为什么 m.weight.shape = (30,20)?

答:因为线性变换的公式是:

y=xAT+b y=xA^T+b
y=xA
T
+b

先生成一个(30,20)的weight,实际运算中再转置,这样就能和x做矩阵乘法了
---------------------
作者:m0_37586991
来源:CSDN
原文:https://blog.csdn.net/m0_37586991/article/details/87861418
版权声明:本文为博主原创文章,转载请附上博文链接!

torch.nn.Linear()函数的理解的更多相关文章

  1. [转载]Pytorch中nn.Linear module的理解

    [转载]Pytorch中nn.Linear module的理解 本文转载并援引全文纯粹是为了构建和分类自己的知识,方便自己未来的查找,没啥其他意思. 这个模块要实现的公式是:y=xAT+*b 来源:h ...

  2. 小白学习之pytorch框架(3)-模型训练三要素+torch.nn.Linear()

    模型训练的三要素:数据处理.损失函数.优化算法    数据处理(模块torch.utils.data) 从线性回归的的简洁实现-初始化模型参数(模块torch.nn.init)开始 from torc ...

  3. 关于torch.nn.Linear的笔记

    关于该类: torch.nn.Linear(in_features, out_features, bias=True) 可以对输入数据进行线性变换: $y  = x A^T + b$ in_featu ...

  4. pytorch中文文档-torch.nn常用函数-待添加-明天继续

    https://pytorch.org/docs/stable/nn.html 1)卷积层 class torch.nn.Conv2d(in_channels, out_channels, kerne ...

  5. torch.nn.LSTM()函数维度详解

    123456789101112lstm=nn.LSTM(input_size,                     hidden_size,                      num_la ...

  6. torch.nn.MSELoss()函数解读

    转载自:https://www.cnblogs.com/tingtin/p/13902325.html

  7. pytorch函数之nn.Linear

    class torch.nn.Linear(in_features,out_features,bias = True )[来源] 对传入数据应用线性变换:y = A x+ b 参数: in_featu ...

  8. PyTorch官方中文文档:torch.nn

    torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...

  9. PyTorch里面的torch.nn.Parameter()

    在刷官方Tutorial的时候发现了一个用法self.v = torch.nn.Parameter(torch.FloatTensor(hidden_size)),看了官方教程里面的解释也是云里雾里, ...

随机推荐

  1. vss操作说明

    vss命令行   一.环境变量配置 “我的电脑”->“属性”->“高级”->“系统环境变量” 1) 添加追加环境变量 名称为:Path:值为:VSS应用程序所在目录 例如:D:\Pr ...

  2. body和html

    1 关于html和body的背景颜色的一些变现 当给body设置背景颜色时(html没有背景颜色),这时body被当做根节点被浏览器俘获,浏览器界面的背景颜色就为body的background颜色:当 ...

  3. linux上部署javaWeb项目

    将web项目打成war包,上传到Linux操作系统tomcat安装目录下的webapps下即可!

  4. jQuery easyui datagrid pagenation 的分页数据格式

    {"total":28,"rows":[    {"productid":"FI-SW-01","unitco ...

  5. pascal倒序输出

    program Project6; {$APPTYPE CONSOLE} uses SysUtils; var a,b,c,d:integer; begin { TODO -oUser -cConso ...

  6. JavaScript 算法与数据结构(转载)

    JavaScript 算法与数据结构 https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md

  7. (转)IE内存泄露,iframe内存泄露造成的原因和解决方案

    http://my.oschina.net/jsan/blog/11169 http://blog.csdn.net/tianma630/article/details/8502395 jQuery ...

  8. bzoj 1072: [SCOI2007]排列perm【状压dp】

    先写了个next_permutation结果T了,于是开始写状压 设f[s][i]为选取状态为s,选的数模d为i的方案数,去重的话直接除以每个数字的出现次数的阶乘即可 #include<iost ...

  9. bzoj 3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛【dp】

    设f[i]为i为牡牛的方案数,f[0]=1,s为f的前缀和,f[i]=s[max(i-k-1,0)] #include<iostream> #include<cstdio> u ...

  10. poj 3130 How I Mathematician Wonder What You Are! 【半平面交】

    求多边形的核,直接把所有边求半平面交判断有无即可 #include<iostream> #include<cstdio> #include<algorithm> # ...