Notes

← Back to home

A collection of fragments of understanding in the pursuit of deeper questions.

Attention Is All You Need

Content of the Lecture

  • Attention in Neuroscience
    • Definition of Attention (Cognitive Neuroscience).
    • What is Selective Attention in Biology (Cognitive Neuroscience).
    • Neuronal Substrates of Attention (Cognitive Neuroscience).
  • Attention Models in Machine Learning
    • Self-Attention.
    • Transformers.
    • GPT3 and ChatGPT.

Attention in Neuroscience - Selective Attention & Visual Saliency

Selective Sensory Attention in Cognitive Neuroscience "It is the taking possession by the mind in clear and vivid form, of one out of what seem several simultaneously possible objects or trains of thought ... It implies withdrawal from some things in order to deal effectively with other,..." - William James in Principles of Psychology (1890)

image300

"Attention is the flexible control of limited computational resources" - Lindsay (2020)

Function of Selective Attention

  • In primates, 10ˆ6 retinal ganglion cell axons can carry about 1 MB/s of visual information (already compressed).
  • This is too much to be handled in real-time by the brain (there are many other sensory domains that feed data into the brain).
  • Some fraction of sensory input is selected for processing by higher-level, cognitive routines, while the non-attended portion of the input is processed at a reduced bandwidth.
  • Attention is common to all animals.
image301

Bottom-Up Attention Something immediately draws our eye to the Toblerone and the Matterhorn. This something is visual saliency.

image302

Visual Saliency

  • Single map that encodes saliency is feed by multiple feature maps. Which features draw the gaze independent of the task is unknown. Typically, these are faces, orientations, color and intensity.
  • After detecting the most salient location, its salience is inhibited.
  • Single cell evidence for salience map in the initial response of LIP and FEF neurons in primates.
image303

Nowadays DL is exploited to predict saliency spots in pictures. They take ground truth data and train neural networks to predict saliency points in images with higher accuracy than older models. Such models are also exploited to work in the other direction and help in designing images to convey attention towards specific points of the figure.

image304

Top-Down Attention

  • Is flexible and task dependent.
  • Can last indefinitely with effort.
  • Spatial based, feature-based and object based are forms of top-down attention.
  • A close relationship between neuronal mechanics controlling eye gaze and selective attention (LIP, FEF, V4, SC).
image305

Selective Top-Down Attention Neural Signals have been measured in Primates. The primates watch a screen and fix the white dot. Then the three stimuli went on and the cue went on (the central dot that changes color). The researchers used electrophysiology to record the activity of individual neurons in the visual cortex of monkeys as they performed a visual search task. The goal of the task was to detect a target stimulus among a set of distractors. The researches found that attentional modulation was present in both feedforward and feedback signals in the visual cortex, with top-down attentional signals strengthening target-selective responses and suppressing responses to distractors. This study provides evidence for the role of top-down attentional signals in modulating both feedforward and feedback processing in the primate visual cortex, and suggests a mechanism by which attention can selectively enhance target representation and improve perceptual processing.

image306

The paper "Selective Top-Down Attention Modulates Feedforward and Feedback Neural Signals in Primates" by Paperi et al. (2017) investigated the effect of top-down attention on feedforward and feedback signals in primate brains. The study found that when attention was directed towards a particular visual stimulus, both feedforward and feedback signals increased in the regions of the brain responsible for processing that stimulus, demonstrating that top-down attention can modulate both feedforward and feedback signals. These results suggest that attention plays a crucial role in shaping neural processing in the visual system, and that top-down attention can dynamically influence feedforward and feedback signals to enhance processing of behaviorally relevant information.

Attention Models in Machine Learning - Self-Attention, Transformers and GPTs

Attention in Deep Learning In the field of Machine Learning, attention is a mechanism used by certain types of neural networks to selectively focus on certain parts of an input when processing it. This is useful because it allows the model to automatically learn to focus on the most relevant parts of the input, which can improve its performance on a given task. For example, an attention mechanism might be used in a machine translation model to automatically focus on the words in the source sentence that are most relevant for generating the correct translation.

We'll focus here on time-series attention models.

image307 image308

This is a sequence-to-sequence neural network. You want your architecture to produce an output for every timestamp. And usually you do these through RNNs (LSTMs), which features hidden states and recalls previous states of the network, thus requires a sequential step-by-step calculation of the states. On the other hand we have CNNs which are great in parallelization, but the downside is their finite and fixed memory (loose flexibility). Can we combine these two architectures? That is, can we have the long dependencies and flexibility of RNNs together with the parallelization properties of CNNs? Here is where Self-Attention (SA) Models come in.

image309

The outputs are calculate according to the formula in the right figure, however the key peculiarity is that W is not a parameter of the model that is learnt through gradient descent. It is a parameter that indicates how similar the inputs are similar between them and are calculated as shown in the following figure. These weights are passed through a Softmax such that the sum of all weights add up to 1.

image311 image310
image313 image312
  • In simple self-attention WiiW_{ii}, i.e., Xi to YiX_{i}\ to\ Y_{i} usually has the most weight. Which is not a big problem, but we will allow this to change later.
  • Simple self-attention has no parameters. Whatever parametrized mechanism generates xix_{i} (like an embedding layer) drives the self-attention.
  • There is a linear operation between X and Y. Non-vanishing gradients through Y=WXTY = WX^{T}, vanishing gradients through W = softmax(XTX)X^{T}X).
image314

Intuition how Self-Attention works: The Dot Product Let's say we want to try a model to understand the sentiment of some restaurant reviews (like in the example below). The word "terrible" is something that probably we wouldn't like to have in a restaurant review, however if it comes after "not too" is probably not such a bad review. Hence, our model should pose attention also to these part of the sentence to understand the overall sentiment of the phrase, i.e., the weight between "not" and "terrible" should be a major one in our self-attention matrix. Which implies that the model should be capable of understanding the dependencies between the input "not" and "terrible" and how it modulates the meaning.

image315 image316

Improving Self-Attention

image317
  • Scaled Dot Product: If we have longer and longer input vectors than WijW_{ij} gets larger and larger. In order to avoid that we scale it through a normalization factor K.
  • Key, Value and Query transformations. These is the main breakthrough towards transformers architectures: the Key value is not anymore just the input value, but rather a linear transformation of the input, i.e. the input value times K plus a bias term b. In this step we are actually allowing the network to learn these attentional weights because the parameters (K, Q, V, bk,bq,bvb_{k},b_{q},b_{v}) can be trained through gradient descent.
image319 image318
  • Multi-Head Attention: The input vector is projected into some low-dimensional version, you concatenate them and you obtain multiple sets of attention weights that can be learnt. This allows the network to have a self-attention that can understand the relation between words in the sentence.
image321 image320

Recap of Self-Attention Self-Attention: Sequence-To-Sequence layer with Parallel Computation and Perfect Long-Term Memory. Fundamentally a set-to-set layer, no access to the sequential structure of the input. A large part of the behavior comes from the parameters upstream.

Transformers Any sequence-based model that primarily uses self-attention to propagate information along the time dimension. More broadly: any model that primarily uses self-attention to propagate information between the basic units of our instances.

  • Pixels Image Transformer.
  • Graph Nodes Graph Transformer.

The transformer architecture is based on blocks as the one showed below that are stucked on each other as shown in the right figure.

image323 image322

The Auto-Regressive Transformer One problem that we still have is making sure that the network cannot access information regarding upcoming targets from the previous layers. In order to do this we calculate the Causal Self-Attention weight matrix and all future values are set to minus infinity.

image324

Causal Self-Attention By using Causal Self-Attention we force the network to figure out which words are important in the previous sequence to generate the following letter or word. The blocks that feature such characteristic are named Causal Transformer Blocks.

The Causal Autoregressive Transformer

image326 image325

The Transformer: Position Embeddings The last problem we have to address is "repetition of words" or again different meanings associated with the same word (e.g., "The"). In order to overcome this limitation, Position Embeddings are used, so that same words are considered differently by the network.

image327

The "Original Transformer" (ELMo) Adding all these features together, we obtain the very first transformer ELMo from the paper "Attention Is All You Need" that was exploited for language translation. It was a machine translation model with no recurrent layers or convolutions, but rather an encoder/decoder configuration with positional encoding. It featured 512 dims, 8 heads, 2x6 blocks. It has been trained for 3.5 days on 8 GPUs.

image328

The GPT3 Transformer Autoregressive language model. Single stack of causal trf blocks with positional embeddings. 12288 dims, 96 heads, 96 blocks, sequence size 2048 and 175 billion total parameters. It has been trained on 10000 GPUs, likely in around 12 days for about $4,6 million.

image329

Training Transformer Models

image330

Jeopardy Questions

  • The value of the current state (Vs) changes according to the "immediate reward" and its "estimated difference to the next state" in this rule. What is the Temporal Difference Rule?
  • This system is used to express genes in a cell specific manner (in GMOs). What is the CRE (/ loxP) system?
  • This man is the creator of a binary network with Hebbian learning rules that use attractors as memory. Who is John Hopfield?
  • This well-studied brain area is most important for memory. What is the Hippocampus?
  • This is what x1 and x2 are, given <x1,x2> - <x1><x2> = 0. What is uncorrelated?
  • This animal is rarely used as a model organism in neuroscience. What is [fill in animal name here] that is not one of the classical ones used?
  • This property of SNNs makes it difficult to train them. What is spiking nonlinearity?
  • The adaptive linear neuron was introduced in 1960 under this name. What is ADALINE?
  • He postulated "What fires together, wires together". Who is Donald O. Hebb?
  • DeltaWij = xiyj is commonly used to formalize the simplest form of this rule. What is Hebbian Learning Rule?
  • These ions are responsible for generating an action potential. What is Sodium and Potassium?
  • This method uses an infrared laser to image, e.g., Ca2+ activities. What is 2-photon microscopy?
  • This method generates random feature that do not require learning and acts like jelly. What is reservoir computing?
  • This neuromodulator is thought to encode reward prediction error in the brain. What is Dopamine?
  • Responding to glutamate, this receptor opens for sodium and calcium ions. What is the NMDA receptor?
  • In short-term plasticity, this "gain" may precede depletion. What is facilitation/post-tetanic potentiation?
  • CamKII is dependent on this tiny second messenger. What is Calcium ion?
  • For consecutive action potentials, these properties tend to stay constant. What are amplitude and duration (shape)?
  • This unsupervised method enforces independence of its components. What is Independent Component Analysis (ICA)?
  • In this probabilistic Autoencoder the latent representation of an input is sampled from a set of means and standard deviations. What is a Variational Autoencoder?
  • This network is trained via the "Wake/Sleep" algorithm. What is Helmholtz Machine? (Not covered).
  • These are the methods that pytorch.autograd.Function has. What are the forward and backward methods?
  • This signal can be detected when dopamine binds to dLight1.1. What is Fluorescence?
  • This method/network has a short, counter-intuitive name and is used as an alternative to backprop through time. What is (an) LSTM?
  • In the SuperSpike rule, this operation is used to generate eligibility traces. What is Convolution?
  • In this rule, a presynaptic event followed by a postsynaptic event followed by a presynaptic event leads to local learning. What is the
  • A counterargument for this method can be constructed by locking yourself up in a dark room. What is Free Energy Principle?
  • Investigated in owls, this computation is used to determine direction. What is ITD?
  • Updating weights with respect to an approximation of the feedforward activations instead of the true feedforward activations is the key difference between these two methods. What are Predictive Coding and Backpropagation?
  • Experts commented on sales events from this company with (e.g.) "I don't think there was anything revolutionary in the presentation". What is Neuralink?