TensorFlow如何實(shí)現(xiàn)反向傳播
使用TensorFlow的一個(gè)優(yōu)勢(shì)是,它可以維護(hù)操作狀態(tài)和基于反向傳播自動(dòng)地更新模型變量。
TensorFlow通過(guò)計(jì)算圖來(lái)更新變量和最小化損失函數(shù)來(lái)反向傳播誤差的。這步將通過(guò)聲明優(yōu)化函數(shù)(optimization function)來(lái)實(shí)現(xiàn)。一旦聲明好優(yōu)化函數(shù),TensorFlow將通過(guò)它在所有的計(jì)算圖中解決反向傳播的項(xiàng)。當(dāng)我們傳入數(shù)據(jù),最小化損失函數(shù),TensorFlow會(huì)在計(jì)算圖中根據(jù)狀態(tài)相應(yīng)的調(diào)節(jié)變量。
回歸算法的例子從均值為1、標(biāo)準(zhǔn)差為0.1的正態(tài)分布中抽樣隨機(jī)數(shù),然后乘以變量A,損失函數(shù)為L(zhǎng)2正則損失函數(shù)。理論上,A的最優(yōu)值是10,因?yàn)樯傻臉永龜?shù)據(jù)均值是1。
二個(gè)例子是一個(gè)簡(jiǎn)單的二值分類算法。從兩個(gè)正態(tài)分布(N(-1,1)和N(3,1))生成100個(gè)數(shù)。所有從正態(tài)分布N(-1,1)生成的數(shù)據(jù)標(biāo)為目標(biāo)類0;從正態(tài)分布N(3,1)生成的數(shù)據(jù)標(biāo)為目標(biāo)類1,模型算法通過(guò)sigmoid函數(shù)將這些生成的數(shù)據(jù)轉(zhuǎn)換成目標(biāo)類數(shù)據(jù)。換句話講,模型算法是sigmoid(x+A),其中,A是要擬合的變量,理論上A=-1。假設(shè),兩個(gè)正態(tài)分布的均值分別是m1和m2,則達(dá)到A的取值時(shí),它們通過(guò)-(m1+m2)/2轉(zhuǎn)換成到0等距的值。后面將會(huì)在TensorFlow中見(jiàn)證怎樣取到相應(yīng)的值。
同時(shí),指定一個(gè)合適的學(xué)習(xí)率對(duì)機(jī)器學(xué)習(xí)算法的收斂是有幫助的。優(yōu)化器類型也需要指定,前面的兩個(gè)例子會(huì)使用標(biāo)準(zhǔn)梯度下降法,它在TensorFlow中的實(shí)現(xiàn)是GradientDescentOptimizer()函數(shù)。
# 反向傳播
#----------------------------------
#
# 以下Python函數(shù)主要是展示回歸和分類模型的反向傳播
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# 創(chuàng)建計(jì)算圖會(huì)話
sess = tf.Session()
# 回歸算法的例子:
# We will create sample data as follows:
# x-data: 100 random samples from a normal ~ N(1, 0.1)
# target: 100 values of the value 10.
# We will fit the model:
# x-data * A = target
# Theoretically, A = 10.
# 生成數(shù)據(jù),創(chuàng)建占位符和變量A
x_vals = np.random.normal(1, 0.1, 100)
y_vals = np.repeat(10., 100)
x_data = tf.placeholder(shape=[1], dtype=tf.float32)
y_target = tf.placeholder(shape=[1], dtype=tf.float32)
# Create variable (one model parameter = A)
A = tf.Variable(tf.random_normal(shape=[1]))
# 增加乘法操作
my_output = tf.multiply(x_data, A)
# 增加L2正則損失函數(shù)
loss = tf.square(my_output - y_target)
# 在運(yùn)行優(yōu)化器之前,需要初始化變量
init = tf.global_variables_initializer()
sess.run(init)
# 聲明變量的優(yōu)化器
my_opt = tf.train.GradientDescentOptimizer(0.02)
train_step = my_opt.minimize(loss)
# 訓(xùn)練算法
for i in range(100):
rand_index = np.random.choice(100)
rand_x = [x_vals[rand_index]]
rand_y = [y_vals[rand_index]]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
if (i+1)%25==0:
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
# 分類算法例子
# We will create sample data as follows:
# x-data: sample 50 random values from a normal = N(-1, 1)
# + sample 50 random values from a normal = N(1, 1)
# target: 50 values of 0 + 50 values of 1.
# These are essentially 100 values of the corresponding output index
# We will fit the binary classification model:
# If sigmoid(x+A) < 0.5 -> 0 else 1
# Theoretically, A should be -(mean1 + mean2)/2
# 重置計(jì)算圖
ops.reset_default_graph()
# Create graph
sess = tf.Session()
# 生成數(shù)據(jù)
x_vals = np.concatenate((np.random.normal(-1, 1, 50), np.random.normal(3, 1, 50)))
y_vals = np.concatenate((np.repeat(0., 50), np.repeat(1., 50)))
x_data = tf.placeholder(shape=[1], dtype=tf.float32)
y_target = tf.placeholder(shape=[1], dtype=tf.float32)
# 偏差變量A (one model parameter = A)
A = tf.Variable(tf.random_normal(mean=10, shape=[1]))
# 增加轉(zhuǎn)換操作
# Want to create the operstion sigmoid(x + A)
# Note, the sigmoid() part is in the loss function
my_output = tf.add(x_data, A)
# 由于指定的損失函數(shù)期望批量數(shù)據(jù)增加一個(gè)批量數(shù)的維度
# 這里使用expand_dims()函數(shù)增加維度
my_output_expanded = tf.expand_dims(my_output, 0)
y_target_expanded = tf.expand_dims(y_target, 0)
# 初始化變量A
init = tf.global_variables_initializer()
sess.run(init)
# 聲明損失函數(shù) 交叉熵(cross entropy)
xentropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=my_output_expanded, labels=y_target_expanded)
# 增加一個(gè)優(yōu)化器函數(shù) 讓TensorFlow知道如何更新和偏差變量
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(xentropy)
# 迭代
for i in range(1400):
rand_index = np.random.choice(100)
rand_x = [x_vals[rand_index]]
rand_y = [y_vals[rand_index]]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
if (i+1)%200==0:
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
print('Loss = ' + str(sess.run(xentropy, feed_dict={x_data: rand_x, y_target: rand_y})))
# 評(píng)估預(yù)測(cè)
predictions = []
for i in range(len(x_vals)):
x_val = [x_vals[i]]
prediction = sess.run(tf.round(tf.sigmoid(my_output)), feed_dict={x_data: x_val})
predictions.append(prediction[0])
accuracy = sum(x==y for x,y in zip(predictions, y_vals))/100.
print('最終精確度 = ' + str(np.round(accuracy, 2)))
輸出:
Step #25 A = [ 6.12853956] Loss = [ 16.45088196] Step #50 A = [ 8.55680943] Loss = [ 2.18415046] Step #75 A = [ 9.50547695] Loss = [ 5.29813051] Step #100 A = [ 9.89214897] Loss = [ 0.34628963] Step #200 A = [ 3.84576249] Loss = [[ 0.00083012]] Step #400 A = [ 0.42345378] Loss = [[ 0.01165466]] Step #600 A = [-0.35141727] Loss = [[ 0.05375391]] Step #800 A = [-0.74206048] Loss = [[ 0.05468176]] Step #1000 A = [-0.89036471] Loss = [[ 0.19636908]] Step #1200 A = [-0.90850282] Loss = [[ 0.00608062]] Step #1400 A = [-1.09374011] Loss = [[ 0.11037558]] 最終精確度 = 1.0
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單http服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Python對(duì)口紅進(jìn)行數(shù)據(jù)分析來(lái)選定情人節(jié)禮物
情人節(jié)送小仙女什么禮物?讓我們來(lái)用Python對(duì)口紅進(jìn)行數(shù)據(jù)分析,那個(gè)女孩子會(huì)拒絕這樣精心挑選的禮物,感興趣的小伙伴快來(lái)看看吧2022-02-02
使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標(biāo)記函
這篇文章主要介紹了使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標(biāo)記函,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python flask幾分鐘實(shí)現(xiàn)web服務(wù)的例子
今天小編就為大家分享一篇python flask幾分鐘實(shí)現(xiàn)web服務(wù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
使用Python PIL庫(kù)讀取文件批量處理圖片大小實(shí)現(xiàn)
這篇文章主要為大家介紹了使用Python PIL庫(kù)讀取文件批量處理圖片大小實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式
今天小編就為大家分享一篇Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
深入理解NumPy簡(jiǎn)明教程---數(shù)組3(組合)
本篇文章對(duì)NumPy數(shù)組進(jìn)行較深入的探討。首先介紹自定義類型的數(shù)組,接著數(shù)組的組合,最后介紹數(shù)組復(fù)制方面的問(wèn)題,有興趣的可以了解一下。2016-12-12
Yolov5(v5.0)+pyqt5界面設(shè)計(jì)圖文教程
眾所周知界面設(shè)計(jì)一般指UI設(shè)計(jì),下面這篇文章主要給大家介紹了關(guān)于Yolov5(v5.0)+pyqt5界面設(shè)計(jì)的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
python中*args與**kwarsg及閉包和裝飾器的用法
這篇文章主要介紹了python中*args與**kwarsg及閉包和裝飾器的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
python中l(wèi)ambda與def用法對(duì)比實(shí)例分析
這篇文章主要介紹了python中l(wèi)ambda與def用法對(duì)比,實(shí)例分析了lambda與def的區(qū)別與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

