human
Complete this python program:
def dynamic_decode(decoder, output_time_major=False, impute_finished=False, maximum_iterations=None, parallel_iterations=32, swap_memory=False, scope=None):
assistant
#Here is the Python code you requested.

    """Perform dynamic decoding with `decoder`.
    Args:
    decoder: A `Decoder` instance.
    output_time_major: Python boolean.  Default: `False` (batch major).  If
    `True`, outputs are returned as time major tensors (this mode is faster).
    Otherwise, outputs are returned as batch major tensors (this adds extra
    time to the computation).
    impute_finished: Python boolean.  If `True`, then states for batch
    entries which are marked as finished get copied through and the
    corresponding outputs get zeroed out.  This causes some slowdown at
    each time step, but ensures that the final state and outputs have
    the correct values and that backprop ignores time steps that were
    marked as finished.
    maximum_iterations: `int32` scalar, maximum allowed number of decoding
    steps.  Default is `None` (decode until the decoder is fully done).
    parallel_iterations: Argument passed to `tf.while_loop`.
    swap_memory: Argument passed to `tf.while_loop`.
    scope: Optional variable scope to use.
    Returns:
    `(final_outputs, final_state)`.
    Raises:
    TypeError: if `decoder` is not an instance of `Decoder`.
    ValueError: if maximum_iterations is provided but is not a scalar.
    """
    if (not isinstance(decoder, Decoder)):
        raise TypeError(('Expected    decoder    to    be    type    Decoder,    but    saw:    %s' % type(decoder)))
    with variable_scope.variable_scope((scope or 'decoder')) as varscope:
        if (varscope.caching_device is None):
            varscope.set_caching_device((lambda op: op.device))
        if (maximum_iterations is not None):
            maximum_iterations = ops.convert_to_tensor(maximum_iterations, dtype=dtypes.int32, name='maximum_iterations')
            if (maximum_iterations.get_shape().ndims != 0):
                raise ValueError('maximum_iterations    must    be    a    scalar')
        (initial_finished, initial_inputs, initial_state) = decoder.initialize()
        zero_outputs = _create_zero_outputs(decoder.output_size, decoder.output_dtype, decoder.batch_size)
        if (maximum_iterations is not None):
            initial_finished = math_ops.logical_or(initial_finished, (0 >= maximum_iterations))
        initial_time = constant_op.constant(0, dtype=dtypes.int32)
        def _shape(batch_size, from_shape):
            if (not isinstance(from_shape, tensor_shape.TensorShape)):
                return tensor_shape.TensorShape(None)
            else:
                batch_size = tensor_util.constant_value(ops.convert_to_tensor(batch_size, name='batch_size'))
                return tensor_shape.TensorShape([batch_size]).concatenate(from_shape)
        def _create_ta(s, d):
            return tensor_array_ops.TensorArray(dtype=d, size=0, dynamic_size=True, element_shape=_shape(decoder.batch_size, s))
        initial_outputs_ta = nest.map_structure(_create_ta, decoder.output_size, decoder.output_dtype)
        def condition(unused_time, unused_outputs_ta, unused_state, unused_inputs, finished):
            return math_ops.logical_not(math_ops.reduce_all(finished))
        def body(time, outputs_ta, state, inputs, finished):
            'Internal    while_loop    body.\n\n                        Args:\n                                time:   ...