日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

torch.nn.Linear()函数的理解

發布時間:2023/11/28 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 torch.nn.Linear()函数的理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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()函数的理解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。