def evaluate(data_source, batch_size, ctx=None):
total_L = <NUM_LIT:0><EOL>hidden = cache_cell.begin_state(func=mx.nd.zeros, batch_size=batch_size, ctx=context[<NUM_LIT:0>])<EOL>next_word_history = None<EOL>cache_history = None<EOL>for i in range(<NUM_LIT:0>, len(data_source) - <NUM_LIT:1>, args.bptt):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>'%<EOL>(i, len(data_source), math.exp(total_L/i)))<EOL><DEDENT>data, target = get_batch(data_source, i)<EOL>data = data.as_in_context(ctx)<EOL>target = target.as_in_context(ctx)<EOL>L = <NUM_LIT:0><EOL>outs, next_word_history, cache_history, hidden =cache_cell(data, target, next_word_history, cache_history, hidden)<EOL>for out in outs:<EOL><INDENT>L += (-mx.nd.log(out)).asscalar()<EOL><DEDENT>total_L += L / data.shape[<NUM_LIT:1>]<EOL>hidden = detach(hidden)<EOL><DEDENT>return total_L / len(data_source)<EOL>
Evaluate the model on the dataset with cache model.

    Parameters
    ----------
    data_source : NDArray
        The dataset is evaluated on.
    batch_size : int
        The size of the mini-batch.
    ctx : mx.cpu() or mx.gpu()
        The context of the computation.

    Returns
    -------
    loss: float
        The loss on the dataset