import tensorflow as tf

        source_length = encoder_input_length[0]
        target_length = tf.to_int32(tf.reduce_sum(trg_mask, axis=1))
        true_src_len = tf.reshape(source_length, shape=[batch_size, 1, 1]) - 1
        true_trg_len = tf.reshape(target_length, shape=[batch_size, 1, 1]) - 1

        src_mask = tf.to_float(tf.sequence_mask(source_length, maxlen=src_len))
        mask = tf.matmul(tf.expand_dims(trg_mask, axis=2), tf.expand_dims(src_mask, axis=1))

        monotonous = tf.sqrt(((true_trg_len * src_indices - true_src_len * trg_indices) ** 2)
                             / (true_trg_len**2 + true_src_len**2))
        monotonous = tf.to_float(monotonous < monotonicity_dist)
        non_monotonous = (1 - monotonous) * mask
        attn_loss = tf.reduce_sum(attention_weights * tf.stop_gradient(non_monotonous)) / tf.to_float(batch_size)

        if monotonicity_decay:
            decay = tf.stop_gradient(0.5 ** (tf.to_float(global_step) / monotonicity_decay))
        else:
            decay = 1.0

        xent_loss += monotonicity_weight * decay * attn_loss

    losses = [xent_loss, reinforce_loss, baseline_loss_]

    return losses, [outputs], encoder_state, attention_states, attention_weights, samples, beam_fun, initial_data
