英文:
Python doesn't know the class attributes, while Jupyter does
问题
我有StatQuest频道的这段代码。这段代码在Jupyter中运行正常,但当我在一个.py
文件中运行时,报告错误:
AttributeError: 'BasicNNTrain'对象没有'w00'属性。
这是代码:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import SGD
import matplotlib.pyplot as plt
import seaborn as sns
class BasicNNTrain(nn.Module):
def __int__(self):
super().__init__()
self.w00 = nn.Parameter(torch.tensor(1.7), requires_grad=False)
self.b00 = nn.Parameter(torch.tensor(-0.85), requires_grad=False)
self.w01 = nn.Parameter(torch.tensor(-40.8), requires_grad=False)
self.w10 = nn.Parameter(torch.tensor(12.6), requires_grad=False)
self.b10 = nn.Parameter(torch.tensor(0.0), requires_grad=False)
self.w11 = nn.Parameter(torch.tensor(2.7), requires_grad=False)
self.final_bias = nn.Parameter(torch.tensor(0.0), requires_grad=True)
def forward(self, input):
input_to_top_relu = input * self.w00 + self.b00
top_relu_output = F.relu(input_to_top_relu)
scaled_top_relu_output = top_relu_output * self.w01
input_to_bottom_relu = input * self.w10 + self.b10
bottom_relu_output = F.relu(input_to_bottom_relu)
scaled_bottom_relu_output = bottom_relu_output * this.w11
input_to_final_relu = scaled_top_relu_output + scaled_bottom_relu_output + this.final_bias
output = F.relu(input_to_final_relu)
return output
model = BasicNNTrain()
for name, param in model.named_parameters():
print(name, param.data)
input_doses = torch.linspace(start=0, end=1, steps=11)
print(input_doses)
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:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import SGD
import matplotlib.pyplot as plt
import seaborn as sns
class BasicNNTrain(nn.Module):
def __int__(self):
super().__init__()
self.w00 = nn.Parameter(torch.tensor(1.7), requires_grad=False)
self.b00 = nn.Parameter(torch.tensor(-0.85), requires_grad=False)
self.w01 = nn.Parameter(torch.tensor(-40.8), requires_grad=False)
self.w10 = nn.Parameter(torch.tensor(12.6), requires_grad=False)
self.b10 = nn.Parameter(torch.tensor(0.0), requires_grad=False)
self.w11 = nn.Parameter(torch.tensor(2.7), requires_grad=False)
self.final_bias = nn.Parameter(torch.tensor(0.0), requires_grad=True)
def forward(self, input):
input_to_top_relu = input * self.w00 + self.b00
top_relu_output = F.relu(input_to_top_relu)
scaled_top_relu_output = top_relu_output * self.w01
input_to_bottom_relu = input * self.w10 + self.b10
bottom_relu_output = F.relu(input_to_bottom_relu)
scaled_bottom_relu_output = bottom_relu_output * self.w11
input_to_final_relu = scaled_top_relu_output + scaled_bottom_relu_output + self.final_bias
output = F.relu(input_to_final_relu)
return output
model = BasicNNTrain()
for name, param in model.named_parameters():
print(name, param.data)
input_doses = torch.linspace(start=0, end=1, steps=11)
print(input_doses)
model(input_doses)
答案1
得分: 1
可能是因为def __int__
中的拼写错误?
英文:
Probably because of the typo in def __int__
?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论