import tensorflow as tf


    target_weights = get_weights(targets[1][:, 1:], utils.EOS_ID, include_first_eos=True)
    xent_loss += reconstruction_weight * sequence_loss(logits=reconstructed_outputs, targets=targets[1][:, 1:],
                                                       weights=target_weights)

    max_src_len = tf.shape(reconstructed_weights)[1]
    batch_size = tf.shape(reconstructed_weights)[0]

    attn_loss = tf.matmul(reconstructed_weights, attention_weights) - tf.eye(max_src_len)

    src_mask = tf.sequence_mask(encoder_input_length[0], maxlen=max_src_len, dtype=tf.float32)
    src_mask = tf.einsum('ij,ik->ijk', src_mask, src_mask)
    attn_loss *= tf.to_float(src_mask)    # don't take padding words into account

    attn_loss = tf.norm(attn_loss) / tf.to_float(batch_size)
    xent_loss += reconstruction_attn_weight * attn_loss

    attention_weights = [attention_weights, reconstructed_weights]
    losses = [xent_loss, None, None]
    return losses, [outputs], encoder_state, attention_states, attention_weights, samples, beam_fun, initial_data


def chained_encoder_decoder(encoders, decoders, encoder_inputs, targets, feed_previous,
                            chaining_strategy=None, align_encoder_id=0, chaining_non_linearity=False,
