import tensorflow as tf

        bw_result = directional_attention_with_selections(
            rep_tensor, rep_mask, dep_selection, head_selection,
            'backward', hn, keep_unselected,
            'backward_resa', keep_prob, is_train, wd, activation
        )
        return tf.concat([fw_result, bw_result], -1)


def directional_attention_with_selections(
        rep_tensor, rep_mask, dep_selection, head_selection, direction=None, hn=None, keep_unselected=True,
        scope=None, keep_prob=1., is_train=None, wd=0., activation='elu'):

    bs, sl, vec = tf.shape(rep_tensor)[0], tf.shape(rep_tensor)[1], tf.shape(rep_tensor)[2]
    org_ivec = rep_tensor.get_shape().as_list()[2]
    ivec = hn or org_ivec

    with tf.variable_scope(scope or 'directional_attention_%s' % direction or 'diag'):
        # non-linear
        rep_map = bn_dense_layer(rep_tensor, ivec, True, 0., 'bn_dense_map', activation,
                                 False, wd, keep_prob, is_train)
        # ensure the seletion is right
        dep_selection = tf.logical_and(rep_mask, dep_selection)
        head_selection = tf.logical_and(rep_mask, head_selection)
        rep_dep_tensor, rep_dep_mask, dep_org_idx = reduce_data_rep_max_len(rep_map, dep_selection)
