Simple Neural Network Using PyTorch

huangapple go评论86阅读模式
英文:

Simple Neural Network Using Pytorch

问题

I want to build Simple Neural Network with pytorch.
And I want to teach this network.

the network has
y = w(weight) * x + b(bias)
with w = 3 and b = 0

so I have the data
x = [1,2,3,4,5,6]
y = [3,6,9,12,15,18]

But I have some problem while building this Simple Neural Network

  1. import torch
  2. import torch.nn as nn
  3. class MyNeuralNetwork(nn.Module):
  4. def __init__(self):
  5. super(MyNeuralNetwork, self).__init__()
  6. self.layer=nn.Linear(in_features=1, out_features=1, bias=True)
  7. weight = torch.rand(1)
  8. bias = torch.rand(1)
  9. self.layer.weight = nn.Parameter(weight)
  10. self.layer.bias = nn.Parameter(bias)
  11. def forward(self, input):
  12. output = self.layer(input)
  13. return output
  14. model = MyNeuralNetwork().to("cpu")
  15. print(model)
  16. print(f"weight : {model.layer.weight}")
  17. print(f"bias : {model.layer.bias}")
  18. input = torch.tensor([1.]).view(-1,1)
  19. model(input)
  20. # out = model(input)
  21. # print(out)
  22. # y = torch.tensor([3.,6.,9.,12.,15.,18.])

I have an error which says that
"RuntimeError: mat2 must be a matrix, got 1-D tensor"

What should I do to fix this problem?
Thanks OTL....

英文:

I want to build Simple Neural Network with pytorch.
And I want to teach this network.

the network has
y = w(weight) * x + b(bias)
with w = 3 and b = 0

so I have the data
x = [1,2,3,4,5,6]
y = [3,6,9,12,15,18]

But I have some problem while building this Simple Neural Network

  1. import torch
  2. import torch.nn as nn
  3. class MyNeuralNetwork(nn.Module):
  4. def __init__(self):
  5. super(MyNeuralNetwork, self).__init__()
  6. self.layer=nn.Linear(in_features=1, out_features=1, bias=True)
  7. weight = torch.rand(1)
  8. bias = torch.rand(1)
  9. self.layer.weight = nn.Parameter(weight)
  10. self.layer.bias = nn.Parameter(bias)
  11. def forward(self, input):
  12. output = self.layer(input)
  13. return output
  14. model = MyNeuralNetwork().to("cpu")
  15. print(model)
  16. print(f"weight : {model.layer.weight}")
  17. print(f"bias : {model.layer.bias}")
  18. input = torch.tensor([1.]).view(-1,1)
  19. model(input)
  20. # out = model(input)
  21. # print(out)
  22. # y = torch.tensor([3.,6.,9.,12.,15.,18.])

I have an error which says that
"RuntimeError: mat2 must be a matrix, got 1-D tensor"

What should I do to fix this problem?
Thanks OTL....

答案1

得分: 1

你的权重应该是大小为[1, 1],但你已经用一个一维权重张量进行了覆盖。我不知道为什么你要覆盖层的权重和偏差,但要么用正确的形状进行覆盖,要么如果你只是删除__init__中的4行代码应该可以工作,或者修改成这样:

  1. self.layer.weight = nn.Parameter(torch.rand(1, 1)) # 2维矩阵
英文:

your weight should be of size [1, 1] which you have overrided with a 1d weight tensor. I do not know why you have overwrite the weight and bias of the layer, but either override with correct shape or if you just remove the 4 lines inside the init should work.or change but this:

  1. self.layer.weight = nn.Parameter(torch.rand(1, 1)) # 2d Matrix

huangapple
  • 本文由 发表于 2023年2月16日 16:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定