在python中实现sigmoid函数 [英] Implementing sigmoid function in python
问题描述
我正在尝试为XOR函数实现一个简单的神经网络。
我使用的激活函数是Sigmoid函数。
sigmoid函数的代码为:
I am trying to implement a simple neural network for XOR function. The activation function I am using is Sigmoid function. The code for the sigmoid function is:
def ActivationFunction(a)
e = 2.671 # Sigmoid Function
expo = e ** a
val = expo / (1 + expo)
return val
我的问题是此函数始终返回介于0.7和0.8之间的值。这个问题在输出过程中显示出主要影响。
My problem is that this function is always returning a value between 0.7 and 0.8. This problem is showing a major effect in the output process.
任何建议都会得到应用。
Any suggestions would be appriciated.
推荐答案
您的函数已正确实现,但是 e
的值不正确。
Your function is implemented correctly, however, the value of e
is incorrect.
我建议导入数学
并使用预定义的 e
从此处常量。
I'd recommend importing math
and using the predefined e
constant from there.
import math
def sigmoid(x):
return 1 / (1 + math.e ** -x) # mathematically equivalent, but simpler
导数:
def sigmoid_derivative(a):
return a * (1 - a)
其中 a
是向前传递的隐藏激活。
Where a
is the hidden activation from the forward pass.
除此之外,我认为您的实现没有错。因此,如果您仍然在修复后仍无法获得所需的价值,那么麻烦的原因就在其他地方。
Besides this, I see nothing wrong with your implementation. So if you're still getting values you don't expect after the fix, the cause of the trouble lies elsewhere.
这篇关于在python中实现sigmoid函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!