神经网络中的神经元常见激活函数绘制

近年来,神经网络方法在多个领域表现出优于传统算法的特点,其使用神经元的组合来提取数据中的深度特征,在非线性任务中表现出优越的性能。

神经元的非线性,主要是通过其激活函数f(x),将wx+b进行非线性化映射,其过程如下:

y = f(wx+b)

总结并绘制学术论文中可用的激活函数图,绘制的函数图包括: sigmoid函数、ReLU函数、tanh函数和pReLU函数(参数为0.5)。

1. sigmoid函数

import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    return 1. / (1 + np.exp(-x))

def plot_sigmoid():
    x = np.arange(-10, 10, 0.1)
    y = sigmoid(x)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    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()
    
plot_sigmoid()

2. tanh函数

import numpy as np
import matplotlib.pyplot as plt


def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))

def plot_sigmoid():
    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.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([-0.02, 1.02])
    ax.set_yticks([-1.0, -0.5, 0.5, 1.0])
    ax.set_xticks([-10, -5, 5, 10])
    plt.tight_layout()
    
plot_sigmoid()

3. ReLU函数

import numpy as np
import matplotlib.pyplot as plt


def relu(x):
    return np.where(x<0, 0, x)

def plot_sigmoid():
    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['left'].set_position(('data', 0))
    ax.plot(x, y)
    plt.xlim([-10.05, 10.05])
    plt.ylim([-0.02, 1.02])
    ax.set_yticks([2, 4, 6, 8, 10])
    plt.tight_layout()
    
plot_sigmoid()

4.pReLU函数

import numpy as np
import matplotlib.pyplot as plt


def prelu(x):
    return np.where(x<0, x * 0.5, x)

def plot_sigmoid():
    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['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    ax.plot(x, y)
    plt.tight_layout()
    
plot_sigmoid()

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值