以下是使用Python绘制5种常见激活函数的图的代码:

import numpy as np import matplotlib.pyplot as plt # <em>Sigmoid</em> <em>激活函数</em> def <em>sigmoid</em>(x): return 1 / (1 + np.exp(-x)) # <em>tanh</em> <em>激活函数</em> def <em>tanh</em>(x): return np.<em>tanh</em>(x) # Re<em>LU</em> <em>激活函数</em> def re<em>lu</em>(x): return np.maximum(0, x) # Leaky Re<em>LU</em> <em>激活函数</em> def leaky_re<em>lu</em>(x, alpha=0.01): return np.maximum(alpha * x, x) # E<em>LU</em> <em>激活函数</em> def e<em>lu</em>(x, alpha=1.0): return np.where(x > 0, x, alpha * (np.exp(x) - 1)) # 生成 x 值 x = np.linspace(-10, 10, 100) # 绘制<em>激活函数</em>图像 plt.figure(figsize=(10, 6)) plt.plot(x, <em>sigmoid</em>(x), label='<em>Sigmoid</em>') plt.plot(x, <em>tanh</em>(x), label='<em>tanh</em>') plt.plot(x, re<em>lu</em>(x), label='Re<em>LU</em>') plt.plot(x, leaky_re<em>lu</em>(x), label='Leaky Re<em>LU</em>') plt.plot(x, e<em>lu</em>(x), label='E<em>LU</em>') plt.xlabel('x') plt.ylabel('Activation') plt.title('Activation Functions') plt.legend() plt.grid(True) plt.show()
讯享网
希望这个代码可以帮助你绘制出Sigmoid、tanh、ReLU、Leaky ReLU和ELU激活函数的图像。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/139686.html