def iterate_function(func, list_of_minibatch_args, minibatch_size,
                     list_of_non_minibatch_args=None,
                     list_of_minibatch_functions=[make_minibatch],
                     list_of_output_names=None,
                     n_epochs=1000, n_status=50, status_func=None,
                     previous_epoch_results=None,
                     shuffle=False, random_state=None):
    """
    Minibatch arguments should come first.

    Constant arguments which should not be iterated can be passed as
    list_of_non_minibatch_args.

    If list_of_minbatch_functions is length 1, will be replicated to length of
    list_of_args - applying the same function to all minibatch arguments in
    list_of_args. Otherwise, this should be the same length as list_of_args

    list_of_output_names simply names the output of the passed in function.
    Should be the same length as the number of outputs from the function.

    status_func is a function run periodically (based on n_status_points),
    which allows for validation, early stopping, checkpointing, etc.

    previous_epoch_results allows for continuing from saved checkpoints

    shuffle and random_state are used to determine if minibatches are run
    in sequence or selected randomly each epoch.

    By far the craziest function in this file.

    Example validation function:
    n_chars = 84
    text_minibatcher = gen_text_minibatch_func(n_chars)

    cost_function returns one value, the cost for that minibatch

    valid_results = iterate_function(
        cost_function, [X_clean_valid, y_clean_valid], minibatch_size,
        list_of_output_names=["valid_cost"],
        list_of_minibatch_functions=[text_minibatcher], n_epochs=1,
        shuffle=False)

    Example training loop:

    fit_function returns 3 values, nll, kl and the total cost

    epoch_results = iterate_function(fit_function, [X, y], minibatch_size,
                                 list_of_output_names=["nll", "kl", "cost"],
                                 n_epochs=2000,
                                 status_func=status_func,
                                 previous_epoch_results=previous_epoch_results,
                                 shuffle=True,
                                 random_state=random_state)
    """
    if previous_epoch_results is None:
        epoch_results = defaultdict(list)
    else:
        epoch_results = previous_epoch_results
    # Input checking and setup
    if shuffle:
        assert random_state is not None
    status_points = list(range(n_epochs))
    if len(status_points) >= n_status:
        intermediate_points = status_points[::n_epochs // n_status]
        status_points = intermediate_points + [status_points[-1]]
    else:
        status_points = range(len(status_points))

    for arg in list_of_minibatch_args:
        assert len(arg) == len(list_of_minibatch_args[0])

    indices = minibatch_indices(list_of_minibatch_args[0], minibatch_size)
    if len(list_of_minibatch_args[0]) % minibatch_size != 0:
        print ("length of dataset should be evenly divisible by "
               "minibatch_size.")
    if len(list_of_minibatch_functions) == 1:
        list_of_minibatch_functions = list_of_minibatch_functions * len(
            list_of_minibatch_args)
    else:
        assert len(list_of_minibatch_functions) == len(list_of_minibatch_args)
    # Function loop
    for e in range(n_epochs):
        results = defaultdict(list)
        if shuffle:
            random_state.shuffle(indices)
        for i, j in indices:
            minibatch_args = []
            for n, arg in enumerate(list_of_minibatch_args):
                minibatch_args += list_of_minibatch_functions[n](arg, i, j)
            if list_of_non_minibatch_args is not None:
                all_args = minibatch_args + list_of_non_minibatch_args
            else:
                all_args = minibatch_args
            minibatch_results = func(*all_args)
            if type(minibatch_results) is not list:
                minibatch_results = [minibatch_results]
            for n, k in enumerate(minibatch_results):
                if list_of_output_names is not None:
                    assert len(list_of_output_names) == len(minibatch_results)
                    results[list_of_output_names[n]].append(
                        minibatch_results[n])
                else:
                    results[n].append(minibatch_results[n])
        avg_output = {r: np.mean(results[r]) for r in results.keys()}
        for k in avg_output.keys():
            epoch_results[k].append(avg_output[k])
        if status_points in e:
            if status_func is not None:
                epoch_number = e
                status_number = np.searchsorted(status_points, e)
                status_func(status_number, epoch_number, epoch_results)
    return epoch_results
dataset/ETHPy150Open kastnerkyle/SciPy2015/graphics_code/cvae/kdl_template.py/iterate_function/swapped operands of `in`