我正在构建一个GAN,我的鉴别器功能被定义为
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))在测试代码时,使用以下命令得到了一个错误
discriminator(gen_series)其中gen_series是维数为15*50的张量。此错误发生在
---------------------------------------------------------------------------
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'我没有发现任何相关的问题。任何帮助都将不胜感激!
发布于 2021-09-28 14:08:32
您可以使用这段代码,我认为它会运行得很好。
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发布于 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()
https://stackoverflow.com/questions/69360048
复制相似问题