[0473b3]: / tests / test_decorators.py

Download this file

188 lines (151 with data), 6.4 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
 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
import keras.backend as K
import matplotlib
import numpy as np
import pytest
from keras.layers import Conv2D
from keras.layers import Dense
from keras.layers import Input
from keras.models import Model
from janggu import inputlayer
from janggu import outputconv
from janggu import outputdense
matplotlib.use('AGG')
# ==========================================================
# Test without decorators
def make_dense_wo_decorator(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
layer = Dense(params[0])(input[0])
output = [Dense(outshapes[name]['shape'][0], name=name,
activation=params[1])(layer)
for name in outshapes]
return input, output
# ==========================================================
# Test without output decorator, sigmoid as string
@outputdense('sigmoid')
def make_dense_w_top_str(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
output = Dense(params[0])(input[0])
return input, output
# ==========================================================
# Test without output decorator, sigmoid as string
@outputdense({'testout': 'sigmoid'})
def make_dense_w_top_dict(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
output = Dense(params[0])(input[0])
return input, output
# ==========================================================
# Test without output decorator, sigmoid as string
@outputdense(K.tanh)
def make_dense_w_top_func(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
output = Dense(params[0])(input[0])
return input, output
# ==========================================================
# Test without input decorator
@inputlayer
def make_dense_w_bottom(input, inshapes, outshapes, params):
layer = Dense(params[0])(input[0])
output = [Dense(outshapes[name]['shape'][0], name=name,
activation=params[1])(layer)
for name in outshapes]
return input, output
# ==========================================================
# Test without input and output decorator
@inputlayer
@outputdense('sigmoid')
def make_dense_w_topbottom(input, input_props, output_props, params):
output = Dense(params[0])(input[0])
return input, output
# ==========================================================
# Test without decorators
def make_conv_wo_decorator(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
layer = Conv2D(params[0], (6, 4))(input[0])
output = [Conv2D(outshapes[name]['shape'][-1],
(1, 1),
name=name,
activation=params[1])(layer)
for name in outshapes]
return input, output
# ==========================================================
# Test without output decorator
@outputconv('sigmoid')
def make_conv_w_top_str(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
output = Conv2D(params[0], (6, 4))(input[0])
return input, output
# ==========================================================
# Test without output decorator
@outputconv({'testout': 'sigmoid'})
def make_conv_w_top_dict(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
output = Conv2D(params[0], (6, 4))(input[0])
return input, output
# ==========================================================
# Test without output decorator
@outputconv(K.tanh)
def make_conv_w_top_func(input, inshapes, outshapes, params):
input = [Input(inshapes[name]['shape'], name=name)
for name in inshapes]
output = Conv2D(params[0], (6, 4))(input[0])
return input, output
# ==========================================================
# Test without input decorator
@inputlayer
def make_conv_w_bottom(input, inshapes, outshapes, params):
input
layer = Conv2D(params[0], (6, 4))(input[0])
output = [Conv2D(outshapes[name]['shape'][-1],
(1, 1),
name=name,
activation=params[1])(layer)
for name in outshapes]
return input, output
# ==========================================================
# Test without input and output decorator
@inputlayer
@outputconv('sigmoid')
def make_conv_w_topbottom(input, input_props, output_props, params):
output = Conv2D(params[0], (6, 4))(input[0])
return input, output
def test_dense_decorators():
inp = {'testin': {'shape': (10,)}}
oup = {'testout': {'shape': (3,)}}
funclist = [make_dense_w_top_str, make_dense_w_top_dict,
make_dense_w_top_func,
make_dense_w_bottom, make_dense_w_topbottom]
i, o = make_dense_wo_decorator(None, inp, oup, (30, 'relu'))
ref_model = Model(i, o)
for func in funclist:
i, o = func(None, inp, oup, (30, 'relu'))
model = Model(i, o)
for i in range(len(model.layers)):
np.testing.assert_equal(model.layers[i].input_shape,
ref_model.layers[i].input_shape)
np.testing.assert_equal(model.layers[i].output_shape,
ref_model.layers[i].output_shape)
def test_conv_decorators():
inp = {'testin': {'shape': (10, 4, 1)}}
oup = {'testout': {'shape': (5, 1, 3)}}
funclist = [make_conv_w_top_str,
make_conv_w_top_dict,
make_conv_w_top_func,
make_conv_w_bottom, make_conv_w_topbottom]
i, o = make_conv_wo_decorator(None, inp, oup, (30, 'relu'))
ref_model = Model(i, o)
ref_model.summary()
for func in funclist:
i, o = func(None, inp, oup, (30, 'relu'))
model = Model(i, o)
for i in range(len(model.layers)):
np.testing.assert_equal(model.layers[i].input_shape,
ref_model.layers[i].input_shape)
np.testing.assert_equal(model.layers[i].output_shape,
ref_model.layers[i].output_shape)