I need to calculate the lambda i score for two given tensors.
def lambda_ij_score(score_i, score_j, label_i, label_j):
if label_i > label_j:
S_i_j = 1
elif label_j > label_i:
S_i_j = -1
else:
S_i_j = 0
return 0.5 * (1 - S_i_j) - (1 / (1 + np.exp(score_i - score_j)))
def compute_lambda_i(scores, labels):
all_costs = []
for i in range(len(scores)):
current_costs = []
for j in range(len(scores)):
if i == j:
continue
cost = lambda_ij_score(scores[i], scores[j], labels[i], labels[j])
current_costs.append(cost)
all_costs.append(np.sum(current_costs))
return torch.FloatTensor(all_costs).unsqueeze(1)
scores is a tensor of size [N, 1] and labels a tensor of size [N].
While this function does work, it seems very inefficient.
I am very new to PyTorch and would like to find out if there is a neat vectorized version to calculate the desired result.
I am happy about any hints on how to do that!