|
| 1 | +#!/usr/local/bin/python |
| 2 | +from __future__ import division, print_function |
| 3 | + |
| 4 | +import unittest |
| 5 | +import numpy as np |
| 6 | +import tensorflow as tf |
| 7 | +from itertools import product |
| 8 | + |
| 9 | +from tfscripts import layers |
| 10 | + |
| 11 | + |
| 12 | +class TestConvModule(unittest.TestCase): |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.random_state = np.random.RandomState(42) |
| 16 | + |
| 17 | + def test_new_conv_nd_layers(self): |
| 18 | + """Test new_conv_nd_layers |
| 19 | + """ |
| 20 | + data = tf.constant(self.random_state.normal(size=[3, 4, 4, 4, 2]), |
| 21 | + dtype=tf.float32) |
| 22 | + |
| 23 | + shapes = [[3, 3, 3, 2, 3], [3, 3, 3, 3, 7], [3, 3, 3, 7, 1]] |
| 24 | + weights_list = [tf.constant(self.random_state.normal(size=shape), |
| 25 | + dtype=tf.float32) for shape in shapes] |
| 26 | + |
| 27 | + shapes = [[3], [7], [1]] |
| 28 | + biases_list = [tf.constant(self.random_state.normal(size=shape), |
| 29 | + dtype=tf.float32) for shape in shapes] |
| 30 | + |
| 31 | + pooling_strides_list = [[1, 1, 1, 1, 1], |
| 32 | + [1, 2, 2, 2, 1], |
| 33 | + [1, 2, 2, 2, 1]] |
| 34 | + pooling_ksize_list = [[1, 1, 1, 1, 1], |
| 35 | + [1, 2, 2, 2, 1], |
| 36 | + [1, 2, 2, 2, 1]] |
| 37 | + pooling_type_list = [None, 'max', 'max'] |
| 38 | + activation_list = ['elu', 'relu', ''] |
| 39 | + filter_size_list = [[3, 3, 3], [2, 0, 3], [3, 3, 3]] |
| 40 | + num_filters_list = [3, 7, 1] |
| 41 | + method_list = ['convolution', 'hex_convolution', 'convolution'] |
| 42 | + |
| 43 | + layer, weights, biases = layers.new_conv_nd_layers( |
| 44 | + data, |
| 45 | + filter_size_list=filter_size_list, |
| 46 | + num_filters_list=num_filters_list, |
| 47 | + pooling_type_list=pooling_type_list, |
| 48 | + pooling_strides_list=pooling_strides_list, |
| 49 | + pooling_ksize_list=pooling_ksize_list, |
| 50 | + activation_list=activation_list, |
| 51 | + method_list=method_list, |
| 52 | + weights_list=weights_list, |
| 53 | + biases_list=biases_list, |
| 54 | + verbose=False, |
| 55 | + ) |
| 56 | + |
| 57 | + result_true = [[[[[1.5821583]]]], |
| 58 | + [[[[1.8506659]]]], |
| 59 | + [[[[1.6045672]]]]] |
| 60 | + |
| 61 | + with tf.Session() as sess: |
| 62 | + result = sess.run(layer[-1]) |
| 63 | + |
| 64 | + self.assertTrue(np.allclose(result_true, result)) |
| 65 | + |
| 66 | + def test_new_fc_layers(self): |
| 67 | + """Test new_fc_layers |
| 68 | + """ |
| 69 | + data = tf.constant(self.random_state.normal(size=[3, 7]), |
| 70 | + dtype=tf.float32) |
| 71 | + |
| 72 | + shapes = [[7, 3], [3, 7], [7, 1]] |
| 73 | + weights_list = [tf.constant(self.random_state.normal(size=shape), |
| 74 | + dtype=tf.float32) for shape in shapes] |
| 75 | + |
| 76 | + shapes = [[3], [7], [1]] |
| 77 | + biases_list = [tf.constant(self.random_state.normal(size=shape), |
| 78 | + dtype=tf.float32) for shape in shapes] |
| 79 | + |
| 80 | + activation_list = ['elu', 'relu', ''] |
| 81 | + fc_sizes = [3, 7, 1] |
| 82 | + |
| 83 | + layer, weights, biases = layers.new_fc_layers( |
| 84 | + data, |
| 85 | + fc_sizes=fc_sizes, |
| 86 | + activation_list=activation_list, |
| 87 | + weights_list=weights_list, |
| 88 | + biases_list=biases_list, |
| 89 | + verbose=False, |
| 90 | + ) |
| 91 | + result_true = [[0.06657974], |
| 92 | + [-0.3617597], |
| 93 | + [0.00241985]] |
| 94 | + |
| 95 | + with tf.Session() as sess: |
| 96 | + result = sess.run(layer[-1]) |
| 97 | + |
| 98 | + self.assertTrue(np.allclose(result_true, result)) |
0 commit comments