QBoard » Artificial Intelligence & ML » AI and ML - Python » Prediction with tensorflow keras

Prediction with tensorflow keras

  • 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.Flatten())
    model.add(keras.layers.Dense(60, activation='relu'))
    model.add(keras.layers.Dropout(0.4))
    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? This post was edited by Pranav B at June 11, 2019 5:42 PM IST
      June 11, 2019 5:23 PM IST
    0
  • On the output layer, softmax and cross-entropy are used for classification. I am not too familiar with cross-entropy, but softmax essentially tries to hammer a bunch of numbers into something that looks like probabilities (although there's no good theoretical basis I know of that this actually makes more sense than simple normalization).

    These 2 are not usually used in hidden layers.

    Linear is also used in the output layer, when you want a real-numbered output (regression). It doesn't make sense to use linear activation in hidden layers, because then the whole system is linear, and you can collapse the whole thing into a 0-hidden-layer network.
      June 14, 2019 11:36 AM IST
    0
  • @pranav 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/…
      June 14, 2019 12:17 PM IST
    0
  • # Import the libraries required in this example:
    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras import layers
    
    inputs = keras.Input(shape=(784,), name="digits")
    x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
    x = layers.Dense(64, activation="relu", name="dense_2")(x)
    outputs = layers.Dense(10, activation="softmax", name="predictions")(x)
    
    model = keras.Model(inputs=inputs, outputs=outputs)
    
    (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
    
    # Preprocess the data (NumPy arrays):
    x_train = x_train.reshape(60000, 784).astype("float32") / 255
    x_test = x_test.reshape(10000, 784).astype("float32") / 255
    
    y_train = y_train.astype("float32")
    y_test = y_test.astype("float32")
    
    # Allocate 10,000 samples for validation:
    x_val = x_train[-10000:]
    y_val = y_train[-10000:]
    x_train = x_train[:-10000]
    y_train = y_train[:-10000]
    
    model.compile(
        optimizer=keras.optimizers.RMSprop(),  # Optimizer
        # Minimize loss:
        loss=keras.losses.SparseCategoricalCrossentropy(),
        # Monitor metrics:
        metrics=[keras.metrics.SparseCategoricalAccuracy()],
    )
    
    print("Fit model on training data")
    history = model.fit(
        x_train,
        y_train,
        batch_size=64,
        epochs=2,
        # Validation of loss and metrics
        # at the end of each epoch:
        validation_data=(x_val, y_val),
    )
    
    history.history
    
    print("Evaluate model on test data")
    results = model.evaluate(x_test, y_test, batch_size=128)
    print("test loss, test acc:", results)
    
    # Generate a prediction using model.predict() 
    # and calculate it's shape:
    print("Generate a prediction")
    prediction = model.predict(x_test[:1])
    print("prediction shape:", prediction.shape)
    
      October 23, 2021 1:51 PM IST
    0
  • import tensorflow as tf
    import keras


    this is a text, not a code

    This post was edited by Raji Reddy A at November 13, 2019 5:47 PM IST
      November 13, 2019 5:46 PM IST
    0
  • 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.
      June 11, 2019 5:39 PM IST
    0