-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.py
More file actions
72 lines (51 loc) · 918 Bytes
/
math.py
File metadata and controls
72 lines (51 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
# MATRIX MULTIPLY
a = np.array([
[1, 4, 2],
[2, 4, 6],
[1, 7, 8],
[2, 5, 7]])
b = np.array([
[1, 4, 2, 5, 6],
[2, 4, 6, 2, 1],
[1, 7, 8, 1, 3]])
print(b.T.shape, a.T.shape)
print(np.matmul(b.T, a.T).shape)
'''
f(x) = 2x^2 + 3
f'(x) = 4x
g(x, y) = 2x^2 + xy + y^3
dgdx = 4x + y
dgdy = x + 3y^2
gradient = [
dgdx: 4x + y,
dgdy: x + 3y^2
]
'''
#GRADIENT DESCENT
lr = 0.01
x = 5
y = 7
f = 2 * x ** 2 + x * y + y ** 3
print(f)
for i in range(2000):
g = 2 * x ** 2 + x * y + y ** 3
print(g)
dfdx = 4 * x + y
dfdy = x + 2 * y
x += -dfdx * lr
y += -dfdy * lr
# CHAIN RULE
# set some inputs
x = -2
y = 5
z = -4
# perform the forward pass
q = x + y # q becomes 3
f = q * z # f becomes -12
dqdx = 1
dqdy = 1
dfdq = z
dfdz = q
dfdx = 1 * z
dfdy = 1 * z