I am trying to build a binary classifier with tensorflow.keras Currently unable to identify a solution to having the model generating only 0s and 1s.
The code for compiling my tensorflow model.
from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers def build_model(train_dataset): model = keras.Sequential([ layers.Dense(10, activation='relu', input_shape=[len(train_dataset.keys())]), layers.Dense(1, activation='sigmoid') ]) optimizer = 'adam' model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=[tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), tf.keras.metrics.Accuracy()]) return model model = build_model(X)
After calling model.fit(X,y)
and model.predict(X_test)
an array of numbers are produced as prediction values:
array([[8.3726919e-01], [9.1233850e-04], [8.3726919e-01], ..., [4.6819448e-05], [1.5565268e-08], [0.0000000e+00]], dtype=float32)
My current solution to the problem is to get the output as a number in the range (0,1) from the sigmoid activation function; and then transforming the values to 0 or 1
reset_predictions = [] for p in predictions: if p >= 0.5: reset_predictions.append(1) else: reset_predictions.append(0)
However this is not ideal; what I hope to achieve is for the model to classify 0 or 1 for me without an external function.