激活函数图像代码python
时间: 2023-08-28 07:18:18 浏览: 92
以下是使用Python绘制激活函数图像的代码示例:
ReLU激活函数图像代码:
```python
from matplotlib import pyplot
import numpy as np
def relu(x):
if x > 0:
return x
else:
return 0
def func():
x = np.arange(-5,5,0.02)
y = []
for i in x:
yi = relu(i)
y.append(yi)
pyplot.xlabel("x")
pyplot.ylabel("y")
pyplot.title("ReLU")
pyplot.plot(x,y)
pyplot.show()
func()
```
ELU激活函数图像代码:
```python
import numpy as np
import matplotlib.pyplot as plt
def elu(x, a):
y = x.copy()
for i in range(y.shape < 0:
y[i = a * (np.exp(y[i]) - 1)
return y
x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)
y_elu = elu(x, 0.3)
plt.plot(x, y_elu, 'b', linewidth=2.5, label='ELU')
plt.legend(loc='upper left', fontsize=16, frameon=False)
plt.tight_layout()
plt.savefig("elu.jpeg", dpi=600, format="jpeg")
plt.show()
```
Sigmoid激活函数图像代码:
```python
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1 / (1 + np.exp(-x))
x = np.arange(-10, 10, 0.01)
plt.tick_params(labelsize=14)
y_sigmoid = sigmoid(x)
plt.plot(x, y_sigmoid, 'b', linewidth=2.5, label='Sigmoid')
plt.legend(loc='upper left', fontsize=16, frameon=False)
plt.tight_layout()
plt.savefig("sigmoid.jpeg", dpi=600, format="jpeg")
plt.show()
```
请注意,这些代码是根据引用的内容编写的,用于绘制ReLU、ELU和Sigmoid激活函数的图像。你可以根据自己的需求进行修改和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [神经网络中常用激活函数图像绘制(Python)](https://blog.csdn.net/qq_36035111/article/details/93405736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python绘制激活函数图像](https://blog.csdn.net/m0_49878324/article/details/122395348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文