'Why is my tensorflow model transforming binary classification labels from 0 and 1 to .7?

I have been working on a tensorflow model that predicts short term positive and negative trends in the stock market using momentum indicators. I have the model set up to take in a 32x7 matrix of the 7 indicators at each of the last 32 time steps. If the price increases after the last time step the label is 1 and if it stays the same or decreases it is a 0.

I have a custom metric function that prints out the prediction and the true value for each epoch and what I have found is that the true values are not 0 or 1 they hover at ~.7. For binary classification this is not helpful because all the predictions will also hover at ~.7. Can anyone explain to me what may be causing the model to alter the labels in this way?

Here is the model and a screenshot of the first 10 epochs for reference.

def true(y_true, y_pred):
  return y_true
def pred(y_true, y_pred):
  return y_pred

model = tf.keras.models.Sequential([
          tf.keras.layers.Conv2D(50, kernel_size=(3,3), padding='same', 
            kernel_regularizer=tf.keras.regularizers.l2(0.01)),
          tf.keras.layers.AveragePooling2D((2,2)),
          tf.keras.layers.Conv2D(25, kernel_size=(2,2), padding='same', 
            kernel_regularizer=tf.keras.regularizers.l2(0.01)),
          tf.keras.layers.AveragePooling2D((2,2)),
          tf.keras.layers.Flatten(),
          tf.keras.layers.Dense(1, activation='sigmoid'),
  ])
model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-5,), loss=tf.keras.losses.Huber(), metrics= 
     ['binary_accuracy', 'mae', pred, true])
model.fit(X,Y, validation_data = (X_val, Y_val), epochs=100, batch_size=100, callbacks=[])

First 10 epochs image



Solution 1:[1]

Because you have used sigmoid activation function in the final layer of your binary classification model, which is absolutely correct.

Sigmoid activation function will give output results between 0 to 1.

For small values (<=0.5),
where sigmoid returns a value close to zero, the predicted class will be assumed 0.
For large values (>0.5),
the result of when the function gets close to 1, then the predicted class will be assumed 1.

You can use the below code to change the output values from decimal to 0,1

predictions = tf.where(predictions < 0.5, 0, 1)
print('Predictions:\n', predictions.numpy())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer2