Relation between logistic regression and neural network?
Logistic regression is mathematically equivalent to a single-layer neural network with one output neuron using a sigmoid activation and binary cross-entropy loss. Deep neural networks generalize this building block by stacking multiple hidden layers with non-linear activations, transforming linearly inseparable inputs into separable latent representations.
Study Card: Relationship between Logistic Regression and Neural Networks
Direct Answer
Logistic regression is a special case of a neural network: specifically, a single-layer neural network (zero hidden layers) with a single output unit, sigmoid activation, and binary cross-entropy loss:
Because there are no hidden layers, its decision boundary is strictly linear in input space . Multi-layer neural networks (MLPs) extend logistic regression by introducing hidden layers with non-linear activations (such as ReLU or GELU), allowing the network to learn hierarchical feature transformations and curved, non-linear decision boundaries before the final linear/sigmoid (or softmax) classifier head.
Key Terms
- Logistic Regression: A linear probabilistic classifier modeling log-odds linearly and outputting .
- Neural Network: Layered architecture composing affine transformations and non-linear activations to learn complex input-output mappings.
- Activation Function: Element-wise non-linearity () preventing stacked layers from collapsing into a single linear map.
- Sigmoid Function: Output activation mapping logits to binary class probabilities; generalizes to softmax for multi-class problems.
- Hidden Layer: Intermediate parameterized layer extracting learned representations fed into subsequent layers.
Example
Imagine predicting customer churn from age and monthly_spend. Logistic regression can only draw a straight line separating churners from non-churners. A neural network with a hidden ReLU layer can learn composite intermediate concepts—such as "young high-spend customer" or "low-spend long-tenure customer"—and feed those learned features into a final sigmoid output head that behaves just like logistic regression on the learned representation space.
Code Implementation
import tensorflow as tf
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
# Generate synthetic binary classification data
X, y = make_classification(n_samples=1000, n_features=2, n_informative=2, n_redundant=0, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 1. Logistic Regression (scikit-learn uses L2 regularization C=1.0 and L-BFGS by default)
lr_model = LogisticRegression()
lr_model.fit(X_train, y_train)
print(f"Logistic Regression Accuracy: {lr_model.score(X_test, y_test):.4f}")
# 2. Neural Network equivalent to unregularized Logistic Regression (0 hidden layers, 1 sigmoid unit)
nn_model = tf.keras.Sequential([
tf.keras.Input(shape=(X_train.shape[1],)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
nn_model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])
nn_model.fit(X_train, y_train, epochs=50, verbose=0)
_, nn_accuracy = nn_model.evaluate(X_test, y_test, verbose=0)
print(f"Single-Layer Sigmoid NN Accuracy: {nn_accuracy:.4f}")
# 3. Neural Network with a ReLU hidden layer (5 hidden units) for non-linear boundaries
nn_model_hidden = tf.keras.Sequential([
tf.keras.Input(shape=(X_train.shape[1],)),
tf.keras.layers.Dense(5, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
nn_model_hidden.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
nn_model_hidden.fit(X_train, y_train, epochs=50, verbose=0)
_, nn_accuracy_hidden = nn_model_hidden.evaluate(X_test, y_test, verbose=0)
print(f"MLP (with ReLU Hidden Layer) Accuracy: {nn_accuracy_hidden:.4f}")
Related Concepts
- Perceptron vs. Logistic Regression: Rosenblatt’s classic Perceptron applies a hard step threshold trained with mistake-driven updates (which only converge on linearly separable data), whereas logistic regression uses a smooth differentiable sigmoid trained with cross-entropy to output calibrated probabilities.
- Multi-Layer Perceptron (MLP): Stacks multiple hidden layers with non-linearities and generalizes multinomial/binary logistic regression to arbitrary non-linear mappings.
- Backpropagation: Applies the chain rule to compute gradients of the cross-entropy loss across all hidden layers efficiently.
- Why Non-Linearity Matters: Without non-linear activations in hidden layers, collapses back into a single linear matrix , offering no extra expressivity over logistic regression.
- Universal Approximation Theorem: States that a feedforward network with even a single sufficiently wide hidden layer and a non-polynomial activation can approximate any continuous function on a compact subset of arbitrarily well.