玩玩机器学习4——TensorFlow基础之激活函数
? ? ? ?激活函數(shù)(activation function)運(yùn)行時(shí)激活神經(jīng)網(wǎng)絡(luò)中某一部分神經(jīng)元,將激活信息向后傳入下一層的神經(jīng)網(wǎng)絡(luò)。神經(jīng)網(wǎng)絡(luò)之所以能解決非線性問(wèn)題(如語(yǔ)音、圖像識(shí)別),本質(zhì)上就是激活函數(shù)加入了非線性因素,彌補(bǔ)了線性模型的表達(dá)力,把“激活的神經(jīng)元的特征”通過(guò)函數(shù)保留并映射到下一層。
? ? ? ?因?yàn)樯窠?jīng)網(wǎng)絡(luò)的數(shù)學(xué)基礎(chǔ)是處處可微的,所以選取的激活函數(shù)要能保證數(shù)據(jù)輸入與輸出也是可微的。那么激活函數(shù)在 TensorFlow 中是如何表達(dá)的呢?激活函數(shù)不會(huì)更改輸入數(shù)據(jù)的維度,也就是輸入和輸出的維度是相同的。TensorFlow 中有如下激活函數(shù),它們定義在 tensorflow-1.1.0/tensorflow/python/ops/nn.py 文件中,這里包括平滑非線性的激活函數(shù),如 sigmoid、tanh、elu、softplus 和 softsign,也包括連續(xù)但不是處處可微的函數(shù) relu、relu6、crelu 和 relu_x,以及隨機(jī)正則化函數(shù) dropout
? ? ? ??
#4,激活函數(shù)的使用 import numpy as np import matplotlib.pyplot as plt import tensorflow as tf# fake data x = np.linspace(-5, 5, 200) #產(chǎn)生-5至5之間200個(gè)點(diǎn) y_relu = tf.nn.relu(x)#定義relu激活函數(shù),輸入數(shù)據(jù)點(diǎn) y_sigmoid = tf.nn.sigmoid(x)#定義sigmod激活函數(shù),輸入數(shù)據(jù)點(diǎn) y_tanh = tf.nn.tanh(x)#定義tanh激活函數(shù),輸入數(shù)據(jù)點(diǎn) y_softplus = tf.nn.softplus(x)#定義softplus,輸入數(shù)據(jù)點(diǎn) sess = tf.Session()#創(chuàng)建session y_relu, y_sigmoid, y_tanh, y_softplus = sess.run([y_relu, y_sigmoid, y_tanh, y_softplus])#生成激活函數(shù)#繪制relu的圖像 plt.figure(1, figsize=(8, 6)) plt.subplot(221) plt.plot(x, y_relu, c='red', label='relu') plt.ylim((-1, 5)) plt.legend(loc='best')#繪制sigmoid的圖像 plt.subplot(222) plt.plot(x, y_sigmoid, c='red', label='sigmoid') plt.ylim((-0.2, 1.2)) plt.legend(loc='best')#繪制tanh的圖像 plt.subplot(223) plt.plot(x, y_tanh, c='red', label='tanh') plt.ylim((-1.2, 1.2)) plt.legend(loc='best')#繪制softplus的圖像 plt.subplot(224) plt.plot(x, y_softplus, c='red', label='softplus') plt.ylim((-0.2, 6)) plt.legend(loc='best')plt.show()輸出結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的玩玩机器学习4——TensorFlow基础之激活函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用 Packer、Ansible 和
- 下一篇: java数组怎么倒循环_java – 用