8.1 mnist_soft,TensorFlow构建回归模型
背景
之前已經(jīng)寫了很多TensorFlow的基本知識,現(xiàn)在利用TensorFlow實(shí)現(xiàn)一些簡單的功能,對原來的知識進(jìn)行串聯(lián),并初步入門,該部分共包括三篇,分別實(shí)現(xiàn)的是回歸模型,淺層神經(jīng)網(wǎng)絡(luò),KNN。
TensorFlow構(gòu)建回歸模型
本代碼的構(gòu)建步驟
建立計(jì)算圖
在TensorFlow中構(gòu)建模型,我們首先需要實(shí)現(xiàn)的一個(gè)計(jì)算圖,然后再在Session中運(yùn)行圖,并加載數(shù)據(jù),那么首先計(jì)算圖。
公式到計(jì)算圖的轉(zhuǎn)化
首先假如,我們有公式 e = (a+b) * (b +1)那么我們就可以將其拆解成五個(gè)節(jié)點(diǎn)
1. a節(jié)點(diǎn),輸入節(jié)點(diǎn) 2. b節(jié)點(diǎn),輸入節(jié)點(diǎn) 3. a+b 節(jié)點(diǎn),計(jì)算節(jié)點(diǎn),命名為c 4. b+1 節(jié)點(diǎn),計(jì)算節(jié)點(diǎn),命名為d 5. e = c * d 計(jì)算節(jié)點(diǎn),輸出節(jié)點(diǎn),節(jié)點(diǎn)e如圖表示就是
回歸模型
同理logits :y = wx+b可以轉(zhuǎn)化為
1.x 輸入節(jié)點(diǎn) 2.w 權(quán)重 3.b 偏執(zhí) 4. y0 = xw 計(jì)算節(jié)點(diǎn) 5. y = y0 + b 計(jì)算節(jié)點(diǎn),輸出節(jié)點(diǎn)如圖,這就是我們要實(shí)現(xiàn)的計(jì)算圖,但是在實(shí)際的使用過程中卻還有兩點(diǎn)不同,
1. 第一我們實(shí)現(xiàn)模型實(shí)際上已經(jīng)向量化好了的,這是機(jī)器學(xué)習(xí)的基礎(chǔ),這里不再重復(fù),你可以去網(wǎng)易云課堂學(xué)習(xí)吳恩達(dá)教授的深度學(xué)習(xí)課程,里面有不錯(cuò)的介紹。
2. 在TensorFlow中實(shí)現(xiàn)該計(jì)算圖時(shí),是直接一行代碼實(shí)現(xiàn)的,并沒有再構(gòu)建y0,實(shí)際無影響。詳細(xì)情況請看代碼:
注意:后面我們使用的時(shí)交叉熵回歸分類器
損失函數(shù)與優(yōu)化算法
損失函數(shù),我們這里使用的是平均值(平均的是交叉熵分類器的損失)
學(xué)習(xí)率,設(shè)定的為0.5
優(yōu)化算法,使用的隨機(jī)梯度下降
因?yàn)門ensorFlow已經(jīng)實(shí)現(xiàn)了自動(dòng)求導(dǎo),所以我們就不需要想之前用其他Python類庫寫機(jī)器學(xué)習(xí)代碼那樣在自己編寫反向求導(dǎo)了(老鐵,這波穩(wěn)如狗)
數(shù)據(jù)的加載
我們這次使用的時(shí)mnist的手寫數(shù)字?jǐn)?shù)據(jù),你也可以使用其他數(shù)據(jù)來測試這個(gè)回歸模型,但是注意修改之前的Tensor 的shape
mnist = input_data.read_data_sets("/home/fonttian/CODE/TensoFlow/Data/MNIST_data", one_hot=True) # 訓(xùn)練數(shù)據(jù) batch_xs, batch_ys = mnist.train.next_batch(100)最后初始化定義session,初始化所有節(jié)點(diǎn),開始訓(xùn)練和測試吧
sess = tf.InteractiveSession() tf.global_variables_initializer().run() sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})# Test trained modelcorrect_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))if (i+1) % 100 == 0:print(i+1,":",sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))全部代碼
import tensorflow as tf import osfrom tensorflow.examples.tutorials.mnist import input_dataos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'# Import data mnist = input_data.read_data_sets("/home/fonttian/CODE/TensoFlow/Data/MNIST_data", one_hot=True)# Create the model x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.matmul(x, W) + b# Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 10])cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)sess = tf.InteractiveSession() tf.global_variables_initializer().run() # Train for i in range(10000):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})# Test trained modelcorrect_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))if (i+1) % 100 == 0:print(i+1,":",sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))參考
【1】https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_softmax.py
總結(jié)
以上是生活随笔為你收集整理的8.1 mnist_soft,TensorFlow构建回归模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu16.04 安装Python
- 下一篇: Ubuntu 16.04 安装JDK