我在Pytorch中有此代码,但无法正常工作。我使用Numpy作为回报(X.T * W).sum(axis = 1)+ B
但是使用Pytorch,我会不断收到此错误...
def neural_network_neurons(W, B, X):
W = W.view(W.size(0), -1)
z1 = torch.matmul(X, W) + B
return ReLU(z1)
# --------------------------------------------------
W = torch.tensor([[1.2, 0.3, 0.1], [.01, 2.1, 0.7]])
B = torch.tensor([2.1, 0.89])
X = torch.tensor([0.3, 6.8, 0.59])
neural_network_neurons(W, B, X)
---------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-21-8a5d3a425c16> in <module>
4 X = torch.tensor([0.3, 6.8, 0.59])
5
----> 6 neural_network_neurons(W, B, X)
<ipython-input-20-7450924eb4a5> in neural_network_neurons(W, B, X)
5 ###
6 W = W.view(W.size(0), -1)
----> 7 z1 = torch.matmul(X, W) + B
8 return ReLU(z1)
9 #return (X.T * W).sum(axis=1) + B
RuntimeError: size mismatch, m1: [1 x 3], m2: [2 x 3] at
/pytorch/aten/src/TH/generic/THTensorMath.cpp:197
答案 0 :(得分:1)
W
的方向错误:您定义了2x3矩阵,但是算法需要3x2。尝试使用W.T
吗?