【问题标题】:TypeError: argument 0 is not a Variable类型错误:参数 0 不是变量
【发布时间】:2019-05-05 08:48:41
【问题描述】:

我正在尝试使用自己的数据来学习 CNN。数据的形状是(1224, 15, 23)。 1224为数据条数,每条数据为(15, 23)。 CNN 是用 PyTorch 构建的。

我认为没有逻辑错误,因为 conv2D 需要 4-D 张量,我喂 (batch, channel, x, y)

当我构建 Net 类的实例时,我得到了这个错误。

TypeError: argument 0 is not a Variable

我已经使用PyTroch半年了,但这个错误是第一次,我仍然很困惑。

这是我的代码。

class Net(nn.Module):
    def __init__(self, n):
        super(Net,self).__init__()

        self.conv = nn.Sequential(nn.Conv2d(1, 32, kernel_size=3, stride=1),
                                 nn.ReLU(),
                                 nn.Conv2d(32, 64, kernel_size=3, stride=1),
                                 nn.ReLU(),
                                 nn.Conv2d(64, 64, kernel_size=3, stride=1),  # 64 x 9 x 17
                                 nn.ReLU()
                                )

        conv_out_size = self._get_conv_out(input_shape)
        self.fc = nn.Sequential(nn.Linear(64 * 9 * 17, 128),
                               nn.ReLU(),
                               nn.Linear(128, n)
                               )

    def _get_conv_out(self, shape):
        o = self.conv(torch.zeros(1, *shape))
        return int(np.prod(o.size()))

    def forward(self, x):
        conv_out = self.conv(x).view(x.size()[0], -1)
        return sefl.fc(conv_out)
if __name__=='__main__':
    num_epochs = 1
    num_classes = 2
    input_shape = train_img[0].shape  # 1, 15, 23

    net = Net(num_classes)
    iteration = 51
    BATCH_SIZE = 24 
    LEARNING_RATE = 0.0001

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)

    loss_list= []
    batch_index = 0

    # train
    for epoch in range(num_epochs):
        for i in range(iteration):
            input_img = torch.FloatTensor(train_img[batch_index: batch_index + BATCH_SIZE])
            print(input_img.size())  # 24, 1, 15, 23

            outputs = net(input_img)
            loss = criterion(outputs, labels)
            loss_list.append(loss.item())

            # Backprop
            opimizer.zero_grad()
            loss.backward()
            optimizer.step()

还有错误信息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-179-0f6bc7588c29> in <module>
      4     input_shape = train_img[0].shape  # 1, 15, 23
      5 
----> 6     net = Net(num_classes)
      7     iteration = 51
      8     BATCH_SIZE = 24

<ipython-input-178-8a68d4a0dc4a> in __init__(self, n)
     11                                 )
     12 
---> 13         conv_out_size = self._get_conv_out(input_shape)
     14         self.fc = nn.Sequential(nn.Linear(64 * 9 * 17, 128),
     15                                nn.ReLU(),

<ipython-input-178-8a68d4a0dc4a> in _get_conv_out(self, shape)
     18 
     19     def _get_conv_out(self, shape):
---> 20         o = self.conv(torch.zeros(1, *shape))
     21         return int(np.prod(o.size()))
     22 

C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
     65     def forward(self, input):
     66         for module in self._modules.values():
---> 67             input = module(input)
     68         return input
     69 

C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
    280     def forward(self, input):
    281         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 282                         self.padding, self.dilation, self.groups)
    283 
    284 

C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\functional.py in conv2d(input, weight, bias, stride, padding, dilation, groups)
     88                 _pair(0), groups, torch.backends.cudnn.benchmark,
     89                 torch.backends.cudnn.deterministic, torch.backends.cudnn.enabled)
---> 90     return f(input, weight, bias)
     91 
     92 

TypeError: argument 0 is not a Variable

【问题讨论】:

    标签: variables deep-learning artificial-intelligence pytorch


    【解决方案1】:

    您的代码实际上适用于PyTorch &gt;= 0.4.1。我猜你的 PyTorch 版本是

    o = conv(torch.autograd.Variable(torch.zeros(1, *x.shape)))
    

    PyTorch &gt;= 0.4.1 中,变量的概念不再存在。因此,torch.FloatTensor 可以直接传递给 NN 层。

    【讨论】:

    • 谢谢你。我使用的是 pytorhc 0.3.0 版 // 我升级了它所以它工作正常
    猜你喜欢
    • 2015-11-22
    • 2021-08-19
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多