tensorflow-internals閱讀筆記(1)

原書(shū)作者:劉光聰
簡(jiǎn)書(shū)主頁(yè):http://www.itdecent.cn/u/49d1f3b7049e
Github地址:https://github.com/horance-liu/tensorflow-internals

總覺(jué)得做機(jī)器學(xué)習(xí)不能一直流于表面,一直調(diào)用框架的API,這樣會(huì)失去個(gè)人的競(jìng)爭(zhēng)力。想要看框架的源代碼,卻總感覺(jué)項(xiàng)目過(guò)于龐大而無(wú)從下手。感謝作者這本開(kāi)源的中文書(shū)籍填補(bǔ)了這方面的空白。于此記錄下閱讀過(guò)程中的一些心得與疑惑,作為一個(gè)長(zhǎng)期系列不定時(shí)更新。

基礎(chǔ)概念

OP:即operation,節(jié)點(diǎn)。節(jié)點(diǎn)用來(lái)構(gòu)建計(jì)算圖,可以是數(shù)據(jù)的起點(diǎn)、終點(diǎn)或中間的運(yùn)算。
Kernel:

Kernel是OP在某種硬件設(shè)備的特定實(shí)現(xiàn),它負(fù)責(zé)執(zhí)行OP的運(yùn)算。

環(huán)境配置

因?yàn)槭褂玫腤indows系統(tǒng),并且Linux服務(wù)器上我的權(quán)限不夠,所以沒(méi)有一步一步地按照書(shū)上的教程用源碼編譯,不過(guò)也簡(jiǎn)單構(gòu)建了一個(gè)C++工程用來(lái)閱讀源碼,具體操作如下:
安裝tensorflow:傻瓜式操作

pip install tensorflow-gpu

使用Visual Studio構(gòu)建工程,將頭文件的目錄導(dǎo)入:

15286196-31471697c94942d4.png

(第二章結(jié)束)

單層感知器構(gòu)建與TensorBoard初體驗(yàn)

按照書(shū)中3.2章內(nèi)容構(gòu)建單層感知器訓(xùn)練mnist數(shù)據(jù)集,整合的代碼如下:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 引入mnist數(shù)據(jù)集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 定義輸入和標(biāo)簽
x = tf.placeholder(tf.float32, [None, 28, 28, 1])
t = tf.placeholder(tf.float32, [None, 10])

x = tf.reshape(x, [-1, 784])

# 定義模型參數(shù)
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

init_op = tf.global_variables_initializer()

# matmul:矩陣乘法
y = tf.nn.softmax(tf.matmul(x, w) + b)

# 交叉熵
cross_entropy = -tf.reduce_sum(t*tf.log(y))

# argmax:返回指定維度上的最大值
is_correct = tf.equal(tf.argmax(y, 1), tf.argmax(t,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

# 優(yōu)化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.003)
train_step = optimizer.minimize(cross_entropy)

with tf.Session() as sess:
    # 初始化所有參數(shù)
    sess.run(init_op)

    # 將loss, 準(zhǔn)確度在tensorboard中進(jìn)行輸出
    tf.summary.scalar('loss', cross_entropy)
    tf.summary.scalar('accuracy', accuracy)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter('E:\\DeepLearning\\tensorflow源碼剖析\\logs\\mnist1\\train', sess.graph)
    test_writer = tf.summary.FileWriter('E:\\DeepLearning\\tensorflow源碼剖析\\logs\\mnist1\\test')

    for step in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        summary, _ = sess.run([merged, train_step], feed_dict={x:batch_xs, t:batch_ys})
        train_writer.add_summary(summary, step)

        if step%100 == 0:
            summary, acc = sess.run([merged, accuracy], feed_dict={x:mnist.test.images, t:mnist.test.labels})
            test_writer.add_summary(summary, step)

其他代碼在書(shū)中都有提到,這里主要記錄TensorBoard相關(guān)的代碼。
首先記錄想要輸出的兩個(gè)標(biāo)量,loss和準(zhǔn)確度,再merge起來(lái)

# 將loss, 準(zhǔn)確度在tensorboard中進(jìn)行輸出
tf.summary.scalar('loss', cross_entropy)
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()

定義writer,將變量寫(xiě)入文件

train_writer = tf.summary.FileWriter('E:\\DeepLearning\\tensorflow源碼剖析\\logs\\mnist1\\train', sess.graph)
test_writer = tf.summary.FileWriter('E:\\DeepLearning\\tensorflow源碼剖析\\logs\\mnist1\\test')

在訓(xùn)練的同時(shí),計(jì)算相關(guān)數(shù)據(jù)并寫(xiě)入文件。

summary, _ = sess.run([merged, train_step], feed_dict={x:batch_xs, t:batch_ys})
train_writer.add_summary(summary, step)
summary, acc = sess.run([merged, accuracy], feed_dict={x:mnist.test.images, t:mnist.test.labels})
test_writer.add_summary(summary, step)

啟動(dòng)Tensorboard

tensorboard --logdir=E:\DeepLearning\tensorflow源碼剖析\logs\mnist1

在瀏覽器中打開(kāi)對(duì)應(yīng)的地址


image.png

image.png

在scalars頁(yè)面中可以看到記錄的數(shù)據(jù)的變化趨勢(shì),在graph頁(yè)面中可以看到網(wǎng)絡(luò)結(jié)構(gòu)。但由于沒(méi)有使用子命名空間整理節(jié)點(diǎn),無(wú)法從圖中理解有意義的網(wǎng)絡(luò)結(jié)構(gòu)。在下一步多層感知器的構(gòu)建中我將嘗試使用子命名空間整理節(jié)點(diǎn),使圖的可視化效果更好一點(diǎn)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容