MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / next_batch

Method next_batch

neural_network/input_data.py:200–243  ·  view source on GitHub ↗

Return the next `batch_size` examples from this data set.

(self, batch_size, fake_data=False, shuffle=True)

Source from the content-addressed store, hash-verified

198 return self._epochs_completed
199
200 def next_batch(self, batch_size, fake_data=False, shuffle=True):
201 """Return the next `batch_size` examples from this data set."""
202 if fake_data:
203 fake_image = [1] * 784
204 fake_label = [1] + [0] * 9 if self.one_hot else 0
205 return (
206 [fake_image for _ in range(batch_size)],
207 [fake_label for _ in range(batch_size)],
208 )
209 start = self._index_in_epoch
210 # Shuffle for the first epoch
211 if self._epochs_completed == 0 and start == 0 and shuffle:
212 perm0 = np.arange(self._num_examples)
213 self._rng.shuffle(perm0)
214 self._images = self.images[perm0]
215 self._labels = self.labels[perm0]
216 # Go to the next epoch
217 if start + batch_size > self._num_examples:
218 # Finished epoch
219 self._epochs_completed += 1
220 # Get the rest examples in this epoch
221 rest_num_examples = self._num_examples - start
222 images_rest_part = self._images[start : self._num_examples]
223 labels_rest_part = self._labels[start : self._num_examples]
224 # Shuffle the data
225 if shuffle:
226 perm = np.arange(self._num_examples)
227 self._rng.shuffle(perm)
228 self._images = self.images[perm]
229 self._labels = self.labels[perm]
230 # Start next epoch
231 start = 0
232 self._index_in_epoch = batch_size - rest_num_examples
233 end = self._index_in_epoch
234 images_new_part = self._images[start:end]
235 labels_new_part = self._labels[start:end]
236 return (
237 np.concatenate((images_rest_part, images_new_part), axis=0),
238 np.concatenate((labels_rest_part, labels_new_part), axis=0),
239 )
240 else:
241 self._index_in_epoch += batch_size
242 end = self._index_in_epoch
243 return self._images[start:end], self._labels[start:end]
244
245
246@deprecated(None, "Please write your own downloading logic.")

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected