# Similarity

Similarity functions.

expected_likelihood(h, r, t, exact=True)[source]

Compute the similarity based on expected likelihood.

$D((\mu_e, \Sigma_e), (\mu_r, \Sigma_r))) = \frac{1}{2} \left( (\mu_e - \mu_r)^T(\Sigma_e + \Sigma_r)^{-1}(\mu_e - \mu_r) + \log \det (\Sigma_e + \Sigma_r) + d \log (2 \pi) \right) = \frac{1}{2} \left( \mu^T\Sigma^{-1}\mu + \log \det \Sigma + d \log (2 \pi) \right)$

with $$\mu_e = \mu_h - \mu_t$$ and $$\Sigma_e = \Sigma_h + \Sigma_t$$.

Parameters
Return type

FloatTensor

Returns

torch.Tensor, shape: (batch_size, num_heads, num_relations, num_tails) The similarity.

kullback_leibler_similarity(h, r, t, exact=True)[source]

Compute the negative KL divergence.

This is done between two Gaussian distributions given by mean mu_* and diagonal covariance matrix sigma_*.

$D((\mu_0, \Sigma_0), (\mu_1, \Sigma_1)) = 0.5 * ( tr(\Sigma_1^-1 \Sigma_0) + (\mu_1 - \mu_0) * \Sigma_1^-1 (\mu_1 - \mu_0) - k + ln (det(\Sigma_1) / det(\Sigma_0)) )$

with $$\mu_e = \mu_h - \mu_t$$ and $$\Sigma_e = \Sigma_h + \Sigma_t$$.

Note

This methods assumes diagonal covariance matrices $$\Sigma$$.

Parameters
Return type

FloatTensor

Returns

torch.Tensor, shape: (s_1, …, s_k) The similarity.