AttributeError: 'ReLU' object has no attribute 'dim'
AttributeError: 'ReLU' object has no attribute 'dim'
我正在构建一个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'
没有找到相关问题。感谢任何帮助!!
nn.ReLU() 创建一个 nn.Module,您可以添加它,例如到 nn.Sequential 模型。
nn.functional.relu 另一方面只是对 relu 函数的功能 API 调用,这样你就可以添加它,例如在你自己的 forward 方法中。 (来自 https://discuss.pytorch.org/t/whats-the-difference-between-nn-relu-vs-f-relu/27599)
所以你应该将 torch.nn.ReLU() 替换为 torch.nn.functional.relu()
你可以使用这段代码,我认为它会很好用
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
我正在构建一个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'
没有找到相关问题。感谢任何帮助!!
nn.ReLU() 创建一个 nn.Module,您可以添加它,例如到 nn.Sequential 模型。 nn.functional.relu 另一方面只是对 relu 函数的功能 API 调用,这样你就可以添加它,例如在你自己的 forward 方法中。 (来自 https://discuss.pytorch.org/t/whats-the-difference-between-nn-relu-vs-f-relu/27599)
所以你应该将 torch.nn.ReLU() 替换为 torch.nn.functional.relu()
你可以使用这段代码,我认为它会很好用
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