<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Jacob Reinhold]]></title><description><![CDATA[probability | programming]]></description><link>https://www.jcreinhold.com/</link><image><url>https://www.jcreinhold.com/favicon.png</url><title>Jacob Reinhold</title><link>https://www.jcreinhold.com/</link></image><generator>Ghost 5.79</generator><lastBuildDate>Fri, 03 Apr 2026 20:44:44 GMT</lastBuildDate><atom:link href="https://www.jcreinhold.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Image enhancement and the data processing inequality]]></title><description><![CDATA[On the ability and limitations of the data processing inequality to answer questions about image enhancement]]></description><link>https://www.jcreinhold.com/image-enhancement-and-the-data-processing-inequality/</link><guid isPermaLink="false">622e3dc7e647261e657d5914</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[Information Theory]]></category><dc:creator><![CDATA[Jacob Reinhold]]></dc:creator><pubDate>Fri, 23 Oct 2020 22:36:40 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1539186607619-df476afe6ff1?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1539186607619-df476afe6ff1?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" alt="Image enhancement and the data processing inequality"><p>Information theory studies the communication of information with the language of probability theory and statistics. Claude Shannon laid out the foundations of the field with his work <em><a href="http://people.math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf?ref=jcreinhold.com">A Mathematical Theory of Communication</a></em> in 1948. The paper defines a reasonable measure of information (more specifically, a measure of <em>average</em> information), and proves some results regarding how efficiently you can communicate over a noiseless and noisy channel (e.g., radio over the air or storage of data in an imperfect medium like a hard drive). Since that time, information theory has continued to be studied and is partially behind many of the advances in telecommunications, and it provides some useful methods of analysis for statistics and machine learning.</p><p>While many results of information theory have practical importance, there is one inequality that is often misguidedly invoked; namely, the <em>data processing inequality.</em> We&apos;ll quickly overview the basics of information theory so that we can properly motivate the inequality, and then we&apos;ll discuss its ability and its limitations in illuminating the impact of data processing. Specifically, we&apos;ll discuss how it applies to image enhancement such as super-resolution; we&apos;ll show that it is both an essential component of analysis because it shows that image enhancement cannot create new information, and that it is useless in practice because it is the wrong way to analyze image enhancement methods.</p><h2 id="information-and-entropy">Information and entropy</h2><p>Mathematically, information is defined as the logarithm of the reciprocal of a probability mass, i.e., for a (discrete) random variable $x\sim p(x)$, the information gained by observing $x=a$ for $x\in\mathcal{X}$ is</p><p>$$h(x=a)=\log_2 \frac{1}{p(a)}.$$</p><p>The heuristic argument for this quantity is that rare events should communicate more information than common events&#x2014;the more surprising an event, the more you update your beliefs upon observing the event&#x2014;which is why the reciprocal of the probability mass is used. The intuition behind the logarithm is more complex. I&apos;ll motivate it in the sense of optimal code-lengths.</p><p>First we need to define a (source) <em>code</em> which is a mapping from some set of events (i.e., whatever $\mathcal{X}$ represents) to a set of strings of symbols (e.g., strings of 0s and 1s). Then the log of the reciprocal of the probability mass of an event is equal to a (competitively) optimal code-length that describes the event, where &quot;optimality&quot; is in the sense of maximal average compression of a sequence of transmitted symbols.</p><p>For example, if some event $x=b$ has $p(b)=1/2$, then the optimal (source) code-length is 1. If some event $x=c$ has $p(c)=1/256$, then the optimal code-length is 8. Why is encoding in this way optimal? The intuition is that common events should be encoded with short codes because they are frequently encountered, and short codes reduce the total number of symbols that you need to transmit to communicate a message. The opposite holds for rare events; you pay little penalty for encoding rare sequences with long codes because they are communicated infrequently. In the end, you&apos;ll reduce the average number of symbols used to communicate a string of events if you encode in this way.</p><p>To show this mathematically, let $f:\mathcal{X}\to\{0,1\}^m$ describe the encoder&#x2014;i.e., the map from events $\mathcal{X}$ to strings of symbols $\{0,1\}^m$ (where $m$ can possibly vary)&#x2014;and let $\ell(f(x)):\{0,1\}^m\to\mathbb{N}$ represent the length of the code associated with event $x\in\mathcal{X}$. Then the average length of this encoding scheme is:</p><p>$$\mathbb{E}[\ell(f(X))]=\sum_{x\in\mathcal{X}}p(x)\ell(f(x))=\sum_{x\in\mathcal{X}}p(x)\log_2\frac{1}{p(x)}=H(X).$$</p><p>where the right-hand side of the equation is equal to the entropy of the distribution $p(x)$. Thus the entropy&#x2014;a measure of uncertainty&#x2014;is both the average information content of a random variable and the average length of the shortest description of a random variable. In fact these two views are equivalent, and the entropy lower bounds the minimum description length.</p><p>We can extend ideas of information to two or more variables, e.g., quantifying the amount of (average) information a random variable $X$ contains about another random variable $Y$. This is represented by <em>mutual information</em>. Formally, the mutual information between $X$ and $Y$ is</p><p>$$I(X;Y) = H(X)-H(X\mid Y).$$</p><p>That is, it&apos;s the reduction of uncertainty in $X$ due to the knowledge of $Y$.</p><p>Before we continue to the data processing inequality, I need to define a Markov chain which simply describes a way to factorize a joint probability distribution. Specifically, a Markov chain implies that for a set of ordered random variables $X_1,X_2,\ldots,X_n$, knowledge of $X_i$ only relies on $X_{i-1}$ and nothing preceding that value. Formally,</p><p>\begin{align*}p(x_1,x_2,\ldots,x_n)&amp;=p(x_1)p(x_2|x_1)p(x_3|x_2,x_1)\cdots p(x_n|x_{n-1},\ldots,x_2,x_1)\\ &amp;=p(x_1)p(x_2|x_1)p(x_3|x_2)\cdots p(x_n|x_{n-1})\end{align*}</p><p>where the first equality is true for any probability distribution, and the second equality is due to the set of conditional independencies implied by the Markov property.</p><h2 id="data-processing-inequality">Data processing inequality</h2><p>This brings us to the topic of this article: the <em>data processing inequality</em>. This inequality states that processing a signal&#x2014;of any kind&#x2014;results in a loss of information. Formally, the inequality states that for a Markov chain $X \to Y \to Z$, we have</p><p>$$ I(X;Y) \ge I(X;Z) \implies H(X\mid Y) \le H(X\mid Z).$$</p><p>That is, the mutual information of $X$ and $Y$ is greater than $X$ and $Z$; equivalently, the uncertainty about $X$ given $Y$ is less than the uncertainty about $X$ given $Z$.</p><p>To motivate what this means, simply think of $X$ as a message, $Y$ as the message transmitted over a channel, and $Z$ as the received message. That is, $Y=f(X)$ and $Z=g(Y)$, are functions of only the previous step in the chain of events. Given this setup, the inequality states that $Y$ contains more information about $X$ than $Z$.</p><p>This inequality should intuitively make sense. When you process data, you are often applying a non-invertible transformation (e.g., filtering the signal to remove noise). As implied by &quot;non-invertible&quot;, this is a one-way process. Once you&apos;ve applied a non-invertible transformation, you cannot recover the original signal. You can make orange juice with an orange, but you can&apos;t make an orange with orange juice. Similarly, in processing data, the information that is removed is lost to oblivion, which is what is conveyed by the data processing inequality.</p><h2 id="does-modern-image-enhancement-break-the-data-processing-inequality">Does modern image enhancement break the data processing inequality?</h2><p>Following Betteridge&apos;s law of headlines, the answer is <em>no</em>.</p><p>When we look at image enhancement methods like de-noising and super-resolution, the output $Z$ looks like it better reflects what the true image $X$ would be if it were observed under better conditions as compared to the observed noisy or low-resolution image $Y$. Mathematically, it appears like the following statement is true: $I(X;Y)\le I(X;Z)$&#x2014;a reversal of the inequality.</p><p>The state-of-the-art in machine learning methods&#x2014;most frequently deep neural networks&#x2014;create images that closely resemble realistic images without noise (inverting the noise process), or images that have a greater resolution than what than what the measurements allow with respect to the Nyquist rate.</p><p>An argument can be made that because the images are passed through a set of filter banks whose parameters are learned (i.e., a neural network), the data processing inequality doesn&apos;t apply. Vaguely, the hope is that $Z$ is not only a function of $Y$ but something like $Z=f(Y,g(X))$. In what follows, I argue that this is a flimsy hope that doesn&apos;t withstand serious scrutiny.</p><h3 id="applicability-of-the-inequality">Applicability of the inequality</h3><p>As stated before, the data-processing inequality still applies to de-noising and super-resolution. These methods cannot invert an un-invertible function and add lost information. Learning the filters doesn&apos;t change the fact that a series of filters&#x2014;in the case of neural networks&#x2014;are applied to the image. I&apos;ll provide a couple of thought experiments to show why this is the case.</p><p>Consider the case where the neural network was randomly initialized to the exact weights that were learned after training. After passing an image through this randomly initialized network, would you say that information was added? The resulting image would be exactly the same as the image generated by the network that arrived at the parameters due to training.</p><p>If you think that the learned weights add information but the randomly initialized weights don&apos;t, why would one case add information but not the other if the result is the same? What is special about trained parameters $\theta_1$ compared to randomly initialized parameters $\theta_{2}$, when $\theta_1=\theta_2$?</p><p>If you think that both sets of parameters add information, would you argue that the data processing is broken when learned and also with some probability $p &gt; 0$ (the case where the weights were randomly initialized to the learned weights)?</p><p>I think most reasonable people would see that answering in the affirmative to either argument is absurd, and consequently neither sets of parameters add information.</p><p>Admittedly, the above situation would only happen with a vanishingly small (but potentially strictly positive!) probability, and it would only happen with certain initialization schemes. To give a more realistic example, let&apos;s compare machine learning methods to plain ol&apos; engineering.</p><p>Consider the case of a radio engineer. They inject their <em>learned</em> knowledge into a system by <em>hand-crafting</em> the modules of the system to accomplish some task. For example, they will multiply a received signal with a sinusoid to baseband the signal, and then low-pass filter the signal to remove harmonics and noise before using it for some other task. I doubt that there are many&#x2014;if any&#x2014;information theorists or signal processing experts that would consider this to be a case where the data processing inequality doesn&apos;t apply.</p><p>For a more traditional image enhancement step, consider the case where you observe an image with salt-and-pepper noise and you apply a median filter&#x2014;the optimal filter for that type of noise. Does the filtered image now have more information than the original image? Most image processing experts would agree that would not be the case.</p><p>If neither of the above cases add information, why would a neural network add information? As mentioned before, when the engineer applies their craft, they are using their learned knowledge to enhance the signal or image. What is so materially different about learned engineering knowledge and learned neural network parameters that one adds information and the other doesn&apos;t? What could be so sacred and magical about <em>data-driven</em> methods? (As if engineering knowledge wasn&apos;t at least partially data-driven.)</p><p>To be clear, I am <em>not</em> arguing that the learned parameters of a deep neural network aren&apos;t related to the observed data. Of course the parameters are related to the data, otherwise the network would do nothing useful. My argument is much more limited: the learned parameters do not add information that was not already contained in the data. There is no guarantee that the de-noised or super-resolved image match what would have been generated if the image were acquired under less noisy conditions or at a higher resolution.</p><p>But if information is not added, what&apos;s going on? The de-noised and super-resolved images can often be shown to have lower average error in experiments when compared to more traditional image processing methods. The dilemma lies in the very limited definition of information that is implied in the data processing inequality.</p><h3 id="limitations-of-the-inequality">Limitations of the inequality</h3><p>Let me provide one more example which will more clearly outline the limitations of the definition of information used in the data processing inequality.</p><p>Let&apos;s consider the case of the information in light, with and without glasses, before the light enters the eye. Loosely speaking, let&apos;s describe the case as follows: let $X$ be a vector of the characteristics (e.g., color) of some point on an object; let $Y$ be the (amplitude, frequency, phase, trajectory of the light, etc.) of some light reflected from that point $X$; and let $Z$ be the (amplitude, frequency, phase, trajectory of the light, etc.) of the light $Y$ after having passed through a lens in a pair of glasses. Then we can model $Y = f(X)$ and $Z = g(Y)$ for appropriate functions $f$ and $g$. That is, we have the Markov chain $X \to Y \to Z$.</p><p>Then, again, we have a situation where the data processing inequality applies. The data processing inequality then states that $Z$ has less information about $X$ than $Y$. For concreteness, let&apos;s say $I(X; Y) = n$ bits and $I(X; Z) = m$ bits for $m \le n$.</p><p>Are all the poor simpletons that wear glasses simply misguided due to their lack of education in elementary information theory? I, for one, will keep on wearing my glasses despite the information loss.</p><!--kg-card-begin: markdown--><p>This is a confusing result because, as someone with myopia, I perceive much more information in the light that passes through my glasses than without my glasses<sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup>. This is because my glasses alter the trajectory of the light such that my focal length is different. As a result, the light through my glasses is not aggregated, sub-optimally, to a collection of retinal cells&#x2014;causing a perceived blur when viewing distant objects. However, the light $Y$, before it enters my eye, contains what information it contains about $X$ ($n$ bits in this case) and no more. My glasses, the function $g$, cannot add more information to $Y$ about $X$ with $Y$ alone, and $Y$, the light, is the only input to my glasses.</p>
<p>A naive application of the data processing inequality can have this kind of result. Unless you&apos;re going to do a thorough information-theoretic analysis of each problem&#x2014;proving that the problem has all the criteria that the relevant theorems require&#x2014;a more useful way to think about the effect of processing a signal is in answering the following question: What utility does this processing provide to achieve our goal?</p>
<p>The question that leads people to process signals usually isn&apos;t strictly about the amount of information in a signal; it is about how humans or machines can efficiently use the information contained in the signal for some other purpose. Clearly the reason people wear glasses is because they are useful; you probably wouldn&apos;t want drivers to forego wearing their glasses just so they could have &quot;more information&quot; according to the (naive) information-theoretic definition.</p>
<p>In the radio example I gave previously, the non-baseband, non-filtered signal is useless. It is full of noise and would require extremely expensive equipment to process the signal at the carrier frequency (notwithstanding the noise). Likewise with the salt-and-pepper image example; if the image is meant for human viewing, why not apply the optimal filter? If the image is an artistic photo, the filtered image could be more pleasant. If the image is for some scientific purpose, perhaps the filtered image will be more interpretable.</p>
<h2 id="takeaways">Takeaways</h2>
<p>While I gave several examples of the general uselessness of the data processing inequality in practical settings, there is a major caveat in this uselessness. The data processing inequality does simply and clearly state that image enhancement techniques like de-noising and super-resolution (outside of cases of multiple measurements or strong assumptions known to be true) cannot recover lost information.</p>
<p>While the resulting images may look better, the image enhancement steps cannot guarantee recovery of the lost information. That is why care must be taken when using these methods in high-stakes applications like medical imaging. It would be reprehensible if de-noising or super-resolution result in the addition or subtraction of a treatable tumor from a clinical image&#x2014;when normal imaging would not have&#x2014;and as a result either induces unnecessary treatment (e.g., shooting high-energy particles at the tumor area until the cells die) or goes ignored so long as to condemn the person to death.</p>
<p>Image enhancement techniques need to be motivated by their utility, not their magical abilities. Image enhancement is an important research area because it can be very useful&#x2014;not because it can accomplish the image processing equivalent of transmuting lead into gold. In natural images, de-noised and super-resolved images can be more visually pleasing, as previously mentioned, and can be used for that purpose.</p>
<p>In medical imaging, they can be used by researchers to collect more stable measurements extracted from images both across subjects and longitudinally (e.g., better whole-brain segmentation). When large sample sizes are used, the individual mistakes due to the methods&#x2014;if present&#x2014;can be washed out (assuming no bias). Or more simply, the methods can simply allow a study to happen.</p>
<p>For example, suppose you are a scientist doing research on a disease that is apparent in medical images, and you only have access to a set of clinical images that are very noisy and have a resolution of $1\times 1\times 3 \text{ mm}^3$. You want to segment the diseased portion of the tissue and either do not have the data or technical skill to implement a segmentation deep neural network to do the task. However, there is a publicly-available deep neural network that was trained for this segmentation task on nearly noise-free data at $1 \text{ mm}^3$. In this case, it would be beneficial to first de-noise and super-resolve the images, otherwise the network may perform poorly because of domain shift.</p>
<p>The data processing inequality is a mixed bag. It can be illuminating under certain conditions, but the limited definition of information central to the inequality can be deceiving.</p>
<p><strong>Addendum</strong>: Side information can be provided about $X$ if you have some independent $T(X)$ or have access to $W$ such that $X = h(W)$. But then we&apos;re analyzing a different problem because $X \to Y \to Z$ (assuming you include $T(X)$ or $W$ to create $Z$) doesn&apos;t form a Markov chain, so the data processing inequality doesn&apos;t apply.</p>
<p>Bayesianism won&apos;t save you here. A good prior on $X$ won&apos;t add information about $X$ if you only have access to $Y$; a Markov chain $X\to Y \to Z$ is simply a joint distribution that factorizes as $p(x,y,z) = p(x)p(y|x)(z|y)$. The exact, true prior of $X$ is implicit in the proof of the theorem!</p>
<hr class="footnotes-sep">
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p>Your brain will actually receive more information for technical reasons, but I&apos;m narrowly focusing on the information content of the light itself before it enters the eye. <a href="#fnref1" class="footnote-backref">&#x21A9;&#xFE0E;</a></p>
</li>
</ol>
</section>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Learning to read from memory with a neural network]]></title><description><![CDATA[Adding a fundamental operation to feedforward neural networks]]></description><link>https://www.jcreinhold.com/reading-from-memory-with-a-feedforward-deep-neural-network/</link><guid isPermaLink="false">622e3dc7e647261e657d5913</guid><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Jacob Reinhold]]></dc:creator><pubDate>Wed, 09 Sep 2020 17:30:30 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1580432522609-d073f3f4b4f9?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1580432522609-d073f3f4b4f9?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" alt="Learning to read from memory with a neural network"><p>To compute complex functions, computers rely on the ability to read from and write to memory. This ability is missing, however, from standard deep neural neural networks (DNNs), and <a href="https://www.nature.com/articles/nature20101?curator=TechREDEF&amp;ref=jcreinhold.com">research has shown</a> that reading and writing to external memory facilitates certain types of computation. But how can we train a DNN to access memory? Both reading and writing are non-differentiable functions&#x2014;as devised in digital computers&#x2014;so they are incompatible with backpropagation which is the standard approach to training a DNN. There has been a substantial amount of work that infuses recurrent neural networks with external memory; however, in this post, we&#x2019;ll make use of the Gumbel-softmax reparameterization to allow a feedforward DNN to read from an external memory bank.</p><h2 id="integers-and-indexing">Integers and indexing</h2><p>A real-world digital computer has a finite bank of memory which is addressed with a binary number. When written to, the memory location associated with that number stores a value that the computer can load for later processing. This ability to store and load data to memory enables computers to compute a large class of functions (that is, the set of functions that are computable by a Turing machine or any equivalent model of computation). Theoretically, recurrent neural networks <a href="https://www.sciencedirect.com/science/article/pii/089396599190080F?ref=jcreinhold.com">are capable</a> of simulating a Turing machine and, consequently, can compute the same set of functions. However, to the best of my knowledge, no such proof has been shown for feedforward networks (especially ones of finite-depth and width).</p><p>Let&apos;s briefly consider how memory works in a real-world computer. Suppose you have a computer with 16-bit memory. You might have an instruction located at memory address (hexadecimal) <code>0x0000</code> and a datum, necessary for some computation, stored at <code>0x1000</code>. Suppose the computer has an instruction at <code>0x0000</code> saying to load the datum at <code>0x1000</code> to a register. When the computer is started&#x2014;in this simplistic example&#x2014;a piece of circuitry called a program counter will configure the state of the circuitry in the CPU to load the instruction at <code>0x0000</code> which configure the state of the circuitry to load the datum at <code>0x1000</code> into a specified register and used for the further computation (that is, the instructions that follow <code>0x0000</code>).</p><p>Standard feedforward DNNs cannot learn to do this type of loading or any type of loading similar to this from an external memory source. DNNs have real-valued (really, floating-point) parameters and treat input and output as real-valued arrays of numbers, even if the input and output are integer-valued (or, as in the case of categorical variables, can be mapped to the integers). For example, 2D natural images are often composed of integers between 0 and 255; however, this property is ignored in standard DNNs and all inputs are cast to real numbers. In classification tasks, the output of the network&#x2014;before any user-defined thresholding or argmax operation&#x2014;represents a probability and is correspondingly real-valued. This transformation from integers to real-valued numbers is necessary for backpropagation to update the weights of the DNN.</p><p>But what if you are in a situation where you require integer-values in the middle of the network? A naive solution would be to use rounding. This, however, is non-differentiable (or at least has a zero gradient almost everywhere). So if rounding is used, the DNN cannot update its weights.</p><p>Let&#x2019;s assume that we are working with an external memory bank that is an array of numbers (a tensor, if you like), and the job of the network is to calculate the (one-hot) index location of a value in that memory bank. An way to do this is to allow for some fuzziness and take a weighted average of several locations (using the softmax function on what represents the memory indices) as in the <a href="https://arxiv.org/abs/1410.5401?ref=jcreinhold.com">Neural Turning Machine</a>. However, I&#x2019;d argue that soft indexing is not as interpretable as simply addressing <em>one</em> location from memory.</p><p>As a silly example to illustrate the point, let&#x2019;s say you want to train one end-to-end DNN to compare user-input pictures of dogs and cats to a prototypical picture of a dog and cat based on whatever class of image the user input (e.g., compare a user-input picture of a cat, in some way, to the prototypical picture of a cat). If you use soft indexing, then the input to the comparison section of the network will be comparing a pixelwise weighted average of the prototypical cat and dog image to the user image. The more desirable function of the DNN would be to use only the prototypical cat image if the user image is of a cat and likewise for a dog.</p><h3 id="hard-indexing-with-the-gumbel-softmax-reparameterization">Hard indexing with the Gumbel-softmax reparameterization</h3><p>We can create hard indices&#x2014;true one-hot index vectors&#x2014;through a trick called the <a href="https://arxiv.org/abs/1611.01144?ref=jcreinhold.com">Gumbel-softmax (GS) reparameterization</a>. The GS relaxes the hard indexing into a soft indexing problem that can be (asymptotically) viewed as an argmax operation.</p><p>Similar to the Gaussian reparameterization discussed in the <a href="https://arxiv.org/abs/1312.6114?ref=jcreinhold.com">variational autoencoder</a>, the GS allows backpropagation to work with a sampling step in the middle of a DNN&#x2014;a normally non-differentiable operation. GS works by changing the sampling operation in such a way that all of the component operations are differentiable. While there are other ways that you can estimate the gradient of a function of integer-valued variables, the GS reparameterization provides a better estimate of the gradient (in the sense that the gradient estimate has lower variance).</p><p>While the theoretical construction of the GS is outside the scope of this post, I&apos;ll give a high-level overview of how the GS works and provide working code in PyTorch. Suppose we have a neural network $f(\cdot)$ with a hidden layer $f_i(\cdot)$ producing a representation $h \in \mathbb{R}^n$ that we intend to use as memory indices.</p><p>To use $h$ to create a one-hot vector that indexes a memory location we:</p><!--kg-card-begin: markdown--><ol>
<li>Generate a sample $g\in\mathbb{R}^n$ from a Gumbel distribution,</li>
<li>Add $h$ and $g$,</li>
<li>Divide the result by a <em>temperature</em> value $\tau &gt; 0$,</li>
<li>Take softmax of the result.</li>
</ol>
<!--kg-card-end: markdown--><p>The temperature value $\tau$, as it goes to zero, makes the softmax operation functionally equivalent to an argmax operation. An example of the result is shown in Fig. 1 where an example distribution is given in the left-most image and the result of the GS sampling process is given in the four plots to the right, each with different values of $\tau$. We see that, as $\tau$ gets close to 0, the sampling does function as an approximate argmax operation. When $\tau$ grows, the less the operation looks like an argmax. In practice, setting $\tau$ very close to zero causes exploding gradients which make training unstable, so we set $\tau$ reasonably low or scale it down it as training progresses.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/temperature.png" class="kg-image" alt="Learning to read from memory with a neural network" loading="lazy" width="930" height="264" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/temperature.png 600w, https://www.jcreinhold.com/content/images/2023/04/temperature.png 930w" sizes="(min-width: 720px) 720px"><figcaption><strong>Fig. 1</strong>: Example of a distribution and the resulting sampling using the Gumbel-softmax trick with varying levels of temperature ($\tau\in\{0.1,0.25,0.5,2.0\}$)</figcaption></figure><p>Because we cannot set $\tau$ to zero, we will have non-zero values in other indices&#x2014;a result that we want to avoid according to the problem setup. We will use the <em>straight-through</em> GS trick to create a true one-hot vector. Let $y$ be the output of the GS and let $y_{\mathrm{oh}}$ be the one-hot construction of the argmax of $y$. Then the straight-through GS simply does the following:</p><p>$$ y_{\mathrm{st}} = \mathrm{detach}(y_{\mathrm{oh}} - y) + y,$$</p><p>where $\mathrm{detach}(\cdot)$ detaches the argument from the computation graph keeping track of the gradients for backpropagation. Adding a number is differentiable and the resulting number, $y_{\mathrm{st}}$ has the same values as $y_{\mathrm{oh}}$ but is still on the computation graph (with the gradients of $y$).</p><p>Implementing the above in PyTorch is simple and the code is below where the <code>gumbel_softmax</code> function takes in the hidden representation (e.g., $h$) and outputs the straight-through GS result.</p><pre><code class="language-python">def sample_gumbel(logits, eps=1e-8):
    U = torch.rand_like(logits)
    return -torch.log(-torch.log(U + eps) + eps)

def sample_gumbel_softmax(logits, temperature):
    y = logits + sample_gumbel(logits)
    return F.softmax(y / temperature, dim=-1)

def gumbel_softmax(logits, temperature=0.67):
    y = sample_gumbel_softmax(logits, temperature)
    shape = y.size()
    _, ind = y.max(dim=-1)
    y_hard = torch.zeros_like(y).view(-1, shape[-1])
    y_hard.scatter_(1, ind.view(-1, 1), 1)
    y_hard = y_hard.view(*shape)
    return (y_hard - y).detach() + y
</code></pre><h2 id="learning-to-address-memory">Learning to address memory</h2><p>I&#x2019;ll show a very basic example of a network that can learn to address a memory location using the Gumbel-softmax. (A Jupyter notebook with the full implementation can be found <a href="https://nbviewer.org/gist/jcreinhold/c04d2dd682e1f0f9af6ff63839e49438?ref=jcreinhold.com">here</a>.)</p><p>We&#x2019;ll have a feedforward neural network fit a (noisy) quadratic function using a memory bank of 5 values uniformly spaced in the interval $[0, 1]$. If the network learns to pick the correct values of the memory bank, we should see the resulting estimator get close to the best piecewise constant estimate of the true quadratic function, where the constants are those values in the memory bank.</p><p>The training data&#x2014;shown in Fig. 2&#x2014;will consist of values between -1 and 1, representing the independent variable $x$, with corresponding dependent variables $y=x^2+\varepsilon$ where $\varepsilon \stackrel{iid}{\sim} \mathcal N(0,\sigma^2)$.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/training-data.png" class="kg-image" alt="Learning to read from memory with a neural network" loading="lazy" width="372" height="264"><figcaption><strong>Fig. 2</strong>: Training data for the experiment</figcaption></figure><p>The network will consist of a few fully-connected layers using batch norm and ReLU activations (but no ReLU or batch norm on the final layer).</p><p>The output of these several layers will be fed into the <code>gumbel_softmax</code> function, as described in the previous section, and the resulting one-hot vector will be used to address the memory bank by simply matrix multiplying (or taking the inner product, if you prefer) the one-hot vector with the memory bank. An example implementation of external memory for our simple setup is shown in PyTorch below, where the the instantiation argument is a tensor $\mathrm{memory} \in \mathbb{R}^m$ and $\mathrm{idx} \in \mathbb{R}^{n\times m}$ where $m$ is the number of memory elements and $n$ is the batch size.</p><pre><code class="language-python">class MemoryTensor(nn.Module):
    def __init__(self, memory:Tensor):
        super().__init__()
        memory.unsqueeze_(1)
        self.memory = memory
        
    def __getitem__(self, idx:Tensor):
        if self.training:
            idx = gumbel_softmax(idx)
            out = idx @ self.memory
        else:
            idx = torch.argmax(idx, dim=1)
            out = self.memory[idx]
        return out</code></pre><p>The <code>MemoryTensor</code> can then be indexed in the network with something like the following (where <code>net</code> is some neural network that outputs a tensor $\mathbb{R}^{n\times 3}$ in this illustration which is not specifically related to the experiment).</p><pre><code class="language-python">memory = MemoryTensor(torch.tensor([1.,2.,3.]))
idx = net(x)
y = memory[idx]</code></pre><p>Note that when training the <code>MemoryTensor</code> uses the GS, and when in production it uses argmax. The argmax is often more appropriate in production because it removes the random sampling; in some cases, the random sampling may be desired but not in this application.</p><p>Because only one location of the memory bank is 1 and all other entries are 0 (in both training and production), the result will consist only of the value from one memory location; that is, the network will have functionally addressed a memory location and read its value.</p><p>We train the network for several hundred iterations with the Adam optimizer using MSE as a loss function and get the result seen in Fig. 3 in the dashed blue &quot;Fit&quot; line. The optimal fit&#x2014;the best piecewise constant estimate of the quadratic&#x2014;using the values in memory is shown in the solid green line labeled &quot;Best Fit.&quot; The two are (qualitatively) nearly identical and we can say that this simple experiment was successful.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/fit.png" class="kg-image" alt="Learning to read from memory with a neural network" loading="lazy" width="483" height="466"><figcaption><strong>Fig. 3</strong>: Resulting output of the neural network trained to use the values stored in memory (Fit) compared to the optimal fit using those same memory values (Best Fit).</figcaption></figure><p>Caveat: This method will not know how to use additional memory locations. If you doubled the resolution of the memory bank, the network would fail to use that extra information. So this form of memory addressing is brittle. But, you could swap the memory out for another set of variables if the mapping was the same. For example, if the quadratic was changed to $2x^2$, then you could swap the memory with the values multiplied by two and expect the same performance. Something you can&#x2019;t do with a standard neural network.</p><h2 id="takeaways">Takeaways</h2><p>We showed a method to read from external memory inside a feedforward neural network, which is normally an operation that prevents the network from being trained. We used the Gumbel-softmax reparameterization to create true one-hot vectors that index one location in memory, and we walked through a toy experiment showing that the proposed method to read from memory does work as expected.</p><p>While the toy experiment was too simplistic to showcase the possibilities of this framework, you can, for example, easily extend this method train a network to read from a bank of images. Like the cat and dog example discussed in the <em>Integers and Indexing</em> section, it seems plausible that there are scenarios where you want to use some prototypical or ideal examples of a class of images inside the network to be used for further processing or improve the performance on some task. To be more concrete, training a network end-to-end with an external memory bank of images could be used to improve classification (e.g., find a nearest example) or improve multi-atlas segmentation by choosing the best image to register to another image.</p><p>Regardless of whether you believe that this method can improve performance in the concrete examples above, the research points to the fact that reading from external memory does facilitate certain types of computation, and this method is one simple way to do so&#x2014;potentially increasing the space of functions practically approximated by a feedforward neural network.</p>]]></content:encoded></item><item><title><![CDATA[Aleatory or epistemic? WTF are those?]]></title><description><![CDATA[Defining and quantifying aleatory and epistemic uncertainty in deep neural networks]]></description><link>https://www.jcreinhold.com/aleatory-or-epistemic/</link><guid isPermaLink="false">622e3dc7e647261e657d5911</guid><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Jacob Reinhold]]></dc:creator><pubDate>Fri, 31 Jul 2020 02:10:50 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1570303345338-e1f0eddf4946?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1570303345338-e1f0eddf4946?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" alt="Aleatory or epistemic? WTF are those?"><p><a href="https://www.deeplearningbook.org/?ref=jcreinhold.com">Deep neural networks</a> (DNNs) are easy-to-implement, versatile machine learning models that can achieve state-of-the-art performance in many domains (for example, <a href="https://arxiv.org/pdf/2004.08955v1.pdf?ref=jcreinhold.com">computer vision</a>, <a href="https://arxiv.org/abs/2005.14165?ref=jcreinhold.com">natural language processing</a>, <a href="https://arxiv.org/pdf/2005.09629v1.pdf?ref=jcreinhold.com">speech recognition</a>, <a href="https://dl.acm.org/doi/abs/10.1145/3285029?ref=jcreinhold.com">recommendation systems</a>). DNNs, however, are not perfect. You can read any <a href="https://www.economist.com/technology-quarterly/2020/06/11/an-understanding-of-ais-limitations-is-starting-to-sink-in?ref=jcreinhold.com">number</a> <a href="https://arxiv.org/abs/1604.00289?ref=jcreinhold.com">of</a> <a href="https://www.aaai.org/ojs/index.php/aimagazine/article/view/2756?ref=jcreinhold.com">articles</a>, <a href="https://medium.com/@mijordan3/artificial-intelligence-the-revolution-hasnt-happened-yet-5e1d5812e1e7?ref=jcreinhold.com">blog posts</a>, and <a href="http://rebooting.ai/?ref=jcreinhold.com">books</a> discussing the various problems with supervised deep learning. In this article we&apos;ll focus on a (relatively) narrow but major issue: the inability for a standard DNN to reliably show when it is uncertain about a prediction. For a <a href="https://www.youtube.com/watch?v=GiPe1OiKQuk&amp;ref=jcreinhold.com">Rumsfeldian</a> take on it: The inability of DNNs to know &quot;known unknowns.&quot;</p><p>As a simple example of this failure mode in DNNs, consider training a DNN for a binary classification task. You might reasonably presume that the softmax (or sigmoid) output of a DNN could be used to measure how certain or uncertain the DNN is in its prediction; you would expect that seeing a softmax output close to 0 or 1 would indicate certainty, and an output close to 0.5 would indicate uncertainty. In reality, <a href="https://arxiv.org/pdf/1706.04599.pdf?ref=jcreinhold.com">the softmax outputs are rarely close to 0.5</a> and are, more frequently than not, close to 0 or 1 regardless of whether the DNN is making a correct prediction. Unfortunately, this fact makes naive uncertainty estimates unreliable (for instance, <a href="https://en.wikipedia.org/wiki/Entropy_%28information_theory%29?ref=jcreinhold.com">entropy</a> over the softmax outputs).</p><p>To be fair, uncertainty estimates are not needed for every application of a DNN. If a social media company uses a DNN to detect faces in images so that its users can more easily tag their friends, and the DNN fails, then the failure of the method is nearly inconsequential. A user might be slightly inconvenienced, but in low-stakes environments like social media or advertising, uncertainty estimates aren&apos;t vital to creating value from a DNN.</p><p>In high-stakes environments, however, like self-driving cars, health care, or military applications, a measure of how uncertain the DNN is in its prediction could be vital. Uncertainty measurements can reduce the risk of deploying a model because they can alert a user to the fact that a scenario is either inherently difficult to do prediction in, or the scenario has not been seen by the model before.</p><p>In a self-driving car, it seems plausible that a DNN should be more uncertain about predictions at night (at least in the measurements coming from optical cameras) because of the lower signal-to-noise ratio. In health care, a DNN that diagnoses skin cancer should be more uncertain if it were shown a particularly blurry image, especially if the model had not seen such blurry examples in the training set. In a model to segment satellite imagery, a DNN should be more uncertain if an adversary changed how they disguise certain military installations. If the uncertainty inherent in these situations were relayed to the user, the information could be used to change the behavior of the system in a safer way.</p><p>In this article, we explore how to estimate two types of statistical uncertainty alongside a prediction in a DNN. We first discuss the definition of both types of uncertainty, and then we highlight one popular and easy-to-implement technique to estimate these types of uncertainty. Finally, we show and implement some examples for both classification and regression that makes use of these uncertainty estimates.</p><p>For those who are most interested in looking at code examples, here are two Jupyter Notebooks one with a <a href="https://nbviewer.jupyter.org/gist/jcreinhold/cddf290b1d3722b0c88bbc3c82df38a3?ref=jcreinhold.com">toy regression example</a> and the other with a <a href="https://nbviewer.jupyter.org/gist/jcreinhold/745385337944dbcc98b47578b5a769f9?ref=jcreinhold.com">toy classification example</a>. There are also PyTorch-based code snippets in the &quot;Examples and Applications&quot; section below.</p><h3 id="what-do-we-mean-by-uncertainty-">What do we mean by &apos;uncertainty&apos;?</h3><p>Uncertainty is <a href="https://dictionary.cambridge.org/dictionary/english/uncertainty?ref=jcreinhold.com">defined by the Cambridge Dictionary</a> as: &quot;a situation in which something is not known.&quot; There are several reasons why something may not be known, and&#x2014;taking a statistical perspective&#x200A;&#x2014;we will discuss two types of uncertainty called <em>aleatory</em> (sometimes referred to as <em>aleatoric</em>) and <em>epistemic</em> uncertainty.</p><p>Aleatory uncertainty relates to an objective or physical concept of uncertainty&#x2014;it is a type of uncertainty that is intrinsic to the data-generating process. Since aleatory uncertainty has to do with an intrinsic quality of the data, we presume it cannot be decreased by collecting more data; that is, it is <em>irreducible</em>.</p><p>Aleatory uncertainty can be explained best with a simple example: Suppose we have a coin which has some positive probability of being heads or tails. Then, even if the coin is biased, we cannot predict&#x2014;with certainty&#x2014;what the next toss will be, regardless of how many observations we make. (For instance, if the coin is biased such that heads turn up with probability 0.9, we might reasonably guess that heads will show up in the next toss, but we cannot be certain that it will happen.)</p><p>Epistemic uncertainty relates to a subjective or personal concept of uncertainty&#x2014;it is a type of uncertainty due to knowledge or ignorance of the true data-generating process. Since this type of uncertainty has to do with knowledge, we presume that it can be decreased <em>(</em>for example, when more data has been collected and used for training); that is, it is <em>reducible.</em></p><p>Epistemic uncertainty can be explained with a regression example. Suppose we are fitting a linear regression model and we have independent variables $x$ between -1 and 1, and corresponding dependent variables $y$ for all $x$. Suppose we chose a linear model because we believe that when $x$ is between -1 and 1, the model is linear. We don&apos;t, however, know what happens when a test sample $x&apos;$ is far outside this range; say at $x&apos;$ = 100. So, in this scenario, there is uncertainty about the model specification (for example, the true function may be quadratic) and there is uncertainty because the model hasn&apos;t seen data in the range of the test sample. These uncertainties can be bundled into uncertainty regarding the knowledge of true data-generating distribution, which is epistemic uncertainty.</p><p>The terms <em>aleatory</em> and <em>epistemic</em>, with regards to probability and uncertainty, seem to have been brought into the modern lexicon by Ian Hacking in his book &quot;<a href="https://www.goodreads.com/book/show/1446901.The_Emergence_of_Probability?ref=jcreinhold.com">The Emergence of Probability</a>,&quot; which discusses the history of probability from 1600&#x2013;1750. The terms are not clear for the uninitiated reader, but their definitions are related to the deepest question in the foundations of probability and statistics: <a href="https://plato.stanford.edu/entries/probability-interpret/?ref=jcreinhold.com">What does probability mean</a>? If you are familiar with terms <em>frequentist</em> and <em>Bayesian</em>, then you will see the respective relationship between aleatory (objective) and epistemic (subjective) uncertainty. I&apos;m not about to solve this philosophical issue in this blog post, but know that the definitions of aleatory and epistemic uncertainty are nuanced, and what falls into which category is debatable. For a more comprehensive (but still applied) review of these terms, take a look at the article: &quot;<a href="https://www.sciencedirect.com/science/article/pii/S0167473008000556?casa_token=yWEYH7OP70sAAAAA%3AFnmF4crmHui1-NCqSx99tkamdl-ITjwAQ7TSlxlHFTpoYp_9xPtAVqtw0CH4ByYr7-gRC71c5Q&amp;ref=jcreinhold.com">Aleatory or Epistemic? Does it matter?</a>&quot;</p><p>Why is it important to distinguish between aleatory and epistemic uncertainty? Suppose we are developing a self-driving car, and we take a prototype that was trained on normal roads and have it drive through the <a href="https://en.wikipedia.org/wiki/Autodromo_Nazionale_di_Monza?ref=jcreinhold.com">Monza racing track</a>, which has extremely banked turns.</p><figure class="kg-card kg-image-card kg-width-wide kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/monza.jpeg" class="kg-image" alt="Aleatory or epistemic? WTF are those?" loading="lazy" width="1024" height="686" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/monza.jpeg 600w, https://www.jcreinhold.com/content/images/size/w1000/2023/04/monza.jpeg 1000w, https://www.jcreinhold.com/content/images/2023/04/monza.jpeg 1024w"><figcaption><strong>Fig. 1</strong>: Banked turns on the Monza Racing Track</figcaption></figure><p>Since the car hasn&apos;t seen the situation before, we would expect the image segmentation DNN in the self-driving car, for example, to be uncertain because it has never seen the sky nearly to the left of ground. In this case, the uncertainty would be classified as epistemic because the DNN doesn&apos;t have knowledge of roads like this.</p><p>Suppose instead that we take the same self-driving car and take it for a drive on a rainy day; assume that the DNN has been trained on lots of rainy-day conditions. In this situation, there is more uncertainty about objects on the road simply due to lower visibility. In this case, the uncertainty would be classified as aleatory because there is inherently more randomness in the data.</p><p>These two situations should be dealt with differently. In the race track, the uncertainty could tell the developers that they need to gather a particular type of training data to make the model more robust, or the uncertainty could tell the car could try to safely maneuver to a location where it can hand-off control to the driver. In the rainy-day situation, the uncertainty could alert the system to simply slow down or enable certain safety features.</p><h3 id="estimating-uncertainty-in-dnns">Estimating uncertainty in DNNs</h3><p>There has been a <a href="https://arxiv.org/abs/1505.05424?ref=jcreinhold.com">cornucopia</a> of <a href="http://proceedings.mlr.press/v37/hernandez-lobatoc15.pdf?ref=jcreinhold.com">proposed</a> <a href="http://papers.nips.cc/paper/4329-practical-variational-inference-for-neural-networks.pdf?ref=jcreinhold.com">methods</a> to estimate uncertainty in DNNS in recent years. Generally, uncertainty estimation is formulated in the context of Bayesian statistics. In a standard DNN for classification, we are implicitly training a <a href="https://en.wikipedia.org/wiki/Discriminative_model?ref=jcreinhold.com">discriminative</a> model where we obtain maximum-likelihood estimates of the neural network weights (depending on the loss function chosen to train the network). This point-estimate of the network weights is not amenable to understanding what the model knows and does not know. If we instead find a distribution over the weights, as opposed to the point-estimate, we can sample network weights with which we can compute corresponding outputs.</p><p>Intuitively, this sampling of network weights is like creating an ensemble of networks to do the task: We sample a set of &quot;experts&quot; to make a prediction. If the experts are inconsistent, there is high epistemic uncertainty. If the experts think it is too difficult to make an accurate prediction, there is high aleatory uncertainty.</p><p>In this article, we&apos;ll take a look at a popular and easy-to-implement method to estimate uncertainty in DNNs by <a href="https://arxiv.org/abs/1506.02142?ref=jcreinhold.com">Yarin Gal and Zoubin Ghahramani</a>. They showed that <a href="http://jmlr.org/papers/v15/srivastava14a.html?ref=jcreinhold.com">dropout</a> can be used to learn an approximate distribution over the weights of a DNN (as previously discussed). Then, during prediction, dropout is used to sample weights from this fitted approximate distribution&#x2014;akin to creating the ensemble of experts.</p><p>Epistemic uncertainty is estimated by taking the sample variance of the predictions from the sampled weights. The intuition behind relating sample variance to epistemic uncertainty is that the sample variance will be low when the model predicts nearly identical outputs, and it will be high when the model makes inconsistent predictions; this is akin to when the set of experts consistently makes a prediction and when they do not, respectively.</p><p>Simultaneously, aleatory uncertainty is estimated by modifying a DNN to have a second output, as well as using a modified loss function. Aleatory uncertainty will correspond to the <a href="https://arxiv.org/abs/1703.04977?ref=jcreinhold.com">estimated variance</a> <a href="https://ieeexplore.ieee.org/abstract/document/374138?ref=jcreinhold.com">of the output</a>. This predicted variance has to do with an intrinsic quantity of the data, which is why it is related to aleatory uncertainty; this is akin to when the set of experts judges the situation too difficult to make a prediction.</p><p>Altogether the final network structure is something like what is shown in Fig. 2. There is an input <strong><em>x</em></strong> which is fed to a DNN with dropout after every layer (dropout after every layer is what is originally specified, but&#x2014;in practice&#x2014;dropout after every layer often makes training too difficult). The output of this DNN is an estimated target $\hat{\mathbf{y}}$ and an estimated variance or scale parameter $\hat{\sigma}^2$.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/bnn.png" class="kg-image" alt="Aleatory or epistemic? WTF are those?" loading="lazy" width="1620" height="896" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/bnn.png 600w, https://www.jcreinhold.com/content/images/size/w1000/2023/04/bnn.png 1000w, https://www.jcreinhold.com/content/images/size/w1600/2023/04/bnn.png 1600w, https://www.jcreinhold.com/content/images/2023/04/bnn.png 1620w" sizes="(min-width: 720px) 720px"><figcaption><strong>Fig. 2</strong>: Example of DNN architecture with capability to estimate aleatory and epistemic uncertainty</figcaption></figure><p>This DNN is trained with a loss function like:</p><!--kg-card-begin: markdown--><p>$$  \mathcal{L}(\mathbf{y}, (\hat{\mathbf{y}},\hat{\sigma}^2)) = \frac{1}{M} \sum_{i=1}^M \frac{1}{2} \hat{{\sigma}}_i^{-2} \lVert \mathbf{y}_i - \mathbf{\hat{y}}_i \rVert_2^2 + \frac{1}{2} \log \hat{\sigma}_i^2 $$</p>
<!--kg-card-end: markdown--><p>or</p><!--kg-card-begin: markdown--><p>$$ \mathcal{L}(\mathbf{y}, (\hat{\mathbf{y}}, \hat{\mathbf{b}})) =  \frac{1}{M} \sum_{i=1}^M \hat{\mathbf{b}}_i^{-1} \lVert \mathbf{y}_i - \hat{\mathbf{y}}_i \rVert_1 + \log \hat{\mathbf{b}}_i $$</p>
<!--kg-card-end: markdown--><p>If the network is being trained for a regression task. The first loss function shown above is an MSE variant with uncertainty, whereas the second is an L1 variant. These are derived from assuming a Gaussian and <a href="https://en.wikipedia.org/wiki/Laplace_distribution?ref=jcreinhold.com">Laplace</a> distribution for the <a href="https://en.wikipedia.org/wiki/Posterior_probability?ref=jcreinhold.com#Definition">likelihood</a>, respectively, where each component is independent and the variance (or scale parameter) is estimated and fitted by the network.</p><p>As mentioned above, these loss functions have mathematical derivations, but we can intuit why this variance parameter captures a type of uncertainty: The variance parameter provides a trade-off between the variance and the MSE or L1 loss term. If the DNN can easily estimate the true value of the target (that is, get <strong><em>&#x177;</em></strong> close to the true <strong><em>y</em></strong>), then the DNN should estimate a low variance term on that so as to minimize the loss. If, however, the DNN cannot estimate the true value of the target (for example, there is low signal-to-noise ratio), then the network can minimize the loss by estimating a high variance. This will reduce the MSE or L1 loss term because that term will be divided by the variance; however, the network should not always do this because of the log variance term which penalizes high variance estimates.</p><p>If the network is being trained for a classification (or segmentation) task, the loss would look something like this two-part loss function:</p><!--kg-card-begin: markdown--><p>$$ \hat{\mathbf{x}}_t = \hat{\mathbf{y}} + \varepsilon_t \qquad \varepsilon_t \sim \mathcal{N}(\mathbf{0},\mathrm{diag}(\hat{\sigma}^2)) $$</p>
<p>$$ \mathcal{L}(\mathbf{x}, \hat{\mathbf{x}}) = \frac{1}{T} \sum_{t=1}^T \mathrm{Cross\ entropy}(\mathbf{x}, \hat{\mathbf{x}}_t) $$</p>
<!--kg-card-end: markdown--><p>The intuition here with this loss function is: When the DNN can easily estimate the right class of a component, the value $\hat{\mathbf{y}}$ will be high for that class and the DNN should estimate a low variance so as to minimize the added noise (so that all samples will be concentrated around the correct class). If, however, the DNN cannot easily estimate the class of the component, the $\hat{\mathbf{y}}$ value should be low and adding noise can increase the guess, by chance, for the correct class which can overall minimize the loss function. (See <a href="https://alexgkendall.com/media/papers/alex_kendall_phd_thesis_compressed.pdf?ref=jcreinhold.com">Pg. 41 of Alex Kendall&apos;s thesis</a> for more discussion on this loss function.)</p><p>Finally, in testing, the network is sampled <em>T</em> times to create <em>T</em> estimated targets and <em>T</em> estimated variance outputs. These <em>T</em> outputs are then combined in various ways to make the final estimated target and uncertainty estimates as shown in Fig. 3.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/uncertainty.png" class="kg-image" alt="Aleatory or epistemic? WTF are those?" loading="lazy" width="1454" height="966" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/uncertainty.png 600w, https://www.jcreinhold.com/content/images/size/w1000/2023/04/uncertainty.png 1000w, https://www.jcreinhold.com/content/images/2023/04/uncertainty.png 1454w" sizes="(min-width: 720px) 720px"><figcaption><strong>Fig. 3</strong>: DNN output to final estimated target and uncertainty estimates</figcaption></figure><p>Mathematically, the epistemic and aleatory uncertainty are (for the MSE regression variant):</p><!--kg-card-begin: markdown--><p>$$ \mathrm{Aleatory\ uncertainty} = \frac{1}{T} \sum_{t=1}^T \hat{\sigma}^2_t$$</p>
<p>$$ \mathrm{Epistemic\ uncertainty} = \frac{1}{T} \sum_{t=1}^T (\mathbf{\hat{y}}_t - \bar{\mathbf{y}})^2 $$</p>
<!--kg-card-end: markdown--><p>There are various interpretations of epistemic uncertainty for the classification case: <a href="https://arxiv.org/abs/1808.01200?ref=jcreinhold.com">entropy</a>, <a href="https://pubmed.ncbi.nlm.nih.gov/29259224/?ref=jcreinhold.com">sample variance</a>, <a href="https://arxiv.org/abs/1703.02910?ref=jcreinhold.com">mutual information</a>. Each has been shown to be useful in its own right, and the choice of what type to choose will be application dependent.</p><h3 id="examples-and-applications">Examples and applications</h3><p>To make the theory more concrete, we&apos;ll go through two toy examples for estimating uncertainty with DNNs in a regression and classification task with PyTorch. The code below are excerpts from full implementations which are available in Jupyter notebooks (mentioned at the beginning of the next two subsections). Finally, we&apos;ll discuss calculating uncertainty in a real-world data example with medical images.</p><h4 id="regression-example">Regression example</h4><p>In the <a href="https://nbviewer.jupyter.org/gist/jcreinhold/cddf290b1d3722b0c88bbc3c82df38a3?ref=jcreinhold.com">regression notebook</a>, we fit a very simple neural network&#x2014;consisting of two fully-connected layers with dropout on the hidden layer&#x2014;to one-dimensional input and output data with the MSE variant of the uncertainty loss (implemented below).</p><pre><code class="language-python">class ExtendedMSELoss(nn.Module):
    &quot;&quot;&quot; modified MSE loss for variance fitting &quot;&quot;&quot;
    def forward(self, out:torch.Tensor, y:torch.Tensor) -&gt; torch.Tensor:
        yhat, s = out
        loss = torch.mean(0.5 * (torch.exp(-s) * F.mse_loss(yhat, y, reduction=&apos;none&apos;) + s))
        return loss</code></pre><p>Note that instead of fitting the variance term directly, we fit the <em>log of the variance</em> term for numerical stability.</p><p>In the regression scenario, we could also use the L1 variant of the uncertainty loss which is in the notebook and implemented below.</p><pre><code class="language-python">class ExtendedL1Loss(nn.Module):
    &quot;&quot;&quot; modified L1 loss for scale param. fitting &quot;&quot;&quot;
    def forward(self, out:torch.Tensor, y:torch.Tensor) -&gt; torch.Tensor:
        yhat, s = out
        loss = torch.mean((torch.exp(-s) * F.l1_loss(yhat, y, reduction=&apos;none&apos;)) + s)
        return loss</code></pre><p>Sometimes using L1 loss instead of MSE loss results in <a href="https://ieeexplore.ieee.org/abstract/document/7797130?ref=jcreinhold.com">better performance</a> for regression tasks, although this is application dependent.</p><p>The aleatory and epistemic uncertainty estimates in this scenario are then computed as in the implementation below (see the notebook for more context).</p><pre><code class="language-python">def regression_uncertainty(yhat:torch.Tensor, s:torch.Tensor, mse:bool=True) -&gt; Tuple[torch.Tensor, torch.Tensor]:
    &quot;&quot;&quot; calculate epistemic and aleatory uncertainty quantities based on whether MSE or L1 loss used &quot;&quot;&quot;
    # variance over samples (dim=0), mean over channels (dim=1, after reduction by variance calculation)
    epistemic = torch.mean(yhat.var(dim=0, unbiased=True), dim=1, keepdim=True)
    aleatory = torch.mean(torch.exp(s), dim=0) if mse else torch.mean(2*torch.exp(s)**2, dim=0)
    return epistemic, aleatory</code></pre><p>In Fig. 4, we visualize the fit function and the uncertainty results. In the plot to the far right, we show the thresholded epistemic uncertainty which demonstrates the capabilities of uncertainty estimates to detect out-of-distribution data (at least in this toy scenario).</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/uncertainty-simple-function.png" class="kg-image" alt="Aleatory or epistemic? WTF are those?" loading="lazy" width="938" height="482" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/uncertainty-simple-function.png 600w, https://www.jcreinhold.com/content/images/2023/04/uncertainty-simple-function.png 938w" sizes="(min-width: 720px) 720px"><figcaption><strong>Fig. 4</strong>: Various types of uncertainty for a regression example. Original training data in orange. The two plots on the left show the function fit by the neural network in blue with aleatory and epistemic uncertainty in the first and second plot, respectively. The plot on the far right shows a thresholded epistemic uncertainty. See the Jupyter Notebook for the full implementation.</figcaption></figure><h4 id="classification-example">Classification example</h4><p>In the <a href="https://nbviewer.jupyter.org/gist/jcreinhold/745385337944dbcc98b47578b5a769f9?ref=jcreinhold.com">classification notebook</a>, we, again, fit a neural network composed of two fully-connected layers with dropout on the hidden layer. In this case, we are trying to do binary classification. Consequently, the loss function is as implemented below.</p><pre><code class="language-python">class ExtendedBCELoss(nn.Module):
    &quot;&quot;&quot; modified BCE loss for variance fitting &quot;&quot;&quot;
    def forward(self, out:torch.Tensor, y:torch.Tensor, n_samp:int=10) -&gt; torch.Tensor:
        logit, sigma = out
        dist = torch.distributions.Normal(logit, torch.exp(sigma))
        mc_logs = dist.rsample((n_samp,))
        loss = 0.
        for mc_log in mc_logs:
            loss += F.binary_cross_entropy_with_logits(mc_log, y)
        loss /= n_samp
        return loss</code></pre><p>There are numerous uncertainty estimates we could compute in this scenario. In the below implementation, we calculate epistemic, entropy, and aleatory uncertainty. Entropy could reasonably be argued to belong to one of aleatory and epistemic uncertainty, but below it is separated out so that aleatory and epistemic uncertainty are calculated as previously described.</p><pre><code class="language-python">def classification_uncertainty(logits:torch.Tensor, sigmas:torch.Tensor, eps:float=1e-6) -&gt; Tuple[torch.Tensor, torch.Tensor]:
    &quot;&quot;&quot; calculate epistemic, entropy, and aleatory uncertainty quantities &quot;&quot;&quot;
    probits = torch.sigmoid(logits)
    epistemic = probits.var(dim=0, unbiased=True)
    probit = probits.mean(dim=0)
    entropy = -1 * (probit * (probit + eps).log2() + ((1 - probit) * (1 - probit + eps).log2()))
    aleatory = torch.exp(sigmas).mean(dim=0)
    return epistemic, entropy, aleatory</code></pre><p>In Fig. 5, we visualize the resulting epistemic and aleatory uncertainty, as well as entropy, over the training data. As we can see the training data classes overlaps near zero, and the uncertainty measures peak there. In this toy example, all three measures of uncertainty are highly correlated. Discussion as to why is provided in the notebook for the interested reader.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/uncertainty-histograms-1.png" class="kg-image" alt="Aleatory or epistemic? WTF are those?" loading="lazy" width="1144" height="424" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/uncertainty-histograms-1.png 600w, https://www.jcreinhold.com/content/images/size/w1000/2023/04/uncertainty-histograms-1.png 1000w, https://www.jcreinhold.com/content/images/2023/04/uncertainty-histograms-1.png 1144w" sizes="(min-width: 720px) 720px"><figcaption><strong>Fig. 5</strong>: Various measures of uncertainty for a binary classification example. See the <a href="https://nbviewer.jupyter.org/gist/jcreinhold/745385337944dbcc98b47578b5a769f9?ref=jcreinhold.com">Jupyter Notebook</a> for the full implementation.</figcaption></figure><h4 id="medical-image-example">Medical image example</h4><p>In this last example, I&apos;ll show some results and applications of uncertainty in a real-world example published as a conference paper (<a href="https://arxiv.org/abs/2002.04626?ref=jcreinhold.com">pre-print here</a>). The task explored is an image-to-image translation task, akin to the notable <a href="https://phillipi.github.io/pix2pix/?ref=jcreinhold.com">pix2pix</a> example, but with medical images. In this case, we wanted to make a computed tomography (CT) image of the brain look like the corresponding magnetic resonance (MR) image of the brain. This is a regression loss and we used the MSE variant of the uncertainty loss to train a <a href="https://arxiv.org/abs/1505.04597?ref=jcreinhold.com">U-Net</a> modified to have <a href="https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Tompson_Efficient_Object_Localization_2015_CVPR_paper.pdf?ref=jcreinhold.com">spatial dropout</a> (see <a href="https://towardsdatascience.com/dropout-on-convolutional-layers-is-weird-5c6ab14f19b2?ref=jcreinhold.com">here</a> for a discussion as to why <em>spatial</em> dropout) after every layer, and to output two images instead of only one; one output is the estimated MR image and the other is the pixel-wise variance.</p><p>Example inputs and outputs are shown in Fig. 6. The CT image on the far left has an anomaly in the left hemisphere of the occipital lobe (lower-left of the brain in the image; it is more easily visualized in the corresponding MR image to the right). The DNN was only trained on healthy images, so the DNN should be ignorant of such anomalous data, and it should reflect this&#x2014;according to the theory of epistemic uncertainty as previously discussed&#x2014;by having high sample variance (that is, high epistemic uncertainty) in that region.</p><figure class="kg-card kg-image-card kg-width-wide kg-card-hascaption"><img src="https://www.jcreinhold.com/content/images/2023/04/mr-images-uncertainty.png" class="kg-image" alt="Aleatory or epistemic? WTF are those?" loading="lazy" width="1725" height="411" srcset="https://www.jcreinhold.com/content/images/size/w600/2023/04/mr-images-uncertainty.png 600w, https://www.jcreinhold.com/content/images/size/w1000/2023/04/mr-images-uncertainty.png 1000w, https://www.jcreinhold.com/content/images/size/w1600/2023/04/mr-images-uncertainty.png 1600w, https://www.jcreinhold.com/content/images/2023/04/mr-images-uncertainty.png 1725w" sizes="(min-width: 1200px) 1200px"><figcaption><strong>Fig. 6</strong>: From left to right &#x2013; CT image is input to the network, MR image is target in training (shown here because anomaly visible in the occipital lobe in the left hemisphere), pixel-wise epistemic uncertainty, pixel-wise aleatory uncertainty, and the pixel-wise ratio of epistemic over aleatory is shown under the title &quot;Scibilic&quot; which clearly highlights the anomalous region.</figcaption></figure><p>When this image was input to the network, we calculated the epistemic and aleatory uncertainty. The anomaly is clearly highlighted in the epistemic uncertainty, but there are many other regions which are also predicted to have high epistemic uncertainty. If we take the pixel-wise ratio of epistemic and aleatory uncertainty, we get the image shown on the far-right, labeled &quot;Scibilic&quot; (which is discussed more in the pre-print). This image is easily thresholded to predict the anomaly (the out-of-distribution region of the image).</p><p>This method of anomaly detection is by no means foolproof. It is quite fickle actually, but it shows a way to apply this type of uncertainty estimation for real-world data.</p><h3 id="takeaways">Takeaways</h3><p>Uncertainty estimates in machine learning have the potential to reduce the risk of deploying models in high-stakes scenarios. Aleatory and epistemic uncertainty estimates can show the user or developer different information about the performance of a DNN and can be used to modify the system for better safety. We discussed and implemented one approach to uncertainty estimation with dropout. The approach is not perfect, dropout-based uncertainty provides a way to get some&#x2014;often reasonable&#x2014;measure of uncertainty. Whether the measure is trustworthy enough to be used in deployment is another matter. The question practitioners should ask themselves when implementing this method is whether the resulting model with uncertainty estimates is more useful&#x2014;for example, safer&#x2014;than a model without uncertainty estimates.</p>]]></content:encoded></item><item><title><![CDATA[New website and blog]]></title><description><![CDATA[<p> I&apos;ve been getting into the habit of regularly publishing my writing online. Writing for a broader audience than academic papers has been a great exercise in clarifying my thinking on a subject and improving my writing in general. </p><p>Previously I had a personal website using GitHub pages (along</p>]]></description><link>https://www.jcreinhold.com/new-webiste-and-blog/</link><guid isPermaLink="false">622e3dc7e647261e657d5912</guid><dc:creator><![CDATA[Jacob Reinhold]]></dc:creator><pubDate>Wed, 29 Jul 2020 16:38:00 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1557079310-f6e639f0d069?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1557079310-f6e639f0d069?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" alt="New website and blog"><p> I&apos;ve been getting into the habit of regularly publishing my writing online. Writing for a broader audience than academic papers has been a great exercise in clarifying my thinking on a subject and improving my writing in general. </p><p>Previously I had a personal website using GitHub pages (along with an attached Jekyll blog which served as a repository for URLs and notes), and I separately posted longer-form articles on <a href="https://medium.com/@jcreinhold?ref=jcreinhold.com">Medium</a>. (I also occasionally write longer-form articles for <a href="https://innolitics.com/articles/?ref=jcreinhold.com">Innolitics</a>.) My personal website was bare-bones to the point that it wasn&apos;t responsive to screen sizes. And I wasn&apos;t happy with the experience of writing technical content on Medium; Medium doesn&apos;t support <a href="https://www.mathjax.org/?ref=jcreinhold.com">MathJax</a> or syntax highlighting which makes writing about machine learning arduous.</p><p>Those problems plus some inspiration from <a href="https://zalberico.com/essay/2020/07/14/the-serfs-of-facebook.html?ref=jcreinhold.com">other blog posts</a> motivated me to create a new website and blog using the <a href="https://ghost.org/?ref=jcreinhold.com">Ghost</a> platform. This website is the result, and I think it is nicer looking than my old personal website, responsive, and supports technical writing much better than Medium.</p><p>As far as the content I plan to post, I&apos;ll continue to focus on topics in medical image analysis and machine learning (which is the focus of my PhD). You can follow me here using an RSS feed or on Twitter at <a href="https://twitter.com/JacobCReinhold?ref=jcreinhold.com">@JacobCReinhold</a>.</p>]]></content:encoded></item></channel></rss>