def sparse_hermitian_product(emb, tuples):
"""
Compute the Hermitian inner product between selected complex embeddings
This corresponds to the usual dot product applied on the conjugate of the first vector: <conj(x), y>
where conj is the complex conjugate (obtained by inverting the imaginary part)
We consider that the embedding dimension is twice the rank, where the first part is in embeddings[:,:rk] and
the imaginary part is in embeddings[:,rk:].
It computes
S[i] = <conj(E[I[i,1]], E[I[i,2]]>
Usage:
S = sparse_hermitian_product(E, I):
:param emb: embedding matrix of size [n_emb, 2 * r] containing float numbers where r is the complex rank
:param tuples: tuple matrix of size [n_t, 2] containing integers that correspond to the indices of the embeddings
:return: a pair containing the real and imaginary parts of the Hermitian dot products
"""
rk = emb.get_shape()[1].value // 2
emb_re = emb[:, :rk]
emb_im = emb[:, rk:]
emb_sel_a_re = tf.gather(emb_re, tuples[:, 0])
emb_sel_a_im = tf.gather(emb_im, tuples[:, 0])
emb_sel_b_re = tf.gather(emb_re, tuples[:, 1])
emb_sel_b_im = tf.gather(emb_im, tuples[:, 1])
pred_re = tf.reduce_sum(tf.mul(emb_sel_a_re, emb_sel_b_re) + tf.mul(emb_sel_a_im, emb_sel_b_im), 1)
pred_im = tf.reduce_sum(tf.mul(emb_sel_a_re, emb_sel_b_im) - tf.mul(emb_sel_a_im, emb_sel_b_re), 1)
return pred_re, pred_im
评论列表
文章目录