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

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

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__ ?

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:

确定