-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtest_layers_lambda.py
215 lines (159 loc) · 7.12 KB
/
test_layers_lambda.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/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_Lambda_Test(CustomTestCase):
@classmethod
def setUpClass(cls):
cls.data_x = np.random.random([100, 1]).astype(np.float32)
cls.data_y = cls.data_x**3 + np.random.random() * cls.data_x**2 + np.random.random() * cls.data_x
@classmethod
def tearDownClass(cls):
pass
def test_lambda_keras(self):
layers = [
tf.keras.layers.Dense(10, activation=tf.nn.relu),
tf.keras.layers.Dense(5, activation=tf.nn.sigmoid),
tf.keras.layers.Dense(1, activation=tf.identity)
]
perceptron = tf.keras.Sequential(layers)
# in order to get trainable_variables of keras
_ = perceptron(np.random.random([100, 5]).astype(np.float32))
class CustomizeModel(tl.models.Model):
def __init__(self):
super(CustomizeModel, self).__init__()
self.dense = tl.layers.Dense(in_channels=1, n_units=5)
self.lambdalayer = tl.layers.Lambda(perceptron, perceptron.trainable_variables)
def forward(self, x):
z = self.dense(x)
z = self.lambdalayer(z)
return z
optimizer = tf.optimizers.Adam(learning_rate=0.1)
model = CustomizeModel()
print(model.lambdalayer)
model.train()
for epoch in range(10):
with tf.GradientTape() as tape:
pred_y = model(self.data_x)
loss = tl.cost.mean_squared_error(pred_y, self.data_y)
gradients = tape.gradient(loss, model.trainable_weights)
optimizer.apply_gradients(zip(gradients, model.trainable_weights))
print("epoch %d, loss %f" % (epoch, loss))
def test_lambda_func_with_args(self):
def customize_func(x, foo=42):
if foo == 0:
return tf.nn.relu(x)
elif foo == 1:
return tf.nn.sigmoid(x)
else:
return tf.identity(x)
class CustomizeModel(tl.models.Model):
def __init__(self):
super(CustomizeModel, self).__init__()
self.dense = tl.layers.Dense(in_channels=1, n_units=5)
self.lambdalayer = tl.layers.Lambda(customize_func, fn_weights=[], fn_args={'foo': 0})
def forward(self, x, bar):
z = self.dense(x)
if bar == -1:
zf = self.lambdalayer(z)
else:
zf = self.lambdalayer(z, foo=bar)
return z, zf
model = CustomizeModel()
print(model.lambdalayer)
model.train()
out, out2 = model(self.data_x, bar=-1)
self.assertTrue(np.array_equal(out2.numpy(), tf.nn.relu(out).numpy()))
out, out2 = model(self.data_x, bar=0)
self.assertTrue(np.array_equal(out2.numpy(), tf.nn.relu(out).numpy()))
out, out2 = model(self.data_x, bar=1)
self.assertTrue(np.array_equal(out2.numpy(), tf.nn.sigmoid(out).numpy()))
out, out2 = model(self.data_x, bar=2)
self.assertTrue(np.array_equal(out2.numpy(), out.numpy()))
def test_lambda_func_with_weight(self):
a = tf.Variable(1.0)
def customize_fn(x):
return x + a
class CustomizeModel(tl.models.Model):
def __init__(self):
super(CustomizeModel, self).__init__()
self.dense = tl.layers.Dense(in_channels=1, n_units=5)
self.lambdalayer = tl.layers.Lambda(customize_fn, fn_weights=[a])
def forward(self, x):
z = self.dense(x)
z = self.lambdalayer(z)
return z
model = CustomizeModel()
print(model.lambdalayer)
model.train()
out = model(self.data_x)
print(out.shape)
def test_lambda_func_without_args(self):
class CustomizeModel(tl.models.Model):
def __init__(self):
super(CustomizeModel, self).__init__()
self.dense = tl.layers.Dense(in_channels=1, n_units=5)
self.lambdalayer = tl.layers.Lambda(lambda x: 2 * x)
def forward(self, x):
z = self.dense(x)
zf = self.lambdalayer(z)
return z, zf
model = CustomizeModel()
print(model.lambdalayer)
model.train()
out, out2 = model(self.data_x)
self.assertTrue(np.array_equal(out2.numpy(), out.numpy() * 2))
def test_elementwiselambda_func_with_args(self):
def customize_func(noise, mean, std, foo=42):
return mean + noise * tf.exp(std * 0.5) + foo
class CustomizeModel(tl.models.Model):
def __init__(self):
super(CustomizeModel, self).__init__()
self.dense1 = tl.layers.Dense(in_channels=1, n_units=5)
self.dense2 = tl.layers.Dense(in_channels=1, n_units=5)
self.dense3 = tl.layers.Dense(in_channels=1, n_units=5)
self.lambdalayer = tl.layers.ElementwiseLambda(customize_func, fn_args={'foo': 1024})
def forward(self, x, bar=None):
noise = self.dense1(x)
mean = self.dense2(x)
std = self.dense3(x)
if bar is None:
out = self.lambdalayer([noise, mean, std])
else:
out = self.lambdalayer([noise, mean, std], foo=bar)
return noise, mean, std, out
model = CustomizeModel()
print(model.lambdalayer)
model.train()
noise, mean, std, out = model(self.data_x)
self.assertTrue(np.allclose(out.numpy(), customize_func(noise, mean, std, foo=1024).numpy()))
noise, mean, std, out = model(self.data_x, bar=2048)
self.assertTrue(np.allclose(out.numpy(), customize_func(noise, mean, std, foo=2048).numpy()))
def test_elementwiselambda_func_without_args(self):
def customize_func(noise, mean, std):
return mean + noise * tf.exp(std * 0.5)
class CustomizeModel(tl.models.Model):
def __init__(self):
super(CustomizeModel, self).__init__()
self.dense1 = tl.layers.Dense(in_channels=1, n_units=5)
self.dense2 = tl.layers.Dense(in_channels=1, n_units=5)
self.dense3 = tl.layers.Dense(in_channels=1, n_units=5)
self.lambdalayer = tl.layers.ElementwiseLambda(customize_func, fn_weights=[])
def forward(self, x):
noise = self.dense1(x)
mean = self.dense2(x)
std = self.dense3(x)
out = self.lambdalayer([noise, mean, std])
return noise, mean, std, out
model = CustomizeModel()
print(model.lambdalayer)
model.train()
noise, mean, std, out = model(self.data_x)
self.assertTrue(np.array_equal(out.numpy(), customize_func(noise, mean, std).numpy()))
if __name__ == '__main__':
unittest.main()