第一章
1、什麼是佔位符和變數?
無論是佔位符還是變數,都是tensor,tensor是tensorflow計算的節點。
佔位符和變數是不同型別的tensor。佔位符的值由使用者自行傳遞,不依賴於其他tensor,通常用來儲存樣本資料和標籤。
tf.tensor類是核心類,佔位符(tf.placeholder)和變數(tf.variable)都可以看作特殊的tensor。
可以參閱
2、什麼是會話?變數和佔位符在會話中如何傳遞?
會話是乙個核心概念,tensor是圖計算的節點,會話是對這些節點進行計算的上下文。
變數是計算過程中可以改變的值的tensor,變數的值會被儲存下來。在對變數進行操作前必須進行變數初始化,即在會話中儲存變數的初始值。
訓練時,每次提取一部分資料進行訓練,把他們放入對應的佔位符中,在會話中,不需要計算佔位符的值,而是直接把佔位符的值傳遞給會話。
會話中,變數的值會被儲存下來,佔位符的值不會被儲存,每次可以給佔位符傳遞不同的值。
import tensorflow as tf3、計算圖流程(畫出思維導圖)from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist_data/", one_hot=true)
# x是乙個佔位符,表示待識別的
# 形狀是[none, 784],none表示這一維的大小可以是任意的
x = tf.placeholder(tf.float32, [none, 784])
# 變數引數用tf.variable
w = tf.variable(tf.zeros([784, 10]))
b = tf.variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, w) + b)
# y_是乙個佔位符,表示實際的影象標籤,獨熱表示
y_ = tf.placeholder(tf.float32, [none, 10])
# 交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))
# 梯度下降,學習率是0.01
train_step = tf.train.gradientdescentoptimizer(0.01).minimize(cross_entropy)
# 建立session,只有在session中才能執行優化步驟train_step
sess = tf.interactivesession()
# 執行之前必須要初始化所有變數,分配記憶體
tf.global_variables_initializer().run()
print('start training...')
for _ in range(1000):
# batch_xs: (100, 784), batch_ys: (100, 10)
batch_xs, batch_ys = mnist.train.next_batch(100)
# sess中執行train_step,執行時要使用feed_dict傳入對應佔位符的值
sess.run(train_step, feed_dict=)
# 獨熱表示的y_ 需要通過sess.run(y_)才能獲取此tensor的值4、擴充套件閱讀print(tf.argmax(y, 1))
# output: tensor("argmax:0", shape=(?,), dtype=int64)
print(tf.argmax(y_, 1))
# output: tensor("argmax_1:0", shape=(?,), dtype=int64)
# tf.equal 比較是否相等,輸出true和false
# tf.argmax(y,1), tf.argmax(y_,1), 取出陣列中最大值的下標,可以用獨熱表示以及模型輸出轉換為數字標籤
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
# tf.cast 將比較值轉換為float32型的變數,true轉換為1,false轉換為0
# tf.reduce_mean 計算陣列中的所有元素的平均值,得到模型的**準確率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 使用全體測試樣本**,mnist.test.images, mnist.test.labels
print(sess.run(accuracy, feed_dict=))
# 只有輸入了x,y_,通過sess.run才可以計算出correct_prediction,accuracy
第二章tensorflow的資料讀取原理
畫出思維導圖
tensorflow學習筆記
tensorflow安裝可以直接通過命令列或者原始碼安裝,在此介紹tensorflow8命令列安裝如下 安裝tensorflow sudo pip install upgrade 另外,解除安裝tensorflow命令為 sudo pip uninstall tensorflow tensorflo...
Tensorflow學習筆記
1.如何在虛擬機器中安裝tensor flow 1 首先安裝pip pip install 2 pip install 2.學習tensorflow需要學習 python and linux 3.使用 tensorflow,你必須明白 tensorflow 1 使用圖 graph 來表示計算任務.2...
TensorFlow學習筆記
1 擬合直線 import the library import tensorflow as tf import numpy as np prepare train data train x np.linspace 1,1,100 temp1 train x,temp2 train x.shape,...