|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import unittest |
| 4 | + |
1 | 5 | import tensorflow as tf |
2 | 6 | import tensorlayer as tl |
3 | 7 |
|
4 | | -x = tf.placeholder(tf.float32, [None, 100]) |
5 | | -n = tl.layers.InputLayer(x, name='in') |
6 | | -n = tl.layers.DenseLayer(n, n_units=80, name='d1') |
7 | | -n = tl.layers.DenseLayer(n, n_units=80, name='d2') |
8 | | -print(n) |
9 | | -n.print_layers() |
10 | | -n.print_params(False) |
11 | | -print(n.count_params()) |
12 | 8 |
|
13 | | -if n.count_params() != 14560: |
14 | | - raise Exception("params do not match") |
| 9 | +class Layer_Basic_Test(unittest.TestCase): |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + |
| 13 | + x = tf.placeholder(tf.float32, [None, 100]) |
| 14 | + n = tl.layers.InputLayer(x, name='in') |
| 15 | + n = tl.layers.DenseLayer(n, n_units=80, name='d1') |
| 16 | + n = tl.layers.DenseLayer(n, n_units=80, name='d2') |
| 17 | + |
| 18 | + n.print_layers() |
| 19 | + n.print_params(False) |
| 20 | + |
| 21 | + n2 = n[:, :30] |
| 22 | + n2.print_layers() |
| 23 | + |
| 24 | + cls.n_params = n.count_params() |
| 25 | + cls.shape_n = n.outputs.get_shape().as_list() |
| 26 | + cls.shape_n2 = n2.outputs.get_shape().as_list() |
| 27 | + cls.all_layers = n.all_layers |
| 28 | + cls.all_params = n.all_params |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def tearDownClass(cls): |
| 32 | + tf.reset_default_graph() |
| 33 | + |
| 34 | + def test_n_params(self): |
| 35 | + self.assertEqual(self.n_params, 14560) |
| 36 | + |
| 37 | + def test_shape_n(self): |
| 38 | + self.assertEqual(self.shape_n[-1], 80) |
15 | 39 |
|
16 | | -shape = n.outputs.get_shape().as_list() |
17 | | -if shape[-1] != 80: |
18 | | - raise Exception("shape do not match") |
| 40 | + def test_all_layers(self): |
| 41 | + self.assertEqual(len(self.all_layers), 2) |
19 | 42 |
|
20 | | -if len(n.all_layers) != 2: |
21 | | - raise Exception("layers do not match") |
| 43 | + def test_all_params(self): |
| 44 | + self.assertEqual(len(self.all_params), 4) |
22 | 45 |
|
23 | | -if len(n.all_params) != 4: |
24 | | - raise Exception("params do not match") |
| 46 | + def test_shape_n2(self): |
| 47 | + self.assertEqual(self.shape_n2[-1], 30) |
25 | 48 |
|
26 | | -for l in n: |
27 | | - print(l) |
28 | 49 |
|
29 | | -n2 = n[:, :30] |
30 | | -print(n2) |
31 | | -n2.print_layers() |
| 50 | +if __name__ == '__main__': |
32 | 51 |
|
33 | | -shape = n2.outputs.get_shape().as_list() |
34 | | -if shape[-1] != 30: |
35 | | - raise Exception("shape do not match") |
| 52 | + # tf.logging.set_verbosity(tf.logging.INFO) |
| 53 | + tf.logging.set_verbosity(tf.logging.DEBUG) |
36 | 54 |
|
37 | | -for l in n2: |
38 | | - print(l) |
| 55 | + unittest.main() |
0 commit comments