Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
Lecture 6: Math Review II
1
Lecture 6: Math Review II Justin Johnson EECS 442 WI 2020: Lecture - - PowerPoint PPT Presentation
Lecture 6: Math Review II Justin Johnson EECS 442 WI 2020: Lecture 6 - 1 January 28, 2020 Administrative HW0 due tomorrow , 1/29 11:59pm HW1 due 1 week from tomorrow , 2/5 11:59pm Justin Johnson EECS 442 WI 2020: Lecture 6 - 2
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
1
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
2
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
3
S Exponent Fraction
8 bits 2127 ≈ 1038 23 bits ≈ 7 decimal digits
S
Exponent Fraction
11 bits 21023 ≈ 10308 52 bits ≈ 15 decimal digits IEEE 754 Single Precision / Single / float32 IEEE 754 Double Precision / Double / float64
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
4
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
5
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
6
Horizontally concatenate n, m-dim column vectors and you get a mxn matrix A (here 2x3)
(scalar) lowercase undecorated a (vector) lowercase bold or arrow
(matrix) uppercase bold
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
7
Horizontally concatenate n, m-dim column vectors and you get a mxn matrix A (here 2x3)
Watch out: In math, it’s common to treat D-dim vector as a Dx1 matrix (column vector); In numpy these are different things
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
8
Vertically concatenate m, n-dim row vectors and you get a mxn matrix A (here 2x3)
/
/
Transpose: flip rows / columns
/
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
9
Linear combination of columns of A
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
10
𝑼𝒚
Dot product between rows of A and x
𝑼𝒚
𝑼
𝑼
3 3
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
11
𝑼
𝑼
Yes – in A, I’m referring to the rows, and in B, I’m referring to the columns
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
12
𝑼
𝑼
𝑼𝒄𝟐
𝑼𝒄𝒒
𝑼 𝒄𝟐
𝑼 𝒄𝒒
𝑼𝒄𝒌
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
13
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
14
convolution (which we’ll cover later)
math linear algebra class
space (Ax)
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
15
Suppose someone hands you this matrix. What’s wrong with it?
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
16
Typical way to change the contrast is to apply a nonlinear correction pixelvalueT The quantity 𝛿 controls how much contrast gets added
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
17
10% 50% 90% Now the darkest regions (10th pctile) are much darker than the moderately dark regions (50th pctile). new 10% new 50% new 90%
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 - 18
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 - 19
Phew! Much Better.
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
20
imNew = im**4 Python+Numpy (right way): Python+Numpy (slow way – why? ): imNew = np.zeros(im.shape) for y in range(im.shape[0]): for x in range(im.shape[1]): imNew[y,x] = im[y,x]**expFactor
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
21
“Hadamard Product” / Element-wise multiplication
Element-wise division
Z
Element-wise power – beware notation
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 - 22
Suppose have Nx2 matrix A
ND col. vec.
Ha$ '
Ha$ '
2D row vec
Note – libraries distinguish between N-D column vector and Nx1 matrix.
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
23
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
24
If you want to be pedantic and proper, you expand e by multiplying a matrix of 1s (denoted 1) Many smart matrix libraries do this automatically. This is the source of many bugs.
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
25
Given: a nx2 matrix P and a 2D column vector v, Want: nx2 difference matrix D
Blue stuff is assumed / broadcast
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
26
Suppose we have numpy arrays x and y. How will they broadcast?
For example: x: (10,) y: (20, 10)
with ones until they have the same number of dimensions For example: x: (10,) y: (20, 10) à x: (1, 10) y: (20, 10)
(a) Dimension match. Everything is good (b) Dimensions don’t match, but one is =1. ”Duplicate” the smaller array along that axis to match (c) Dimensions don’t match, neither are =1. Error!
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
27
x = np.ones(10, 20) y = np.ones(20) z = x + y print(z.shape) x = np.ones(10, 20) y = np.ones(10, 1) z = x + y print(z.shape) x = np.ones(10, 20) y = np.ones(10) z = x + y print(z.shape) x = np.ones(1, 20) y = np.ones(10, 1) z = x + y print(z.shape) (10,20) ERROR (10,20) (10,20)
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
28
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
29
x = np.ones(30) y = np.ones(20, 1) z = np.ones(10, 1, 1) w = x + y + z print(w.shape) (10, 20, 30) The same broadcasting rules apply to tensors with any number of dimensions!
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
30
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
31
dimensional vector
between {x1, …, xN} and {y1, …, yM} so I can find, for every xi the nearest yj
𝒚 * + 𝒛 * − 2𝒚/𝒛
= 𝒚 * + 𝒛 * − 2𝒚/𝒛 $/*
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
32
𝒀 = − 𝒚$ − ⋮ − 𝒚k − 𝒁 = − 𝒛$ − ⋮ − 𝒛m − 𝒀𝒁𝑼
HI = 𝒚𝒋 𝑼𝒛𝒌
𝒁𝑼 = | | 𝒛$ ⋯ 𝒛m | | 𝚻 𝒀𝟑, 𝟐 = 𝒚𝟐
𝟑
⋮ 𝒚𝑶
𝟑
Compute a Nx1 vector
(can also do Mx1) Compute a NxM matrix
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
33
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
𝒚𝟐
𝟑
⋮ 𝒚𝑶
𝟑
+ 𝒛$
𝟑
⋯ 𝒛m
𝟑
Σ 𝒀*, 1 + Σ 𝒁*, 1 /
HI =
𝒚H
* + 𝒛I *
𝒚𝟐
* + 𝒛𝟐 *
⋯ 𝒚𝟐
* + 𝒛𝑵 *
⋮ ⋱ ⋮ 𝒚𝑶
* + 𝒛𝟐 *
⋯ 𝒚𝑶
* + 𝒛𝑵 *
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
34
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
35
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
36
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
37
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
38
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
39
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
40
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
Get in the habit of thinking about shapes as tuples. Suppose X is (N, D), Y is (M, D):
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
41
𝐄HI = 𝒚𝒋
* + 𝒛𝒌 * + 2𝒚𝑼𝒛
Numpy code: XNorm = np.sum(X**2,axis=1,keepdims=True) YNorm = np.sum(Y**2,axis=1,keepdims=True) D = (XNorm+YNorm.T-2*np.dot(X,Y.T))**0.5
𝐄 = Σ 𝒀𝟑, 1 + Σ 𝒁𝟑, 1
𝑼 − 2𝒀𝒁𝑼 $/*
*May have to make sure this is at least 0 (sometimes roundoff issues happen)
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
42
Computing pairwise distances between 300 and 400 128-dimensional vectors
distance: 0.8s
faster than 2) Expressing things in primitives that are optimized is usually faster Even more important with special hardware like GPUs or TPUs!
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
43
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
44
𝒛 = −2 1 = 1 2 𝒃 − 1 3 𝒄 𝒚 = 4 =
𝒃 = 2 𝒄 = 6 𝒅 = 5
A set of vectors is linearly independent if you can’t write one as a linear combination of the others.
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
45
Span: all linear combinations of a set
Span({ }) = Span({[0,2]}) = ? All vertical lines through origin = 𝜇 0,1 : 𝜇 ∈ 𝑆 Is blue in {red}’s span?
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
46
Span: all linear combinations of a set
Span({ , }) = ?
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
47
Span: all linear combinations of a set
Span({ , }) = ?
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
48
Right-multiplying A by x mixes columns of A according to entries of x
the span of the columns of A.
columns
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
49
y1 y2 y3
x1 x2 x3
x – knobs on machine (e.g., fuel, brakes) y – state of the world (e.g., where you are) A – machine (e.g., your car)
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
50
Suppose the columns of 3x3 matrix A are not linearly independent (c1, αc1, c2 for instance)
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
51
Knobs of x are redundant. Even if y has 3 outputs, you can only control it in two directions
y1 y2 y3
x1 x2 x3
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
52
𝑩𝒚 = 𝑦$ + 𝛽𝑦* 𝒅𝟐 + 𝑦+𝒅𝟑
s.t. y =Ax
(assuming 𝒅𝟐 and 𝒅𝟐have dimension >= 3) 𝒛 = 𝑩 𝑦$ + 𝛾 𝑦* − 𝛾/𝛽 𝑦+
𝛾 to x1 and subtracting
~
= 𝑦$ + 𝛾 + 𝛽𝑦* − 𝛽 𝛾 𝛽 𝑑$ + 𝑦+𝑑*
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
53
𝑩𝒚 = 𝑦$ + 𝛽𝑦* 𝒅𝟐 + 𝑦+𝒅𝟑
to a zero-vector y
𝒛 = 𝑩 𝛾 −𝛾/𝛽 = 𝛾 − 𝛽 𝛾 𝛽 𝒅𝟐 + 0𝒅𝟑
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
54
independent columns (or rows) of A / the dimension of the span of the columns
can be inverted, span the full output space, are
knob is useful and every output state can be made by the machine
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
55
able to invert this mapping.
produced it?
compute it. Solving for x is much faster and stable than obtaining A-1. Bad: y = np.linalg.inv(A).dot(y) Good: y = np.linalg.solve(A, y)
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
56
𝑩HI = 𝑩IH
properties
Any matrix of the form 𝑩 = 𝒀𝑼𝒀 is symmetric. Quick check: 𝑩𝑼 = 𝒀𝑼𝒀
𝑼
𝑩𝑼 = 𝒀𝑼 𝒀𝑼 𝑼 𝑩𝑼 = 𝒀𝑼𝒀
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
57
𝑠
$$
𝑠
$*
𝑠
$+
𝑠
*$
𝑠
**
𝑠
*+
𝑠
+$
𝑠
+*
𝑠
++
change vector L2 norms ( 𝑺𝒚 * = 𝒚 *)
flip/reflection), eigenvalues are 1
Justin Johnson January 28, 2020 EECS 442 WI 2020: Lecture 6 -
58