pytorch如何计算(N,*,input)矩阵class(output,input)矩阵(结果为(N,*,output))?

How to calculate (N,*, input) matrix class (output, input) matrix(the result is (N,*,output)) by pytorch?

我想重写 nn.Linear 所做的事情。问题是输入大小为(N,*,in_feature),权重大小为(out_feature,in_feature)。如果我希望结果是 (N,*,out_feature) 使用 python,我应该如何编写代码?

input @ weight.T 

不对,很遗憾。

大小需要匹配才能应用@ __matmul__:输入x的形状为(N, *, in_feature) 并且权重张量 w 的形状为 (out_feature, in_feature).

x = torch.rand(2, 4, 4, 10)
w = torch.rand(5, 10)

w 进行转置将得到 (in_feature, out_feature) 的形状。在 xw.T 之间应用 __matmul__ 将减少到 (N, *, out_feature):

的形状
>>> z = x@w.T
>>> z.shape
torch.Size([2, 4, 4, 5])

或等效地使用 torch.matmul:

>>> z = torch.matmul(x, w.T)