前言:
def __init__(self, n_gmm=2, latent_dim=3): # n_gmm=gmm_k=4 super(DaGMM, self).__init__() # (固定寫法) layers = [] layers += [nn.Linear(118, 60)] layers += [nn.Tanh()] # 激活函數 layers += [nn.Linear(60, 30)] layers += [nn.Tanh()] layers += [nn.Linear(30, 10)] layers += [nn.Tanh()] layers += [nn.Linear(10, 1)] self.encoder = nn.Sequential(*layers)
class torch.nn.Linear(in_features, out_features, bias = True)
對傳入數據應用線性變換:y = A x + b(是一維函數給我們的理解的)
參數:
in_features:每個輸入(x)樣本的特征的大小
out_features:每個輸出(y)樣本的特征的大小
bias:如果設置為False,則圖層不會學習附加偏差。默認值是True
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 #torch.mm(a, b)是矩陣a和b矩陣相乘 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
為什么m.weight.shape = (30,20)?
答:因為線性變換公式是:
先生成一個(30,20)的weight,實踐運算中再轉置,這樣就能和x做矩陣乘法了。
別老去糾結y = A x + b(是一維函數給我們的理解的)。
參考: