Neural Networks
Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6
61 cards
Introduction
- What is a feedforward network, and why is modern neural network work called deep learning?A network in which computation proceeds from one layer of units to the next, with no cycles. “Deep” because modern networks often have many layers.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, Introduction, p. 151
- How does the power of neural networks compare to logistic regression?They are more powerful: even a network with a single hidden layer can in principle learn any function.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, Introduction, p. 151
- How does using neural networks differ from using logistic regression in practice?Logistic regression relies on rich hand-designed feature templates. Neural networks usually take raw tokens as input and learn to induce features themselves (representation learning), so they suit tasks with enough data to learn features automatically.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, Introduction, p. 151
6.1 Units
- What does a single neural unit compute?A weighted sum of its inputs plus a bias, \(z = w \cdot x + b\), then a non-linear activation function: \(a = f(z)\).
Weighted sum, bias, non-linearity. - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 152
- What is the activation value of a unit?The output \(a = f(z)\) of its non-linear function. For a single-unit network it is also the network output \(y\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 152
- Compute the output of a sigmoid unit with \(w = [0.2, 0.3, 0.9]\), \(b = 0.5\), on \(x = [0.5, 0.6, 0.1]\).\(z = 0.1 + 0.18 + 0.09 + 0.5 = 0.87\), so \(y = \sigma(0.87) = \frac{1}{1 + e^{-0.87}} \approx 0.70\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 153
- What is the tanh activation function, and how does it compare to the sigmoid?\(\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}}\), a variant of the sigmoid ranging from −1 to +1. It is almost always better than the sigmoid; it is smoothly differentiable and maps outliers toward the mean.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 153
- What is the ReLU?The rectified linear unit, \(\text{ReLU}(z) = \max(z, 0)\): equal to \(z\) when positive, 0 otherwise. The simplest and perhaps most commonly used activation.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 153
- What does it mean for a sigmoid or tanh unit to be saturated, and why is that a problem?For large |z| its output is extremely close to its limit and its derivative is close to 0. Since training multiplies gradients across layers, near-zero gradients shrink the error signal until it is too small to learn from: the vanishing gradient problem.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 154
- Why don’t ReLUs suffer from the vanishing gradient problem?They are close to linear: for positive \(z\) the derivative is 1, not close to 0.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.1, p. 154
6.2 The XOR problem
- What did Minsky and Papert (1969) prove about single neural units?That a single unit (perceptron) cannot compute some very simple functions of its input, notably XOR.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, p. 154
- What is a perceptron?A simple neural unit with a binary output and a step-function activation: \(y = 1\) if \(w \cdot x + b > 0\), else 0.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, p. 154
- Give perceptron weights that compute AND and OR on binary inputs.AND: \(w_1 = w_2 = 1\), \(b = -1\). OR: \(w_1 = w_2 = 1\), \(b = 0\). (Many other settings work.)
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, p. 155
- Why can’t a perceptron compute XOR?A perceptron is a linear classifier: its decision boundary \(w_1x_1 + w_2x_2 + b = 0\) is a line. No line separates XOR’s positive cases (01, 10) from its negative cases (00, 11); XOR is not linearly separable.
Filled = output 1. Axes are x₁ and x₂. - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, p. 155
- What is a decision boundary in higher dimensions?A hyperplane separating the input space into the two output classes.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, p. 155
- How can a neural network compute XOR?With a layered network: e.g. two hidden ReLU units and one output unit (Goodfellow et al., 2016). The hidden layer maps inputs [0, 1] and [1, 0] to the same point, after which positive and negative cases are linearly separable.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, pp. 155–156
- What lesson does the XOR network teach about hidden layers?The hidden layer forms a new representation of the input in which the problem becomes easy. With backpropagation, networks learn such useful representations automatically.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.2, p. 156
6.3 Feedforward neural networks
- Why is the name multi-layer perceptron (MLP) a misnomer?Modern multilayer networks don’t use perceptrons (step functions) but units with non-linearities like ReLU or sigmoid; the name just stuck.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 157
- What three kinds of nodes does a simple feedforward network have?Input units, hidden units, and output units.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 157
- What does it mean that a layer is fully connected?Each unit takes input from all units of the previous layer: there is a link between every pair of units in adjacent layers.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 157
- What does element \(W_{ji}\) of a hidden layer’s weight matrix represent?The weight of the connection from input unit \(x_i\) to hidden unit \(h_j\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 157
- How is a hidden layer computed in matrix form, and what are the shapes?\(h = \sigma(Wx + b)\), with the activation applied element-wise. For input \(x \in \mathbb{R}^{n_0}\) and \(n_1\) hidden units: \(W\) is \([n_1 \times n_0]\), \(b, h \in \mathbb{R}^{n_1}\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 158
- How does the output layer of a classification network produce probabilities?It multiplies the hidden representation by a weight matrix, \(z = Uh\) (\(U\) of shape \([n_2 \times n_1]\)), then normalizes with softmax: \(y = \text{softmax}(z)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 159
- In what sense is a one-hidden-layer network like multinomial logistic regression?It builds a hidden representation \(h\) of the input and runs multinomial logistic regression on it. But the features in \(h\) are induced by the network rather than designed by hand, layers can be stacked, and various activations are used.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 159
- Write the equations of a 2-layer feedforward classifier.\(h = \sigma(Wx + b)\), \(z = Uh\), \(y = \text{softmax}(z)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 159
- How are layers counted in a feedforward network?The input layer is not counted, the output layer is. So a network with one hidden layer is a 2-layer network, and logistic regression is a 1-layer network.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 159
- How is the forward pass of an \(n\)-layer network computed, in layer notation?With \(a^{[0]} = x\): for \(i = 1 \ldots n\), \(z^{[i]} = W^{[i]}a^{[i-1]} + b^{[i]}\) and \(a^{[i]} = g^{[i]}(z^{[i]})\); output \(\hat{y} = a^{[n]}\). Intermediate \(g\) is usually ReLU or tanh, the last is softmax.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 160
- What are the logits of a network?The unnormalized scores \(z^{[n]}\) in the final layer, right before the softmax.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 160
- Why do neural networks need non-linear activation functions?Without them, stacked layers collapse into one: \(W^{[2]}(W^{[1]}x + b^{[1]}) + b^{[2]} = W'x + b'\). A multilayer linear network is just a single-layer network with different weights.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 160
- How can the bias be represented without an explicit bias vector?Add a dummy input \(x_0 = 1\) to each layer; its weight \(W_{j0}\) plays the role of the bias \(b_j\): \(h = \sigma(Wx)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.3, p. 161
6.4 Feedforward networks for classification
- How do you turn a logistic regression sentiment classifier with hand-built features into a neural one, and what does it gain?Add a hidden layer between the features and the softmax output: \(h = \sigma(Wx + b)\), \(z = Uh\), \(\hat{y} = \text{softmax}(z)\). The hidden layer can represent non-linear interactions between features.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.4, p. 162
- How are a whole test set’s outputs computed with a feedforward network?Vectorize: stack inputs as rows of \(X\) \([m \times d]\), then \(H = \sigma(XW^\top + b)\), \(Z = HU^\top\), \(\hat{Y} = \text{softmax}(Z)\), with output \([m \times d_o]\). Always keep track of matrix shapes, since orderings like \(WX\) vs \(XW\) vary.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.4, p. 163
6.5 Embeddings as input to neural classifiers
- What is the difference between static and contextual embeddings as network inputs?A static embedding is a fixed vector per token, trained once and looked up in a dictionary. A contextual embedding differs for each occurrence and is learned by the network as part of the task (e.g. language modeling).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 163
- What is the embedding matrix \(E\)?The dictionary of embeddings: one row per vocabulary token, shape \([|V| \times d]\). Inputs are converted to token indices, and the corresponding rows are selected.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 164
- How is selecting an embedding equivalent to a matrix multiplication?Represent the token as a one-hot vector of shape \([1 \times |V|]\); multiplying it by \(E\) selects that token’s row. A whole sequence is an \([N \times |V|]\) one-hot matrix times \(E\), giving \([N \times d]\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 164
- What are the two common ways of passing \(N\) token embeddings to a classifier?Concatenation into one long \([1 \times Nd]\) vector (keeps order and all information, but needs a big network), or pooling into a single \(d\)-dimensional vector (smaller and efficient, loses order).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, pp. 164–165
- Why is pooling appropriate for sentiment classification?For sentiment, the identity of words (e.g. great) matters more than their exact position.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 165
- What are mean-pooling and max-pooling?Mean-pooling averages the embeddings: \(x_{mean} = \frac{1}{N}\sum_i e(w_i)\). Max-pooling takes, for each dimension, the element-wise maximum over all the inputs.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 165
- What are the advantages and disadvantages of neural language models compared with n-gram models?Advantages: much longer histories, better generalization over contexts of similar words, far more accurate prediction. Disadvantages: slower, more complex, vast energy to train, less interpretable; n-grams may still suit small tasks.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 165
- What does a feedforward neural language model approximate?Like an n-gram model, \(P(w_t \mid w_{1:t-1}) \approx P(w_t \mid w_{t-N+1:t-1})\), but representing the previous words by embeddings rather than by their identities.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 166
- Why can a neural LM predict fed after “…that the dog gets” having only seen “…that the cat gets fed”?Cat and dog have similar embeddings, so the network generalizes from the cat context. An n-gram model, which only knows word identities, cannot.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, pp. 166–167
- Why does a feedforward LM concatenate rather than pool its input embeddings?To predict the next token it helps to know each previous token and their order, which pooling would lose.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, p. 167
- Write the equations of a feedforward neural LM with a 3-word window.\(e = [Ex_{t-3}; Ex_{t-2}; Ex_{t-1}]\) (concatenation), \(h = \sigma(We + b)\), \(z = Uh\), \(\hat{y} = \text{softmax}(z)\): a distribution over all \(|V|\) possible next tokens.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.5, pp. 167–168
6.6 Training neural nets
- What three things does training a neural network require?A loss function (cross-entropy), an optimization algorithm (gradient descent), and a way to compute the gradient of the loss with respect to every parameter (error backpropagation).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 168
- What loss is used for a neural classifier?Cross-entropy: binary \(-[y\log\hat{y} + (1-y)\log(1-\hat{y})]\) with a sigmoid output; for \(K\) classes, \(-\log\hat{y}_c\) for the correct class \(c\), i.e. the negative log likelihood.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 168
- Why can’t we just use the logistic regression gradient for a deep network?It only gives correct updates for the last weight layer; the loss is computed at the end, but we need derivatives with respect to weights deep in early layers.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 170
- What is error backpropagation?The algorithm (Rumelhart et al., 1986) for computing gradients in deep networks; it is an instance of backward differentiation on computation graphs.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 170
- What is a computation graph?A representation of a mathematical computation broken down into separate operations, each a node, with edges carrying outputs to the next operations.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 170
- Run the forward pass of \(L(a, b, c) = c(a + 2b)\) at \(a = 3\), \(b = 1\), \(c = -2\).\(d = 2b = 2\), \(e = a + d = 5\), \(L = c \cdot e = -10\).
Black: forward values. Orange: gradients from the backward pass. - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, pp. 170–171
- What is the intuition of backward differentiation?Pass gradients back from the final node: each node multiplies the upstream gradient it receives by its local gradient (of its output with respect to each input) to get the downstream gradient for earlier nodes (chain rule).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 171
- Compute \(\partial L/\partial a\), \(\partial L/\partial b\), \(\partial L/\partial c\) for \(L = c(a + 2b)\) at \(a = 3, b = 1, c = -2\).\(\partial L/\partial c = e = 5\). \(\partial L/\partial e = c = -2\), so \(\partial L/\partial a = \frac{\partial L}{\partial e}\frac{\partial e}{\partial a} = -2 \cdot 1 = -2\) and \(\partial L/\partial b = \frac{\partial L}{\partial e}\frac{\partial e}{\partial d}\frac{\partial d}{\partial b} = -2 \cdot 1 \cdot 2 = -4\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, pp. 171–172
- Why is the forward pass needed before the backward pass?It computes the intermediate values (like \(d\) and \(e\)) that the local derivatives depend on.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 172
- What are the derivatives of the sigmoid, tanh and ReLU?\(\sigma'(z) = \sigma(z)(1 - \sigma(z))\); \(\tanh'(z) = 1 - \tanh^2(z)\); ReLU’(z) = 0 for \(z < 0\), 1 for \(z \ge 0\) (undefined at 0, by convention 1).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, pp. 173–174
- For a sigmoid output with cross-entropy loss, what is \(\partial L/\partial z\) at the output?\(a^{[2]} - y\): \(\frac{\partial L}{\partial a}\frac{\partial a}{\partial z} = \big(-\frac{y}{a} + \frac{1-y}{1-a}\big)a(1-a) = a - y\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 174
- How should neural network weights and inputs be initialized and prepared, compared with logistic regression?Network training is non-convex. Weights must be initialized with small random numbers (not 0, as in logistic regression), and inputs are normalized to zero mean and unit variance.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, pp. 174–175
- What is dropout?A regularization method: during training, at each update, each unit’s output is set to zero with probability \(p\) (and the rest renormalized), randomly dropping units and their connections.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 175
- Name typical neural network hyperparameters, and how they are set.Learning rate, mini-batch size, architecture (number of layers, hidden units per layer, activation functions), regularization (e.g. dropout rate), optimizer variant (e.g. Adam). They are tuned on a devset, not learned by gradient descent.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 175
- What role do frameworks like PyTorch and TensorFlow play?They build networks as computation graphs, making gradient computation and parallelization on GPUs easy.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, §6.6, p. 175
Historical notes
- Trace the early history of neural networks up to their decline.McCulloch–Pitts neuron (1943); perceptron research in the late 1950s–60s (Rosenblatt at Cornell, Widrow at Stanford, who turned the threshold into a bias); then decline after Minsky & Papert (1969) showed a perceptron can’t model XOR.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, Historical notes, p. 176
- What revived neural networks in the 1980s, and what ideas came from that period?Practical tools for deeper networks, especially backpropagation (Rumelhart et al., 1986). The connectionist / parallel distributed processing movement contributed distributed representations, recurrent networks and tensors for compositionality.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, Historical notes, p. 176
- When and why did the term “deep learning” arise?In the early 2000s, when better hardware and optimization techniques made it possible to train much larger and deeper networks (Hinton et al., 2006; Bengio et al., 2007).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 6, Historical notes, p. 176
Source
- Daniel Jurafsky and James H. Martin. Speech and Language Processing. 3rd edition (draft). https://web.stanford.edu/~jurafsky/slp3/