How can I implement a simple dot counting problem using a regression instead of a classification approach in Pytorch?
I'm trying to figure out how to solve the really simple problem of counting how many pixels in an image are white.
I have 20x20 pixel images (zeros matrix) with 1 to 20 pixels randomly set to 1.
Checking some tutorials Im able to solve this problem via a classification approach with the model using CrossEntropyLoss() and Adam optimizer:
 nn.Linear(20*20, 100),
 nn.ReLU(),
 nn.Linear(100, 30),            
 nn.ReLU(),
 nn.Linear(30, 20)```

Here my output is a vector of size 20 with the probabilities that the image contains 1 to 20 white pixels (ones)
Using argmax() I can simply retreive the most possible &quot;class&quot;
What I wanna do now is to solve the same problem but not as a classification one, but like a &quot;counter&quot;, where, wiht the image as imput, the NN can estimate the number of white pixels as one single Integer output.
I've made some changes to the code using one neuron as output and MSELoss() and other functions as loss functions, but still cannot get the output to be an integer (1 to 20) instead of a real number
At the end, what I try to understand is how to see a NN as a function, that, in this case, goes from Rn to R
Any ideas?
Using one neuron as output, the predictions are real numbers and the accuracy is always around 4.9% with a loss that keeps quite constant across epochs

Keep in mind that you probably have to normalize the outputs. So your model should still output something between 0 and 1 where 0 means 0 white pixels, 1 means 20 white pixels and 0.5 means 10 and so on. Therefore use sigmoid on the output neuron and mulltiply it by 20 to get the estimated amount of white pixels.
