

def rnn(step_function, inputs, initial_states, go_backwards=False, mask=None, constants=None, unroll=False, input_length=None):
    "Iterates over the time dimension of a tensor.\n\n    # Arguments\n        inputs: tensor of temporal data of shape (samples, time, ...)\n            (at least 3D).\n        step_function:\n            Parameters:\n                input: tensor with shape (samples, ...) (no time dimension),\n                    representing input for the batch of samples at a certain\n                    time step.\n                states: list of tensors.\n            Returns:\n                output: tensor with shape (samples, ...) (no time dimension),\n                new_states: list of tensors, same length and shapes\n                    as 'states'.\n        initial_states: tensor with shape (samples, ...) (no time dimension),\n            containing the initial values for the states used in\n            the step function.\n        go_backwards: boolean. If True, do the iteration over\n            the time dimension in reverse order.\n        mask: binary tensor with shape (samples, time, 1),\n            with a zero for every element that is masked.\n        constants: a list of constant values passed at each step.\n        unroll: with TensorFlow the RNN is always unrolled, but with Theano you\n            can use this boolean flag to unroll the RNN.\n        input_length: not relevant in the TensorFlow implementation.\n            Must be specified if using unrolling with Theano.\n\n    # Returns\n        A tuple (last_output, outputs, new_states).\n\n        last_output: the latest output of the rnn, of shape (samples, ...)\n        outputs: tensor with shape (samples, time, ...) where each\n            entry outputs[s, t] is the output of the step function\n            at time t for sample s.\n        new_states: list of tensors, latest states returned by\n            the step function, of shape (samples, ...).\n    "
    ndim = len(inputs.get_shape())
    assert (ndim >= 3), 'Input should be at least 3D.'
    axes = ([1, 0] + list(range(2, ndim)))
    inputs = tf.transpose(inputs, axes)
    input_list = tf.unpack(inputs)
    if (constants is None):
        constants = []
    states = initial_states
    successive_states = []
    successive_outputs = []
    if go_backwards:
        input_list.reverse()
    if (mask is not None):
        mask = tf.cast(mask, tf.uint8)
        if (len(mask.get_shape()) == (ndim - 1)):
            mask = expand_dims(mask)
        mask = tf.cast(tf.transpose(mask, axes), tf.bool)
        mask_list = tf.unpack(mask)
        if go_backwards:
            mask_list.reverse()
        for (input, mask_t) in zip(input_list, mask_list):
            (output, new_states) = step_function(input, (states + constants))
            tiled_mask_t = tf.tile(mask_t, tf.pack([1, tf.shape(output)[1]]))
            if (len(successive_outputs) == 0):
                prev_output = zeros_like(output)
            else:
                prev_output = successive_outputs[(- 1)]
            output = tf.select(tiled_mask_t, output, prev_output)
            return_states = []
            for (state, new_state) in zip(states, new_states):
                tiled_mask_t = tf.tile(mask_t, tf.pack([1, tf.shape(new_state)[1]]))
                return_states.append(tf.select(tiled_mask_t, new_state, state))
            states = return_states
            successive_outputs.append(output)
            successive_states.append(states)
    else:
        for input in input_list:
            (output, states) = step_function(input, (states + constants))
            successive_outputs.append(output)
            successive_states.append(states)
    last_output = successive_outputs[(- 1)]
    outputs = tf.pack(successive_outputs)
    new_states = successive_states[(- 1)]
    axes = ([1, 0] + list(range(2, len(outputs.get_shape()))))
    outputs = tf.transpose(outputs, axes)
    return (last_output, outputs, new_states)

dataset/ETHPy150Open fchollet/keras/keras/backend/tensorflow_backend.py rnn/original