自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python實現(xiàn)之激活函數(shù)

開發(fā) 后端
激活函數(shù)(Activation Function),就是在人工神經(jīng)網(wǎng)絡的神經(jīng)元上運行的函數(shù),負責將神經(jīng)元的輸入映射到輸出端。

 [[416737]]

本文轉(zhuǎn)載自微信公眾號「python與大數(shù)據(jù)分析」,作者一只小小鳥鳥。轉(zhuǎn)載本文請聯(lián)系python與大數(shù)據(jù)分析公眾號。

激活函數(shù)(Activation Function),就是在人工神經(jīng)網(wǎng)絡的神經(jīng)元上運行的函數(shù),負責將神經(jīng)元的輸入映射到輸出端。

如果不用激活函數(shù),每一層輸出都是上層輸入的線性函數(shù),無論神經(jīng)網(wǎng)絡有多少層,輸出都是輸入的線性組合,這種情況就是最原始的感知機。

如果使用的話,激活函數(shù)給神經(jīng)元引入了非線性因素,使得神經(jīng)網(wǎng)絡可以任意逼近任何非線性函數(shù),這樣神經(jīng)網(wǎng)絡就可以應用到眾多的非線性模型中。

  1. #!/usr/bin/env python 
  2. # -*- coding: UTF-8 -*- 
  3. #                     _ooOoo_ 
  4. #                   o8888888o 
  5. #                    88" . "88 
  6. #                 ( | -  _  - | ) 
  7. #                     O\ = /O 
  8. #                 ____/`---'\____ 
  9. #                  .' \\| |// `. 
  10. #                 / \\|||:|||// \ 
  11. #               / _|||||-:- |||||- \ 
  12. #                | | \\\ - /// | | 
  13. #              | \_| ''\---/'' | _/ | 
  14. #               \ .-\__ `-` ___/-. / 
  15. #            ___`. .' /--.--\ `. . __ 
  16. #         ."" '< `.___\_<|>_/___.' >'""
  17. #       | | : `- \`.;`\  _ /`;.`/ - ` : | | 
  18. #          \ \ `-. \_ __\ /__ _/ .-` / / 
  19. #      ==`-.____`-.___\_____/___.-`____.-'== 
  20. #                     `=---=' 
  21. ''
  22. @Project :pythonalgorithms  
  23. @File :Activationfunction.py 
  24. @Author :不勝人生一場醉@Date :2021/8/11 0:14  
  25. ''
  26. import numpy as np 
  27. from matplotlib import pyplot as plt 
  28.  
  29.  
  30. def drawpic(x, y, label=' ', title=' '): 
  31.    plt.figure(figsize=(10, 8)) 
  32.    ax = plt.gca()  # 通過gca:get current axis得到當前軸 
  33.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  34.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負號 
  35.    plt.plot(x, y, label=label) 
  36.  
  37.    # 設置圖片的右邊框和上邊框為不顯示 
  38.    ax.spines['right'].set_color('none'
  39.    ax.spines['top'].set_color('none'
  40.  
  41.    # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  42.    # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置 
  43.    ax.spines['bottom'].set_position(('data', 0)) 
  44.    # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置 
  45.    ax.spines['left'].set_position(('axes', 0.5)) 
  46.    # ax.spines['left'].set_position(('data', 0)) 
  47.    plt.title(title) 
  48.    plt.legend(loc='upper right'
  49. plt.show() 
  50.  
  51.  
  52. if __name__ == '__main__'
  53.    std = 0.1  # 標準差為0.1 
  54.    avg = 1  # 平均值為1 
  55.    x = np.linspace(avg - 5 * std, avg + 5 * std, 100) 
  56.    y = normaldistribution(x, avg, std) 
  57.    drawpic(x, y, 'normaldistribution''normal distribution function'
  58.  
  59.    x = np.linspace(-5, 5, 100) 
  60.    y = sigmoid(x) 
  61.    drawpic(x, y, 'sigmoid''sigmoid Activation function'
  62.  
  63.    y = tanh(x) 
  64.    drawpic(x, y, 'tanh''tanh Activation function'
  65.  
  66.    y = stepfunction(x) 
  67.    drawpic(x, y, 'tanh''step Activation function'
  68.  
  69.    y = relu(x) 
  70.    drawpic(x, y, 'relu''relu Activation function'
  71.  
  72.    y = leakyrelu(x) 
  73.    drawpic(x, y, 'leakyrelu''leakyrelu Activation function'
  74.  
  75.    y = softmax(x) 
  76.    drawpic(x, y, 'softmax''softmax Activation function'
  1. # 求正態(tài)分布值,avg表示期望值,std表示標準差 
  2. def normaldistribution(x, avg=0, std=1): 
  3.    return np.exp(-(x - avg) ** 2 / (2 * std ** 2)) / (np.sqrt(2 * np.pi) * std) 
  4.    # return np.exp(-(x - avg) ** 2 / (2 * std ** 2)) / (math.sqrt(2 * math.pi) *  

  1. # Sigmoid函數(shù) 
  2. # Sigmoid函數(shù)是一個在生物學中常見的S型函數(shù),也稱為S型生長曲線。 
  3. # 在信息科學中,由于其單增以及反函數(shù)單增等性質(zhì),Sigmoid函數(shù)常被用作神經(jīng)網(wǎng)絡的閾值函數(shù),將變量映射到0,1之間 
  4. def sigmoid(x): 
  5.    return 1 / (1 + np.power(np.e, -x)) 

  1. # Tanh函數(shù) 
  2. # Tanh是雙曲函數(shù)中的一個,Tanh()為雙曲正切。 
  3. # 在數(shù)學中,雙曲正切“Tanh”是由基本雙曲函數(shù)雙曲正弦和雙曲余弦推導而來。 
  4. # 函數(shù)tanh(藍色)和函數(shù)sigmoid(橙色)一樣,在其飽和區(qū)的接近于0,都容易產(chǎn)生后續(xù)梯度消失、計算量大的問題 
  5. def tanh(x): 
  6.    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x)) 

  1. # 階躍函數(shù) 
  2. def stepfunction(x): 
  3.    return np.array(x > 0, dtype=np.int32) 

  1. # ReLU函數(shù) 
  2. # Relu激活函數(shù)(The Rectified Linear Unit),用于隱層神經(jīng)元輸出。 
  3. # Relu會使一部分神經(jīng)元的輸出為0,這樣就造成了網(wǎng)絡的稀疏性,并且減少了參數(shù)的相互依存關系,緩解了過擬合問題的發(fā)生。 
  4. def relu(x): 
  5.    return np.maximum(0, x) 

  1. # leaky ReLU函數(shù) 
  2. def leakyrelu(x): 
  3.    return np.maximum(0.01 * x, x) 

  1. # softmax函數(shù) 
  2. # softmax函數(shù)可以看做是Sigmoid函數(shù)的一般化,用于多分類神經(jīng)網(wǎng)絡輸出。 
  3. def softmax(x): 
  4.    return np.exp(x) / np.sum(np.exp(x)) 

 

 

責任編輯:武曉燕 來源: python與大數(shù)據(jù)分析
相關推薦

2021-07-30 05:00:04

Python初等函數(shù)

2021-08-03 05:00:57

Python實現(xiàn)導數(shù)

2022-09-26 00:00:00

神經(jīng)網(wǎng)絡激活函數(shù)sigmoid

2021-07-27 05:04:12

python初等函數(shù)

2010-09-16 09:35:17

SQL函數(shù)

2022-04-14 09:30:22

深度學習激活函數(shù)人工神經(jīng)

2021-05-22 23:08:08

深度學習函數(shù)算法

2010-07-05 11:38:02

SQL Server

2021-03-31 07:39:18

pythonHIVEUDF函數(shù)

2020-09-04 06:32:20

Pythonshutil函數(shù)

2010-09-10 14:05:12

SQL聚合函數(shù)

2023-04-18 15:15:06

神經(jīng)網(wǎng)絡激活函數(shù)開發(fā)

2017-11-20 05:08:16

深度學習激活函數(shù)神經(jīng)網(wǎng)絡

2010-07-12 11:38:24

SQL Server函

2024-11-07 08:26:31

神經(jīng)網(wǎng)絡激活函數(shù)信號

2024-07-24 08:04:24

神經(jīng)網(wǎng)絡激活函數(shù)

2024-04-08 14:29:41

大型語言模型SwiGLU

2021-01-10 08:46:43

神經(jīng)網(wǎng)絡激活函數(shù)人工神經(jīng)網(wǎng)絡

2021-07-30 06:58:27

python實現(xiàn)三角函數(shù)

2011-07-21 10:07:58

JavaScript
點贊
收藏

51CTO技術棧公眾號