Compute the minibatch indices for a training dataset. Parameters ---------- X : :py:class:`ndarray ` of shape `(N, \*)` The dataset to divide into minibatches. Assumes the first dimension represents the number of training examples. batchsize : int
(X, batchsize=256, shuffle=True)
| 6 | |
| 7 | |
| 8 | def minibatch(X, batchsize=256, shuffle=True): |
| 9 | """ |
| 10 | Compute the minibatch indices for a training dataset. |
| 11 | |
| 12 | Parameters |
| 13 | ---------- |
| 14 | X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, \*)` |
| 15 | The dataset to divide into minibatches. Assumes the first dimension |
| 16 | represents the number of training examples. |
| 17 | batchsize : int |
| 18 | The desired size of each minibatch. Note, however, that if ``X.shape[0] % |
| 19 | batchsize > 0`` then the final batch will contain fewer than batchsize |
| 20 | entries. Default is 256. |
| 21 | shuffle : bool |
| 22 | Whether to shuffle the entries in the dataset before dividing into |
| 23 | minibatches. Default is True. |
| 24 | |
| 25 | Returns |
| 26 | ------- |
| 27 | mb_generator : generator |
| 28 | A generator which yields the indices into X for each batch |
| 29 | n_batches: int |
| 30 | The number of batches |
| 31 | """ |
| 32 | N = X.shape[0] |
| 33 | ix = np.arange(N) |
| 34 | n_batches = int(np.ceil(N / batchsize)) |
| 35 | |
| 36 | if shuffle: |
| 37 | np.random.shuffle(ix) |
| 38 | |
| 39 | def mb_generator(): |
| 40 | for i in range(n_batches): |
| 41 | yield ix[i * batchsize : (i + 1) * batchsize] |
| 42 | |
| 43 | return mb_generator(), n_batches |
| 44 | |
| 45 | |
| 46 | ####################################################################### |
no test coverage detected