我一直在研究log-sum-exp问题.我有一个以对数形式存储的数字列表,我希望将它们相加并存储在对数中.
天真的算法是
def naive(listOfLogs):
return math.log10(sum(10**x for x in listOfLogs))
Run Code Online (Sandbox Code Playgroud)
许多网站包括: 在C中实现logsumexp? 和 http://machineintelligence.tumblr.com/post/4998477107/ 推荐使用
def recommend(listOfLogs):
maxLog = max(listOfLogs)
return maxLog + math.log10(sum(10**(x-maxLog) for x in listOfLogs))
Run Code Online (Sandbox Code Playgroud)
又名
def recommend(listOfLogs):
maxLog = max(listOfLogs)
return maxLog + naive((x-maxLog) for x in listOfLogs)
Run Code Online (Sandbox Code Playgroud)
我不明白的是,如果推荐算法更好,为什么我们应该递归调用它?那能提供更多的好处吗?
def recursive(listOfLogs):
maxLog = max(listOfLogs)
return maxLog + recursive((x-maxLog) for x in listOfLogs)
Run Code Online (Sandbox Code Playgroud)
虽然我问是否还有其他技巧可以让这个计算在数值上更稳定?