forked from hunkim/DeepLearningZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_square.py
34 lines (26 loc) · 1.01 KB
/
test_square.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
# https://www.tensorflow.org/api_guides/python/test
import tensorflow as tf
import numpy as np
class SquareTest(tf.test.TestCase):
def testSquare(self):
with self.test_session():
x = tf.square([2, 3])
self.assertAllEqual(x.eval(), [4, 9])
def testBroadcast(self):
with self.test_session():
hypothesis = np.array([[1], [2], [3]])
y = np.array([1, 2, 3])
print(hypothesis - y)
cost = tf.reduce_mean(tf.square(hypothesis - y))
self.assertNotEqual(cost.eval(), 0)
y = y.reshape(-1, 1)
print(y, hypothesis - y)
cost = tf.reduce_mean(tf.square(hypothesis - y))
self.assertAllEqual(cost.eval(), 0)
def testNormalize(self):
with self.test_session():
values = np.array([[10, 20], [1000, -100]], dtype=np.float32)
norm_values = tf.nn.l2_normalize(values, dim=1)
print(norm_values.eval())
if __name__ == '__main__':
tf.test.main()