I have two questions:

1) I have images and I must predict a continuous value.

What is the approach I must follow?

Use for example a pretrained network like this?

 last_layer = pretrained net
 x = Flatten()(last_layer)
 x = Dense(512, activation='relu')(x)
 x = Dropout(0.4)(x)
 predictions = Dense(1)(x)

So, just use a Dense layer at the end with 1 node?

and then procees to prediction as usual?

2) If I have again a numerical values to predict but it is ordinal.

If I want to go with regression in this case, can I work like in previous example?

And just assign some probabilities to the result?

If result >0 and <=0.5 then classify as 0 
If result >0.5 and <=1 then classify as 1 

and so on?


1 Answer 1


It seems you are doing it right. The difference between case 1) and 2) should be mainly the loss function (and maybe the activation of the predictions layer).

For 1) you would use e.g. mean_squared_error, while for 2) you would use the binary cross entropy from logits (logits are what you would feed into a sigmoid or softmax layer). In the keras-documentation it seems like keras does not provide cross entropy from logits as a loss, so you have to set the activation of predictions to sigmoid and use binary_crossentropy. I guess then you could do the If result >0 and <= 0.5 then classify as 0 ..., the result >0 would be taken care ouf by definition of the sigmoid, however you might want optimize the threshold w.r.t. the precision and recall you need.

  • $\begingroup$ Ok, thanks!When you say optimize threshold with precision and recall?And since the result will be a refression result?Can you give an example?Thanks! (upv) $\endgroup$
    – George
    Commented Jul 5, 2019 at 8:50
  • $\begingroup$ When training a classifier, you are training the network to assign high values if the correct label is 1, and low values if the label is 0. So you won't get 0s and 1s as outputs, but something like 0.34 or 0.73, but you still have to decide, where the threshold is, above which you decide which class it is. I assume Keras just uses 0.5, when computing the accuracy in fit, however, since the threshold is not part of the loss function, that is basically an arbitrary value. $\endgroup$
    – matthiaw91
    Commented Jul 5, 2019 at 9:15
  • $\begingroup$ After fitting the model, you could further adjust the threshold and optimize for precision and recall. These are two quality measures for binary classifiers. Let's say your training data is 95 examples of class 0 and 5 examples of class 1, your classifier could still reach an accuracy of 95% by just labeling everything as 0, without learning anything. so to gauge the quality, you have to look at precision (roughly: how many 1s labeled by the classifier are are truely 1s) and recall (how many true 1s did the classifier label 1). These measures however change with the threshold you use. $\endgroup$
    – matthiaw91
    Commented Jul 5, 2019 at 9:20
  • $\begingroup$ Yes, I understand that.My question is practical.After finishing training, and predict, I will have a prediction array which will have values [0.15, 1.24, 2.3 ...].In order to use recall and precision I must compare it with true label array (string array) which will contain [0, 1,2, 3...].How to compare them? I can't convert prediction array to string. $\endgroup$
    – George
    Commented Jul 5, 2019 at 9:26
  • $\begingroup$ "[0, 1,2, 3...]" so do you have more than two classes? $\endgroup$
    – matthiaw91
    Commented Jul 5, 2019 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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