Python不知道类属性,而Jupyter知道

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

Python doesn't know the class attributes, while Jupyter does

问题

我有StatQuest频道的这段代码。这段代码在Jupyter中运行正常,但当我在一个.py文件中运行时,报告错误:

AttributeError: 'BasicNNTrain'对象没有'w00'属性。

这是代码:

  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from torch.optim import SGD
  5. import matplotlib.pyplot as plt
  6. import seaborn as sns
  7. class BasicNNTrain(nn.Module):
  8. def __int__(self):
  9. super().__init__()
  10. self.w00 = nn.Parameter(torch.tensor(1.7), requires_grad=False)
  11. self.b00 = nn.Parameter(torch.tensor(-0.85), requires_grad=False)
  12. self.w01 = nn.Parameter(torch.tensor(-40.8), requires_grad=False)
  13. self.w10 = nn.Parameter(torch.tensor(12.6), requires_grad=False)
  14. self.b10 = nn.Parameter(torch.tensor(0.0), requires_grad=False)
  15. self.w11 = nn.Parameter(torch.tensor(2.7), requires_grad=False)
  16. self.final_bias = nn.Parameter(torch.tensor(0.0), requires_grad=True)
  17. def forward(self, input):
  18. input_to_top_relu = input * self.w00 + self.b00
  19. top_relu_output = F.relu(input_to_top_relu)
  20. scaled_top_relu_output = top_relu_output * self.w01
  21. input_to_bottom_relu = input * self.w10 + self.b10
  22. bottom_relu_output = F.relu(input_to_bottom_relu)
  23. scaled_bottom_relu_output = bottom_relu_output * this.w11
  24. input_to_final_relu = scaled_top_relu_output + scaled_bottom_relu_output + this.final_bias
  25. output = F.relu(input_to_final_relu)
  26. return output
  27. model = BasicNNTrain()
  28. for name, param in model.named_parameters():
  29. print(name, param.data)
  30. input_doses = torch.linspace(start=0, end=1, steps=11)
  31. print(input_doses)
  32. model(input_doses)

注意:上述代码有一个拼写错误,将 "this" 替换为 "self",以修复错误。

英文:

I have this code from StatQuest channel. This code works in Jupyter, however when I run this in a .py file, it reports the error

>AttributeError: 'BasicNNTrain' object has no attribute 'w00'

This is the code:

  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from torch.optim import SGD
  5. import matplotlib.pyplot as plt
  6. import seaborn as sns
  7. class BasicNNTrain(nn.Module):
  8. def __int__(self):
  9. super().__init__()
  10. self.w00 = nn.Parameter(torch.tensor(1.7), requires_grad=False)
  11. self.b00 = nn.Parameter(torch.tensor(-0.85), requires_grad=False)
  12. self.w01 = nn.Parameter(torch.tensor(-40.8), requires_grad=False)
  13. self.w10 = nn.Parameter(torch.tensor(12.6), requires_grad=False)
  14. self.b10 = nn.Parameter(torch.tensor(0.0), requires_grad=False)
  15. self.w11 = nn.Parameter(torch.tensor(2.7), requires_grad=False)
  16. self.final_bias = nn.Parameter(torch.tensor(0.0), requires_grad=True)
  17. def forward(self, input):
  18. input_to_top_relu = input * self.w00 + self.b00
  19. top_relu_output = F.relu(input_to_top_relu)
  20. scaled_top_relu_output = top_relu_output * self.w01
  21. input_to_bottom_relu = input * self.w10 + self.b10
  22. bottom_relu_output = F.relu(input_to_bottom_relu)
  23. scaled_bottom_relu_output = bottom_relu_output * self.w11
  24. input_to_final_relu = scaled_top_relu_output + scaled_bottom_relu_output + self.final_bias
  25. output = F.relu(input_to_final_relu)
  26. return output
  27. model = BasicNNTrain()
  28. for name, param in model.named_parameters():
  29. print(name, param.data)
  30. input_doses = torch.linspace(start=0, end=1, steps=11)
  31. print(input_doses)
  32. model(input_doses)

答案1

得分: 1

可能是因为def __int__ 中的拼写错误?

英文:

Probably because of the typo in def __int__ ?

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

发表评论

匿名网友

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

确定