• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python3 用matplotlib绘制sigmoid函数的案例

python 搞代码 4年前 (2022-01-08) 8次浏览 已收录 0个评论

这篇文章主要介绍了Python3 用matplotlib绘制sigmoid函数的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧~

 import matplotlib.pyplot as plt import numpy as np def sigmoid(x): # 直接返回sigmoid函数 return 1. / (1. + np.exp(-x)) def plot_sigmoid(): # param:起点,终点,间距 x = np.arange(-8, 8, 0.2) y = sigmoid(x) plt.plot(x, y) plt.show() if __name__ == '__main__': plot_sigmoid()

如图:

补充知识:python:实现并绘制 sigmoid函数,tanh函数,ReLU函数,PReLU函数

如下所示:

 # -*- coding:utf-8 -*- from matplotlib import pyplot as plt import numpy as np import mpl_toolkits.axisartist as axisartist def sigmoid(x): return 1. / (1 + np.exp(-x)) def tanh(x): return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x)) def relu(x): return np.where(x<0,0,x) def prelu(x): return np.where(x",size=1.5) ax.spines['left'].set_position(('data', 0)) ax.plot(x, y) plt.xlim([-10.05, 10.05]) plt.ylim([-0.02, 1.02]) plt.tight_layout() plt.savefig("sigmoid.png-600") plt.show() def plot_tanh(): x = np.arange(-10, 10, 0.1) y = tanh(x) fig = plt.figure() ax = fig.add_subplot(111) ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') # ax.sp<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码*网</strong>ines['bottom'].set_color('none') # ax.spines['left'].set_color('none') ax.spines['left'].set_position(('data', 0)) ax.spines['bottom'].set_position(('data', 0)) ax.plot(x, y) plt.xlim([-10.05, 10.05]) plt.ylim([-1.02, 1.02]) ax.set_yticks([-1.0, -0.5, 0.5, 1.0]) ax.set_xticks([-10, -5, 5, 10]) plt.tight_layout() plt.savefig("tanh.png-600") plt.show() def plot_relu(): x = np.arange(-10, 10, 0.1) y = relu(x) fig = plt.figure() ax = fig.add_subplot(111) ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') # ax.spines['bottom'].set_color('none') # ax.spines['left'].set_color('none') ax.spines['left'].set_position(('data', 0)) ax.plot(x, y) plt.xlim([-10.05, 10.05]) plt.ylim([0, 10.02]) ax.set_yticks([2, 4, 6, 8, 10]) plt.tight_layout() plt.savefig("relu.png-600") plt.show() def plot_prelu(): x = np.arange(-10, 10, 0.1) y = prelu(x) fig = plt.figure() ax = fig.add_subplot(111) ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') # ax.spines['bottom'].set_color('none') # ax.spines['left'].set_color('none') ax.spines['left'].set_position(('data', 0)) ax.spines['bottom'].set_position(('data', 0)) ax.plot(x, y) plt.xticks([]) plt.yticks([]) plt.tight_layout() plt.savefig("prelu.png-600") plt.show() if __name__ == "__main__": plot_sigmoid() plot_tanh() plot_relu() plot_prelu()

以上就是Python3 用matplotlib绘制sigmoid函数的案例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python3 用matplotlib绘制sigmoid函数的案例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址