-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtest_tensorflow.py
More file actions
84 lines (65 loc) · 2.7 KB
/
test_tensorflow.py
File metadata and controls
84 lines (65 loc) · 2.7 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
import unittest
import os.path
import numpy as np
import tensorflow as tf
from common import gpu_test
class TestTensorflow(unittest.TestCase):
def test_addition(self):
result = tf.add([1, 2], [3, 4])
self.assertEqual([2], result.shape)
def test_conv2d(self):
input = tf.random.normal([1,2,2,1])
filter = tf.random.normal([1,1,1,1])
result = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
self.assertEqual(4, len(result.shape))
def test_tf_keras(self):
x_train = np.random.random((100, 28, 28))
y_train = np.random.randint(10, size=(100, 1))
x_test = np.random.random((20, 28, 28))
y_test = np.random.randint(10, size=(20, 1))
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1)
result = model.evaluate(x_test, y_test)
self.assertEqual(2, len(result))
# exercices pydot path.
tf.keras.utils.plot_model(model, to_file="tf_plot_model.png")
self.assertTrue(os.path.isfile("tf_plot_model.png"))
def test_lstm(self):
x_train = np.random.random((100, 28, 28))
y_train = np.random.randint(10, size=(100, 1))
x_test = np.random.random((20, 28, 28))
y_test = np.random.randint(10, size=(20, 1))
model = tf.keras.Sequential([
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, input_shape=(28, 28))),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1)
result = model.evaluate(x_test, y_test)
self.assertEqual(2, len(result))
@gpu_test
def test_gpu(self):
with tf.device('/gpu:0'):
m1 = tf.constant([2.0, 3.0], shape=[1, 2], name='a')
m2 = tf.constant([3.0, 4.0], shape=[2, 1], name='b')
result = tf.matmul(m1, m2)
self.assertEqual([1, 1], result.shape)
@gpu_test
def test_is_built_with_cuda(self):
self.assertTrue(tf.test.is_built_with_cuda())
@gpu_test
def test_is_gpu_available(self):
self.assertTrue(tf.test.is_gpu_available(cuda_only=True))