MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / minibatch

Function minibatch

numpy_ml/preprocessing/general.py:16–51  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

mb_generatorFunction · 0.70

Tested by

no test coverage detected