python警告异常而不停止程序

2024-04-20 01:46:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚在做神经网络的时候遇到了这个错误,这个错误是在我通过激活函数(sigmoid)中的节点时产生的,我怀疑这个错误是因为它是一个卷积神经网络,所以卷积层使用ReLU作为激活函数,可以使数变得非常大;在我的sigmoid函数中:

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoidprime(x):
    return np.exp(-x) / ((1 + np.exp(-x))**2)

在函数中,x是我的节点,但有时这个数字会变得很大,而且由于sigmoid函数只在0和1之间有效,我相信这是引起错误的原因,但是当我尝试使用一个非常大的数字时,它不会引起错误,而且,错误实际上不会停止我的程序。我想知道的是如何防止异常,以及即使出现错误,我的激活函数是否通过

Warning (from warnings module): File "C:\Python27\saves\CNN\CNN_flat_layer.py", line 17 return np.exp(-x) / ((1 + np.exp(-x))**2) RuntimeWarning: overflow encountered in exp

Warning (from warnings module): File "C:\Python27\saves\CNN\CNN_flat_layer.py", line 17 return np.exp(-x) / ((1 + np.exp(-x))**2) RuntimeWarning: overflow encountered in square

Warning (from warnings module): File "C:\Python27\saves\CNN\CNN_flat_layer.py", line 17 return np.exp(-x) / ((1 + np.exp(-x))**2) RuntimeWarning: invalid value encountered in divide


Tags: 函数fromlayerreturn错误npcnnfile