【发布时间】:2019-05-16 18:23:15
【问题描述】:
我正在尝试为 Mnist 数据库构建神经网络。在计算 softmax 函数时,我收到与“您无法存储该大小的浮点数”相同的错误
代码如下:
def softmax(vector): # REQUIRES a unidimensional numpy array
adjustedVals = [0] * len(vector)
totalExp = np.exp(vector)
print("totalExp equals")
print(totalExp)
totalSum = totalExp.sum()
for i in range(len(vector)):
adjustedVals[i] = (np.exp(vector[i])) / totalSum
return adjustedVals # this throws back an error sometimes?!?!
经过检查,最推荐使用十进制模块。但是,当我弄乱了这个模块在命令行中使用的值时,那就是:
from decimal import Decimal
import math
test = Decimal(math.exp(720))
对于 math.exp(>709) 的任何值,我都会收到类似的错误。
OverflowError: (34, 'Numerical result out of range')
我的结论是即使十进制也无法处理这个数字。有谁知道我可以用来表示这些非常大的浮点数的另一种方法。
【问题讨论】:
-
我认为你使用 Decimal 不正确,你仍然在调用 math.exp(720) ,这同样存在数量太大的问题,不管你要去的事实如何将它传递给十进制,它还不知道。我想你想要 Decimal(720).exp()
标签: python-3.x machine-learning neural-network floating-point decimal