12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import paddle . fluid as fluid
16
- import paddle . dataset . mnist as mnist
15
+ import numpy
16
+
17
17
import paddle
18
+ import paddle .dataset .mnist as mnist
19
+ import paddle .fluid as fluid
18
20
import paddle .v2
19
- import threading
20
- import numpy
21
21
22
22
23
23
def network (is_train ):
24
- reader , queue = fluid .layers .py_reader (
24
+ reader = fluid .layers .py_reader (
25
25
capacity = 10 ,
26
26
shapes = ((- 1 , 784 ), (- 1 , 1 )),
27
27
dtypes = ('float32' , 'int64' ),
@@ -37,32 +37,7 @@ def network(is_train):
37
37
38
38
prediction = fluid .layers .fc (input = hidden , size = 10 , act = 'softmax' )
39
39
loss = fluid .layers .cross_entropy (input = prediction , label = label )
40
- return fluid .layers .mean (loss ), queue , reader
41
-
42
-
43
- def pipe_reader_to_queue (reader_creator , queue ):
44
- with fluid .program_guard (fluid .Program (), fluid .Program ()):
45
- feeder = fluid .DataFeeder (
46
- feed_list = [
47
- fluid .layers .data (
48
- name = 'img' , dtype = 'float32' , shape = [784 ]),
49
- fluid .layers .data (
50
- name = 'label' , dtype = 'int64' , shape = [1 ])
51
- ],
52
- place = fluid .CPUPlace ())
53
-
54
- def __thread_main__ ():
55
- for data in feeder .decorate_reader (
56
- reader_creator , multi_devices = False )():
57
- tmp = fluid .core .LoDTensorArray ()
58
- tmp .append (data ['img' ])
59
- tmp .append (data ['label' ])
60
- queue .push (tmp )
61
- queue .close ()
62
-
63
- th = threading .Thread (target = __thread_main__ )
64
- th .start ()
65
- return th
40
+ return fluid .layers .mean (loss ), reader
66
41
67
42
68
43
def main ():
@@ -71,15 +46,15 @@ def main():
71
46
72
47
with fluid .program_guard (train_prog , startup_prog ):
73
48
with fluid .unique_name .guard ():
74
- loss , train_queue , train_reader = network (True )
49
+ loss , train_reader = network (True )
75
50
adam = fluid .optimizer .Adam (learning_rate = 0.01 )
76
51
adam .minimize (loss )
77
52
78
53
test_prog = fluid .Program ()
79
54
test_startup = fluid .Program ()
80
55
with fluid .program_guard (test_prog , test_startup ):
81
56
with fluid .unique_name .guard ():
82
- test_loss , test_queue , test_reader = network (False )
57
+ test_loss , test_reader = network (False )
83
58
84
59
fluid .Executor (fluid .CUDAPlace (0 )).run (startup_prog )
85
60
fluid .Executor (fluid .CUDAPlace (0 )).run (test_startup )
@@ -90,21 +65,21 @@ def main():
90
65
tester = fluid .ParallelExecutor (
91
66
use_cuda = True , share_vars_from = trainer , main_program = test_prog )
92
67
68
+ train_reader .decorate_paddle_reader (
69
+ paddle .v2 .reader .shuffle (
70
+ paddle .batch (mnist .train (), 256 ), buf_size = 8192 ))
71
+
72
+ test_reader .decorate_paddle_reader (paddle .batch (mnist .test (), 256 ))
73
+
93
74
for epoch_id in xrange (10 ):
94
- train_data_thread = pipe_reader_to_queue (
95
- paddle .batch (paddle .v2 .reader .firstn (mnist .train (), 32 ), 64 ),
96
- train_queue )
97
75
try :
98
76
while True :
99
77
print 'train_loss' , numpy .array (
100
78
trainer .run (fetch_list = [loss .name ]))
101
79
except fluid .core .EOFException :
102
80
print 'End of epoch' , epoch_id
103
81
train_reader .reset ()
104
- train_data_thread .join ()
105
82
106
- test_data_thread = pipe_reader_to_queue (
107
- paddle .batch (mnist .test (), 32 ), test_queue )
108
83
try :
109
84
while True :
110
85
print 'test loss' , numpy .array (
@@ -113,11 +88,6 @@ def main():
113
88
print 'End of testing'
114
89
test_reader .reset ()
115
90
116
- test_data_thread .join ()
117
- break
118
- del trainer
119
- del tester
120
-
121
91
122
92
if __name__ == '__main__' :
123
93
main ()
0 commit comments