Cost function in logistic regression gives NaN as a result
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Logistic regression is a foundational machine learning algorithm used for binary classification tasks. It estimates the probability that a given input belongs to a category, usually employing the logistic (sigmoid) function. However, during the training phase, particularly when calculating the cost function, practitioners may encounter an unusual problem where the cost function returns NaN (Not a Number). This can result from numerical instability or data preprocessing issues. This article aims to explore the reasons why this happens and how to resolve it.
The Logistic Regression Model
Logistic Function
Logistic regression models the probability that the dependent variable belongs to a particular category. The logistic, or sigmoid function, is defined as:
where is the weighted sum of the input features.
Cost Function
The cost function for logistic regression is given by the log loss function:
where: • is the number of training samples. • is the true label of sample . • is the predicted probability for sample .
Why Does the Cost Function Return NaN?
Several issues can lead to NaN values in the cost function:
- Division by Zero: When is exactly 0 or 1, or can result in
NaN. - Numerical Overflow/Underflow: Large positive or negative inputs to the sigmoid function can cause overflow in exponential calculations, leading to
NaN. - Data Precision: Very small feature values or a large range of values in datasets might affect calculations due to floating-point precision limits.
- Improper Data Scaling: Data that has not been normalized or standardized can lead to poor performance and numerical issues.
- Extreme Learning Rate: A learning rate that is too large can result in drastic updates to parameters leading to undefined operations.
Handling NaN in the Cost Function
Techniques to Prevent NaN
- Clipping Predictions: Constrain the values of to a range slightly away from 0 and 1:
Related reading
- Cost Function, Linear Regression, trying to avoid hard coding theta. Octave.
- Couch DB cluster immediate read not available
- Could Keras prefetch data like tensorflow Dataset?
- Could not convert string to float error from the Titanic competition
- Could not access Kubernetes Ingress in Browser on MacBook
- Could not auto-determine entry point from rollupOptions
- Could not create cudnn handle CUDNN STATUS INTERNAL ERROR
- Could not load dynamic library 'cudnn64_8.dll'; dlerror cudnn64_8.dll not found
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.