Select Batch Method

Randomly select a batch of samples from the training data.

Parameters:

Name Type Description Default
X_train ndarray

Input training data.

required
Y_train ndarray

True labels for training data.

required
batch_size int

The size of the batch to select.

required

Returns:

Name Type Description
tuple

(X_batch, Y_batch) X_batch (numpy.ndarray): The selected batch of input data. Y_batch (numpy.ndarray): The corresponding batch of labels.

Example
X_batch, Y_batch = select_batches(X_train, Y_train, 32)
print(X_batch.shape, Y_batch.shape)
Note

This function handles both 1D and 2D label arrays (Y_train).

Source code in microkeras/optimizers/select_batch.py
def select_batches(X_train, Y_train, batch_size):
    """
    Randomly select a batch of samples from the training data.

    Args:
        X_train (numpy.ndarray): Input training data.
        Y_train (numpy.ndarray): True labels for training data.
        batch_size (int): The size of the batch to select.

    Returns:
        tuple: (X_batch, Y_batch)
            X_batch (numpy.ndarray): The selected batch of input data.
            Y_batch (numpy.ndarray): The corresponding batch of labels.

    Example:
        ```python
        X_batch, Y_batch = select_batches(X_train, Y_train, 32)
        print(X_batch.shape, Y_batch.shape)
        ```

    Note:
        This function handles both 1D and 2D label arrays (Y_train).
    """
    m = X_train.shape[1]  # number of training examples
    batch_indices = np.random.choice(m, batch_size, replace=False)
    X_batch = X_train[:, batch_indices]

    # Check if Y_train is 1D or 2D and select accordingly
    if Y_train.ndim == 1:
        Y_batch = Y_train[batch_indices]
    else:
        Y_batch = Y_train[:, batch_indices]

    return X_batch, Y_batch