Skip to content

Commit 6472fd9

Browse files
committed
gradient descent exercise
1 parent f2438a1 commit 6472fd9

File tree

1 file changed

+25
-0
lines changed
  • linear_regression_class

1 file changed

+25
-0
lines changed

linear_regression_class/gd.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import matplotlib.pyplot as plt
2+
3+
lr = 1e-2
4+
x1 = 5
5+
x2 = -5
6+
7+
def J(x1, x2):
8+
return x1**2 + x2**4
9+
10+
def g1(x1):
11+
return 2*x1
12+
13+
def g2(x2):
14+
return 4*x2**3
15+
16+
values = []
17+
for i in range(1000):
18+
values.append(J(x1, x2))
19+
x1 -= lr * g1(x1)
20+
x2 -= lr * g2(x2)
21+
values.append(J(x1, x2))
22+
23+
print(x1, x2)
24+
plt.plot(values)
25+
plt.show()

0 commit comments

Comments
 (0)