【问题标题】:unexpected keyword argument trying to instantiate a class inheriting from torch.nn.Module意外的关键字参数试图实例化从 torch.nn.Module 继承的类
【发布时间】:2022-07-28 23:09:59
【问题描述】:

我见过类似的问题,但大多数似乎都涉及更多。我的问题在我看来很简单,但我无法弄清楚。我只是想定义一个类然后实例化它,但是传递给构造函数的参数无法识别。

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torchvision.transforms as transforms

# fully connected network
class NN(nn.Module):
    def __int__(self, in_size, num_class):
        super(NN, self).__init__()
        self.fc1 = nn.Linear(in_size, 50)
        self.fc2 = nn.Linear(50, num_class)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# initialize network
model = NN(in_size=input_size, num_class=num_classes) 

我收到错误:__init__() got an unexpected keyword argument 'in_size' 我正在使用 Python 3.1、PyTorch 1.7.1,在 macOS Monterey 上使用 PyCharm。谢谢!

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    您有一个错字:应该是 __init__ 而不是 __int__

    【讨论】:

    • 天哪!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多