Skip to content

Commit 5906baa

Browse files
authored
Adding L2 Regularization to Recognize digits MLP example (#5186)
1 parent 79c5a46 commit 5906baa

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

python/paddle/v2/framework/layer_helper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ def input_dtype(self, input_param_name='input'):
131131
return dtype
132132

133133
def create_parameter(self, attr, shape, dtype, suffix='w'):
134-
if attr['name'] is None:
135-
attr['name'] = unique_name(".".join([self.name, suffix]))
134+
# Deepcopy the attr so that parameters can be shared in program
135+
attr_copy = copy.deepcopy(attr)
136+
if attr_copy['name'] is None:
137+
attr_copy['name'] = unique_name(".".join([self.name, suffix]))
136138
self.init_program.global_block().create_parameter(
137-
dtype=dtype, shape=shape, **attr)
139+
dtype=dtype, shape=shape, **attr_copy)
138140
return self.program.global_block().create_parameter(
139-
name=attr['name'], dtype=dtype, shape=shape)
141+
name=attr_copy['name'], dtype=dtype, shape=shape)
140142

141143
def create_tmp_variable(self, dtype):
142144
return self.program.current_block().create_var(

python/paddle/v2/framework/tests/test_recognize_digits_mlp.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
from paddle.v2.framework.framework import Program, g_program
77
from paddle.v2.framework.executor import Executor
8+
from paddle.v2.framework.regularizer import L2DecayRegularizer
89

910
import numpy as np
1011

12+
BATCH_SIZE = 128
1113
init_program = Program()
1214
program = Program()
1315
image = layers.data(
@@ -17,22 +19,35 @@
1719
program=program,
1820
init_program=init_program)
1921

22+
param_attr = {
23+
'name': None,
24+
'init_attr': {
25+
'type': 'uniform_random',
26+
'min': -1.0,
27+
'max': 1.0
28+
},
29+
'regularization': L2DecayRegularizer(0.0005 * BATCH_SIZE)
30+
}
31+
2032
hidden1 = layers.fc(input=image,
2133
size=128,
2234
act='relu',
2335
program=program,
24-
init_program=init_program)
36+
init_program=init_program,
37+
param_attr=param_attr)
2538
hidden2 = layers.fc(input=hidden1,
2639
size=64,
2740
act='relu',
2841
program=program,
29-
init_program=init_program)
42+
init_program=init_program,
43+
param_attr=param_attr)
3044

3145
predict = layers.fc(input=hidden2,
3246
size=10,
3347
act='softmax',
3448
program=program,
35-
init_program=init_program)
49+
init_program=init_program,
50+
param_attr=param_attr)
3651

3752
label = layers.data(
3853
name='y',
@@ -48,8 +63,6 @@
4863
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001)
4964
opts = sgd_optimizer.minimize(avg_cost)
5065

51-
BATCH_SIZE = 128
52-
5366
train_reader = paddle.batch(
5467
paddle.reader.shuffle(
5568
paddle.dataset.mnist.train(), buf_size=8192),

0 commit comments

Comments
 (0)