Implement the log softmax function. Given a 1D array of logits, compute the log of the softmax probabilities in a numerically stable way.
import numpy as np
def log_softmax(logits):
logits = np.array(logits, dtype=np.float64)
max_val = np.max(logits)
shifted = logits - max_val
log_sum_exp = max_val + np.log(np.sum(np.exp(shifted)))
return (logits - log_sum_exp).tolist()exp).log(sum(exp(shifted))) and add back the max to get log_sum_exp.logit - log_sum_exp.