Skip to content

Commit 7ac5282

Browse files
committed
PEP8 fixes
1 parent 802382c commit 7ac5282

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

pylearn2/models/mlp.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,7 +2673,7 @@ def get_monitoring_channels_from_state(self, state, target,
26732673
rval = self._get_monitoring_channels_for_activations(state)
26742674

26752675
return rval
2676-
2676+
26772677
def cost(self, Y, Y_hat):
26782678
"""
26792679
Parameters
@@ -2682,7 +2682,7 @@ def cost(self, Y, Y_hat):
26822682
Output of `fprop`
26832683
Y_hat : theano.gof.Variable
26842684
Targets
2685-
batch_axis : integer
2685+
batch_axis : integer
26862686
axis representing batch dimension
26872687
26882688
Returns
@@ -2694,8 +2694,6 @@ def cost(self, Y, Y_hat):
26942694
str(type(self)) + " does not implement cost function.")
26952695

26962696

2697-
2698-
26992697
class IdentityConvNonlinearity(ConvNonlinearity):
27002698

27012699
"""
@@ -2722,16 +2720,15 @@ def get_monitoring_channels_from_state(self,
27222720
rval["misclass"] = T.cast(incorrect, config.floatX).mean()
27232721

27242722
return rval
2725-
2723+
27262724
@wraps(ConvNonlinearity.cost)
27272725
def cost(self, Y, Y_hat, batch_axis):
27282726
"""
27292727
Notes
27302728
-----
2731-
Mean squared error across batches
2729+
Mean squared error across batches
27322730
"""
2733-
return T.sum(T.mean(T.sqr(Y-Y_hat), axis = batch_axis))
2734-
2731+
return T.sum(T.mean(T.sqr(Y-Y_hat), axis=batch_axis))
27352732

27362733

27372734
class RectifierConvNonlinearity(ConvNonlinearity):
@@ -2849,7 +2846,7 @@ def get_monitoring_channels_from_state(self, state, target,
28492846
rval['per_output_f1_min'] = f1.min()
28502847

28512848
return rval
2852-
2849+
28532850
@wraps(ConvNonlinearity.cost)
28542851
def cost(self, Y, Y_hat, batch_axis):
28552852
"""
@@ -2886,8 +2883,6 @@ def cost(self, Y, Y_hat, batch_axis):
28862883
raise NotImplementedError(
28872884
str(type(self)) + " does not implement cost function.")
28882885

2889-
2890-
28912886

28922887
class ConvElemwise(Layer):
28932888
"""
@@ -3304,18 +3299,18 @@ def fprop(self, state_below):
33043299
p = self.output_normalization(p)
33053300

33063301
return p
3307-
3302+
33083303
@wraps(Layer.cost)
33093304
def cost(self, Y, Y_hat):
33103305
"""
33113306
Notes
33123307
-----
33133308
The cost method calls self.nonlin.cost
3314-
"""
3315-
3309+
"""
3310+
33163311
batch_axis = self.output_space.get_batch_axis()
33173312
return self.nonlin.cost(Y=Y, Y_hat=Y_hat, batch_axis=batch_axis)
3318-
3313+
33193314

33203315
class ConvRectifiedLinear(ConvElemwise):
33213316

pylearn2/models/tests/test_convelemwise_cost.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"""
22
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
44
implemented cost functions for convolutional layers give the correct output
55
by comparing to standard MLP's.
66
"""
77

88
import numpy as np
99
import theano
10-
import theano.tensor as T
1110
from pylearn2.models.mlp import MLP
1211
from pylearn2.models.mlp import Sigmoid, Tanh, Linear, RectifiedLinear
1312
from pylearn2.models.mlp import ConvElemwise
1413
from pylearn2.space import Conv2DSpace
1514
from pylearn2.models.mlp import SigmoidConvNonlinearity
1615
from pylearn2.models.mlp import TanhConvNonlinearity
1716
from pylearn2.models.mlp import IdentityConvNonlinearity
18-
from pylearn2.models.mlp import RectifierConvNonlinearity
17+
from pylearn2.models.mlp import RectifierConvNonlinearity
18+
1919

2020
def check_implemented_case(ConvNonlinearity, MLPNonlinearity):
2121

@@ -30,43 +30,51 @@ def check_implemented_case(ConvNonlinearity, MLPNonlinearity):
3030
batch_size = 103
3131

3232
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])
3434

3535
x = x.astype('float32')
3636
y = y.astype('float32')
3737

3838
x_mlp = x.flatten().reshape(batch_size, nvis)
3939
y_mlp = y.flatten().reshape(batch_size, output_channels)
4040

41-
# Initialize convnet with random weights.
41+
# Initialize convnet with random weights.
4242

4343
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
4955
)
5056

5157
X = conv_model.get_input_space().make_theano_batch()
5258
Y = conv_model.get_target_space().make_theano_batch()
5359
Y_hat = conv_model.fprop(X)
5460
g = theano.function([X], Y_hat)
5561

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.
5864
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
6270
)
6371

6472
W, b = conv_model.get_param_values()
6573

6674
W = W.astype('float32')
6775
b = b.astype('float32')
6876

69-
W_mlp = np.zeros(shape = (output_channels, nvis))
77+
W_mlp = np.zeros(shape=(output_channels, nvis))
7078
for k in range(output_channels):
7179
W_mlp[k] = W[k, 0].flatten()[::-1]
7280
W_mlp = W_mlp.T
@@ -82,42 +90,47 @@ def check_implemented_case(ConvNonlinearity, MLPNonlinearity):
8290
Y1_hat = mlp_model.fprop(X1)
8391
f = theano.function([X1], Y1_hat)
8492

85-
8693
# 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
8895

8996
# Check that the two models have the same costs:
9097
mlp_cost = theano.function([X1, Y1], mlp_model.cost(Y1, Y1_hat))
9198
conv_cost = theano.function([X, Y], conv_model.cost(Y, Y_hat))
9299

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
94101

95102

96103
def check_unimplemented_case(ConvNonlinearity):
97104

98105
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+
)
105118

106119
X = conv_model.get_input_space().make_theano_batch()
107120
Y = conv_model.get_target_space().make_theano_batch()
108121
Y_hat = conv_model.fprop(X)
109122

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)
112125

113126

114127
def test_all_costs():
115128

116-
ImplementedCases = [[SigmoidConvNonlinearity(), Sigmoid], \
129+
ImplementedCases = [[SigmoidConvNonlinearity(), Sigmoid],
117130
[IdentityConvNonlinearity(), Linear]]
118131

119-
UnimplementedCases = [[TanhConvNonlinearity(), Tanh], \
120-
[RectifierConvNonlinearity, RectifiedLinear]]
132+
UnimplementedCases = [[TanhConvNonlinearity(), Tanh],
133+
[RectifierConvNonlinearity, RectifiedLinear]]
121134

122135
for ConvNonlinearity, MLPNonlinearity in UnimplementedCases:
123136
check_unimplemented_case(ConvNonlinearity)

0 commit comments

Comments
 (0)