Compute one-directional InfoNCE loss for two aligned embedding batches. Build the similarity logits:
S=τZ1Z2TTreat entries Sii as positive pairs and every entry in row i as a candidate:
L=−N1i=1∑Nlog(∑j=1NeSijeSii)Here, N is batch size, τ is temperature, and row i of Z1 is paired with row i of Z2. Subtract each row maximum before exponentiation and return the mean loss as a Python float.
Input: Z1 = [[1, 0], [0, 1]], Z2 = [[1, 0], [0, 1]], temperature = 0.1
Output: 0.000045
Explanation: Each diagonal similarity is much larger than the competing similarity in its row.
Input: Z1 = [[1, 0], [0, 1]], Z2 = [[0, 1], [1, 0]], temperature = 0.1
Output: 10.000045
Input: Z1 = [[1, 0], [0, 1]], Z2 = [[1, 0], [0, 1]], temperature = 1.0
Output: 0.313262
Use Z1 @ Z2.T / temperature to build the logits.
Subtract np.max(logits, axis=1, keepdims=True) before exponentiation.
Use np.diag(shifted) for the positive-pair logits.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Compute one-directional InfoNCE loss for two aligned embedding batches. Build the similarity logits:
S=τZ1Z2TTreat entries Sii as positive pairs and every entry in row i as a candidate:
L=−N1i=1∑Nlog(∑j=1NeSijeSii)Here, N is batch size, τ is temperature, and row i of Z1 is paired with row i of Z2. Subtract each row maximum before exponentiation and return the mean loss as a Python float.
Input: Z1 = [[1, 0], [0, 1]], Z2 = [[1, 0], [0, 1]], temperature = 0.1
Output: 0.000045
Explanation: Each diagonal similarity is much larger than the competing similarity in its row.
Input: Z1 = [[1, 0], [0, 1]], Z2 = [[0, 1], [1, 0]], temperature = 0.1
Output: 10.000045
Input: Z1 = [[1, 0], [0, 1]], Z2 = [[1, 0], [0, 1]], temperature = 1.0
Output: 0.313262
Use Z1 @ Z2.T / temperature to build the logits.
Subtract np.max(logits, axis=1, keepdims=True) before exponentiation.
Use np.diag(shifted) for the positive-pair logits.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number