def calc_error_by_batches(model, data_size, batch_size):
    """Calculate error in batches using the given valid/test model."""
    err = 0.0
    beg = 0
    while (beg < data_size):
        end = min(beg+batch_size, data_size)
        err += model(beg,end) * (end-beg)
        beg = end
    return err / data_size