-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_dropout.py
49 lines (36 loc) · 1.18 KB
/
test_dropout.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
import unittest
import numpy as np
import dezero
from dezero import Variable
import dezero.functions as F
from dezero.utils import gradient_check, array_equal
class TestDropout(unittest.TestCase):
def test_forward1(self):
x = np.random.randn(100, 100)
y = F.dropout(Variable(x), dropout_ratio=0.0)
res = array_equal(y.data, x)
self.assertTrue(res)
def test_forward2(self):
x = np.random.randn(100, 100)
with dezero.test_mode():
y = F.dropout(x)
res = array_equal(y.data, x)
self.assertTrue(res)
def test_backward1(self):
x_data = np.random.randn(10, 10)
def f(x):
np.random.seed(0)
return F.dropout(x, 0.5)
self.assertTrue(gradient_check(f, x_data))
def test_backward2(self):
x_data = np.random.randn(10, 20)
def f(x):
np.random.seed(0)
return F.dropout(x, 0.99)
self.assertTrue(gradient_check(f, x_data))
def test_backward3(self):
x_data = np.random.randn(10, 10)
def f(x):
np.random.seed(0)
return F.dropout(x, 0.0)
self.assertTrue(gradient_check(f, x_data))