<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amy Pajak on Medium]]></title>
        <description><![CDATA[Stories by Amy Pajak on Medium]]></description>
        <link>https://medium.com/@pajakamy?source=rss-b1190baed26------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*V1rVbYn_fvaCwavnj9ImYQ.jpeg</url>
            <title>Stories by Amy Pajak on Medium</title>
            <link>https://medium.com/@pajakamy?source=rss-b1190baed26------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 08 Apr 2026 16:27:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pajakamy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[ALiBi: Attention with Linear Biases]]></title>
            <link>https://medium.com/@pajakamy/alibi-attention-with-linear-biases-942abe042e9f?source=rss-b1190baed26------2</link>
            <guid isPermaLink="false">https://medium.com/p/942abe042e9f</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[positional-encoding]]></category>
            <dc:creator><![CDATA[Amy Pajak]]></dc:creator>
            <pubDate>Mon, 03 Jul 2023 07:02:13 GMT</pubDate>
            <atom:updated>2023-07-08T04:04:43.407Z</atom:updated>
            <content:encoded><![CDATA[<h4>Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation</h4><p>This <a href="https://arxiv.org/pdf/2108.12409.pdf">paper</a> was published at ICLR 2022 by <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Press%2C+O">Ofir Press</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Smith%2C+N+A">Noah A. Smith</a>, and <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Lewis%2C+M">Mike Lewis</a> from University of Washington, Facebook AI Research and Allen Institute for AI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*btrWDJG9FOLn-d2o4xwYIA.png" /></figure><h4>Summary</h4><p>At a high level this paper discusses how the authors replace positional encodings of transformers by using a new and very simple system which enables transformers to extrapolate to much longer sequences at inference time than what they have been trained on. ALiBi allows training on smaller sequences where performance will not suffer/degrade even if the inference sequence length is much longer than the training sequence length. This holds from 2x longer to 8x longer to even more.</p><p>The extent of ALiBis performance, when compared to techniques using positional embeddings, depends on the size of the dataset a model is trained on — where models trained on smaller datasets using ALiBi show comparatively greater extrapolation abilities.</p><p>It’s a simple technique to use. The code and implementation steps are available in <a href="https://github.com/ofirpress/attention_with_linear_biases">github</a>.</p><p><strong>TLDR;</strong> This technique is useful to implement on transformer based models to infer on longer sequences than what have been trained on i.e., extrapolate.</p><h4>Background</h4><p>A quick bit of background about this technique — its already seen some great adoption in industry. Here’s a list of popular models that use ALiBi:</p><ul><li>MPT-7B</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eBAtSl_gFvo4xdjBtW7xvw.png" /><figcaption>From mosaicMLs MPT-7B <a href="https://www.mosaicml.com/blog/mpt-7b">blog</a></figcaption></figure><ul><li>MPT-30B</li><li>BLOOM</li><li>BloombergGPT</li></ul><p>MPT is part of <a href="https://www.mosaicml.com/">MosaicMLs</a> foundation series. MPT-30B was released in June 2023 and a few days later MosaicML announced a US$1.3 billion <a href="https://www.databricks.com/company/newsroom/press-releases/databricks-signs-definitive-agreement-acquire-mosaicml-leading-generative-ai-platform">acquisition</a> by Databricks - so ALiBi is definitely seeing some success.</p><h4>Introduction</h4><p>Problem this paper works on: <strong>Positional encodings</strong>.</p><p>The transformer architecture was released in the 2017 <a href="https://arxiv.org/pdf/1706.03762.pdf">paper</a> ‘Attention Is All You Need’ by Vaswani et al. In this the authors dealt with the question of positional encodings — they had to because technically the transformer isn’t a sequence model per-se, it processes all elements in the input simultaneously which means it treats the input more like a set than a sequence. So it’s actually a set model that has been adapted to handle sequence data.</p><p>The ALiBi paper deals exclusively with the text generation task, it may also be useful in other areas, but here the goal is to predict the next token from a sequence of tokens. So with 5 tokens you want to predict the 6th, then the 7th and so on…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KFShLeMJsMkl51tvBVWv7Q.gif" /><figcaption>Next word prediction. <a href="https://developers.reinfer.io/blog/2022/05/04/prompting">Source</a></figcaption></figure><p>Since a transformer essentially transforms a sequence of inputs into an equally sized sequence of outputs in every layer — the transformer itself doesn’t really know where a particular item is relative to its position in the sequence.</p><p>Recognising this, the original paper came up with sinusoidal position embeddings to represent multiple dimensions of positional encoding using a vector.</p><h4>Sinusoidal Embedding</h4><p>The sinusoidal embedding uses sine and cosine waves of different frequencies across multiple dimensions (y-axis) which we index all the tokens against (x-axis) to return a unique vector which we can assign to represent an inputs position. An advantage of this, hypothesized in the original paper, was that the transformer can use these vectors to learn and reason about the relative distances between tokens.</p><p>We can choose a dimension and reason that if two tokens have a close value then they are somewhat close together in the input sequence. And if we look at the rest of the dimensions in their vectors and they’re very similar-valued then they’re likely right next to each other. This allows us to inject some information about tokens absolute and relative positions to each other in the sequence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/0*uUw94JqPcUYkvjUD.png" /><figcaption>Visualizing the Sinusoidal Positional Matrix. <a href="https://machinelearningmastery.com/a-gentle-introduction-to-positional-encoding-in-transformer-models-part-1/">Source</a></figcaption></figure><p>(The sine and cosine functions were chosen in tandem because they have linear properties the model can easily learn to attend to).</p><h4>Transformer Architecture</h4><p>Positional encodings are usually added to the input embeddings (which then become the Query, Key, and Value vectors) before they are fed into the self-attention mechanism. In this way, the position of each word or token in the sequence can affect the attention scores and the subsequent processing by the transformer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l5_zNrTvt4BIf7Bc-qdR0g.png" /><figcaption>Self Attention. <a href="https://stats.stackexchange.com/questions/421935/what-exactly-are-keys-queries-and-values-in-attention-mechanisms">Source</a></figcaption></figure><p>The Q, K, and V vectors are crucial components of the self-attention mechanism. Here’s how they work:</p><ol><li><strong>Query</strong>: A representation of a word that we’re trying to understand in a certain context. The Transformer generates a Query vector for each word in the input. The purpose of the Query vector is to score how well each word (Key) in the context correlates with the word that the Query vector represents.</li><li><strong>Key</strong>: A representation of a word in a given context that we use to score against the Query. The Transformer generates a Key vector for each word in the input. The dot product of a Query and a Key gives us a score, indicating the compatibility or relevance of that particular Key to the Query.</li><li><strong>Value</strong>: A representation of the actual content of a word. The Transformer generates a Value vector for each word in the input. These Value vectors are used in the final weighted sum of the attention output, where the weights are determined by the scores calculated using Query and Key vectors.</li></ol><p>In practice, the Transformer takes an input sequence, creates Q, K, and V vectors for each word in the sequence through linear transformations (which are learnable parameters), and then uses these vectors to compute a new representation for each word that takes the entire context into account. The self-attention mechanism takes into account all words in the sequence, assigning different weights based on their relevance, as determined by the scores calculated from the Query and Key vectors. These individual vectors for each token are stacked together to form matrices when considering the entire input sequence.</p><h4>Results on WikiText-103</h4><p>The authors first demonstrate the results of ALiBi on the WikiText language modeling dataset. This dataset is a collection of over 100 million tokens from 28,588 Wikipedia articles verified as ‘Good’ or ‘Featured’ collected by Salesforce Research.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JuM2TISf-3tW9qTgaSpjMQ.png" /><figcaption>Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>Here they compare different embeddings across four 247M parameter models:</p><ul><li>Sinusoidal</li><li>Rotary embeddings — used in GPT-j</li><li>T5 Bias — used in T5 collection</li><li>ALiBi</li></ul><p>In figure 1 (right), the models are trained on sequences of 1024 tokens in their training distribution, however, when they perform inference (x-axis) on larger inputs than what they were trained on the perplexity (y-axis) explodes. This is especially the case for sinusoidal and rotary embeddings, but perplexity remains very low for ALiBi. Even for extrapolating on input sizes of up to 16,000!</p><p>A lower perplexity means the model’s predictions are closer to the expected outcomes, i.e., it’s less “surprised” by the data it sees, indicating better predictive performance. For example, if a model predicts the next word of a sentence accurately, this means the model has a good understanding of the language structure and context, resulting in a lower perplexity score.</p><p>T5 Bias performs a bit better than the first two techniques but it’s a learned embedding so it requires more memory and takes longer to compute and train. Learned embeddings are part of the model itself and are learned during the training process. They start from random initialization (or some pretraining) and are then updated via backpropagation and gradient descent alongside all the other parameters of the model. The authors explain T5 drops off around 12,000 tokens due to running out of memory on their 32GB GPU. Regardless of this, it also shows significant degradation over larger input sequences.</p><p>ALiBi is a fixed valued (or precomputed) approach, as with Sinusoidal and Rotary embeddings. These embeddings are “fixed” in the sense that they do not change during the training of the model in which they are used. Due to this they can deal with much longer sequences and maintain speed with not having to learn embeddings. No wasting memory or compute:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_6moNT0QcH1MnSLylaZl7A.png" /><figcaption>Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>(If you’re wondering why ALiBi’s inference speed in the chart above is slightly higher — it was later explained that there’s no reason for this. There was just variance when running inference/training their hardware and, as explained in the paper, the speed of sinusoidal and ALiBi is basically identical, those variations are all within variance.)</p><h4>How does ALiBi work?</h4><p>Introducing ALiBi:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9D_DIgDJz5WQbreZ1a9xTA.png" /><figcaption>ALiBi. Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>We’re working with autoregressive language modeling which involves ‘causal attention’ / next word prediction. This is why the upper-triangular area is masked out; we don’t want to extend any attention to the future — only the past words. (We could fill in the rest of the matrix for full self-attention). This means a current query node can only pay attention to all the keys in the nodes that come before it.</p><p>So query 2 would only be multiplied by key 1 and key 2 and not key 3 etc because it can’t peek into the future:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HOVnEwTg41MGjM7ZrcLzZw.png" /><figcaption>q2 can only be multiplied by keys of current or previous tokens to determine an attention score</figcaption></figure><p>If it were just this calculation then there is no notable difference between q_2*k_1 and q_2*k_2 — it only depends on what the value of the key is to impact the information, not the position at all. So what the authors do is pretty simple… They add the distance between the two positions, multiplied by a number ‘m’.</p><p>M is a constant predefined number e.g., 0.4. It’s needed as the q*k — position numbers can get big too quickly, so m normalizes them as it is a float between 0–1.</p><p>We can see that the further into the past a given key is — the higher a value is subtracted from the attention value (numbers in the left matrix are attention values). If numbers in the left matrix are high then it means that key is really relevant to the query.</p><p>Whatever value you compute, however important it is, the further in the past it is then the more we’re going to subtract from it and we do this in a linear fashion. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*jfDQX3dvhrS9GoQg42ZtFQ.png" /><figcaption><a href="https://www.youtube.com/watch?v=Pp61ShI9VGc&amp;t=704s">Source</a></figcaption></figure><p>So it degrades linearly (hence the name; Attention with Linear Biases) and can go to a negative value for dropoff. We then apply softmax to the query-key dot products which will give us a distribution.</p><p>This technique doesn’t require additional parameters but does result in a slight slow down due to injecting the positional encodings into every attention head within each layer. This is only applied to the query and key computation — not the value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nDaLFbUnvPbHtStaB59MdA.png" /><figcaption>Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>Additional information about m:<br>M is different for each head to control the slope in the attention head and make each head slightly different from each other so the model doesn’t just rely on noise and build an ensemble. This can make the heads more effective by making them slightly different in how they work so the model can choose which one to utilize most. Please refer to the paper for more detail on this.</p><h4>Experimental Results</h4><p>The authors expand their experiments to compare ALiBi models trained and evaluated on varying input subsequence lengths to the sinusoidal baseline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nFtQSGfRtYyPVS9PqwAW1Q.png" /><figcaption>Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>Here they show that ALiBi is efficient and enables training models with short input subsequences that outperform strong baselines even when the ALiBi models extrapolate to more than six times the number of tokens that they were trained on.</p><p>In figure 4 above the square dots represent models trained using classic sinusoidal embeddings. These are always tested on as long a sequence as they were trained on because, as we saw in figure 1, if we make the sequence longer they will just fail explosively.</p><p>The dotted purple line represents a model trained using the ALiBi technique with input lengths of 512. As we can see during validation, it already performs better in perplexity than all baseline sinusoidal models.</p><p>We can see in general that on longer texts the perplexity decreases, so as we train ALiBi on longer sequences, such as 1024 and 2048, we’ll experience a gain in performance. However, it’s not too bad if we stick to training on short sequences like 512 and then extrapolate to longer ones. The ALiBi models are able to surpass the sinusoidal baseline when not extrapolating while also outperforming it when extrapolating to longer sequences.</p><h4>Results on the CC100 + RoBERTa Corpus</h4><p>The final set of experiments investigate whether ALiBis performance transfers to a larger model trained on a larger dataset than the ones previously used.</p><p>Since WikiText-103 is considered quite small (100 million tokens) compared to more recently available datasets, a model with a strong inductive bias will easily achieve great results on this - but that advantage almost disappears when you train on a much much larger dataset with a much greater compute budget.</p><p>The datasets used in this experiment are the 161 gigabyte RoBERTa training corpus (Toronto Book Corpus, English Wikipedia, CC-News, OpenWeb-Text and Stories ), and the 300 gigabyte English part of the CC-100 corpus, totalling 461GB. The validation set contains 649K tokens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9fUMC0wcPGBEwYgzEgBkBQ.png" /></figure><p>The models used for this dataset consist of 25 transformer layers with 16 heads and have 1.3B parameters. <br>The only differences between the models, other than positional encoding, is the length L of the input sequences used during training. The authors decided to limit the ALiBi models to only be trained on half the L of the sinusoidal model. This was done to demonstrate the savings in time and compute without sacrificing any extrapolation power.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*KNfdAl7YxF-5V0KcMC6s3A.png" /><figcaption>Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>We can see figure 5 (left) compares the validation perplexity for L_valid = 1024 throughout the training process for an ALiBi model trained with L = 512 compared to a sinusoidal model trained with L = 1024. They show very similar results and as the ALiBi model is trained on shorter sequences, it is 7% faster and uses 1.6 GB less memory.</p><p>The Figure 5 (right), results become even more impressive, showing that the ALiBi model trained on L = 1024 outperforms by 0.09 perplexity the sinusoidal model trained on L = 2048 (when evaluating with L_valid = 2048) even though the ALiBi model uses 3.1 GB less memory. The ALiBi model maintains a lead in perplexity over the sinusoidal model during the entire training process.</p><p>The authors were able to show that the ALiBi L = 1024 model reaches a given perplexity value, on average, 11% faster than the sinusoidal model does. Stacking more layers would further improve performance (with negligible, if any, runtime cost).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*La1dH_ela2lmzTnA6nNrHQ.png" /><figcaption>Source: <a href="https://arxiv.org/pdf/2108.12409.pdf">Paper</a></figcaption></figure><p>Figure 6 shows that the ALiBi models trained on L = 512 and L = 1024 achieve the best results when extrapolating to about double the tokens that they were trained on. After that their performance starts to degrade.<br>The sinusoidal model cannot extrapolate at all in this setting, with its performance degrading for both the L = 512 and 1024 models as soon as one token more than L is added during evaluation.</p><p>In this experiment the authors were able to show that their method achieves strong results in this more challenging setting, obtaining similar performance to the sinusoidal baseline while using significantly less memory, since they train on shorter subsequences.</p><h4>Conclusion</h4><p>Overall, the gaps of improvement in extrapolating on smaller datasets are huge. On larger datasets ALiBi is still a bit better, but more realistic.</p><p>Eliminating the positional embeddings and calculating ALiBi across the heads can save a lot of computation and time in training. If we train a model on smaller sequences using ALiBi then at inference time it has the potential to understand much larger texts such as books or long reports etc. This helps to explain why we have seen the technique be adopted by models such as MPT and BLOOM.</p><h4>Resources</h4><p>Paper: <a href="https://arxiv.org/pdf/2108.12409.pdf">https://arxiv.org/pdf/2108.12409.pdf</a></p><p>Github: <a href="https://github.com/ofirpress/attention_with_linear_biases">https://github.com/ofirpress/attention_with_linear_biases</a></p><p>Video Talk by Author: <a href="https://www.youtube.com/watch?v=Pp61ShI9VGc">https://www.youtube.com/watch?v=Pp61ShI9VGc</a></p><p><a href="https://docs.google.com/presentation/d/1dYRrbgu0ZrwtCCkl6h7KY_aLEPopi4o0iy7kc0Cy_vo/edit?usp=sharing">Slides</a> I created to give a presentation on this paper</p><p>What’s next? (If you found this interesting): <a href="https://arxiv.org/abs/2306.15595">Extending Context Window of Large Language Models via Positional Interpolation </a>27 Jun, 2023. Meta.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=942abe042e9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[t-SNE: t-distributed stochastic neighbor embedding]]></title>
            <link>https://medium.com/@pajakamy/dimensionality-reduction-t-sne-7865808b4e6a?source=rss-b1190baed26------2</link>
            <guid isPermaLink="false">https://medium.com/p/7865808b4e6a</guid>
            <category><![CDATA[unsupervised-learning]]></category>
            <category><![CDATA[exploratory-data-analysis]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[dimensionality-reduction]]></category>
            <dc:creator><![CDATA[Amy Pajak]]></dc:creator>
            <pubDate>Tue, 27 Jun 2023 17:37:59 GMT</pubDate>
            <atom:updated>2023-06-28T11:53:31.534Z</atom:updated>
            <content:encoded><![CDATA[<h4>An overview of t-SNE as a dimensionality reduction technique</h4><h4><strong>Summary</strong></h4><p>t-SNE (t-Distributed Stochastic Neighbor Embedding) is a dimensionality reduction <strong>tool</strong> used to <strong>help visualize</strong> high dimensional data.</p><p>It’s not typically used as the primary method for training a model. Instead, it’s often the first step for exploratory data analysis, visualization, and clustering. t-SNE provides an intuitive way to understand high-dimensional data by reducing its complexity, which can guide the selection and application of subsequent techniques for more detailed and focused analysis.</p><p>The 2D/3D mapping created by t-SNE allows us (as humans) to view if there are strong relationships and from there decide <em>ourselves</em> the best machine learning algorithm to apply to the data; such as clustering, classification, or deep learning i.e. to LEARN those relationships within the mapping and identify outliers. This can help to improve the performance of our ML algorithm and reduce overfitting.</p><h4>Introduction — High Dimensional Data</h4><p>t-SNE was introduced due to having lots of high dimensional data that practitioners want to visualize. For example:</p><ol><li>Financial data: stock prices, trading volumes, and economic indicators, can be represented as high-dimensional data sets.</li><li>Medical imaging: technologies, such as MRI and CT scans, generate high-dimensional data with intensity values representing different features.</li><li>Genomics: DNA sequences of organisms are represented as high-dimensional data sets with each gene or nucleotide base representing a dimension. The human genome has approximately 3 billion base pairs, which correspond to 3 billion features in the DNA.</li><li>Image and video data where each pixel represents a dimension.</li><li>Social media platforms with user profiles, posts, likes, comments, and other interactions.</li><li>Text data, such as news articles, tweets, and customer reviews, can be represented with each word or token as a dimension.</li><li>Robotics: generates data with sensory inputs from cameras, microphones, and other sensors used in their control systems.</li></ol><p>High dimensional data is everywhere. We need to visualize and explore the complex data in a more intuitive and understandable way. t-SNE serves as a powerful tool to achieve this by effectively transforming the high-dimensional data into a low-dimensional representation without losing significant information.</p><h4>MNIST Digits</h4><p>A great way to explain t-SNE is to show how it works on the MNIST Digits dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TGz3umQUNlFhiVwp" /><figcaption><a href="https://www.mdpi.com/2076-3417/9/15/3169/htm">A Survey of Handwritten Character Recognition with MNIST and EMNIST</a> (2019)</figcaption></figure><p>This is a publicly available labeled dataset of 60,000 28x28 grayscale training images of handwritten digits from 0–9, along with a test set of 10,000 images.</p><p>We will use this dataset to demonstrate different dimensionality reduction techniques.</p><h4>Introducing Principal Component Analysis (PCA)</h4><p>One of the first, and most popular, dimensionality reduction techniques is Principal Component Analysis (PCA) which was published in 1901 by Pearson, Karl et al. It is a linear technique which finds a linear projection, or a new representation, of the original high-dimensional data points onto a lower-dimensional subspace in a way to maximize the variance of the data i.e. preserve as much information as possible. These projected axes/directions are referred to as the principal components of the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8VzZmYrziUtqTCet" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RzD3fN0cZVikU7bEVo_nwQ.png" /><figcaption>View the notebook version <a href="https://colab.research.google.com/drive/1znYpKviaBQ7h0HgfACcVxnP26Ud1cwKO?usp=sharing">here</a></figcaption></figure><p>If we visualize PCA on MNIST Digits the results will be similar to what we see above which is a visualization of around 5,000 images. They’ve been laid out in 2-dimensions where each point corresponds to a digit in the dataset and its color labels which digit the point is representing. What we see here is that PCA captures some of the structure of the data, for example the red points on the right form a cluster of 0’s, and the oranges on the left form a cluster of 1’s. This happens to be the first principal component! — so the main variation between digits is between the 0’s and 1’s. That makes sense in pixel values where 0’s and 1’s have very few overlapping pixels.</p><p>The second principle component is on the top of the visualization where we see 4’s 7’s and 9’s clustered which are slightly more similar in terms of pixel values, and on the bottom we’ve got 3’s, 5’s and 8’s clustered which are also more similar e.g. a 3 will have many overlapping pixels with an 8. So that’s our second source of maximum variation between the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*yxq5GjMWP7LtqF5ATcM-GQ.png" /><figcaption>We can see 4, 7, 9 and 3, 5, 8 have similar overlapping pixel structure</figcaption></figure><p>This is great but a problem arises when the data is unlabelled — as we can see on the right. The color/labels can tell us some information about the relationships in the data, without them we see no clear clusters but rather just many points in a 2D space. So we run into a problem with unlabelled data causing us to be unable to interpret these results.</p><p>Can we do better?</p><h4>Linear vs Non-Linear Data</h4><p>PCA is good, but it’s a linear algorithm.</p><ul><li>It cannot represent complex relationships between features</li><li>It is concerned with preserving large distances in the map (to capture the maximum amount of variance). But are these distances reliable?</li></ul><p>Linear techniques focus on keeping the low-dimensional representations of dissimilar data points far apart (e.g. 0’s and 1’s we’ve just seen). But is that what we want in a visual representation? And how reliable is it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/0*TpOijEW_tlI-nasJ" /><figcaption><a href="https://seoulai.com/presentations/t-SNE.pdf">Dimensionality reduction with t-SNE</a> (2018)</figcaption></figure><p>If we look at the swiss-roll nonlinear manifold above (a), we can see that a Euclidean (straight-line) distance between two points in the red and blue clusters would suggest that the data points are quite close.</p><p>If we consider the entire structure to be represented in a 2D plane (i.e. rolling it out into a flat 2D shape), the red points would actually be on the opposite end to the blue points — one would have to traverse the entire length of the roll to get from one point to the other. PCA would attempt to capture the variance along the longest axis of the roll, essentially flattening it out. This would fail to preserve the spiral structure inherent to the Swiss roll data, where points that are close together in the spiral (and thus should be close together in a good 2D representation) end up being placed far apart.</p><p>So we can see PCA doesn’t work very well for visualization of nonlinear data because it preserves these large distances and we not only need to consider the straight-line distance, but also the surrounding structure of each data point.</p><h4>Introducing t-SNE</h4><p>Stochastic Neighbor Embedding was first developed and published in 2002 by <a href="https://www.cs.toronto.edu/~fritz/absps/sne.pdf">Hinton et al</a>, which was then modified in 2008 to what we’re looking at today — t-SNE (t-Distributed Stochastic Neighbor Embedding).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mp95O4ptzxdjdUFa-r6R-Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YYz8lNa_eQxMzQb38Cy6ug.png" /></figure><p>For anyone curious another variation was published in 2014 named <a href="https://jmlr.org/papers/volume15/vandermaaten14a/vandermaaten14a.pdf">Barnes-Hut t-SNE</a> which improves on the efficiency of the algorithm via a tree-based implementation.</p><h4>How t-SNE works</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vjfZSFizkdUJM7EzKQTd3g.png" /><figcaption><a href="https://seoulai.com/presentations/t-SNE.pdf">Dimensionality reduction with t-SNE</a> (2018)</figcaption></figure><p>In a high dimensional space (left) we measure similarities between points. We do this in a way so that we only look at local similarities, i.e. nearby points.</p><p>The red dot in the high dimensional space is xi. We first center a gaussian over this point (shown as the purple circle) and then measure the density of all the other points under this gaussian (e.g. xj). We then renormalise all pairs of points that involve the point xi (the denominator/bottom part of the fraction). This gives us the conditional probability pj|i, which basically measures the similarity between pairs of points ij. We can think of this as a probability distribution over pairs of points, where the probability of picking a particular pair of points is proportional to their similarity (distance).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ILFSLJmj-blmVDOa" /><figcaption><a href="https://www.enjoyalgorithms.com/blog/tsne-algorithm-in-ml">t-SNE (t-Distributed Stochastic Neighbor Embedding) Algorithm</a></figcaption></figure><p>This can be visualized as such. If two points are close together in the original high dimensional space, we’re going to have a large value for pj|i. If to points are dissimilar (far apart) in the high dimensional space, we’re going to get a small pj|i.</p><h4>Perplexity</h4><p>Looking at the same equation, perplexity tells us the density of points relative to a particular point. If 4 points of similar characteristics are clustered together, they will have higher perplexity than those not clustered together. Now, points with less density around them have flatter normal curves compared to curves with more density. In the figure below, the purple points are sparse.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fD0NMvzn95pjypXcf0kwhQ.png" /><figcaption><a href="https://www.enjoyalgorithms.com/blog/tsne-algorithm-in-ml">t-SNE (t-Distributed Stochastic Neighbor Embedding) Algorithm</a></figcaption></figure><p>We compute the conditional distribution between points as this allows us to set a different bandwidth (sigma i) for each point, such that the conditional distribution has a fixed perplexity. This is basically scaling the bandwidth of the gaussian in such a way that a fixed number of points fall in the range of this Gaussian. We do this because different parts of the space may have different densities, and this trick allows us to adapt to those different densities.</p><h4>Mapping the lower dimensional space</h4><p>Next we’re going to look at the low dimensional space which will be our final map.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eg4WZoZRchNwgi0t" /><figcaption>[2]</figcaption></figure><p>We start by laying the points out randomly on this map. Each high dimensional object will be represented by a point here.</p><p>We then repeat the same process previously - center a kernel over the point yi and measure the density of all the other points yj under that distribution. We then renormalise by dividing over all pairs of points. This gives us a probability qij, which measures the similarity of two points in the low dimensional map.</p><p>Now, we want these probabilities in qij to reflect the similarities pij which we computed in the high dimensional space just before, as closely as possible. If the qij’s are identical to the pijs then apparently the structure of the map is very similar to the structure of the data in the original high dimensional space.</p><p>We will measure the difference between these pij values in the high dimensional space and the qij values in the low dimensional map by using <a href="https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence">Kullback–Leibler divergence</a>.</p><h4>Stochastic Neighbor Embedding</h4><p>KL divergence is the standard measure of the distance between probability distributions / their similarity. Its shown below in the cost function as the sum over all pairs of points of pj|i times log pj|i over qj|i.</p><ul><li>Similarity of data points in High Dimension</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*rRDIkXTb17iWyJCSNmyUYA.png" /></figure><ul><li>Similarity of data points in Low Dimension</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*OyeY1BEMnflCJ57cOSyeFg.png" /></figure><ul><li>Cost function</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*1jDSaxmLpWcLiud29ZwnMQ.png" /><figcaption>[1]</figcaption></figure><p>Our goal now is to lay out the points in the low dimensional space such that the KL divergence is minimized i.e. as similar as possible to the high dimension values. In order to do that we’re basically going to do gradient descent in this KL divergence, which is essentially moving the points around in such a way that the KL divergence becomes small</p><h4>Mapping the lower dimensional space</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0sDGtZlIt-Bh_SpW" /><figcaption>[2]</figcaption></figure><p>Here we can see the resultant mapping which has been rearranged to be as similar to the higher dimension as possible.</p><p>KL divergence is useful as it measures the similarity between two probability distributions. It is also symmetric (distance xi -&gt; xj is same as distance from xj -&gt; xi).</p><h4>t-SNE Algorithm</h4><p>Let’s take a final look at the overall algorithm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wtq6RVlYyAbJtD64g5-KUw.png" /><figcaption>[1]</figcaption></figure><p>Combining what we’ve seen:</p><p>1. Calculate the pairwise affinities (conditional probabilities) in the high-dimensional space, using a Gaussian distribution. The perplexity parameter defines the effective number of neighbors each point has and helps to balance the focus on local and global aspects of the data.</p><p>2. Symmetrize the probabilities. This means that each point considers the other point as its neighbor, and it is achieved by taking the average of the conditional probabilities for each pair of points. Then normalize by dividing by the total number of points.</p><p>3. Randomly initialize the position of each data point in the low-dimensional space, usually by drawing from a normal distribution with mean 0 and small variance.</p><p>4. Start a loop that will iterate for a fixed number of times T. Each iteration updates the position of the points:</p><p>4.1. Calculate the pairwise affinities (similarities) in the low-dimensional space using a t-Student distribution.</p><p>4.2. Calculate the gradient of the cost function with respect to the position of the points. The cost function is usually the Kullback-Leibler divergence between the high-dimensional and low-dimensional distributions.</p><p>4.3. Update the position of the points in the low-dimensional space. This update step is composed of three parts: the old position Y(t-1), a term proportional to the gradient that helps minimize the cost function, and a momentum term that helps accelerate convergence and avoid local minima.</p><p>5. End of the t-SNE algorithm. The final positions of the points in the low-dimensional space should now provide a useful visualization of the high-dimensional data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y2Pxb9BgsbI5GehzK5Bt4Q.png" /><figcaption>Visual output of t-SNE applied to 1000 MNIST Digits images</figcaption></figure><p>If you want to see this algorithm implemented in detail as code please take a look at the original authors <a href="https://lvdmaaten.github.io/tsne/">github</a> [3] or this great step-by-step <a href="https://towardsdatascience.com/t-sne-from-scratch-ft-numpy-172ee2a61df7">article</a>.</p><h4>Alternatively…</h4><p><em>“To deal with hyperplanes in a 14-dimensional space, visualize a 3D space and say ‘fourteen’ to yourself very loudly. Everyone does it.” </em>— Geoffrey Hinton, A geometrical view of perceptrons, 2018</p><h4>Advantages</h4><ol><li>Visualization: t-SNE can help visualize high-dimensional data that has non-linear relationships as well as outliers</li><li>Good for clustering: t-SNE is often used for clustering and can help identify groups of similar data points within the data.</li></ol><h4>Limitations</h4><ol><li>Computational Complexity: t-SNE involves complex calculations as it calculates the pairwise conditional probability for each point. Due to this, it takes more time as the number of data points increases. <a href="https://jmlr.org/papers/volume15/vandermaaten14a/vandermaaten14a.pdf">Barnes-Hut t-SNE</a> was later developed to improve on this.</li><li>Non-Deterministic: Due to the randomness in the algorithm, even though code and data points are the same in each iteration, we may get different results across runs.</li></ol><h4>Demo</h4><p>In the following notebook I use Python to implement PCA and t-SNE on the MNIST Digits dataset via the sklearn library:</p><p><a href="https://colab.research.google.com/drive/1znYpKviaBQ7h0HgfACcVxnP26Ud1cwKO?usp=sharing">https://colab.research.google.com/drive/1znYpKviaBQ7h0HgfACcVxnP26Ud1cwKO?usp=sharing</a></p><h4>Conclusion and Next Steps</h4><p>To conclude, t-SNE visualization is just the first step in the data analysis process. The insights gained from the visualization need to be followed up with further analysis to gain a deeper understanding of the data or captured by an appropriate ML algorithm to build predictive models, or statistical analysis methods to test specific hypotheses about the data.</p><p>Other popular dimensionality reduction techniques include:</p><ul><li>Non-negative matrix factorization (NMF)</li><li>Kernel PCA</li><li>Linear discriminant analysis (LDA)</li><li>Autoencoders</li><li>Uniform manifold approximation and projection (UMAP)</li></ul><p>You can read more about them <a href="https://en.wikipedia.org/wiki/Dimensionality_reduction">here</a>.</p><h4>Resources</h4><ul><li>[1] Original paper “<a href="https://www.jmlr.org/papers/volume9/vandermaaten08a/vandermaaten08a.pdf">Visualizing Data using t-SNE</a>”, 2008, by L. Maaten and G. Hinton</li><li>[2] YouTube video <a href="https://www.youtube.com/watch?v=RJVL80Gg3lA">Visualizing Data Using t-SNE </a>— Google Tech Talks, L. Maaten, 2013</li><li><a href="https://lvdmaaten.github.io/tsne/">t-SNE implementations, examples and FAQ</a> — L. Maaten GitHub</li><li><a href="https://docs.google.com/presentation/d/1MYjoN-qCSPEuf3TIViXitjteeW2gC8DjJOoIrZ0-EA8/edit?usp=sharing">Slides</a> I created to give a presentation on this topic</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7865808b4e6a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>