I'm trying to predict age from a given picture. I built the model below but the problem is that I'm getting very large loss value with low accuracy while fitting the model.

I think the problem is choosing the wrong loss function (here mean_squared_error). What can be the problem here?

import tensorflow as tf
from tensorflow import keras

X = X.reshape(-1, image_size[0], image_size[1], 1)
model = keras.models.Sequential()

model.add(keras.layers.Conv2D(32, (5, 5), activation='relu', input_shape=X.shape[1:]))
model.add(keras.layers.MaxPooling2D((2, 2)))

model.add(keras.layers.Conv2D(32, (3, 3), activation='relu'))
model.add(keras.layers.MaxPooling2D(2, 2))

model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))

model.add(keras.layers.Dense(60, activation='relu'))
model.add(keras.layers.Dense(1, activation='softmax'))
model.compile(optimizer='adam', loss=keras.losses.mean_squared_error, metrics=['accuracy'])

model.fit(X, Y, epochs=170, shuffle=True, validation_split=0.1)

As another question, are my layers correct to predict a number for a given picture?


2 Answers 2


This is the problem: model.add(keras.layers.Dense(1, activation='softmax'))

For predicting real-valued data such as age, it is customary to set the activation as linear or in this case, you can probably use relu.

To illustrate, softmax will create a distribution over the output, in this case the output of the model will be always 1. Since the age is positive definite continuous number, you need an activation that has a range of real numbers >= 0 which the relu activation satisfies.

As another question , are my layers good to predict a number for given picture ?

The architecture seems reasonable so try changing the model's final activation first.

  • $\begingroup$ i changed the activation it seems things are better but the accuracy is still zero . is there any other problem in model ? $\endgroup$ Jun 8, 2019 at 16:50
  • 1
    $\begingroup$ @Mehdibmp accuracy is not an accurate metric for a regression problem, it's well defined for classification problems but not for this type of model. You should change that for something that's more apt for the problem, e.g., mean_absolute_error. Please check these references en.wikipedia.org/wiki/Mean_absolute_error and tensorflow.org/api_docs/python/tf/keras/metrics/… $\endgroup$ Jun 8, 2019 at 17:19
  • $\begingroup$ @mehdibmp I hope my answer addressed your question and made your model work! Let me know if there are other issues that you may have encountered. In the meantime, I'd appreciate if you can accept my answer if it helped you! Cheers. :) $\endgroup$ Jun 10, 2019 at 7:04

I somehow feel that the issue rather than being technical is more functional in nature. One should try converting age into bins of age ranges and then use it as a multiclass classification problem statement. Predicting age from a picture is something that even humans fails at so that inherent misintepretation would be "learned" by NN as well and MAE always high


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.