首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'ReLU‘对象没有属性'dim’

AttributeError:'ReLU‘对象没有属性'dim’
EN

Stack Overflow用户
提问于 2021-09-28 10:27:43
回答 2查看 1.9K关注 0票数 1

我正在构建一个GAN,我的鉴别器功能被定义为

代码语言:javascript
复制
class Discriminator(nn.Module):
def __init__(self):
    super(Discriminator, self).__init__()
    
    self.fc1 = nn.Linear(50*15, 32)
    self.fc2 = nn.Linear(32, 32)
    self.fc3 = nn.Linear(32, 1)

def forward(self, x):
    x = x.flatten()
    x = torch.nn.ReLU(self.fc1(x))
    x = torch.nn.ReLU(self.fc2(x))

    return torch.nn.Sigmoid(self.fc3(x))

在测试代码时,使用以下命令得到了一个错误

代码语言:javascript
复制
discriminator(gen_series)

其中gen_series是维数为15*50的张量。此错误发生在

代码语言:javascript
复制
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-99-fa68eff35865> in <module>
     16 valid = Variable(Tensor(piece, time).fill_(1.0), requires_grad=False)
     17 print(gen_series)
---> 18 discriminator(gen_series)
     19 # g_loss = adversarial_loss(discriminator(gen_series), valid)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

<ipython-input-94-7c6c59da67f9> in forward(self, x)
     27         x = x.flatten()
     28         x = torch.nn.ReLU(self.fc1(x))
---> 29         x = torch.nn.ReLU(self.fc2(x))
     30 
     31         return torch.nn.Sigmoid(self.fc3(x))

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---> 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1366         - Output: :math:`(N, *, out\_features)`
   1367     """
-> 1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
   1370         ret = torch.addmm(bias, input, weight.t())

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
    583                 return modules[name]
    584         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 585             type(self).__name__, name))
    586 
    587     def __setattr__(self, name, value):

AttributeError: 'ReLU' object has no attribute 'dim'

我没有发现任何相关的问题。任何帮助都将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-28 14:08:32

您可以使用这段代码,我认为它会运行得很好。

代码语言:javascript
复制
class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()

        self.fc = nn.Sequential(
                  nn.Linear(50 * 15, 32),
                  nn.ReLU(),
                  nn.Linear(32, 32),
                  nn.ReLU(),
                  nn.Linear(32, 1),
                  nn.Sigmoid()
        )


    def forward(self, x):
        x = x.flatten()
        x = self.fc(x)

        return x
票数 0
EN

Stack Overflow用户

发布于 2021-09-28 11:20:14

nn.ReLU()创建一个nn.Module,您可以将其添加到nn.Sequential模型中。另一方面,nn.functional.relu只是对relu函数的函数API调用,这样您就可以自己添加它,例如在前进方法中。(来自https://discuss.pytorch.org/t/whats-the-difference-between-nn-relu-vs-f-relu/27599)

因此,应该将torch.nn.ReLU()替换为torch.nn.functional.relu()

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69360048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档