具有多个值的 Tensor 的布尔值不明确

Boolean value of Tensor with more than one value is ambiguous

我有这个 class 的 NN:

class Block(nn.Module):
    def __init__(self, in_planes, out_planes, stride=1):
        super(Block, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride,
             padding=1, groups=in_planes, bias=False)
        self.bn1 = nn.BatchNorm2d(in_planes)
  
    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = nn.ReLU(out)
        return out

我创建了一个模型并将随机输入传递给它,它显示错误:

model = Block(3,3, 1)
x = torch.rand(64, 3, 100, 100)
model(x)

我收到这个错误: RuntimeError: 多于一个的 Tensor 布尔值不明确

问题出在 feedforward() 中的 nn.ReLU()。我正在打印它,这在 ipynb 文件中是不可能的。

class Block(nn.Module):

    def __init__(self, in_planes, out_planes, stride=1):
        super(Block, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride,
             padding=1, groups=in_planes, bias=False)
        self.bn1 = nn.BatchNorm2d(in_planes)
        self.relu = nn.ReLU()

    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        return out