TypeError:mul() 参数'other'(位置 1)必须是张量,而不是 ReLU



我想在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__是错别字,应该是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_kpd3iabx5fszzzm3tilm6tnjh0v


尝试使用时使用:

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

您可以使用功能API:

from torch.nn import functional as F
# ...
x = F.relu(self.fc1(x)))

您不应在__init__中执行任何视图方法。初始化应保持您的结构。例如,这是从Alexnet __init__

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

但是,您的正向方法可能包含重塑,计算,功能。


nn.Sequential应该像Alexnet中的__init__的一部分:

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 in forward。

注意:这是来自Pytorch 0.4的Alexnet的旧模型

最新更新