1
$\begingroup$

How do I program a neural network such that, when an image is inputted, the output is a numerical value that is not the probability of the image being a certain class? In other words, a CNN that doesn't classify. For example, when an input of an image of a chair is given, the model should not give the chance that the image is a chair but rather give the predicted age of the chair, the predicted price of the chair, etc. I'm currently not sure how to program a neural net like this.

$\endgroup$
  • $\begingroup$ I'm confused. Are you talking about a regression problem? $\endgroup$ – DuttaA Aug 31 at 7:12
1
$\begingroup$

This can be thought of as a loss function design problem. If you optimize your network weights for something like multi-class classification, then expect your network to learn weights for this task (You will use cross entropy loss for this task). If you optimize your network to output a single value at the last layer and treat it as a regression problem for age prediction, then your network can learn weights for this particular task (You may use something like Mean Square Error loss here).

Let me give you a weak guidline on how to do this. Suppose say your input is images of chairs and you want to predict their age using a pretrained resnet, this is how you may do it in pytorch.

Definition
X: Input pictures
Y: List of ground truth values which is age here. i.e. : [1.4, 2.5, 2.2, ....]

Modify your neural network to give one output at the last layer
model = torchvision.models.resnet18(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = torch.nn.Linear(num_ftrs, 1)

Design your loss function appropriately. You can treat age prediction as a standard regression problem. Of course, there are better loss designs here.
criterion = torch.nn.MSELoss()

So use this loss during training
loss = criterion(output, target) where output is your neural network prediction and target is your ground truth values.

This is how you can modify an existing architecture for your task. Hope it helps.

$\endgroup$
1
$\begingroup$

You generally use SoftMax Layer as the output layer for a neural network that is used as a Classifier.

Now, if you want your neural network to predict the age of a chair, predicting price of a chair like in linear regression (output is continuous), you have to remove the SoftMax Layer and add one or multiple layers such that the output of the final layer gives only one value at the output. (which is the prediction for you age or price). And, instead of logit loss you can use MSE for back propagation.

So, like keshik mentioned in another answer, it's all about the final layer used and the loss function used. Based on what you use, your weights get trained.

This is what is done in transfer learning also. Based on the task you want to achieve you change the last layers and retrain your network.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.