1
1
import paddle .v2 as paddle
2
2
3
3
4
+ def softmax_regression (img ):
5
+ predict = paddle .layer .fc (input = img ,
6
+ size = 10 ,
7
+ act = paddle .activation .Softmax ())
8
+ return predict
9
+
10
+
11
+ def multilayer_perceptron (img ):
12
+ # The first fully-connected layer
13
+ hidden1 = paddle .layer .fc (input = img , size = 128 , act = paddle .activation .Relu ())
14
+ # The second fully-connected layer and the according activation function
15
+ hidden2 = paddle .layer .fc (input = hidden1 ,
16
+ size = 64 ,
17
+ act = paddle .activation .Relu ())
18
+ # The thrid fully-connected layer, note that the hidden size should be 10,
19
+ # which is the number of unique digits
20
+ predict = paddle .layer .fc (input = hidden2 ,
21
+ size = 10 ,
22
+ act = paddle .activation .Softmax ())
23
+ return predict
24
+
25
+
26
+ def convolutional_neural_network (img ):
27
+ # first conv layer
28
+ conv_pool_1 = paddle .networks .simple_img_conv_pool (
29
+ input = img ,
30
+ filter_size = 5 ,
31
+ num_filters = 20 ,
32
+ num_channel = 1 ,
33
+ pool_size = 2 ,
34
+ pool_stride = 2 ,
35
+ act = paddle .activation .Tanh ())
36
+ # second conv layer
37
+ conv_pool_2 = paddle .networks .simple_img_conv_pool (
38
+ input = conv_pool_1 ,
39
+ filter_size = 5 ,
40
+ num_filters = 50 ,
41
+ num_channel = 20 ,
42
+ pool_size = 2 ,
43
+ pool_stride = 2 ,
44
+ act = paddle .activation .Tanh ())
45
+ # The first fully-connected layer
46
+ fc1 = paddle .layer .fc (input = conv_pool_2 ,
47
+ size = 128 ,
48
+ act = paddle .activation .Tanh ())
49
+ # The softmax layer, note that the hidden size should be 10,
50
+ # which is the number of unique digits
51
+ predict = paddle .layer .fc (input = fc1 ,
52
+ size = 10 ,
53
+ act = paddle .activation .Softmax ())
54
+ return predict
55
+
56
+
4
57
def main ():
5
58
paddle .init (use_gpu = False , trainer_count = 1 )
6
59
@@ -9,45 +62,58 @@ def main():
9
62
name = 'pixel' , type = paddle .data_type .dense_vector (784 ))
10
63
label = paddle .layer .data (
11
64
name = 'label' , type = paddle .data_type .integer_value (10 ))
12
- hidden1 = paddle .layer .fc (input = images , size = 200 )
13
- hidden2 = paddle .layer .fc (input = hidden1 , size = 200 )
14
- inference = paddle .layer .fc (input = hidden2 ,
15
- size = 10 ,
16
- act = paddle .activation .Softmax ())
17
- cost = paddle .layer .classification_cost (input = inference , label = label )
65
+
66
+ # Here we can build the prediction network in different ways. Please
67
+ # choose one by uncomment corresponding line.
68
+ predict = softmax_regression (images )
69
+ #predict = multilayer_perceptron(images)
70
+ #predict = convolutional_neural_network(images)
71
+
72
+ cost = paddle .layer .classification_cost (input = predict , label = label )
18
73
19
74
parameters = paddle .parameters .create (cost )
20
75
21
- adam_optimizer = paddle .optimizer .Adam (learning_rate = 0.01 )
76
+ optimizer = paddle .optimizer .Momentum (
77
+ learning_rate = 0.1 / 128.0 ,
78
+ momentum = 0.9 ,
79
+ regularization = paddle .optimizer .L2Regularization (rate = 0.0005 * 128 ))
22
80
23
81
trainer = paddle .trainer .SGD (cost = cost ,
24
82
parameters = parameters ,
25
- update_equation = adam_optimizer )
83
+ update_equation = optimizer )
84
+
85
+ lists = []
26
86
27
87
def event_handler (event ):
28
88
if isinstance (event , paddle .event .EndIteration ):
29
- if event .batch_id % 1000 == 0 :
30
- result = trainer . test ( reader = paddle . reader . batched (
31
- paddle . dataset . mnist . test (), batch_size = 256 ) )
32
-
33
- print "Pass %d, Batch %d, Cost %f, %s, Testing metrics %s" % (
34
- event . pass_id , event . batch_id , event . cost , event . metrics ,
35
- result . metrics )
36
-
37
- else :
38
- pass
89
+ if event .batch_id % 100 == 0 :
90
+ print "Pass %d, Batch %d, Cost %f, %s" % (
91
+ event . pass_id , event . batch_id , event . cost , event . metrics )
92
+ if isinstance ( event , paddle . event . EndPass ):
93
+ result = trainer . test ( reader = paddle . reader . batched (
94
+ paddle . dataset . mnist . test (), batch_size = 128 ))
95
+ print "Test with Pass %d, Cost %f, %s \n " % (
96
+ event . pass_id , result . cost , result . metrics )
97
+ lists . append (( event . pass_id , result . cost ,
98
+ result . metrics [ 'classification_error_evaluator' ]))
39
99
40
100
trainer .train (
41
101
reader = paddle .reader .batched (
42
102
paddle .reader .shuffle (
43
103
paddle .dataset .mnist .train (), buf_size = 8192 ),
44
- batch_size = 32 ),
45
- event_handler = event_handler )
104
+ batch_size = 128 ),
105
+ event_handler = event_handler ,
106
+ num_passes = 100 )
107
+
108
+ # find the best pass
109
+ best = sorted (lists , key = lambda list : float (list [1 ]))[0 ]
110
+ print 'Best pass is %s, testing Avgcost is %s' % (best [0 ], best [1 ])
111
+ print 'The classification accuracy is %.2f%%' % (100 - float (best [2 ]) * 100 )
46
112
47
113
# output is a softmax layer. It returns probabilities.
48
114
# Shape should be (100, 10)
49
115
probs = paddle .infer (
50
- output = inference ,
116
+ output = predict ,
51
117
parameters = parameters ,
52
118
reader = paddle .reader .batched (
53
119
paddle .reader .firstn (
0 commit comments