简体   繁体   中英

Trying to learn how to implement a single Neuron

I have this code in Pytorch but cant get it to work. I have it working with Numpy as return (XT * 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM