目录

激活函数

import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
1
2
3
4

# Sigmoid

常用于二分类问题上,公式为:$$\sigma(x) = \frac{1}{1+e^{-x}}$$

# Sigmoid函数 (输出层)
# 定义x的取值范围
x = np.linspace(-10,10,100)
# 直接使用tensorflow实现
y = tf.nn.sigmoid(x)
plt.plot(x,y)
plt.grid()
1
2
3
4
5
6
7

# Tanh

数学中的双曲正切函数Tanh也是一种神经网络常用的激活函数,尤其是用于图像生成任务的最后一层,其计算方式为:$$f\left(x\right) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$

avatar

# Tanh函数 (隐藏层)
# 定义x的取值范围
x = np.linspace(-10,10,100)

y = tf.nn.tanh(x)

plt.plot(x,y)
plt.grid()
1
2
3
4
5
6
7
8

# ReLU

ReLU激活函数可以说是深度神经网络中最常用的激活函数之一,常用于隐藏层,计算方式:$$f\left(x\right) = \max\left(0, x\right)$$

avatar

# ReLu函数 (隐藏层)
# 定义x的取值范围
x = np.linspace(-10,10,100)

y = tf.nn.relu(x)

plt.plot(x,y)
plt.grid()
1
2
3
4
5
6
7
8

# Softmax

一种用于多分类问题的激活函数,在多类分类问题中,超过两个类标签则需要类成员关系。对于长度为 K 的任意实向量,Softmax 可以将其压缩为长度为 K,值在(0,1)范围内,并且向量中元素的总和为 1 的实向量。

avatar

# 如何选择激活函数

# 隐藏层
  • 优先选择RELU函数
  • 如果RELU效果不好,可以尝试Leaky ReLu
  • 不要使用sigmoid激活函数,可以尝试使用tanh函数
# 输出层
  • 二分类问题选择sigmoid
  • 多分类问题选择softmax
  • 回归问题选择identity激活函数(线性,未在此详述,CSDN查看)
最近更新
01
基本知识
07-18
02
卷积神经网络识别图像
07-18
03
损失函数
07-18
更多文章>