1
1
"""
2
2
Note: Cost functions are not implemented for RectifierConvNonlinearity,
3
- TanhConvNonlinearity, RectifiedLinear, and Tanh. Here we verify that the
3
+ TanhConvNonlinearity, RectifiedLinear, and Tanh. Here we verify that the
4
4
implemented cost functions for convolutional layers give the correct output
5
5
by comparing to standard MLP's.
6
6
"""
7
7
8
8
import numpy as np
9
9
import theano
10
- import theano .tensor as T
11
10
from pylearn2 .models .mlp import MLP
12
11
from pylearn2 .models .mlp import Sigmoid , Tanh , Linear , RectifiedLinear
13
12
from pylearn2 .models .mlp import ConvElemwise
14
13
from pylearn2 .space import Conv2DSpace
15
14
from pylearn2 .models .mlp import SigmoidConvNonlinearity
16
15
from pylearn2 .models .mlp import TanhConvNonlinearity
17
16
from pylearn2 .models .mlp import IdentityConvNonlinearity
18
- from pylearn2 .models .mlp import RectifierConvNonlinearity
17
+ from pylearn2 .models .mlp import RectifierConvNonlinearity
18
+
19
19
20
20
def check_implemented_case (ConvNonlinearity , MLPNonlinearity ):
21
21
@@ -30,43 +30,51 @@ def check_implemented_case(ConvNonlinearity, MLPNonlinearity):
30
30
batch_size = 103
31
31
32
32
x = np .random .rand (batch_size , r , s , 1 )
33
- y = np .random .randint (2 , size = [batch_size , output_channels , 1 , 1 ])
33
+ y = np .random .randint (2 , size = [batch_size , output_channels , 1 , 1 ])
34
34
35
35
x = x .astype ('float32' )
36
36
y = y .astype ('float32' )
37
37
38
38
x_mlp = x .flatten ().reshape (batch_size , nvis )
39
39
y_mlp = y .flatten ().reshape (batch_size , output_channels )
40
40
41
- # Initialize convnet with random weights.
41
+ # Initialize convnet with random weights.
42
42
43
43
conv_model = MLP (
44
- input_space = Conv2DSpace (shape = shape , axes = ['b' , 0 , 1 , 'c' ], num_channels = 1 ),
45
- layers = [ConvElemwise (layer_name = 'conv' , nonlinearity = ConvNonlinearity , \
46
- output_channels = output_channels , kernel_shape = shape , \
47
- pool_shape = [1 ,1 ], pool_stride = shape , irange = 1.0 )],
48
- batch_size = batch_size
44
+ input_space = Conv2DSpace (shape = shape ,
45
+ axes = ['b' , 0 , 1 , 'c' ],
46
+ num_channels = 1 ),
47
+ layers = [ConvElemwise (layer_name = 'conv' ,
48
+ nonlinearity = ConvNonlinearity ,
49
+ output_channels = output_channels ,
50
+ kernel_shape = shape ,
51
+ pool_shape = [1 , 1 ],
52
+ pool_stride = shape ,
53
+ irange = 1.0 )],
54
+ batch_size = batch_size
49
55
)
50
56
51
57
X = conv_model .get_input_space ().make_theano_batch ()
52
58
Y = conv_model .get_target_space ().make_theano_batch ()
53
59
Y_hat = conv_model .fprop (X )
54
60
g = theano .function ([X ], Y_hat )
55
61
56
- # Construct an equivalent MLP which gives the same output after flattening both.
57
-
62
+ # Construct an equivalent MLP which gives the same output
63
+ # after flattening both.
58
64
mlp_model = MLP (
59
- layers = [MLPNonlinearity (dim = output_channels , layer_name = 'mlp' , irange = 1.0 )],
60
- batch_size = batch_size ,
61
- nvis = nvis
65
+ layers = [MLPNonlinearity (dim = output_channels ,
66
+ layer_name = 'mlp' ,
67
+ irange = 1.0 )],
68
+ batch_size = batch_size ,
69
+ nvis = nvis
62
70
)
63
71
64
72
W , b = conv_model .get_param_values ()
65
73
66
74
W = W .astype ('float32' )
67
75
b = b .astype ('float32' )
68
76
69
- W_mlp = np .zeros (shape = (output_channels , nvis ))
77
+ W_mlp = np .zeros (shape = (output_channels , nvis ))
70
78
for k in range (output_channels ):
71
79
W_mlp [k ] = W [k , 0 ].flatten ()[::- 1 ]
72
80
W_mlp = W_mlp .T
@@ -82,42 +90,47 @@ def check_implemented_case(ConvNonlinearity, MLPNonlinearity):
82
90
Y1_hat = mlp_model .fprop (X1 )
83
91
f = theano .function ([X1 ], Y1_hat )
84
92
85
-
86
93
# Check that the two models give the same output
87
- assert np .linalg .norm (f (x_mlp ).flatten () - g (x ).flatten ()) < 10 ** - 3
94
+ assert np .linalg .norm (f (x_mlp ).flatten () - g (x ).flatten ()) < 10 ** - 3
88
95
89
96
# Check that the two models have the same costs:
90
97
mlp_cost = theano .function ([X1 , Y1 ], mlp_model .cost (Y1 , Y1_hat ))
91
98
conv_cost = theano .function ([X , Y ], conv_model .cost (Y , Y_hat ))
92
99
93
- assert np .linalg .norm (conv_cost (x ,y ) - mlp_cost (x_mlp , y_mlp )) < 10 ** - 3
100
+ assert np .linalg .norm (conv_cost (x , y ) - mlp_cost (x_mlp , y_mlp )) < 10 ** - 3
94
101
95
102
96
103
def check_unimplemented_case (ConvNonlinearity ):
97
104
98
105
conv_model = MLP (
99
- input_space = Conv2DSpace (shape = [1 ,1 ], axes = ['b' , 0 , 1 , 'c' ], num_channels = 1 ),
100
- layers = [ConvElemwise (layer_name = 'conv' , nonlinearity = ConvNonlinearity , \
101
- output_channels = 1 , kernel_shape = [1 ,1 ], \
102
- pool_shape = [1 ,1 ], pool_stride = [1 ,1 ], irange = 1.0 )],
103
- batch_size = 1
104
- )
106
+ input_space = Conv2DSpace (shape = [1 , 1 ],
107
+ axes = ['b' , 0 , 1 , 'c' ],
108
+ num_channels = 1 ),
109
+ layers = [ConvElemwise (layer_name = 'conv' ,
110
+ nonlinearity = ConvNonlinearity ,
111
+ output_channels = 1 ,
112
+ kernel_shape = [1 , 1 ],
113
+ pool_shape = [1 , 1 ],
114
+ pool_stride = [1 , 1 ],
115
+ irange = 1.0 )],
116
+ batch_size = 1
117
+ )
105
118
106
119
X = conv_model .get_input_space ().make_theano_batch ()
107
120
Y = conv_model .get_target_space ().make_theano_batch ()
108
121
Y_hat = conv_model .fprop (X )
109
122
110
- assert np .testing .assert_raises (NotImplementedError , conv_model . cost ( Y , Y_hat ))
111
-
123
+ assert np .testing .assert_raises (NotImplementedError ,
124
+ conv_model . cost , Y , Y_hat )
112
125
113
126
114
127
def test_all_costs ():
115
128
116
- ImplementedCases = [[SigmoidConvNonlinearity (), Sigmoid ], \
129
+ ImplementedCases = [[SigmoidConvNonlinearity (), Sigmoid ],
117
130
[IdentityConvNonlinearity (), Linear ]]
118
131
119
- UnimplementedCases = [[TanhConvNonlinearity (), Tanh ], \
120
- [RectifierConvNonlinearity , RectifiedLinear ]]
132
+ UnimplementedCases = [[TanhConvNonlinearity (), Tanh ],
133
+ [RectifierConvNonlinearity , RectifiedLinear ]]
121
134
122
135
for ConvNonlinearity , MLPNonlinearity in UnimplementedCases :
123
136
check_unimplemented_case (ConvNonlinearity )
0 commit comments