@@ -23,19 +23,19 @@ An example implementation for single item data reader creator:
23
23
24
24
``` python
25
25
def reader_creator_random_image (width , height ):
26
- def reader ():
27
- while True :
28
- yield numpy.random.uniform(- 1 , 1 , size = width* height)
29
- return reader
26
+ def reader ():
27
+ while True :
28
+ yield numpy.random.uniform(- 1 , 1 , size = width* height)
29
+ return reader
30
30
```
31
31
32
32
An example implementation for multiple item data reader creator:
33
33
``` python
34
- def reader_creator_random_imageand_label ( widht , height , label ):
35
- def reader ():
36
- while True :
37
- yield numpy.random.uniform(- 1 , 1 , size = width* height), label
38
- return reader
34
+ def reader_creator_random_image_and_label ( width , height , label ):
35
+ def reader ():
36
+ while True :
37
+ yield numpy.random.uniform(- 1 , 1 , size = width* height), label
38
+ return reader
39
39
```
40
40
41
41
## Batch Reader Interface
@@ -74,11 +74,11 @@ mnist_train_batch_reader = paddle.batch(mnist_train, 128)
74
74
Also easy to create custom batch reader:
75
75
```python
76
76
def custom_batch_reader():
77
- while True :
78
- batch = []
79
- for i in xrange (128 ):
80
- batch.append((numpy.random.uniform(- 1 , 1 , 28 * 28 ),)) # note that it's a tuple being appended.
81
- yield batch
77
+ while True :
78
+ batch = []
79
+ for i in xrange (128 ):
80
+ batch.append((numpy.random.uniform(- 1 , 1 , 28 * 28 ),)) # note that it's a tuple being appended.
81
+ yield batch
82
82
83
83
mnist_random_image_batch_reader = custom_batch_reader
84
84
```
@@ -123,16 +123,16 @@ We can do:
123
123
124
124
```python
125
125
def reader_creator_random_image(width, height):
126
- def reader():
127
- while True :
128
- yield numpy.random.uniform(- 1 , 1 , size = width* height)
129
- return reader
126
+ def reader():
127
+ while True :
128
+ yield numpy.random.uniform(- 1 , 1 , size = width* height)
129
+ return reader
130
130
131
131
def reader_creator_bool(t):
132
- def reader:
133
- while True :
134
- yield t
135
- return reader
132
+ def reader:
133
+ while True :
134
+ yield t
135
+ return reader
136
136
137
137
true_reader = reader_creator_bool(True )
138
138
false_reader = reader_creator_bool(False )
@@ -172,18 +172,18 @@ We decided to use dictionary (`{"image":0, "label":1}`) instead of list (`["imag
172
172
173
173
```python
174
174
def image_reader_creator(image_path, label_path, n):
175
- def reader():
176
- f = open (image_path)
177
- l = open (label_path)
178
- images = numpy.fromfile(
179
- f, ' ubyte' , count = n * 28 * 28 ).reshape((n, 28 * 28 )).astype(' float32' )
180
- images = images / 255.0 * 2.0 - 1.0
181
- labels = numpy.fromfile(l, ' ubyte' , count = n).astype(" int" )
182
- for i in xrange (n):
183
- yield images[i, :], labels[i] # a single entry of data is created each time
184
- f.close()
185
- l.close()
186
- return reader
175
+ def reader():
176
+ f = open (image_path)
177
+ l = open (label_path)
178
+ images = numpy.fromfile(
179
+ f, ' ubyte' , count = n * 28 * 28 ).reshape((n, 28 * 28 )).astype(' float32' )
180
+ images = images / 255.0 * 2.0 - 1.0
181
+ labels = numpy.fromfile(l, ' ubyte' , count = n).astype(" int" )
182
+ for i in xrange (n):
183
+ yield images[i, :], labels[i] # a single entry of data is created each time
184
+ f.close()
185
+ l.close()
186
+ return reader
187
187
188
188
# images_reader_creator creates a reader
189
189
reader = image_reader_creator(" /path/to/image_file" , " /path/to/label_file" , 1024 )
@@ -196,7 +196,7 @@ An example implementation of paddle.train could be:
196
196
197
197
```python
198
198
def train(batch_reader, mapping, batch_size, total_pass):
199
- for pass_idx in range (total_pass):
200
- for mini_batch in batch_reader(): # this loop will never end in online learning.
201
- do_forward_backward(mini_batch, mapping)
199
+ for pass_idx in range (total_pass):
200
+ for mini_batch in batch_reader(): # this loop will never end in online learning.
201
+ do_forward_backward(mini_batch, mapping)
202
202
```
0 commit comments