lecture 5 math review i
play

Lecture 5: Math Review I Justin Johnson EECS 442 WI 2020: Lecture - PowerPoint PPT Presentation

Lecture 5: Math Review I Justin Johnson EECS 442 WI 2020: Lecture 5 - 1 January 23, 2020 Administrative HW0 due Wednesday 1/29 (1 week from yesterday) HW1 out yesterday, due Wednesday 2/5 (3 weeks from yesterday) Justin Johnson EECS 442


  1. Lecture 5: Math Review I Justin Johnson EECS 442 WI 2020: Lecture 5 - 1 January 23, 2020

  2. Administrative HW0 due Wednesday 1/29 (1 week from yesterday) HW1 out yesterday, due Wednesday 2/5 (3 weeks from yesterday) Justin Johnson EECS 442 WI 2020: Lecture 5 - 2 January 23, 2020

  3. Floating Point Arithmetic Justin Johnson EECS 442 WI 2020: Lecture 5 - 3 January 23, 2020

  4. This Lecture and Next: Math Two goals for the next two classes: • Math with computers ≠ Math • Practical math you need to know but may not have been taught Justin Johnson EECS 442 WI 2020: Lecture 5 - 4 January 23, 2020

  5. This Lecture and Next: Goal • Not a “Linear algebra in two lectures” – that’s impossible. • Some of this you should know! • Aimed at reviving your knowledge and plugging any gaps • Aimed at giving you intuitions Justin Johnson EECS 442 WI 2020: Lecture 5 - 5 January 23, 2020

  6. Adding Numbers • 1 + 1 = ? • Suppose 𝑦 " is normally distributed with mean 𝜈 and standard deviation 𝜏 for 1 ≤ 𝑗 ≤ 𝑂 𝟐 𝑶 𝑶 ∑ 𝒋0𝟐 • How is the average, or ) 𝝂 = 𝒚 𝒋 , distributed (qualitatively), in terms of variance? • The Free Drinks in Vegas Theorem : 2 𝜈 has mean 𝜈 3 and standard deviation 4 . Justin Johnson EECS 442 WI 2020: Lecture 5 - 6 January 23, 2020

  7. Free Drinks in Vegas Each game/variable has mean $0.10, std $2 100K games is guaranteed profit: 99.999999% lowest value is $0.064. 100 games is $0.01 for drinks uncertain and $0.054 for profits fun! Justin Johnson EECS 442 WI 2020: Lecture 5 - 7 January 23, 2020

  8. Let’s make it big • What should happen qualitatively? • Theory says that the average is distributed with 5 678 ≈ 10 ;6 mean 31 and standard deviation • What will happen? • Reality: 17.47 Justin Johnson EECS 442 WI 2020: Lecture 5 - 8 January 23, 2020

  9. Trying it out Hmm. Hmm. Justin Johnson EECS 442 WI 2020: Lecture 5 - 9 January 23, 2020

  10. What is a number? 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 0 1 1 1 0 0 1 185 128 + 32 + 16 + 8 + 1 = 185 Justin Johnson EECS 442 WI 2020: Lecture 5 - 10 January 23, 2020

  11. Adding two numbers 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 0 1 1 1 0 0 1 185 0 1 1 0 1 0 0 1 105 1 0 0 1 0 0 0 1 0 34 Result Carry Flag “Integers” on a computer are integers modulo 2 k Justin Johnson EECS 442 WI 2020: Lecture 5 - 11 January 23, 2020

  12. Some Gotchas 32 + (3 / 4) x 40 = 32 Why? 32 + (3 x 40) / 4 = 62 Underflow No Underflow 32 + (3 / 4) x 40 = 32 + (3 x 40) / 4 = 32 + 0 x 40 = 32 + 120 / 4 = 32 + 0 = 32 + 30 = 32 62 Ok – you have to multiply before dividing Justin Johnson EECS 442 WI 2020: Lecture 5 - 12 January 23, 2020

  13. Some Gotchas Should be: 9x4=36 32 + (9 x 40) / 10 = 68 math 32 + (9 x 40) / 10 = 42 uint8 Overflow Why 104? 32 + 9 x 40 / 10 = 32 + 104 / 10 = 9 x 40 = 360 32 + 10 = 360 % 256 = 104 42 Justin Johnson EECS 442 WI 2020: Lecture 5 - 13 January 23, 2020

  14. What is a number? 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 0 1 1 1 0 0 1 185 How can we do fractions? 2 5 2 4 2 3 2 2 2 1 2 0 2 -1 2 -2 1 0 1 1 1 0 0 1 45.25 45 0.25 Justin Johnson EECS 442 WI 2020: Lecture 5 - 14 January 23, 2020

  15. Fixed-Point Arithmetic 2 5 2 4 2 3 2 2 2 1 2 0 2 -1 2 -2 1 0 1 1 1 0 0 1 45.25 What’s the largest number we can represent? 63.75 – Why? How precisely can we measure at 63? 0.25 How precisely can we measure at 0? 0.25 Fine for many purposes but for science, seems silly Justin Johnson EECS 442 WI 2020: Lecture 5 - 15 January 23, 2020

  16. Floating Point Numbers Fraction (F) Sign (S) Exponent (E) 1 0 1 1 1 0 0 1 1 7 1 1+1/8 = 1.125 -1 2 7-7 = 2 0 =1 𝟐 + 𝑮 −𝟐 𝑻 𝟑 𝑭@𝒄𝒋𝒃𝒕 𝟑 𝟒 Bias: allows exponent to be negative (bias = -127 for float32) Note: fraction = significant = mantissa; exponents of all ones or all zeros are special numbers Justin Johnson EECS 442 WI 2020: Lecture 5 - 16 January 23, 2020

  17. Floating Point Numbers Fraction -2 0 x 1.00 = -1 0 0 0 0/8 -2 0 x 1.125 = -1.125 0 0 1 1/8 Sign Exponent -2 0 x 1.25 = -1.25 0 1 0 2/8 1 0 1 1 1 … -1 7-7=0 -2 0 x 1.75 = -1.75 1 1 0 6/8 *(-bias)* -2 0 x 1.875 = -1.875 1 1 1 7/8 Justin Johnson EECS 442 WI 2020: Lecture 5 - 17 January 23, 2020

  18. Floating Point Numbers Fraction -2 2 x 1.00 = -4 0 0 0 0/8 -2 2 x 1.125 = -4.5 0 0 1 1/8 Sign Exponent -2 2 x 1.25 = -5 0 1 0 2/8 1 1 0 0 1 … -1 9-7=2 -2 2 x 1.75 = -7 1 1 0 6/8 *(-bias)* -2 2 x 1.875 = -7.5 1 1 1 7/8 Justin Johnson EECS 442 WI 2020: Lecture 5 - 18 January 23, 2020

  19. Floating Point Numbers Fraction Sign Exponent -2 0 x 1.00 = -1 0 0 0 1 0 1 1 1 -2 0 x 1.125 = -1.125 0 0 1 -2 2 x 1.00 = -4 0 0 0 1 1 0 0 1 -2 2 x 1.125 = -4.5 0 0 1 Gap between numbers is relative , not absolute Justin Johnson EECS 442 WI 2020: Lecture 5 - 19 January 23, 2020

  20. Adding Floating Point Numbers Sign Exponent Fraction -2 -1 x 1.00 = -0.5 1 0 1 1 0 0 0 0 -2 2 x 1.00 = -4 1 1 0 0 1 0 0 0 -2 2 x 1.125 = -4.5 1 1 0 0 1 0 0 1 Actual implementation is complex Justin Johnson EECS 442 WI 2020: Lecture 5 - 20 January 23, 2020

  21. Adding Floating Point Numbers Sign Exponent Fraction -2 -3 x 1.00 = -0.125 1 0 1 0 0 0 0 0 -2 2 x 1.00 = -4 1 1 0 0 1 0 0 0 -2 2 x 1.03125 = -4.125 -2 2 x 1.00 = -4 1 1 0 0 1 0 0 0 ? 1 0 0 1 0 0 1 -2 2 x 1.125 = -4.5 1 Justin Johnson EECS 442 WI 2020: Lecture 5 - 21 January 23, 2020

  22. Adding Floating Point Numbers Sign Exponent Fraction -2 -3 x 1.00 = -0.125 1 0 1 0 0 0 0 0 -2 2 x 1.00 = -4 1 1 0 0 1 0 0 0 -2 2 x 1.03125 = -4.125 -2 2 x 1.00 = -4 1 1 0 0 1 0 0 0 For a and b, these can happen a + b = a a+b-a ≠ b Justin Johnson EECS 442 WI 2020: Lecture 5 - 22 January 23, 2020

  23. Real Floating Point Numbers IEEE 754 Single Precision / Single / float32 8 bits 23 bits 2 127 ≈ 10 38 ≈ 7 decimal digits S Exponent Fraction IEEE 754 Double Precision / Double / float64 11 bits 52 bits 2 1023 ≈ 10 308 ≈ 15 decimal digits Exponent Fraction S Justin Johnson EECS 442 WI 2020: Lecture 5 - 23 January 23, 2020

  24. Real Floating Point Numbers IEEE 754 Half Precision / Half / float16 5 bits 10 bits 2 32 ≈ 10 9 ≈ 3 decimal digits S Exponent Fraction Brain Floating Point / bfloat16 8 bits 7 bits 2 127 ≈ 10 38 ≈ 2 decimal digits S Exponent Fraction Same range as FP32, but reduced precision Justin Johnson EECS 442 WI 2020: Lecture 5 - 24 January 23, 2020

  25. Trying it out Roundoff error occurs a+b=a -> numerator is stuck, denominator isn’t Justin Johnson EECS 442 WI 2020: Lecture 5 - 25 January 23, 2020

  26. Things to Remember • Computer numbers aren’t math numbers • Overflow, accidental zeros, roundoff error, and basic equalities are almost certainly incorrect for some values • Floating point defaults and numpy try to protect you . • Generally safe to use a double and use built-in- functions in numpy (not necessarily others!) • Spooky behavior = look for numerical issues Justin Johnson EECS 442 WI 2020: Lecture 5 - 26 January 23, 2020

  27. Vectors Justin Johnson EECS 442 WI 2020: Lecture 5 - 27 January 23, 2020

  28. Vectors [2,3] = 2 x [1,0] + 3 x [0,1] 2 x + 3 x 2 x e 1 + 3 x e 2 x = [2,3] Can be arbitrary # of dimensions (typically denoted R n ) Justin Johnson EECS 442 WI 2020: Lecture 5 - 28 January 23, 2020

  29. Scaling Vectors 2 x = [4,6] • Can scale vector by a scalar • Scalar = single number • Dimensions changed x = [2,3] independently • Changes magnitude / length , does not change direction . Justin Johnson EECS 442 WI 2020: Lecture 5 - 29 January 23, 2020

  30. Adding Vectors • Can add vectors • Dimensions changed independently • Order irrelevant • Can change direction and magnitude x = [2,3] x+y = [5,4] y = [3,1] Justin Johnson EECS 442 WI 2020: Lecture 5 - 30 January 23, 2020

  31. Scaling and Adding 2 x+y = [7,7] Can do both at the same x = [2,3] time y = [3,1] Justin Johnson EECS 442 WI 2020: Lecture 5 - 31 January 23, 2020

  32. Measuring Length Magnitude / length / (L2) norm of vector 5/G I G 𝒚 = 𝒚 G = H 𝑦 " " x = [2,3] There are other norms; assume L2 unless told otherwise 𝒚 G = 13 y = [3,1] 𝒛 G = 10 Why? Justin Johnson EECS 442 WI 2020: Lecture 5 - 32 January 23, 2020

  33. Normalizing a Vector Diving by norm gives x = [2,3] something on the unit sphere (all vectors with length 1) 𝒚 M = 𝒚/ 𝒚 𝟑 y = [3,1] 𝒛 M = 𝒛/ 𝒛 𝟑 Justin Johnson EECS 442 WI 2020: Lecture 5 - 33 January 23, 2020

  34. Dot Products I 𝑦 " 𝑧 " = 𝒚 𝑼 𝒛 𝒚 ⋅ 𝒛 = H "05 𝒚 ⋅ 𝒛 = cos 𝜄 𝒚 𝒛 What happens with 𝒚 M normalized / unit 𝜄 vectors? 𝒛 M Justin Johnson EECS 442 WI 2020: Lecture 5 - 34 January 23, 2020

  35. Dot Products I 𝒚 = [2,3] 𝒚 ⋅ 𝒛 = H 𝑦 " 𝑧 " " What’s 𝒚 ⋅ 𝒇 𝟐 , 𝒚 ⋅ 𝒇 𝟑 ? Ans: 𝒚 ⋅ 𝒇 𝟐 = 2 ; 𝒚 ⋅ 𝒇 𝟑 = 3 • Dot product is projection 𝒇 𝟐 • Amount of x that’s also pointing in direction of y 𝒇 𝟑 Justin Johnson EECS 442 WI 2020: Lecture 5 - 35 January 23, 2020

Recommend


More recommend