lm-similarity / src /utils.py
Joschka Strueber
[Ref, Fix] indentation error in answer key selection, longer explanation in demo, exclusion of broken dataset
c608f7f
raw
history blame
759 Bytes
import numpy as np
def softmax(logits: np.ndarray) -> np.ndarray:
exp_logits = np.exp(logits - np.max(logits))
return exp_logits / exp_logits.sum(axis=0)
def one_hot(probs: np.array) -> np.array:
one_hot = np.zeros_like(probs)
one_hot[np.argmax(probs)] = 1
return one_hot
def opt_in_pars_to_index(s):
if s.startswith("(") and s.endswith(")"):
letter = s[1] # Extract the letter inside the parentheses
return ord(letter) - ord("A") # Convert to zero-based index
else:
raise ValueError("Invalid format")
def get_test_target(doc):
if "target" in doc:
return doc["target"], "target"
elif "answer" in doc:
return doc["answer"], "answer"
else:
return "", ""