Tensorflow
Logistic Regression
Machine Learning
Deep Learning
Tensorflow 2.0

Logistic Regression using Tensorflow 2.0?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Logistic regression is a simple but strong baseline for binary classification. In TensorFlow 2, the most practical implementation uses Keras with a single sigmoid output unit. This gives clear training behavior, easy evaluation, and a model that can later grow in complexity if needed.

Model Structure in TensorFlow 2

For standard logistic regression, you do not need hidden layers. One dense layer with one unit and sigmoid activation is enough.

python
1import numpy as np
2import tensorflow as tf
3
4# Reproducibility
5np.random.seed(7)
6tf.random.set_seed(7)
7
8# Synthetic binary dataset
9n_samples = 800
10n_features = 6
11X = np.random.randn(n_samples, n_features).astype('float32')
12true_w = np.array([0.8, -1.2, 0.5, 0.0, 1.5, -0.7], dtype='float32')
13logits = X @ true_w + 0.2
14probs = 1 / (1 + np.exp(-logits))
15y = (probs > 0.5).astype('float32')
16
17model = tf.keras.Sequential([
18    tf.keras.layers.Input(shape=(n_features,)),
19    tf.keras.layers.Dense(1, activation='sigmoid')
20])
21
22model.compile(
23    optimizer=tf.keras.optimizers.Adam(learning_rate=0.03),
24    loss='binary_crossentropy',
25    metrics=['accuracy', tf.keras.metrics.AUC(name='auc')]
26)
27
28history = model.fit(X, y, epochs=20, batch_size=32, validation_split=0.2, verbose=0)
29print('final val accuracy:', history.history['val_accuracy'][-1])
30print('final val auc:', history.history['val_auc'][-1])

This is runnable and captures the classic logistic regression setup.

Interpreting the Learned Weights

A useful benefit of logistic regression is interpretability. You can inspect feature weights directly.

python
weights, bias = model.layers[0].get_weights()
print('weights:', weights.reshape(-1))
print('bias:', bias)

Positive weights push predictions toward class one, while negative weights push toward class zero. Magnitude reflects relative influence, assuming feature scales are comparable.

Prediction and Threshold Management

Default threshold is typically 0.5, but many real systems use a custom threshold for precision or recall goals.

python
1pred_prob = model.predict(X[:10], verbose=0).reshape(-1)
2threshold = 0.6
3pred_label = (pred_prob >= threshold).astype(int)
4
5print('probabilities:', pred_prob)
6print('labels at threshold 0.6:', pred_label)

Use validation data to tune this threshold to your business objective.

Improving Training Stability

Even simple models can behave poorly without basic hygiene:

  • scale features when ranges differ significantly
  • use an appropriate learning rate
  • monitor both loss and AUC
  • apply class weights for imbalanced targets

Example with class weights:

python
class_weight = {0: 1.0, 1: 2.0}
model.fit(X, y, epochs=10, batch_size=32, class_weight=class_weight, verbose=0)

This helps when positive samples are underrepresented.

End-to-End Evaluation Workflow

A reliable baseline model includes explicit train-test split and confusion-matrix style validation. This prevents overconfidence from training-only metrics.

python
1import numpy as np
2from sklearn.model_selection import train_test_split
3from sklearn.metrics import classification_report
4
5X_train, X_test, y_train, y_test = train_test_split(
6    X, y, test_size=0.25, random_state=42, stratify=y
7)
8
9model.fit(X_train, y_train, epochs=15, batch_size=32, verbose=0)
10
11prob_test = model.predict(X_test, verbose=0).reshape(-1)
12pred_test = (prob_test >= 0.5).astype(int)
13
14print(classification_report(y_test.astype(int), pred_test))

If this report shows large precision-recall imbalance, adjust class weighting, threshold, or feature engineering before moving to more complex models. A strong logistic baseline gives a trustworthy benchmark for any future deep architecture.

You can also log calibration curves to understand whether probabilities are overconfident or underconfident. Even when classification metrics look acceptable, poor calibration can break downstream decision logic that relies on probability thresholds.

Common Pitfalls

A frequent mistake is treating logistic regression like deep learning and adding unnecessary hidden layers. That changes the model type and can obscure baseline performance.

Another issue is passing integer labels with wrong shape or dtype. Keep labels as numeric values compatible with binary cross entropy.

Feature scaling is often skipped. If one feature range dominates others, optimization may converge slowly or produce unstable coefficients.

Finally, do not rely only on accuracy for imbalanced datasets. Include AUC, precision, recall, and confusion matrix analysis.

Summary

  • TensorFlow 2 logistic regression is a single sigmoid dense layer.
  • Keep the baseline simple before increasing model complexity.
  • Inspect learned weights to understand feature effects.
  • Tune prediction threshold using validation metrics.
  • Use class weighting and proper preprocessing for imbalanced or noisy data.

Course illustration
Course illustration

All Rights Reserved.