-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtest_layers_merge.py
79 lines (56 loc) · 2.34 KB
/
test_layers_merge.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import unittest
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tests.utils import CustomTestCase
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
class Layer_Merge_Test(CustomTestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def test_concat(self):
class CustomModel(tl.models.Model):
def __init__(self):
super(CustomModel, self).__init__()
self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1')
self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1')
self.concat = tl.layers.Concat(concat_dim=1, name='concat_layer')
def forward(self, inputs):
d1 = self.dense1(inputs)
d2 = self.dense2(inputs)
outputs = self.concat([d1, d2])
return outputs
model = CustomModel()
model.train()
inputs = tf.convert_to_tensor(np.random.random([4, 20]).astype(np.float32))
outputs = model(inputs)
print(model)
self.assertEqual(outputs.get_shape().as_list(), [4, 20])
def test_elementwise(self):
class CustomModel(tl.models.Model):
def __init__(self):
super(CustomModel, self).__init__()
self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1')
self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1')
self.element = tl.layers.Elementwise(combine_fn=tf.minimum, name='minimum', act=tf.identity)
def forward(self, inputs):
d1 = self.dense1(inputs)
d2 = self.dense2(inputs)
outputs = self.element([d1, d2])
return outputs, d1, d2
model = CustomModel()
model.train()
inputs = tf.convert_to_tensor(np.random.random([4, 20]).astype(np.float32))
outputs, d1, d2 = model(inputs)
print(model)
min = tf.minimum(d1, d2)
self.assertEqual(outputs.get_shape().as_list(), [4, 10])
self.assertTrue(np.array_equal(min.numpy(), outputs.numpy()))
if __name__ == '__main__':
unittest.main()