Uncertainty in Machine Learning

Author: Asim Kadav

Modeling uncertainty in predictions

There are two most common types of uncertainties in model training:

To provide a simple example, consider a Bayesian linear regression model. The coefficients of the regression are given prior distributions, which capture the epistemic uncertainty about the coefficients. The noise in the data is modeled by the likelihood function, which often assumes that the residuals follow a Gaussian distribution, capturing the aleatoric uncertainty. Note: Whether a method can model these uncertainties often depends on how it's used. For example, a traditional neural network does not model uncertainty, but if you train an ensemble of neural networks, you can get a measure of epistemic uncertainty from the spread of the ensemble's predictions. Similarly, by designing the network to predict the variance as well as the mean (a form of heteroscedasticity), you can get a measure of aleatoric uncertainty.

Comparison of Frequentist and Ensemble methods for uncertainty estimation

Click to expand Python Code for generating above plot
				
					# Define a function to generate synthetic data
					def generate_data(n_samples, noise_level, random_state=None):
						np.random.seed(random_state)
						X = np.linspace(-5, 5, n_samples)
						y = 3 * X + np.random.normal(0, noise_level, n_samples)
						return X, y

					# Generate synthetic data
					n_samples = 100
					noise_level = 3
					X, y = generate_data(n_samples, noise_level, random_state=42)

					# Fit a linear regression model (Frequentist)
					lr = LinearRegression()
					lr.fit(X.reshape(-1, 1), y)
					y_pred_lr = lr.predict(X.reshape(-1, 1))

					# Fit a Gradient Boosting model (Ensemble)
					gbr = GradientBoostingRegressor(n_estimators=100, random_state=42)
					gbr.fit(X.reshape(-1, 1), y)
					y_pred_gbr = gbr.predict(X.reshape(-1, 1))

					# Plot the results
					plt.figure(figsize=(14, 7))

					plt.subplot(1, 2, 1)
					plt.scatter(X, y, color='blue', label='Data')
					plt.plot(X, y_pred_lr, color='red', label='Linear Regression')
					plt.fill_between(X, y_pred_lr - noise_level, y_pred_lr + noise_level, color='red', alpha=0.1)
					plt.title('Frequentist: Linear Regression')
					plt.legend()

					plt.subplot(1, 2, 2)
					plt.scatter(X, y, color='blue', label='Data')
					plt.plot(X, y_pred_gbr, color='green', label='Gradient Boosting Regressor')
					plt.fill_between(X, y_pred_gbr - noise_level, y_pred_gbr + noise_level, color='green', alpha=0.1)
					plt.title('Ensemble: Gradient Boosting Regressor')
					plt.legend()

					plt.show()


			
		
These plots give you a sense of the distribution of the uncertainties estimated by the models. This can be useful for understanding how confident the model is in its predictions. For example, larger aleatoric uncertainties might indicate noisier data, while larger epistemic uncertainties might indicate that the model is less certain about its predictions. In the aleatoric uncertainty plot, logvar is a learnable parameter of the model that estimates the log-variance of the noise in the data. When you take the exponential of logvar, you get the standard deviation of the noise. If the aleatoric uncertainty plot is showing a single line, it means that the model has learned a single standard deviation value for the entire dataset. This suggests that the model believes the noise level is roughly the same across all data points. In the epistemic uncertainty plot, the model uses dropout to create multiple predictions for each data point and then computes the variance of these predictions. A higher variance indicates more disagreement among the predictions, which can be interpreted as higher epistemic uncertainty. If the plot is decreasing, it indicates that there are more data points with low epistemic uncertainty (low variance among predictions) and fewer data points with high epistemic uncertainty. Remember that these are simplified models for illustrating the concepts of aleatoric and epistemic uncertainty. In practice, you might use more sophisticated models to estimate these uncertainties, especially for complex tasks or large datasets.

Bayesian uncertainty visualization



Bayesian nonlinear regression uncertainty



Decision tree ensemble uncertainty estimation

Here is the plot for an ensemble of decision trees: - The observed data points are shown in blue. - The mean prediction of the ensemble is shown in red. - The red shaded area represents the epistemic uncertainty, which is estimated as the standard deviation of the predictions of the individual trees in the ensemble. As you can see, the ensemble is able to capture the non-linearities in the data, and the spread of the individual trees' predictions gives us a measure of the epistemic uncertainty. However, as mentioned before, ensemble methods like this do not directly model aleatoric uncertainty.

[1] Attend and Interact: Higher-Order Object Interactions for Video Understanding. Chih-Yao Ma, Asim Kadav, Iain Melvin, Zsolt Kira, Ghassan AlRegib, and Hans Peter Graf. In CVPR, 2018.

[2] Tripping Through Time: Efficient Localization of Activities in Videos. (Spotlight) Meera Hahn, Asim Kadav, James M. Rehg, Hans Peter Graf. In CVPR Workshop on Language and Vision, 2019. Also, appears in BMVC'20.

Posted at 1:44 AM on Saturday, 21st of August, 2021