forked from WeltXing/PyDyNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautodiff.py
More file actions
31 lines (27 loc) · 696 Bytes
/
autodiff.py
File metadata and controls
31 lines (27 loc) · 696 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
import pydynet.functional as F
from pydynet.tensor import Tensor
import numpy as np
import matplotlib.pyplot as plt
try:
import seaborn as sns
sns.set()
except:
pass
x = Tensor(1., requires_grad=True)
lr = 2
x_list = []
y_list = []
for i in range(20):
x_list.append(x.data.copy())
y = F.log(F.square(x - 7) + 10)
y_list.append(y.data)
x.zero_grad()
y.backward()
x.data -= lr * x.grad
if i % 5 == 4:
print("Epoch {:2d}, y : {:.6f}".format(i + 1, y.data))
x = np.linspace(0, 10, 101)
plt.plot(x, np.log((x - 7)**2 + 10), label="$f(x)=\log((x-7)^2+10)$")
plt.scatter(x_list, y_list, color='orange')
plt.legend()
plt.savefig("../src/autodiff.png")