Perfectly Random

machine learning and stuff

Bernoulli Distribution as a tiny Neural Network

Logistic regression is often considered the smallest neural network for binary classification. We can think of Bernoulli distribution as an even smaller neural network – one that doesn’t even depend on the input data. Such a neural network would likely not be useful in practice. However, given it’s simplicity, it serves as an illuminating example to help us understand the statistical assumptions underlying a neural network model. The assumptions we require for modeling Bernoulli distribution as a neural network are also required for larger neural networks. As an example, using Bernoulli distribution as a tiny neural network, we can easily demonstrate how the famous cross-entropy loss comes into being. We can even extend this Bernoulli distribution model framework to recreate the familiar logistic regression model by simply replacing a constant parameter by a sigmoid-affine function.

(This document is available as a PDF)

Bernoulli distribution

Bernoulli distribution, owing to its simplicity, is used more often than it is noticed. A random variable has the following probability mass function (pmf):

in which, the only parameter, , is a probability and therefore must satisfy . We call the above equation the raw form pmf of the Bernoulli distribution.

The raw form pmf is simple to understand but its multi-case structure makes it difficult to use in other derivation. We can combine the two of the three cases into one equation without changing anything about the distribution. This results in the following two forms of pmf – the additive form:

and the multiplicative form:

All three forms — raw, additive, multiplicative1 — are equivalent to each other and represent the same exact distribution. This implies that no matter which of the three forms we use for our analysis, we should get the exact same analytical result. However, one form may be easier to work with than the others when wrangling algebraic equations. The multiplicative form is the most common one used in both statistical analysis and with neural networks.

Binary classification

Let’s consider a familiar application of supervised binary classification in computer vision – image classification. We would like to classify a given image into one of two classes – a cat image versus a dog image:

Cat

Dog

Image by Pexels

In a supervised setting, we usually have training data available, which is represented as:

in which, is the input data and is the output label. For the cat vs dog example, is a vector of pixel values obtained by flattening the tensor that represents an image and represents the label – cat () or dog ().

Modeling the binary classification problem as Bernoulli distribution

Modeling binary classification

We aim to fit a function to describe the input-output relationship in the training data. We could attempt to find a suitable deterministic function and minimize (w.r.t the model parameters ) some appropriate measurement of discrepancy () between the function’s predicted labels and true labels2. Alternatively, we could model the output label as a random variable3

in which, is the set of model parameters. The training data (shown above) is interpreted as a list of statistical samples of generated along with the corresponding values of .4 When we choose to model the output label as random variable, we have a well-established approach to minimize the discrepancy between the predicted and true labels – maximum likelihood estimation.

Modeling with Bernoulli distribution

Since the true output labels only take values in , it would be ideal if our choice of random variable also assumes values in . Bernoulli distribution is one such choice:

in which, . Note how the model above doesn’t depend on the input at all.

Now that we have a statistical model to describe the output, we can write down the likelihood as follows:

The second equation is the Bayesian form of likelihood, in which we choose to model the parameters as a random variable . The first equation is also the likelihood but it doesn’t consider the model parameters as random variables. We will only use the first form of likelihood in this document because we have no need to model the parameter(s) as random variable(s) at this time.

Assuming independence, we can re-write the likelihood as:

Substituting the multiplicative form and applying the knowledge that the output labels , we obtain:

Taking logarithm, the log-likelihood is:

The expression above is the famous cross-entropy loss5. Maximizing the log-likelihood:

in which is the maximum likelihood estimate for . In order to confirm that this value of actually maximizes the log-likelihood, we can show that the second order derivative is negative as follows:

See Appendix A in the PDF version for the same result obtained using the additive form.

Extension to logistic regression

In the previous subsection, we didn’t even consider the input in our model. If we want to include the input , we could replace the previously constant with a function of . A simple way to include is to model as an affine6 function of instead of a constant

in which, and are model parameters. However, there is a problem with the above affine function – there is no guarantee that the expression would be within and , as required for .7 We can solve this problem easily by passing through a sigmoid function to obtain the following sigmoid-affine function:

The resulting model for becomes:

which is exactly the logistic regression model. The difference is that instead of directly assuming the logistic form, we have chosen to interpret logistic regression model as an extension of the Bernoulli distribution.

We can now perform log-likelihood maximization as usual. The likelihood is given by the following expression:

in which, . The log-likelihood may be written as:

Log-likelihood may be maximized via any of the numerical optimization algorithms such as gradient descent.

Summary

Log-likelihood equations for Bernoulli distribution and logistic regression have the same form. In fact, we can obtain the log-likelihood for logistic regression by simply replacing the in the log-likelihood for the Bernoulli model. Logistic regression is a one-layer8 neural network:

Logistic Regression

Logistic Regression

We can think of the Bernoulli model as an even simpler neural network that isn’t connected to the input layer at all:

Bernoulli Model

Bernoulli Model

Equivalently, Bernoulli model is the same as a logistic regression model that has all the weights set to zero and . Bernoulli model has a constant number of unknown model parameters while logistic regression has a parameter linear complexity in the input image size. Conversely, we can also think of logistic regression as one particular extension of the Bernoulli model framework.

Footnotes

  1. Note that we interpret as and

  2. For certain choices of and , the estimated model parameters may equal the estimated model parameters obtained using the statistical modeling approach, analytically. 

  3. A random variable is also a function (a measurable function) but we choose to highlight the difference between any deterministic function and the restrictive measurable function that is a random variable. 

  4. For this problem, we choose to consider the list of different images (i. e., ’s) as deterministically fixed. Therefore, is not random and we do not condition on

  5. This is a demonstration of the equivalence between maximizing the likelihood and minimizing the KL divergence. 

  6. A linear function would be even simpler than affine function

  7. We could always treat the problem as a constrained optimization problem in which can only take values such that but we do not pursue this line of analysis. 

  8. Typically, we don’t count input layer at all.