python画sigmoid函数_在python中实现sigmoid函数

1586010002-jmsa.png

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

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.

解决方案

Your function is implemented correctly, however, the value of e is incorrect.

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

And, accordingly, the derivative:

def sigmoid_derivative(a):

return a * (1 - 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sigmoid函数是一种常用的数学函数,具有非常广泛的应用,比如在神经网络常用作激活函数Python是一种功能强大的编程语言,可以轻松实现sigmoid函数的图像绘制和标注。 要绘制sigmoid函数的图像,需要使用Python的数学库和绘图库。常用的数学库包括numpy和math,常用的绘图库包括matplotlib和seaborn。下面是使用matplotlib库绘制sigmoid函数图像的代码: ```python import numpy as np import matplotlib.pyplot as plt def sigmoid(x): return 1 / (1 + np.exp(-x)) x = np.linspace(-10, 10, 100) y = sigmoid(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('sigmoid(x)') plt.title('Sigmoid Function') plt.show() ``` 这段代码首先定义了一个sigmoid函数,然后生成了一个-10到10的100个点的数组x,并通过sigmoid函数计算出相应的y值。最后使用plot函数将x和y绘制成曲线,并加上x轴标签、y轴标签和标题。运行这段代码可以得到一个sigmoid函数的图像,如下所示: ![sigmoid](https://img-blog.csdn.net/20180117145301408) 除了绘制图像,我们还可以通过annotate函数向图像添加注释,以便更好地解释和理解sigmoid函数的性质。例如,我们可以加上一个箭头和一段文字,标注出sigmoid函数在x=0处的取值为0.5。修改上面的代码,加上注释: ```python import numpy as np import matplotlib.pyplot as plt def sigmoid(x): return 1 / (1 + np.exp(-x)) x = np.linspace(-10, 10, 100) y = sigmoid(x) plt.plot(x, y) plt.annotate('sigmoid(x=0) = 0.5', xy=(0, 0.5), xytext=(3, 0.8), arrowprops=dict(facecolor='black', shrink=0.05)) plt.xlabel('x') plt.ylabel('sigmoid(x)') plt.title('Sigmoid Function') plt.show() ``` 这段代码使用annotate函数表示在图像加入一个文字和一个箭头。其,xy参数指定箭头起始点的坐标,xytext参数指定注释文字的坐标,arrowprops参数指定箭头的属性。 运行这段代码,我们可以看到图像的注释。这个注释表明sigmoid函数在x=0处的取值为0.5,也就是函数曲线在该点处经过y=0.5的水平线。 总之,Python可以轻松实现sigmoid函数图像的绘制和标注,使我们更好地理解和应用这个函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值