【问题标题】:TypeError: mul() argument 'other' (position 1) must be Tensor, not ReLUTypeError:mul() 参数“其他”(位置 1)必须是张量,而不是 ReLU
【发布时间】:2019-06-24 21:19:33
【问题描述】:

我想在 fc1 和 fc2 层之间添加一个torch.nn.ReLU() 层。

原码:

型号:

# ...
self.fc1 = nn.Linear(4096, 256)
self.fc2 = nn.Linear(256, 4096)
# ...
def forward(...):
    # ...
    x = x.view(-1, 4096)
    x = self.fc1(x))
    if a7 is not None:
        x = x * a7.squeeze()
    # ...

我试过了

# ...
x = x.view(-1, 4096)
x = nn.ReLU(self.fc1(x)))
if a7 is not None:
    x = x * a7.squeeze()
# ...

然后弹出这个错误。

【问题讨论】:

  • 您的意思是__init__: 还是应该是def forward(...):
  • 对。抱歉打错了。
  • 没问题。如果您不介意,我已在您的问题中解决了它。

标签: python pytorch


【解决方案1】:

我的回答假设__init__ 是一个错字,应该是forward。如果不是这样,请告诉我,我会删除它。

import torch
from torch import nn

class SimpleModel(nn.Module):
  def __init__(self, with_relu=False):
    super(SimpleModel, self).__init__()
    self.fc1 = nn.Sequential(nn.Linear(3, 10), nn.ReLU(inplace=True)) if with_relu else nn.Linear(3, 10)
    self.fc2 = nn.Linear(10, 3)

  def forward(self, x):
    x = self.fc1(x)
    print(torch.min(x))  # just to show you ReLU is working...
    return self.fc2(x)

# Model without ReLU
net_without_relu = SimpleModel(with_relu=False)
print(net_without_relu)

# Model with ReLU
net_with_relu = SimpleModel(with_relu=True)
print(net_with_relu)

# random input data
x = torch.randn((5, 3))
print(x)

# we expect it to print something < 0
output1 = net_without_relu(x)

# we expect it to print 0.
output2 = net_with_relu(x)

您可以查看 Colab 上运行的以下代码:https://colab.research.google.com/drive/1W3Dh4_KPd3iABx5FSzZm3tilm6tnJh0v


按照您的尝试使用:

x = nn.ReLU(self.fc1(x)))

您可以使用函数式 API:

from torch.nn import functional as F

# ...
x = F.relu(self.fc1(x)))

【讨论】:

  • 谢谢!我现在正在尝试这个
【解决方案2】:

您不应该在__init__ 中执行任何查看方法。 Init 应该保持你的结构。 例如,这是从 AlexNet __init__复制而来的

nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),

但是,您的转发方法可能包含重塑、计算、函数。


nn.Sequential 应该是 __init__ 的一部分,就像在 AlexNet 中一样:

class AlexNet(nn.Module):

    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), 256 * 6 * 6)
        x = self.classifier(x)
        return x

然后你可以使用类属性self.featuresself.classifier 转发。

注意:这是来自 PyTorch 0.4 的 AlexNet 的旧模型,但相当简单,逻辑相同

【讨论】:

  • 谢谢。那么在这里添加 Relu() 最方便的方法是什么?我试过self.fc1 = nn.Sequential(nn.Linear(4096, 256), nn.ReLU()) ,但还是不行。
  • 查看example,你刚才写的不行。
猜你喜欢
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 2017-10-21
  • 2021-03-12
  • 1970-01-01
相关资源
最近更新 更多