简体   繁体   English

尝试学习如何实现单个神经元

[英]Trying to learn how to implement a single Neuron

I have this code in Pytorch but cant get it to work.我在 Pytorch 中有这段代码,但无法让它工作。 I have it working with Numpy as return (XT * W).sum(axis=1) + B我有它与 Numpy 一起使用作为回报 (XT * W).sum(axis=1) + B

But with Pytorch I keep getting this error...但是使用 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

You have the wrong orientation for W : you defined a 2x3 matrix, but your algorithm requires a 3x2.您的W方向错误:您定义了一个 2x3 矩阵,但您的算法需要一个 3x2。 Try WT instead?试试WT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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