def _weighted_sum(self, alphas, v, contract_dim_a=-3, contract_dim_v=-3):
    num_batch_axes = len(alphas.shape) + contract_dim_a
    pre_str = 'abcdefghij' [:num_batch_axes]
    in_dim_a = -contract_dim_a - 2
    in_dim_v = -contract_dim_v - 2
    in_str_a = 'zyxwv' [:in_dim_a]
    in_str_v = 'zyxwv' [:in_dim_v]
    einsum_str = '{}Q{}M,{}M{}C->{}Q{}C'.format(pre_str, in_str_a, pre_str,
                                                in_str_v, pre_str, in_str_a)
    return tf.einsum(einsum_str, alphas, v)