0

I have this code in Pytorch but cant get it to work. I have it working with Numpy as return (X.T * W).sum(axis=1) + B

But with Pytorch I keep getting this error...


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

Oscar Rangel
  • 848
  • 1
  • 10
  • 18
  • 1
    What do you understand from that error? What is the issue? A simple google search of that error message returns multiple relevant results. – AMC Jan 25 '20 at 03:56
  • Does this answer your question? [How to resolve runtime error due to size mismatch in PyTorch?](https://stackoverflow.com/questions/49606482/how-to-resolve-runtime-error-due-to-size-mismatch-in-pytorch) – AMC Jan 25 '20 at 03:57
  • @AMC I don't see how that helps this problem. The error is not from resizing or passing the matrix through CNN processing; it's simple transposition. – Prune Jan 27 '20 at 19:12

1 Answers1

1

You have the wrong orientation for W: you defined a 2x3 matrix, but your algorithm requires a 3x2. Try W.T instead?

Prune
  • 76,765
  • 14
  • 60
  • 81