A better way to solve python numpy RuntimeWarning: overflow encountered in exp

In the custom neural network, when using the sigmoid function, a data overflow overflow error is reported.

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

RuntimeWarning: overflow encountered in exp

According to the test (the test code is as follows), it is because the exponent has extremely large data, which causes the np.exp operation to overflow

def sigmoid(self, x):
    print(x.min())
    return 1.0 / (1 + np.exp(-x))

The general practice on the Internet is as follows, but it cannot be executed when x is an array.

def sigmoid(x):
    if x>=0: #对sigmoid函数优化,避免出现极大的数据溢出
        return 1.0 / (1 + np.exp(-x))
    else:
        return np.exp(x)/(1+np.exp(x))

Running the above code in my Python, x is an array and reports an error as follows:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

There is more than one value of x, and all xs need to be judged.

Therefore, the better modification plan proposed by me is as follows: If you have any questions, please comment and point out.

def sigmoid(self, x):
    y = x.copy()      # 对sigmoid函数优化,避免出现极大的数据溢出
    y[x >= 0] = 1.0 / (1 + np.exp(-x[x >= 0]))
    y[x < 0] = np.exp(x[x < 0]) / (1 + np.exp(x[x < 0]))
    return y

If you think this article is meaningful, please give it a lot of likes, favorites and support, and you can also click the "Reward" button below to reward and support me~

Guess you like

Origin blog.csdn.net/cgy13347250452/article/details/125276177