neural-networkpytorchconv-neural-networkmobilenet

Boolean value of Tensor with more than one value is ambiguous


I have this class of 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

I create a model and pass it the random input, and it shows the error:

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

I received this error: RuntimeError: Boolean value of Tensor with more than one value is ambiguous


Solution

  • The issue is with the nn.ReLU() in the feedforward(). I was printing it which is not possible in ipynb file.

    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