CodeWithBotina
May 26, 2026 9 min read

Integral calculus at the heart of LLMs: how neurons learn to think

Integral calculus at the heart of LLMs: how neurons learn to think

When we talk about large language models, such as GPT, LLaMA, or Gemini, we tend to think about complex architectures, billions of parameters, and gigawatts of computation. But beneath all of that lies mathematics dating back to the 17th and 18th centuries. Integral calculus, that tool many of us learned as a set of rules for calculating areas under curves, is actually the glue that turns a pile of matrix multiplications into a system capable of writing poetry, reasoning about code, or holding a conversation.

This article is not a complete calculus class. It is a guided journey so you can understand how the integral appears at every stage in the life of an artificial neuron inside an LLM. From the activation function that decides whether a neuron fires, to the learning process that adjusts its connections, including normalization and attention mechanisms. We are going to get technical and mathematical, but without losing sight of the purpose: to show that integrals are not just a blackboard exercise, but the language in which artificial intelligence writes its own code.


The artificial neuron and its activation function

A neuron in a deep neural network receives several inputs, multiplies them by weights, sums them, and then applies a nonlinear function. That activation function is what allows the model to learn complex relationships. Early networks used the step function, but it was discontinuous and non-differentiable. Then came the sigmoid, and with it our first integral.

The standard sigmoid function is:

\sigma(x) = \frac{1}{1 + e^{-x}}

This function is the solution to a well-known differential equation in population dynamics: the logistic equation. But it can also be expressed as an integral. Notice that the derivative of the sigmoid satisfies:

\frac{d\sigma}{dx} = \sigma(x) (1 - \sigma(x))

So the sigmoid itself is the integral of its own derivative:

\sigma(x) = \int_{-\infty}^{x} \sigma(t) (1 - \sigma(t)) \, dt

Although in practice we do not compute the sigmoid this way, the integral property reveals that the activation is a smooth accumulator of the past. In modern LLMs, the most commonly used activation functions are ReLU and its variants (GELU, Swish). The GELU (Gaussian Error Linear Unit) function is defined through the integral of the normal distribution function:

\text{GELU}(x) = x \cdot \Phi(x) = x \int_{-\infty}^{x} \frac{1}{\sqrt{2\pi}} e^{-t^2/2} \, dt

Here, \Phi(x) is the cumulative distribution function of the standard normal distribution. In other words, each GELU neuron multiplies its input by the probability that a normally distributed random variable is smaller than that input. This smooths the activation in a way that improves gradient flow. The integral appears explicitly: you are calculating the area under the Gaussian bell curve up to the point x.


Backpropagation: the chain rule as a cumulative integral

Learning in neural networks is based on gradient descent. To adjust the weights, we need to differentiate the loss function with respect to each weight. This is done with the chain rule, but there is an interesting integral interpretation.

Consider the total loss \mathcal{L} as the sum of the losses over each example. In the continuous case, if we think of a data distribution with density p(x), the expected loss is:

\mathcal{L}(\theta) = \int \ell(f_\theta(x), y) \, p(x) \, dx

Here, f_\theta is the model with parameters \theta. The gradient of this loss is:

\nabla_\theta \mathcal{L} = \int \nabla_\theta \ell(f_\theta(x), y) \, p(x) \, dx

In practice, we approximate this integral with an average over a minibatch. But the fundamental idea is that every optimization step is a Monte Carlo estimate of a high-dimensional integral. LLMs with hundreds of billions of parameters are, in essence, solving an integral in a space of astronomical dimensions.


Normalization: the integral trick for stabilizing training

Modern LLMs use normalization layers (LayerNorm, RMSNorm). The LayerNorm formula is:

\text{LayerNorm}(x) = \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} \cdot \gamma + \beta

where \mu and \sigma^2 are the mean and variance across the feature dimension. But what does this have to do with integrals? The mean \mu is a (discrete) integral:

\mu = \frac{1}{d} \sum_{i=1}^{d} x_i \quad \longleftrightarrow \quad \mu = \int x \, dP(x)

In the continuous case, the mean is the first moment of the activation distribution. Normalization forces the first- and second-order moments to remain constant across layers. This prevents gradients from exploding or vanishing.

A deeper way to see it: batch normalization (BatchNorm) can be interpreted as a technique for controlling the integral of the density function of activations. By keeping the mean and variance fixed, we ensure that the activation integral weighted by its probability does not drift. In current transformers, LayerNorm is preferred because it operates across features rather than batches, and it is more stable for long sequences.


Attention: the smooth integral that allows the model to look back

The attention mechanism is the heart of LLMs. In its simplest version, scaled dot-product attention is defined as:

\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{Q K^T}{\sqrt{d_k}}\right) V

The softmax operation converts a vector of scores into a probability distribution:

\text{softmax}(z)_i = \frac{e^{z_i}}{\sum_{j} e^{z_j}}

But that denominator is nothing more than a discrete integral (a sum). In the limit of a very long sequence, the sum becomes an integral over positions. Imagine we have a continuous sequence of tokens, with a score function s(t, t^{\prime}) between the query at position t and the key at position t^{\prime}. The attention output at point t would be:

a(t) = \frac{\int e^{s(t, t')} \, v(t') \, dt'}{\int e^{s(t, t')} \, dt'}

This is a smoothed integral (or weighted average) where the weight is an exponential. Thus, attention allows the model to “integrate” information from the entire past context to produce a representation in the present. Attention kernels (as in linear transformers) are often derived from integral approximations using random features or quadrature techniques.


Optimization: Adam and momentum as a discounted integral

The Adam optimizer (Adaptive Moment Estimation) is the standard for training LLMs. It maintains a moving average of gradients and squared gradients. Those moving averages are discounted integrals over time. If we denote g_t as the gradient at step t, the first-moment update is:

m_t = \beta_1 m_{t-1} + (1-\beta_1) g_t

This is equivalent to:

m_t = (1-\beta_1) \sum_{i=0}^{t} \beta_1^{t-i} g_i

In the continuous limit, with time step \Delta t, we have:

m(t) = (1-\beta_1) \int_{0}^{t} e^{\ln(\beta_1)(t-\tau)} g(\tau) \, d\tau

That is, m(t) is an exponentially discounted integral of the gradient history. The parameter \beta_1 controls the half-life of memory. Therefore, Adam is solving an integral equation to estimate the smoothed gradient, which accelerates convergence and stabilizes training.


Regularization: the implicit integral in weight decay

Weight decay regularization adds a term to the loss proportional to the squared norm of the parameters:

\mathcal{L}_{\text{reg}}(\theta) = \mathcal{L}_{\text{original}}(\theta) + \frac{\lambda}{2} \|\theta\|^2

That extra term can be viewed as the integral of the derivative of the parameters over time. In continuous space, the squared norm is the integral of energy:

\|\theta\|^2 = \int_{-\infty}^{\infty} \theta(t)^2 \delta(t) \, dt

It is not an integral in the sense of accumulation, but rather a measure of the “amount” of parameters. Regularization forces parameters not to grow uncontrollably, which is equivalent to limiting the integral of their squared magnitude.


Loss function: cross-entropy as an information integral

In LLMs, the typical loss function for next-token prediction is categorical cross-entropy. For a predicted distribution q and the real distribution p (a one-hot vector), the loss is:

\mathcal{L} = -\sum_{i} p_i \log q_i

In the case of a continuous distribution (if we model probability densities), the sum becomes an integral:

\mathcal{L} = -\int p(x) \log q(x) \, dx

This is continuous cross-entropy. Training an LLM is therefore minimizing an integral that measures the divergence between the real language distribution and the one learned by the model. Every predicted token is a small piece of that global integral.


Practical example: calculating the output of a GELU neuron

Let’s look at a concrete example using the formulas. Suppose a neuron receives an input x = 1.5. The GELU function is defined as:

\text{GELU}(x) = x \cdot \frac{1}{2} \left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right]

where \text{erf} is the error function, which itself is an integral:

\text{erf}(z) = \frac{2}{\sqrt{\pi}} \int_{0}^{z} e^{-t^2} dt

For x=1.5, we have z = 1.5 / \sqrt{2} \approx 1.06066. The integral \int_0^{1.06066} e^{-t^2} dt can be approximated numerically (for example, with Simpson’s rule) or through series expansions. The value of \text{erf}(1.06066) \approx 0.855. Therefore:

\text{GELU}(1.5) \approx 1.5 \cdot 0.5 \cdot (1 + 0.855) = 1.5 \cdot 0.5 \cdot 1.855 = 1.5 \cdot 0.9275 = 1.39125

So that neuron would output approximately 1.39. This is a calculation performed millions of times per second in every layer of an LLM, and behind each one there is an integral being evaluated (although in practice a polynomial approximation for \text{erf} is used instead of the raw integral).


Conclusion: the integral as the mathematical glue

Integral calculus is not just an academic relic. It is the language with which nature expresses accumulation, averaging, smoothing, and probability. LLMs, without realizing it, are solving integrals at every moment: in the activation of each neuron, in the attention they pay to each word, in the update of every weight, and in the regularization that keeps them stable.

Understanding this connection is not necessary to use or even train models, but it is fundamental for those who want to go beyond the black box. It allows you to appreciate that modern artificial intelligence is not magic, but centuries of applied mathematics used with purpose.

So the next time you see an integral, think of it as a miniature neuron: it accumulates, weighs, and transforms. And the next time you use an LLM, remember that beneath every generated word lies a network of discrete integrals working in harmony.


References

The following sources support the information presented.

Nielsen, M. A. (2015). Neural Networks and Deep Learning. Determination Press. http://neuralnetworksanddeeplearning.com

Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.

Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. Advances in Neural Information Processing Systems, 30.

Kingma, D. P., & Ba, J. (2015). Adam: A Method for Stochastic Optimization. 3rd International Conference for Learning Representations.

Hendrycks, D., & Gimpel, K. (2016). Gaussian Error Linear Units (GELUs). arXiv preprint arXiv:1606.08415.

Ba, J. L., Kiros, J. R., & Hinton, G. E. (2016). Layer Normalization. arXiv preprint arXiv:1607.06450.

Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory. 2nd ed. Wiley-Interscience.

1 Like 0 Dislike 1 total

Loading reactions...

Comments (0)

Loading session...

No comments yet. Be the first to comment.

Back to all posts