Skip to content

Commit c7267a4

Browse files
Mathew Donald RogersMathew Donald Rogers
authored andcommitted
Added nose tests.
1 parent 689a76f commit c7267a4

File tree

1 file changed

+54
-67
lines changed

1 file changed

+54
-67
lines changed
Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
21
"""
3-
Notes: Cost function is not implemented for IdentityConvNonlinearity, RectifierConvNonlinearity,
4-
TanhConvNonlinearity. It is bugged for SigmoidConvNonlinearity, but we are not triggering the
5-
bug here. The cost function is also not implemented for standard mlp RectifiedLinear or Tanh.
2+
Note: Cost functions are not implemented for RectifierConvNonlinearity,
3+
TanhConvNonlinearity, RectifiedLinear, and Tanh. Here we verify that
4+
cost functions for convolutional layers give the correct output
5+
by comparing to standard MLP's.
66
"""
77

8-
9-
"""
10-
Test costs
11-
"""
128
import numpy as np
139
import theano
1410
import theano.tensor as T
@@ -18,79 +14,70 @@
1814
from pylearn2.space import Conv2DSpace
1915
from pylearn2.models.mlp import SigmoidConvNonlinearity, TanhConvNonlinearity, IdentityConvNonlinearity, RectifierConvNonlinearity
2016

21-
#def test_costs():
22-
23-
# Create fake data
24-
np.random.seed(12345)
25-
26-
27-
r = 31
28-
s = 21
29-
shape = [r, s]
30-
nvis = r*s
31-
output_channels = 13
32-
batch_size = 103
17+
def test_costs():
3318

34-
x = np.random.rand(batch_size, r, s, 1)
35-
y = np.random.randint(2, size = [batch_size, output_channels, 1 ,1])
19+
ImplementedNonLinearities = [[SigmoidConvNonlinearity(), Sigmoid], [IdentityConvNonlinearity(), Linear]]
3620

37-
x = x.astype('float32')
38-
y = y.astype('float32')
21+
for ConvNonlinearity, MLPNonlinearity in ImplementedNonLinearities:
3922

40-
x_mlp = x.flatten().reshape(batch_size, nvis)
41-
y_mlp = y.flatten().reshape(batch_size, output_channels)
23+
# Create fake data
24+
np.random.seed(12345)
4225

43-
nonlinearity = IdentityConvNonlinearity()
26+
r = 31
27+
s = 21
28+
shape = [r, s]
29+
nvis = r*s
30+
output_channels = 13
31+
batch_size = 103
4432

45-
# Initialize convnet with random weights.
33+
x = np.random.rand(batch_size, r, s, 1).astype('float32')
34+
y = np.random.randint(2, size = [batch_size, output_channels, 1 ,1]).astype('float32')
4635

47-
conv_model = MLP(
48-
input_space = Conv2DSpace(shape = shape, axes = ['b', 0, 1, 'c'], num_channels = 1),
49-
layers = [ConvElemwise(layer_name='conv', nonlinearity = nonlinearity, output_channels = output_channels, kernel_shape = shape, pool_shape = [1,1], pool_stride = shape, irange= 1.0)],
50-
batch_size = batch_size
51-
)
36+
x_mlp = x.flatten().reshape(batch_size, nvis)
37+
y_mlp = y.flatten().reshape(batch_size, output_channels)
5238

53-
X = conv_model.get_input_space().make_theano_batch()
54-
Y = conv_model.get_target_space().make_theano_batch()
55-
Y_hat = conv_model.fprop(X)
56-
g = theano.function([X], Y_hat)
39+
# Initialize convnet with random weights.
5740

58-
# Construct an equivalent MLP which gives the same output.
41+
conv_model = MLP(
42+
input_space = Conv2DSpace(shape = shape, axes = ['b', 0, 1, 'c'], num_channels = 1),
43+
layers = [ConvElemwise(layer_name='conv', nonlinearity = ConvNonlinearity, output_channels = output_channels, kernel_shape = shape, pool_shape = [1,1], pool_stride = shape, irange= 1.0)],
44+
batch_size = batch_size
45+
)
5946

60-
mlp_model = MLP(
61-
layers = [Linear(dim = output_channels, layer_name = 'mlp', irange = 1.0)],
62-
batch_size = batch_size,
63-
nvis = nvis
64-
)
47+
X = conv_model.get_input_space().make_theano_batch()
48+
Y = conv_model.get_target_space().make_theano_batch()
49+
Y_hat = conv_model.fprop(X).flatten()
50+
g = theano.function([X], Y_hat)
6551

66-
W, b = conv_model.get_param_values()
67-
W = W.astype('float32')
68-
b = b.astype('float32')
69-
W_mlp = np.zeros(shape = (output_channels, nvis))
70-
for k in range(output_channels):
71-
W_mlp[k] = W[k, 0].flatten()[::-1]
72-
W_mlp = W_mlp.T
73-
b_mlp = b.flatten()
74-
W_mlp = W_mlp.astype('float32')
75-
b_mlp = b_mlp.astype('float32')
76-
mlp_model.set_param_values([W_mlp, b_mlp])
52+
# Construct an equivalent MLP which gives the same output after flattening.
7753

78-
X1 = mlp_model.get_input_space().make_theano_batch()
79-
Y1 = mlp_model.get_target_space().make_theano_batch()
80-
Y1_hat = mlp_model.fprop(X1)
81-
f = theano.function([X1], Y1_hat)
54+
mlp_model = MLP(
55+
layers = [MLPNonlinearity(dim = output_channels, layer_name = 'mlp', irange = 1.0)],
56+
batch_size = batch_size,
57+
nvis = nvis
58+
)
8259

60+
W, b = conv_model.get_param_values()
61+
W = W.astype('float32')
62+
b = b.astype('float32')
63+
W_mlp = np.zeros(shape = (output_channels, nvis))
64+
for k in range(output_channels):
65+
W_mlp[k] = W[k, 0].flatten()[::-1]
66+
W_mlp = W_mlp.T
67+
b_mlp = b.flatten()
68+
W_mlp = W_mlp.astype('float32')
69+
b_mlp = b_mlp.astype('float32')
70+
mlp_model.set_param_values([W_mlp, b_mlp])
8371

84-
# Check that the two models give the same throughput
85-
assert np.linalg.norm(f(x_mlp).flatten() - g(x).flatten()) < 10**-3
86-
print "f-prop ok"
72+
X1 = mlp_model.get_input_space().make_theano_batch()
73+
Y1 = mlp_model.get_target_space().make_theano_batch()
74+
Y1_hat = mlp_model.fprop(X1).flatten()
75+
f = theano.function([X1], Y1_hat)
8776

88-
# Cost functions:
89-
mlp_cost = theano.function([X1, Y1], mlp_model.cost(Y1, Y1_hat))
90-
print "mlp_cost = "+str(mlp_cost(x_mlp, y_mlp))
9177

92-
batch_axis = T.scalar()
93-
conv_cost = theano.function([X, Y], conv_model.cost(Y, Y_hat))
94-
print "conv_cost = "+str(conv_cost(x,y))
78+
# Check that the two models give the same output
79+
assert np.linalg.norm(f(x_mlp) - g(x)) < 10**-3
9580

81+
if __name__ == "__main__":
82+
test_costs()
9683

0 commit comments

Comments
 (0)