forked from tqchen/tinyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ops.py
More file actions
145 lines (130 loc) · 3.97 KB
/
test_ops.py
File metadata and controls
145 lines (130 loc) · 3.97 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import tinyflow as tf
import numpy as np
def check_ewise(ufunc):
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = ufunc(x, y)
ax = np.ones((2, 3))
ay = np.ones((2, 3)) * 4
sess = tf.Session()
az = sess.run(z, feed_dict={x:ax, y:ay})
np.testing.assert_almost_equal(az, ufunc(ax, ay))
def check_ewise_scalar(ufunc):
x = tf.placeholder(tf.float32)
y = 10;
z = ufunc(x, y)
ax = np.ones((2, 3))
sess = tf.Session()
az = sess.run(z, feed_dict={x:ax})
np.testing.assert_almost_equal(az, ufunc(ax, y))
def check_ewise_rscalar(ufunc):
x = 10;
y = tf.placeholder(tf.float32)
z = ufunc(x, y)
ay = np.ones((2, 3))
sess = tf.Session()
az = sess.run(z, feed_dict={y:ay})
np.testing.assert_almost_equal(az, ufunc(x, ay))
def test_ewise():
check_ewise(lambda x, y: x+y)
check_ewise(lambda x, y: x-y)
check_ewise(lambda x, y: x*y)
check_ewise(lambda x, y: x/y)
check_ewise(lambda x, y: x**y)
check_ewise_scalar(lambda x, y: x+y)
check_ewise_scalar(lambda x, y: x-y)
check_ewise_scalar(lambda x, y: x*y)
check_ewise_scalar(lambda x, y: x/y)
check_ewise_rscalar(lambda x, y: x-y)
check_ewise_rscalar(lambda x, y: x**y)
def test_exp():
x = tf.placeholder(tf.float32)
y = tf.exp(x)
ax = np.ones((2, 3)) * 2
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
np.testing.assert_almost_equal(ay, np.exp(ax))
def test_log():
x = tf.placeholder(tf.float32)
y = tf.log(x)
ax = np.ones((2, 3)) * 2
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
np.testing.assert_almost_equal(ay, np.log(ax))
def test_sqrt():
x = tf.placeholder(tf.float32)
y = tf.sqrt(x)
ax = np.ones((2, 3)) * 2
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
np.testing.assert_almost_equal(ay, np.sqrt(ax))
def test_softmax():
x = tf.placeholder(tf.float32)
y = tf.nn.softmax(x)
ax = np.ones((2, 4))
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
np.testing.assert_almost_equal(
ay, ax / np.sum(ax, axis=1, keepdims=True))
def test_matmul():
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
ax = np.ones((2, 3))
ay = np.ones((3, 4)) * 4
z = tf.matmul(x, y) * 4
sess = tf.Session()
az = sess.run(z, feed_dict={x:ax, y:ay})
np.testing.assert_almost_equal(
az, np.dot(ax, ay) * 4)
def test_sum():
axis = [1, 3]
x = tf.placeholder(tf.float32)
y = tf.reduce_sum(x, reduction_indices=axis)
ax = np.random.uniform(size=(2, 4, 8, 7))
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
npy = ax.sum(axis=tuple(axis))
assert(np.mean(np.abs(ay - npy))) < 1e-6
def test_mean():
axis = [1, 3]
x = tf.placeholder(tf.float32)
y = tf.reduce_mean(x, reduction_indices=axis)
ax = np.random.uniform(size=(2, 4, 8, 7))
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
npy = ax.mean(axis=tuple(axis))
assert(np.mean(np.abs(ay - npy))) < 1e-6
def test_argmax():
x = tf.placeholder(tf.float32)
y = tf.argmax(x, 1)
ax = np.random.uniform(size=(700, 10))
sess = tf.Session()
ay = sess.run(y, feed_dict={x:ax})
npy = np.argmax(ax, 1)
assert(np.mean(np.abs(ay - npy))) < 1e-6
def test_pad():
out_filter = 10
in_filter = 4
pad_width = (out_filter-in_filter)//2
x = tf.placeholder(tf.float32)
y = tf.pad(x, dim=1, pad=-pad_width)
z = tf.pad(y, dim=1, pad=pad_width)
nx = np.random.randn(100, 4, 28, 28)
npy = np.pad(nx, ((0, 0), (pad_width, pad_width), (0, 0), (0, 0)),
mode='constant', constant_values=0)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
ay = sess.run(z, feed_dict={x : nx})
assert(np.mean(np.abs(ay - npy))) < 1e-6
if __name__ == "__main__":
test_ewise()
test_exp()
test_log()
test_sqrt()
test_sum()
test_mean()
test_matmul()
test_softmax()
test_argmax()
test_pad()
pass