forked from sihyeong671/Deep-Learning-from-Scratch3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step04.py
44 lines (36 loc) · 837 Bytes
/
step04.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
import numpy as np
class Variable:
def __init__(self, data):
self.data=data
class Function:
def __call__(self, input):
x=input.data
y=self.forward(x)
output=Variable(y)
return output
def forward(self, x):
raise NotImplementedError()
class Square(Function):
def forward(self, x):
return x**2
class Exp(Function):
def forward(self, x):
return np.exp(x)
def numerical_diff(f, x, eps=1e-4):
x0 = Variable(x.data - eps)
x1 = Variable(x.data + eps)
y0 = f(x0)
y1 = f(x1)
return (y1.data - y0.data)/(2*eps)
f = Square()
x = Variable(np.array(2.0))
dy = numerical_diff(f, x)
print(dy)
def f(x):
A = Square()
B = Exp()
C = Square()
return C(B(A(x)))
x = Variable(np.array(0.5))
dy = numerical_diff(f, x)
print(dy)