Skip to content

Commit

Permalink
add GNID
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Imel authored and Nathaniel Imel committed Jul 29, 2023
1 parent be39c8c commit bada161
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/altk/effcomm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,25 @@ def DKL(p, q, axis=None):
return (xlogx(p) - np.where(p > PRECISION, p * np.log2(q + PRECISION), 0)).sum(
axis=axis
)


def gNID(pW_X, pV_X, pX):
"""Compute Generalized Normalized Informational Distance between two encoders.
Args:
pW_X: first encoder of shape `(|meanings|, |words|)`
pV_X: second encoder of shape `(|meanings|, |words|)`
pX: prior over source variables of shape `(|meanings|,)`
"""
if len(pX.shape) == 1:
pX = pX[:, None]
elif pX.shape[0] == 1 and pX.shape[1] > 1:
pX = pX.T
pXW = pW_X * pX
pWV = pXW.T @ (pV_X)
pWW = pXW.T @ (pW_X)
pVV = (pV_X * pX).T @ (pV_X)
score = 1 - MI(pWV) / (np.max([MI(pWW), MI(pVV)]))
return score

0 comments on commit bada161

Please sign in to comment.