0
0
TensorFlowml~5 mins

Binary classification model in TensorFlow

Choose your learning style9 modes available
Introduction
A binary classification model helps us decide between two choices, like yes or no, by learning from examples.
To detect if an email is spam or not spam.
To check if a photo contains a cat or not.
To decide if a patient has a disease or is healthy.
To predict if a customer will buy a product or not.
To identify if a message is positive or negative in sentiment.
Syntax
TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=1, activation='sigmoid', input_shape=(input_features,))
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
The model has one output unit with sigmoid activation to give a probability between 0 and 1.
Binary crossentropy loss is used because we have two classes.
Examples
A model with one hidden layer of 10 neurons before the output layer.
TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(20,)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
A simple model with one output neuron and stochastic gradient descent optimizer.
TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, activation='sigmoid', input_shape=(5,))
])

model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])
Sample Model
This program creates a simple dataset where the label is 1 if the sum of features is positive, else 0. It trains a small neural network to learn this pattern and prints predictions for the first 5 samples and the final accuracy.
TensorFlow
import tensorflow as tf
import numpy as np

# Create dummy data: 100 samples, 3 features
np.random.seed(0)
X = np.random.randn(100, 3)
# Labels: 0 or 1 based on sum of features > 0
Y = (np.sum(X, axis=1) > 0).astype(int)

# Build model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(5, activation='relu', input_shape=(3,)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train model
history = model.fit(X, Y, epochs=10, batch_size=10, verbose=0)

# Predict on first 5 samples
predictions = model.predict(X[:5])

# Print predictions rounded to 0 or 1
print('Predictions:', (predictions > 0.5).astype(int).flatten())

# Print final training accuracy
final_acc = history.history['accuracy'][-1]
print(f'Final training accuracy: {final_acc:.2f}')
OutputSuccess
Important Notes
Make sure your input data shape matches the input_shape in the first layer.
Use 'sigmoid' activation in the last layer for binary classification to get probabilities.
Binary crossentropy loss works well when labels are 0 or 1.
Summary
Binary classification models decide between two classes using a sigmoid output.
Use binary crossentropy loss and accuracy metric to train and evaluate.
Simple models can learn patterns from data to predict yes/no outcomes.