BDS 761: Flashcards II¶

Flashcards Index¶

  1. Calculus · slides · review: Lecture 05
  2. Vector Calculus · slides · review: Lecture 06
  3. Information and Entropy · slides · review: Lecture 07
  4. Classification · slides · review: Lecture 08

Calculus¶

Question 1¶

Write the limit definition of the derivative $f'(x)$.

Answer 1¶

$$ f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}{h} $$

Question 2¶

Geometrically, what is $f'(a)$?

Answer 2¶

The slope of the tangent line to the graph $y=f(x)$ at $x=a$.

Question 3¶

If $f(t)$ is the position of a car at time $t$, what are $f'(t)$ and $f''(t)$?

Answer 3¶

$f'(t)$ is the velocity and $f''(t)$ is the acceleration.

Question 4¶

Write the equation of the tangent line to $y=f(x)$ at $x=a$.

Answer 4¶

$$ y=f(a)+f'(a)(x-a) $$

Question 5¶

Write the Taylor expansion of $f$ about $x=a$, up to the second-order term.

Answer 5¶

$$ f(x)\approx f(a)+f'(a)(x-a)+\tfrac12f''(a)(x-a)^2 $$

Question 6¶

How do you find candidate minima and maxima of a smooth $f(x)$?

Answer 6¶

Solve $f'(x)=0$. Then check each solution.

Question 7¶

Minimize $J(w)=w^2-2w+5$ by hand.

Answer 7¶

$J'(w)=2w-2=0$ gives $w=1$. Since $J''=2>0$, it is a minimum, with $J(1)=4$.

Question 8¶

Write the gradient descent update for $J(w)$ with step size $\eta$.

Answer 8¶

$$ w_{n+1}=w_n-\eta\,J'(w_n) $$

Question 9¶

Why is there a minus sign in the gradient descent update?

Answer 9¶

$J'(w)$ points uphill (the direction $J$ increases). Stepping the opposite way goes downhill.

Question 10¶

What happens if we choose the gradient descent step size $\eta$ too small or too large?

Answer 10¶

It is the learning rate. Too small: many slow steps. Too large: overshooting, oscillation, or divergence.

Question 11¶

Quiz 7: How does finding roots of $f(x)$ differ from minimizing $f(x)$ and how are they similar?

Answer 11¶

A root is an $x$ where $f(x)=0$ (the graph crosses zero). A minimum is an $x$ where $f$ is smallest, so $f'(x)=0$ there. Minimizing $f$ means finding a root of $f'$.

Question 12¶

For $f(x)=x^2-9$, find the roots and the minimum.

Answer 12¶

Roots: $x=\pm3$. Minimum: $f'(x)=2x=0$ at $x=0$, where $f(0)=-9$. These are different points.

Question 13¶

Write Newton's update for finding a root of $f(x)$.

Answer 13¶

$$ x_{n+1}=x_n-\frac{f(x_n)}{f'(x_n)} $$

Question 14¶

Where does Newton's update come from?

Answer 14¶

Replace $f$ by its tangent line at $x_n$, $\ f(x)\approx f(x_n)+f'(x_n)(x-x_n)$. Set the line to $0$ and solve for $x$.

Question 15¶

How do you use Newton's method to minimize $J(w)$ instead of finding its zeros?

Answer 15¶

Find a root of $J'$: $$ w_{n+1}=w_n-\frac{J'(w_n)}{J''(w_n)} $$

Question 16¶

Write ReLU and its derivative.

Answer 16¶

$\text{ReLU}(z)=\max(0,z)$. Its derivative is $0$ for $z<0$ and $1$ for $z>0$; it is undefined at $z=0$.

Question 17¶

What are $\text{ReLU}'(3)$ and $\text{ReLU}'(-2)$?

Answer 17¶

$1$ and $0$.

Question 18¶

What gradient or subgradient would you use for $|x|$?

Answer 18¶

For $x\ne0$, the derivative is $\text{sign}(x)=\pm1$. At $x=0$, the subdifferential is $[-1,1]$. Any value there works; $0$ is the usual choice.

Question 19¶

Write the centered finite-difference approximation of $f'(x)$.

Answer 19¶

$$ f'(x)\approx\frac{f(x+h)-f(x-h)}{2h} $$

Question 20¶

How many function evaluations does a centered-difference gradient of $f:\mathbb R^n\to\mathbb R$ need?

Answer 20¶

$2n$, two for each input variable. That is slow when $n$ is large or $f$ is expensive.

Question 21¶

What is a dual number?

Answer 21¶

A number $a+b\epsilon$, where $a$ and $b$ are real and $\epsilon\ne0$ but $\epsilon^2=0$.

Question 22¶

Compute $(2+3\epsilon)+(1+5\epsilon)$.

Answer 22¶

$3+8\epsilon$. Add the real parts and the $\epsilon$ parts.

Question 23¶

What does this print?

x = torch.tensor(3.0, requires_grad=True)
y = x**2
y.backward()
print(x.grad)

Answer 23¶

tensor(6.), since $dy/dx=2x=6$.

Vector Calculus¶

Question 1¶

What does $f:\mathbb R^n\to\mathbb R^m$ mean?

Answer 1¶

$f$ takes a vector of $n$ numbers and returns exactly one vector of $m$ numbers.

Question 2¶

Define the domain, codomain, and image of $f$.

Answer 2¶

Domain: the allowed inputs. Codomain: the output space. Image: the outputs actually reached, a subset of the codomain.

Question 3¶

Classify each as $\mathbb R^?\to\mathbb R^?$: $x^2$, $\ \mathbf a^T\mathbf x$, $\ (\cos t,\sin t)^T$, $\ \mathbf A\mathbf x$ with $\mathbf A$ of size $m\times n$.

Answer 3¶

$\mathbb R\to\mathbb R$; $\ \mathbb R^n\to\mathbb R$; $\ \mathbb R\to\mathbb R^2$; $\ \mathbb R^n\to\mathbb R^m$.

Question 4¶

Write the sum $x_1+\cdots+x_n$ as $\mathbf a^T\mathbf x$. What is $\mathbf a$?

Answer 4¶

The vector of all ones.

Question 5¶

Is $f(x)=2x+1$ linear?

Answer 5¶

No, it is affine. $f(0)=1\ne0$, and $f(x+y)=2x+2y+1\ne f(x)+f(y)$.

Question 6¶

Compute $\mathbf A\mathbf x$ for $\mathbf A=\begin{pmatrix}1&2&3\\4&5&6\end{pmatrix}$ and $\mathbf x=(1,0,-1)^T$. What kind of map is it?

Answer 6¶

$(1-3,\;4-6)^T=(-2,-2)^T$. It maps $\mathbb R^3\to\mathbb R^2$.

Question 7¶

Find $\partial f/\partial x$ and $\partial f/\partial y$ for $f(x,y)=x^2y+y+2$.

Answer 7¶

$\dfrac{\partial f}{\partial x}=2xy$ and $\dfrac{\partial f}{\partial y}=x^2+1$

Question 8¶

Evaluate the gradient of $f(x,y)=x^2y+y+2$ at $(3,4)$.

Answer 8¶

$\nabla f(3,4)=(2\cdot3\cdot4,\;9+1)^T=(24,\,10)^T$

Question 9¶

What is the gradient of a function of one variable?

Answer 9¶

Just the ordinary derivative $f'(x)$.

Question 10¶

Quiz 8.1: Differentiate $f(t)=\begin{pmatrix}t^2\\at+b\end{pmatrix}$ with respect to $t$.

Answer 10¶

$$ f'(t)=\begin{pmatrix}2t\\a\end{pmatrix} $$

Question 11¶

Differentiate $f(t)=(\cos t,\sin t)^T$. What is it at $t=0$?

Answer 11¶

$f'(t)=(-\sin t,\cos t)^T$, which is $(0,1)^T$ at $t=0$: the velocity of a point moving around the unit circle.

Question 12¶

Quiz 8.2: Find the total derivative of $f(x,y)=\begin{pmatrix}2x\\3x+y\end{pmatrix}$.

Answer 12¶

$$ \mathbf J=\begin{pmatrix}2&0\\3&1\end{pmatrix} $$ Row 1 is $(\partial_x,\partial_y)$ of $2x$; row 2 is the same for $3x+y$.

Question 13¶

Find the Jacobian of $\mathbf y=(x_1^2,\;x_1x_2)^T$.

Answer 13¶

$$ \begin{pmatrix}2x_1&0\\x_2&x_1\end{pmatrix} $$

Question 14¶

Quiz 8.3: What is $\dfrac{d}{d\mathbf x}(\mathbf A\mathbf x+\mathbf b)$?

Answer 14¶

$\mathbf A$. The constant $\mathbf b$ drops out, and the Jacobian is $m\times n$ like $\mathbf A$.

Question 15¶

Show that $\dfrac{d}{d\mathbf x}(\mathbf A\mathbf x)=\mathbf A$ for a $2\times2$ matrix by writing out the equations.

Answer 15¶

$f_1=A_{11}x_1+A_{12}x_2$ and $f_2=A_{21}x_1+A_{22}x_2$. Then $\partial f_i/\partial x_j=A_{ij}$, so the Jacobian is $\mathbf A$.

Question 16¶

What is the Jacobian of an elementwise activation $\boldsymbol\sigma(\mathbf x)=(\sigma(x_1),\ldots,\sigma(x_n))^T$?

Answer 16¶

The diagonal matrix $\text{diag}\big(\sigma'(x_1),\ldots,\sigma'(x_n)\big)$, because output $i$ depends only on input $i$.

Question 17¶

Apply ReLU to $\mathbf x=(1,-2,5)$. What are the output and its Jacobian?

Answer 17¶

The output is $(1,0,5)$ and the Jacobian is $\text{diag}(1,0,1)$.

Question 18¶

HW 7(a): For $2\times1$ vectors, write out $\mathbf a^T\mathbf x$ and find its gradient.

Answer 18¶

$a_1x_1+a_2x_2$, with partials $a_1$ and $a_2$. So $\nabla_{\mathbf x}(\mathbf a^T\mathbf x)=\mathbf a$, and the derivative (row) is $\mathbf a^T$.

Question 19¶

HW 7(c): For a $2\times2$ $\mathbf A$, write out $\mathbf x^T\mathbf A\mathbf x$ as a scalar polynomial.

Answer 19¶

$$ A_{11}x_1^2+(A_{12}+A_{21})\,x_1x_2+A_{22}x_2^2 $$

Question 20¶

HW 7(c), continued: Find the partial derivatives of $\mathbf x^T\mathbf A\mathbf x$ and write the gradient in matrix form.

Answer 20¶

$$ \frac{\partial}{\partial x_1}=2A_{11}x_1+(A_{12}+A_{21})x_2,\qquad \frac{\partial}{\partial x_2}=(A_{12}+A_{21})x_1+2A_{22}x_2 $$ Together, $\nabla_{\mathbf x}(\mathbf x^T\mathbf A\mathbf x)=(\mathbf A+\mathbf A^T)\,\mathbf x$.

Question 21¶

Find $\nabla_{\mathbf x}(\mathbf x^T\mathbf x)$. What is the 1D version?

Answer 21¶

$2\mathbf x$ (take $\mathbf A=\mathbf I$). In 1D it is $\dfrac{d}{dx}x^2=2x$.

Question 22¶

HW 7.2: Find the gradient of $\|\mathbf A\mathbf x-\mathbf y\|_2^2$ with respect to $\mathbf x$.

Answer 22¶

Expand: $$ (\mathbf A\mathbf x-\mathbf y)^T(\mathbf A\mathbf x-\mathbf y)=\mathbf x^T\mathbf A^T\mathbf A\mathbf x-2\,\mathbf y^T\mathbf A\mathbf x+\mathbf y^T\mathbf y $$ Then use the identities above ($\mathbf A^T\mathbf A$ is symmetric; $\mathbf y^T\mathbf A\mathbf x=(\mathbf A^T\mathbf y)^T\mathbf x$): $$ \nabla_{\mathbf x}=2\mathbf A^T\mathbf A\mathbf x-2\mathbf A^T\mathbf y=2\mathbf A^T(\mathbf A\mathbf x-\mathbf y) $$

Question 23¶

State the chain rule for $g(f(\mathbf x))$ when the derivatives are vectors or matrices.

Answer 23¶

$$ \frac{d}{d\mathbf x}g(f(\mathbf x))=\frac{dg}{df}\,\frac{df}{d\mathbf x} $$ A matrix product, in the order of the composition.

Question 24¶

Two linear layers: $\mathbf z=\mathbf W_1\mathbf x+\mathbf b_1$ and $\mathbf y=\mathbf W_2\mathbf z+\mathbf b_2$. What is $\dfrac{d\mathbf y}{d\mathbf x}$, and what is its shape?

Answer 24¶

$\mathbf W_2\mathbf W_1$. With $\mathbf W_1$ of size $h\times n$ and $\mathbf W_2$ of size $m\times h$, the product is $m\times n$.

Question 25¶

Compute $\dfrac{d\mathbf y}{d\mathbf x}$ for $\mathbf W_1=\begin{pmatrix}1&2\end{pmatrix}$ and $\mathbf W_2=\begin{pmatrix}3\\-1\end{pmatrix}$.

Answer 25¶

$$ \mathbf W_2\mathbf W_1=\begin{pmatrix}3&6\\-1&-2\end{pmatrix} $$

Information and Entropy¶

Question 1¶

Define the (self-)information of an outcome $x_i$.

Answer 1¶

$$ I(x_i)=-\log_2P(X=x_i) $$ measured in bits.

Question 2¶

How much information is in observing heads on a fair coin?

Answer 2¶

$-\log_2\tfrac12=1$ bit.

Question 3¶

Compute the information for outcomes with probability $1$, $\tfrac12$, $\tfrac14$, and $\tfrac18$.

Answer 3¶

$0$, $1$, $2$, and $3$ bits.

Question 4¶

Compute the information for outcomes with probability $0.9$ and $0.1$.

Answer 4¶

$-\log_2 0.9\approx0.152$ bits and $-\log_2 0.1\approx3.32$ bits. The rare outcome is the surprising one.

Question 5¶

What is the information of an outcome with probability $0$?

Answer 5¶

Infinite: $-\log_2 0=\infty$. An impossible outcome would be infinitely surprising.

Question 6¶

Why is information never negative?

Answer 6¶

$0\le P\le1$, so $\log_2P\le0$ and $-\log_2P\ge0$.

Question 7¶

Four independent fair coin flips give 0011. How much information is that?

Answer 7¶

$P=(\tfrac12)^4=\tfrac1{16}$, so $I=4$ bits, one per flip.

Question 8¶

Why does information add for independent events?

Answer 8¶

$I(A\cap B)=-\log\big(P(A)P(B)\big)=-\log P(A)-\log P(B)=I(A)+I(B)$

Question 9¶

An event has probability $\tfrac1{256}$. How much information does it carry, and what does that mean?

Answer 9¶

$-\log_2 2^{-8}=8$ bits. It resolves one of 256 equally likely possibilities, like 8 yes/no questions.

Question 10¶

Write the odds of event $A$.

Answer 10¶

$$ \text{odds}=\frac{P(A)}{1-P(A)}=\frac{P(A)}{P(\text{not }A)} $$

Question 11¶

Find the odds and the log-odds (base 2) for $P(A)=0.75$.

Answer 11¶

Odds $=0.75/0.25=3$. Log-odds $=\log_2 3\approx1.585$.

Question 12¶

Write the log-odds in terms of information.

Answer 12¶

$$ \log P(A)-\log P(\text{not }A)=I(\text{not }A)-I(A) $$ For $P(A)=0.75$: $\ 2-0.415=1.585$ bits.

Question 13¶

Define the Shannon entropy $H(X)$.

Answer 13¶

The expected information: $$ H(X)=\mathbb E[I(X)]=-\sum_xP(x)\log_2P(x) $$

Question 14¶

Compute the entropy of a fair coin.

Answer 14¶

$-\tfrac12\log_2\tfrac12-\tfrac12\log_2\tfrac12=1$ bit.

Question 15¶

Compute the entropy of a coin with $P(\text{heads})=0.9$.

Answer 15¶

$-0.9\log_2 0.9-0.1\log_2 0.1\approx0.137+0.332=0.469$ bits.

Question 16¶

Define the K-L divergence $D_{KL}(P\,\|\,Q)$.

Answer 16¶

$$ D_{KL}(P\,\|\,Q)=\sum_xP(x)\log\frac{P(x)}{Q(x)} $$ $P$ is the true (data) distribution and $Q$ is the model.

Question 17¶

A coin has $P=(0.9,\,0.1)$, but the model is $Q=(0.5,\,0.5)$. Compute $D_{KL}(P\,\|\,Q)$ in bits.

Answer 17¶

$$ 0.9\log_2\frac{0.9}{0.5}+0.1\log_2\frac{0.1}{0.5}\approx0.9(0.848)+0.1(-2.322)\approx0.531\text{ bits} $$ That is the average number of extra bits per flip from coding with the wrong distribution.

Question 18¶

Same coins: compute $D_{KL}(Q\,\|\,P)$. What does it show?

Answer 18¶

$$ 0.5\log_2\frac{0.5}{0.9}+0.5\log_2\frac{0.5}{0.1}\approx-0.424+1.161=0.737\text{ bits} $$ This differs from $0.531$: K-L divergence is not symmetric.

Question 19¶

What is $D_{KL}(P\,\|\,P)$? Can $D_{KL}$ be negative?

Answer 19¶

$0$. It is never negative, and it is $0$ only when $P=Q$.

Question 20¶

Is K-L divergence a distance metric?

Answer 20¶

No. It is nonnegative and $0$ only for $P=Q$, but it is not symmetric (and does not satisfy the triangle inequality).

Question 21¶

Define the cross-entropy $H(P,Q)$.

Answer 21¶

$$ H(P,Q)=-\sum_xP(x)\log Q(x) $$

Question 22¶

How are cross-entropy, entropy, and K-L divergence related?

Answer 22¶

$$ H(P,Q)=H(P)+D_{KL}(P\,\|\,Q) $$

Question 23¶

Check $H(P,Q)=H(P)+D_{KL}$ for $P=(0.9,0.1)$, $Q=(0.5,0.5)$.

Answer 23¶

$H(P,Q)=-0.9\log_2 0.5-0.1\log_2 0.5=1$ bit.

$H(P)+D_{KL}\approx0.469+0.531=1.0$ bit.

Question 24¶

A classifier gives probability $0.8$ to the true class of an example. What is the cross-entropy loss for that example?

Answer 24¶

The true distribution puts probability 1 on the true class, so the loss is $-\ln0.8\approx0.223$ nats ($0.322$ bits).

Question 25¶

Why is minimizing cross-entropy over $Q$ the same as minimizing $D_{KL}(P\,\|\,Q)$?

Answer 25¶

$H(P)$ does not depend on the model $Q$, so the two differ by a constant.

Question 26¶

What is t-SNE used for?

Answer 26¶

Visualizing high-dimensional data as a 2D or 3D embedding that keeps neighbors close.

Classification¶

Question 1¶

Classification or regression?

(a) predict a patient's blood pressure

(b) predict whether a patient is diabetic (yes/no)

(c) predict a patient's blood type (A, B, AB, O)

Answer 1¶

(a) regression: the output is a number.

(b) classification: two classes.

(c) classification: four classes.

Rule of thumb: predicting a number is regression; predicting a category is classification.

Question 2¶

A table has the columns age, BMI, smoker, has_disease. We want to predict has_disease.

Which columns are features? Which is the label?

Answer 2¶

Features (the inputs, $\mathbf x$): age, BMI, smoker.

Label (the answer we want to predict, $y$): has_disease.

Question 3¶

What do $\mathbf x$, $y$ and $\hat y$ stand for?

Answer 3¶

$\mathbf x$: the features of one example (a vector).

$y$: its true class.

$\hat y$ ("y-hat"): the class the model predicts. A hat means "estimate".

Question 4¶

What is the difference between binary and multiclass classification? Give one example of each.

Answer 4¶

Binary: exactly 2 classes, e.g. spam / not spam, sick / healthy.

Multiclass: 3 or more classes, e.g. blood type, or the digit 0-9 in an image.

Question 5¶

Why is classification called supervised learning?

Answer 5¶

The training data come with the correct labels $y$, like an answer key. The model learns the mapping from $\mathbf x$ to $y$ from these labeled examples.

Question 6¶

Of 40 patients whose temperature is near $x$, 30 are sick. Estimate $P(\text{sick})$ at this $x$.

Answer 6¶

$P(\text{sick})\approx\dfrac{30}{40}=0.75$

Question 7¶

There are two classes and $P(y=1\mid\mathbf x)=0.3$. What is $P(y=0\mid\mathbf x)$?

Answer 7¶

$1-0.3=0.7$. The probabilities of all classes add up to 1.

Question 8¶

Define the odds of an event that has probability $p$.

Answer 8¶

$$\text{odds}=\frac{p}{1-p}$$

The chance it happens divided by the chance it does not.

Question 9¶

Compute the odds for $p=0.75$, $p=0.5$ and $p=0.2$.

Answer 9¶

$0.75/0.25=3$ ("3 to 1")

$0.5/0.5=1$

$0.2/0.8=0.25$

Question 10¶

Define the logit of a probability $p$.

Answer 10¶

$$\text{logit}(p)=\ln\frac{p}{1-p}$$

The log of the odds, also called the log-odds.

Question 11¶

Write the formula for the sigmoid $\sigma(z)$.

Answer 11¶

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

Question 12¶

What is $\sigma(0)$?

Answer 12¶

$e^0=1$, so $\sigma(0)=\dfrac{1}{1+1}=0.5$.

Question 13¶

What is $\sigma(z)$ when $z$ is a large positive number? When $z$ is a large negative number?

Answer 13¶

Large positive $z$: $e^{-z}\approx 0$, so $\sigma(z)\approx 1$.

Large negative $z$: $e^{-z}$ is huge, so $\sigma(z)\approx 0$.

Question 14¶

Derive $\sigma'(z)$.

Answer 14¶

Chain rule on $(1+e^{-z})^{-1}$: $$ \sigma'(z)=\frac{e^{-z}}{(1+e^{-z})^2}=\sigma(z)\,\big(1-\sigma(z)\big) $$

Question 15¶

Write the logistic regression model for one feature $x$.

Answer 15¶

$$P(y=1\mid x)=\sigma(wx+b)=\frac{1}{1+e^{-(wx+b)}}$$

$w$ is the weight and $b$ the bias: compute the linear score $z=wx+b$, then apply the sigmoid.

Question 16¶

In logistic regression, what is the score $z=wx+b$?

Answer 16¶

The logit (log-odds) of class 1:

$$\ln\frac{p}{1-p}=wx+b$$

Logistic regression is a linear model for the log-odds.

Question 17¶

In logistic regression, what does a positive weight $w$ mean? A negative one?

Answer 17¶

$w>0$: larger $x$ makes class 1 more likely.

$w<0$: larger $x$ makes class 1 less likely.

$w=0$: $x$ has no effect.

Question 18¶

What is a loss function? Is it better when small or large?

Answer 18¶

A function that measures how wrong the model's predictions are. Smaller is better.

Question 19¶

Write the binary cross-entropy loss for one example with true label $y$ and prediction $p=P(y=1\mid\mathbf x)$.

Answer 19¶

$$L=-\big[y\ln p+(1-y)\ln(1-p)\big]$$

If $y=1$: $L=-\ln p$. If $y=0$: $L=-\ln(1-p)$.

The negative of the log of the probability the model gave to the true class.

Question 20¶

True label $y=1$. Compute the loss for $p=0.9$ and for $p=0.1$. ($\ln0.9\approx-0.105$, $\ln0.1\approx-2.30$)

Answer 20¶

$p=0.9$: $L=-\ln0.9\approx0.105$

$p=0.1$: $L=-\ln0.1\approx2.30$

The bad prediction costs about 20 times more.

Question 21¶

Write the softmax for scores $z_1,\dots,z_K$.

Answer 21¶

$$P(\text{class }k)=\frac{e^{z_k}}{\sum_{j=1}^{K}e^{z_j}}$$

Exponentiate each score (makes it positive), then divide by the total (makes them add up to 1).

Question 22¶

A model gets 85 of 100 test examples right. What is its accuracy?

Answer 22¶

$85/100=0.85$, or 85%.

Question 23¶

Define accuracy.

Answer 23¶

$$\text{accuracy}=\frac{\text{number of correct predictions}}{\text{total number of predictions}}$$

The fraction of examples where $\hat y=y$. In sklearn, the score method of a classifier reports it.

Question 24¶

Suppose our test data has 1000 emails, 20 of them spam and the rest are not spam.

Why is accuracy a bad metric for evaluating the classifier on this dataset?

Answer 24¶

The data is imbalanced. Guessing not spam every time gets 98% accuracy