def sparse_hermitian_scoring(params, tuples):
"""
TensorFlow operator that scores tuples by the dot product of their complex embeddings.
It is the same a the multilinear function, but uses complex embeddings instead. The complex
embeddings are of size 2 * R where R is the complex
dimension. They are encoded such that the first columns correspond to the real part, and the last R correspond to
the imaginary part. The result of this function is a length-N vector with values:
S[i] = alpha_0 * Re(sum_j E[I[i,1],j] * E[I[i,2],j]) + alpha_1 * Im(sum_j E[I[i,1],j] * E[I[i,2],j]))
Where:
- I is the tuple tensor of integers with shape (T, 2)
- E is the N * 2R tensor of complex embeddings (R first columns: real part, the last R columsn: imaginary part)
- alpha_0 and alpha_1 are the symmetry coefficients
:param params: tuple (embeddings, symm_coef) containing:
- embeddings: a real tensor of size [N, 2*R] containing the N rank-R embeddings by row (real part in the rank first R
columns, imaginary part in the last R columns)
- the 2-tuple (s0, s1) of symmetry coefficients (or complex-to-real projection coefficients) that are used to
transform the complex result of the dot product into a real number, as used by most statistical models (e.g.
mean of a Gaussian or Poisson distributions, natural parameter of a Bernouilli distribution). The conversion
from complexto real is a simple weighted sum: results = s0 * Re(<e_i, e_j>) + s1 * Im(<e_i, e_j>
:param tuples: tuple matrix of size [T, 2] containing T pairs of integers corresponding to the indices of the
embeddings.
:return: Hermitian dot products of selected embeddings
>>> embeddings = (tf.Variable([[1., 1, 0, 3], [0, 1, 0, 1], [-1, 1, 1, 5]]), (0.0, 1.0))
>>> idx = tf.Variable([[0, 1], [1, 0], [0, 2], [2, 0], [1, 2], [2, 1]])
>>> g = sparse_hermitian_scoring(embeddings, idx)
>>> print(tf_eval(g))
[-2. 2. 3. -3. 4. -4.]
"""
emb, symmetry = params
pred_re, pred_im = sparse_hermitian_product(emb, tuples)
return symmetry[0] * pred_re + symmetry[1] * pred_im
评论列表
文章目录