-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
step27.py
executable file
·53 lines (41 loc) · 1.01 KB
/
step27.py
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
if '__file__' in globals():
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
import math
from dezero import Variable, Function
from dezero.utils import plot_dot_graph
class Sin(Function):
def forward(self, x):
y = np.sin(x)
return y
def backward(self, gy):
x = self.inputs[0].data
gx = gy * np.cos(x)
return gx
def sin(x):
return Sin()(x)
x = Variable(np.array(np.pi / 4))
y = sin(x)
y.backward()
print('--- original sin ---')
print(y.data)
print(x.grad)
def my_sin(x, threshold=0.0001):
y = 0
for i in range(100000):
c = (-1) ** i / math.factorial(2 * i + 1)
t = c * x ** (2 * i + 1)
y = y + t
if abs(t.data) < threshold:
break
return y
x = Variable(np.array(np.pi / 4))
y = my_sin(x) # , threshold=1e-150)
y.backward()
print('--- approximate sin ---')
print(y.data)
print(x.grad)
x.name = 'x'
y.name = 'y'
plot_dot_graph(y, verbose=False, to_file='my_sin.png')