def train_epoch(x, y, train_fn, batchsize):
    """Train batch-wise.

    Parameters
    ----------

    x: np.array
        Input samples.
    y: np.array
        Target classes.
    train_fn: theano.function
        Theano function for training.
    batchsize: int
        Batch size.

    Returns
    -------

    : float
        The training loss.
    """

    batches = int(len(x) / batchsize)
    loss = 0
    for i in range(batches):
        loss += train_fn(x[i * batchsize:(i + 1) * batchsize],
                         y[i * batchsize:(i + 1) * batchsize])
    return loss / batches