Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit ddf0473

Browse files
committed
Add tanh test case
1 parent 281426b commit ddf0473

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

ideep4py/tests/mm/test_tanh.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy
2+
from chainer import testing
3+
import ideep4py
4+
5+
# x = numpy.ndarray(shape=(1,32,224,224), dtype=numpy.float32, order='C')
6+
x = numpy.random.uniform(-1, 1, (1, 32, 2, 224)).astype(numpy.float32)
7+
y = numpy.tanh(x)
8+
9+
mx = ideep4py.mdarray(x)
10+
x2 = numpy.array(mx)
11+
testing.assert_allclose(x, x2)
12+
13+
print("tanh fwd")
14+
my = ideep4py._ideep4py.tanh.Forward(mx)
15+
y2 = numpy.array(my)
16+
testing.assert_allclose(y, y2)
17+
18+
# Test backward
19+
print("tanh bwd")
20+
x = numpy.random.uniform(-1, 1, (1, 32, 224, 224)).astype(numpy.float32)
21+
gy = numpy.random.uniform(-1, 1, (1, 32, 224, 224)).astype(numpy.float32)
22+
gx = gy * (1 - numpy.tanh(x) ** 2)
23+
24+
25+
mx = ideep4py.mdarray(x)
26+
mgy = ideep4py.mdarray(gy)
27+
mgx = ideep4py._ideep4py.tanh.Backward(mx, mgy)
28+
29+
gx1 = numpy.array(mgx)
30+
testing.assert_allclose(gx1, gx)

0 commit comments

Comments
 (0)