Home > Software engineering >  How to get continuous value from output layer
How to get continuous value from output layer

Time:09-17

I have a code (from here) to classify the MINST digits. The code is working fine. Here they used CrossEntropyLoss and Adam optimizer.

The model code is given below

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(         
            nn.Conv2d(
                in_channels=1,              
                out_channels=16,            
                kernel_size=5,              
                stride=1,                   
                padding=2,                  
            ),                              
            nn.ReLU(),                      
            nn.MaxPool2d(kernel_size=2),    
        )
        self.conv2 = nn.Sequential(         
            nn.Conv2d(16, 32, 5, 1, 2),     
            nn.ReLU(),                      
            nn.MaxPool2d(2),                
        )
        # fully connected layer, output 10 classes
        self.out = nn.Linear(32 * 7 * 7, 10)
        # self.softmax = torch.nn.Softmax(dim=1)
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
        x = x.view(x.size(0), -1)       
        output = self.out(x)
        # output = self.softmax(output)
        return output, x    # return x for visualization

The shape of the `b_x` and `b_y` is

torch.Size([100, 1, 28, 28]) torch.Size([100])

Now, I wanted to get continuous value from the output layer. Say, I want the output as alike i.e, 1.0, 0.9, 8.6, 7.0, etc. If the value of the output layer is 1.0 and the label is 1 that means the prediction is perfect. Otherwise, not perfect. More simply, I want to think the MNIST digits as a regression problem.

So, I changed the loss function to MSELoss and optimizer to SGD (the rest of the code remains as the same as the website). But now, I am getting an error

/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/modules/loss.py:528: UserWarning: Using a target size (torch.Size([100])) that is different to the input size (torch.Size([100, 10])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/Opps_0/Desktop/MNIST/src/train.py", line 60, in <module>
    train(NB_EPOCS, model, loaders)
  File "/home/Opps_0/Desktop/MNIST/src/train.py", line 45, in train
    loss = criterion(output, b_y)
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 528, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/functional.py", line 2925, in mse_loss
    expanded_input, expanded_target = torch.broadcast_tensors(input, target)
  File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/functional.py", line 74, in broadcast_tensors
    return _VF.broadcast_tensors(tensors)  # type: ignore
RuntimeError: The size of tensor a (10) must match the size of tensor b (100) at non-singleton dimension 1

Could you tell me what I have to change to get the continuous value for the output layer?

CodePudding user response:

Assuming your targets are shape as (batch_size,), something along the lines of:

 >>> model = CNN()
 >>> criterion = nn.MSELoss()

 >>> output, _ = model(torch.rand(2, 1, 28, 28))
 >>> b_y = torch.randint(0, 10, (2,))
 tensor([1, 2, 6, 5, 7])

Loss computation with MSELoss will result in:

 >>> loss = criterion(output, b_y)

RuntimeError: The size of tensor a (10) must match the size of tensor b (2) at non-singleton dimension 1.

This means the shape of your target b_y is incorrect, it needs to match output's shaped, i.e. it needs to be a two-dimensional tensor.

Since you are optimizing this task with a regression loss you could encode your target as a sparse vector also known as one-hot encoding. You can do so with ease using the builtin torch.nn.functional.one_hot:

>>> ohe_target = torch.nn.functional.one_hot(b_y, num_classes=10)
tensor([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]])

Now you can compute the loss properly:

>>> criterion(output, ohe_target)
tensor(0.1169, grad_fn=<MseLossBackward>)
  • Related