forked from keras-team/keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_shape_inference.py
178 lines (140 loc) · 6.25 KB
/
test_shape_inference.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
import pytest
import numpy as np
from keras import backend as K
from keras.layers.core import *
from keras.layers.convolutional import *
from keras.layers.recurrent import SimpleRNN
def check_layer_output_shape(layer, input_data):
ndim = len(input_data.shape)
layer.input = K.placeholder(ndim=ndim)
layer.set_input_shape(input_data.shape)
expected_output_shape = layer.output_shape[1:]
function = K.function([layer.input], [layer.get_output()])
output = function([input_data])[0]
assert output.shape[1:] == expected_output_shape
########
# Core #
########
def test_Reshape():
layer = Reshape(dims=(2, 3))
input_data = np.random.random((2, 6))
check_layer_output_shape(layer, input_data)
def test_Permute():
layer = Permute(dims=(1, 3, 2))
input_data = np.random.random((2, 2, 4, 3))
check_layer_output_shape(layer, input_data)
def test_Flatten():
layer = Flatten()
input_data = np.random.random((2, 2, 3))
check_layer_output_shape(layer, input_data)
def test_RepeatVector():
layer = RepeatVector(2)
input_data = np.random.random((2, 2))
check_layer_output_shape(layer, input_data)
def test_Dense():
layer = Dense(3)
input_data = np.random.random((2, 2))
check_layer_output_shape(layer, input_data)
def test_TimeDistributedDense():
layer = TimeDistributedDense(2)
input_data = np.random.random((2, 2, 3))
check_layer_output_shape(layer, input_data)
#################
# Convolutional #
#################
def test_Convolution1D():
for border_mode in ['same', 'valid']:
for filter_length in [2, 3]:
for subsample_length in [1]:
if subsample_length > 1 and border_mode == 'same':
continue
for input_data_shape in [(2, 3, 4), (2, 4, 4)]:
layer = Convolution1D(nb_filter=1,
filter_length=filter_length,
border_mode=border_mode,
subsample_length=subsample_length)
input_data = np.random.random(input_data_shape)
check_layer_output_shape(layer, input_data)
def test_Convolution2D():
for border_mode in ['same', 'valid']:
for nb_row, nb_col in [(2, 2), (3, 3)]:
for subsample in [(1, 1), (2, 2)]:
if (subsample[0] > 1 or subsample[1] > 1) and border_mode == 'same':
continue
for input_data_shape in [(2, 1, 3, 3), (2, 1, 4, 4)]:
layer = Convolution2D(nb_filter=1, nb_row=nb_row,
nb_col=nb_row,
border_mode=border_mode,
subsample=subsample,
dim_ordering='th')
input_data = np.random.random(input_data_shape)
check_layer_output_shape(layer, input_data)
for input_data_shape in [(2, 3, 3, 1)]:
layer = Convolution2D(nb_filter=1, nb_row=nb_row,
nb_col=nb_row,
border_mode=border_mode,
subsample=subsample,
dim_ordering='tf')
input_data = np.random.random(input_data_shape)
check_layer_output_shape(layer, input_data)
def test_MaxPooling1D():
for ignore_border in [True, False]:
for pool_length in [1, 2]:
for stride in [1]:
for input_data_shape in [(2, 3, 4), (2, 4, 4)]:
layer = MaxPooling1D(pool_length=pool_length,
stride=stride,
border_mode='valid')
input_data = np.random.random(input_data_shape)
check_layer_output_shape(layer, input_data)
def test_MaxPooling2D():
for ignore_border in [True, False]:
for strides in [(1, 1), (2, 2)]:
for pool_size in [(2, 2), (3, 3), (4, 4)]:
for input_data_shape in [(2, 1, 4, 4), (2, 1, 5, 5), (2, 1, 6, 6)]:
layer = MaxPooling2D(pool_size=pool_size,
strides=strides,
border_mode='valid',
dim_ordering='th')
input_data = np.random.random(input_data_shape)
check_layer_output_shape(layer, input_data)
for input_data_shape in [(2, 4, 4, 1)]:
layer = MaxPooling2D(pool_size=pool_size,
strides=strides,
border_mode='valid',
dim_ordering='tf')
input_data = np.random.random(input_data_shape)
check_layer_output_shape(layer, input_data)
def test_UpSampling1D():
layer = UpSampling1D(length=2)
input_data = np.random.random((2, 2, 3))
check_layer_output_shape(layer, input_data)
def test_UpSampling2D():
layer = UpSampling2D(size=(2, 2), dim_ordering='th')
input_data = np.random.random((2, 1, 2, 3))
check_layer_output_shape(layer, input_data)
layer = UpSampling2D(size=(2, 2), dim_ordering='tf')
input_data = np.random.random((2, 2, 3, 1))
check_layer_output_shape(layer, input_data)
def test_ZeroPadding1D():
layer = ZeroPadding1D(1)
input_data = np.random.random((2, 2, 1))
check_layer_output_shape(layer, input_data)
def test_ZeroPadding2D():
layer = ZeroPadding2D((1, 2), dim_ordering='th')
input_data = np.random.random((2, 1, 2, 3))
check_layer_output_shape(layer, input_data)
layer = ZeroPadding2D((1, 2), dim_ordering='tf')
input_data = np.random.random((2, 2, 3, 1))
check_layer_output_shape(layer, input_data)
# #############
# # Recurrent #
# #############
def test_SimpleRNN():
# all recurrent layers inherit output_shape
# from the same base recurrent layer
layer = SimpleRNN(2)
input_data = np.random.random((2, 2, 3))
check_layer_output_shape(layer, input_data)
if __name__ == "__main__":
pytest.main([__file__])