參考:https://blog.csdn.net/zziahgf/article/details/78489562
pytorch:
def log_sum_exp(x):
"""Utility function for computing log_sum_exp while determining
This will be used to determine unaveraged confidence loss across
all examples in a batch.
Args:
x (Variable(tensor)): conf_preds from conf layers
"""
x_max = x.data.max()
return torch.log(torch.sum(torch.exp(x-x_max), 1, keepdim=True)) + x_max
假設 NN 個 值的資料集 {xn}Nn=1{xn}n=1N,需要計算的值為:
z=log∑Nn=1exp{xn}z=log∑n=1Nexp{xn}
當采用 softmax 對 multinomial 分布進行參數化時,比如,logistic 回歸和有多于 2 個無序類别時,這種計算經常出現.
如果想要計算 log likehood,由于存在歸一化常數,也會看到這種表達式.
直接進行計算,則是災難性的,因為存在下溢(underflow) 和上溢(overflow), 取決于 xnxn 的尺度(sc