简体   繁体   中英

Pytorch, TypeError: object() takes no parameters

This is probably a beginner question, but nevertheless: When running an image classifier build with pytorch, I get this error:

Traceback (most recent call last):
File "/pytorch/kanji_torch.py", line 47, in <module>
    network = Network()
  File "/pytorch/kanji_torch.py", line 113, in __init__
    self.conv1 = nn.Conv2d(1, 32, 5)
  File "/python3.5/site-packages/torch/nn/modules/conv.py", line 233, in __init__
    False, _pair(0), groups, bias)
  File "/python3.5/site-packages/torch/nn/modules/conv.py", line 32, in __init__
    out_channels, in_channels // groups, *kernel_size))
TypeError: object() takes no parameters

I define the network class like this:

class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.conv3 = nn.Conv2d(64, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(64 * 5 * 5, 512)
        self.fc2 = nn.Linear(512, 640)
        self.fc3 = nn.Linear(640, 3756)

Pretty sure that I imported all the relevant pytorch library modules correctly. (import torch.nn as nn and
import torch)

Any ideas of what I'm doing wrong?

Thank you!

You might have a problem with your pytorch version, when I run the code:

class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.conv3 = nn.Conv2d(64, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(64 * 5 * 5, 512)
        self.fc2 = nn.Linear(512, 640)
        self.fc3 = nn.Linear(640, 3756)
print(network)

The output is:

Network (
  (conv1): Conv2d(1, 32, kernel_size=(5, 5), stride=(1, 1))
  (pool): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
  (conv2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))
  (pool2): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
  (conv3): Conv2d(64, 64, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear (1600 -> 512)
  (fc2): Linear (512 -> 640)
  (fc3): Linear (640 -> 3756)
)

I would suggest to update/reinstall pytorch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM