您现在的位置是:亿华云 > IT科技
Python实现之激活函数
亿华云2025-10-03 02:31:15【IT科技】2人已围观
简介本文转载自微信公众号「python与大数据分析」,作者一只小小鸟鸟。转载本文请联系python与大数据分析公众号。激活函数(Activation Function),就是在人工神经网络的神经元上运行的
本文转载自微信公众号「python与大数据分析」,实现数作者一只小小鸟鸟。活函转载本文请联系python与大数据分析公众号。实现数
激活函数(Activation Function),活函就是实现数在人工神经网络的神经元上运行的函数,负责将神经元的活函输入映射到输出端。
如果不用激活函数,实现数每一层输出都是活函上层输入的线性函数,无论神经网络有多少层,实现数输出都是活函输入的线性组合,这种情况就是实现数最原始的感知机。
如果使用的活函话,激活函数给神经元引入了非线性因素,实现数使得神经网络可以任意逼近任何非线性函数,活函这样神经网络就可以应用到众多的源码下载实现数非线性模型中。
#!/usr/bin/env python # -*- coding: UTF-8 -*- # _ooOoo_ # o8888888o # 88" . "88 # ( | - _ - | ) # O\ = /O # ____/`---\____ # . \\| |// `. # / \\|||:|||// \ # / _|||||-:- |||||- \ # | | \\\ - /// | | # | \_| \---/ | _/ | # \ .-\__ `-` ___/-. / # ___`. . /--.--\ `. . __ # ."" < `.___\_<|>_/___. >"". # | | : `- \`.;`\ _ /`;.`/ - ` : | | # \ \ `-. \_ __\ /__ _/ .-` / / # ==`-.____`-.___\_____/___.-`____.-== # `=---= @Project :pythonalgorithms @File :Activationfunction.py @Author :不胜人生一场醉@Date :2021/8/11 0:14 import numpy as np from matplotlib import pyplot as plt def drawpic(x, y, label= , title= ): plt.figure(figsize=(10, 8)) ax = plt.gca() # 通过gca:get current axis得到当前轴 plt.rcParams[font.sans-serif] = [SimHei] # 绘图中文 plt.rcParams[axes.unicode_minus] = False # 绘图负号 plt.plot(x, y, label=label) # 设置图片的右边框和上边框为不显示 ax.spines[right].set_color(none) ax.spines[top].set_color(none) # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 ax.spines[bottom].set_position((data, 0)) # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 ax.spines[left].set_position((axes, 0.5)) # ax.spines[left].set_position((data, 0)) plt.title(title) plt.legend(loc=upper right) plt.show() if __name__ == __main__: std = 0.1 # 标准差为0.1 avg = 1 # 平均值为1 x = np.linspace(avg - 5 * std, avg + 5 * std, 100) y = normaldistribution(x, avg, std) drawpic(x, y, normaldistribution, normal distribution function) x = np.linspace(-5, 5, 100) y = sigmoid(x) drawpic(x, y, sigmoid, sigmoid Activation function) y = tanh(x) drawpic(x, y, tanh, tanh Activation function) y = stepfunction(x) drawpic(x, y, tanh, step Activation function) y = relu(x) drawpic(x, y, relu, relu Activation function) y = leakyrelu(x) drawpic(x, y, leakyrelu, leakyrelu Activation function) y = softmax(x) drawpic(x, y, softmax, softmax Activation function) # 求正态分布值,avg表示期望值,std表示标准差 def normaldistribution(x, avg=0, std=1): return np.exp(-(x - avg) ** 2 / (2 * std ** 2)) / (np.sqrt(2 * np.pi) * std) # return np.exp(-(x - avg) ** 2 / (2 * std ** 2)) / (math.sqrt(2 * math.pi) *很赞哦!(24)