[譯] TensorFlow 教程預告 & #01 - 簡單線性模型

活魚眼發表於2017-07-02

這是本人在知乎上翻譯的文章, 陸續更新中,現在搬運到掘金上來。
在這一系列文章中,你將學到深度學習的一些基本概念以及TensorFlow的使用,並完成手寫體數字識別、影象分類、遷移學習、Deep Dream、風格遷移和強化學習等專案。 github上的Python NoteBook也可以很方便的除錯程式碼。

總而言之, 一份很讚的入門教程。歡迎分享/關注/訂閱

不得不說,掘金支援Markdown真是方便多了。向Aaron Swartz致敬。

by Magnus Erik Hvass Pedersen / GitHub / Videos on YouTube
中文翻譯 thrillerist /Github

如有轉載,請附上本文連結。


介紹

這份教程示範了在TensorFlow中使用一個簡單線性模型的工作流程。在載入稱為MNIST的手寫數字圖片資料集後,我們在TensorFlow中定義並優化了一個數學模型。(我們)會畫出結果並展開討論。

你應該熟悉基本的線性代數,Python和Jupyter Notebook編輯器。如果你對機器學習和分類有基本的理解也很有幫助。

匯入

%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix複製程式碼

使用Python3.5.2(Anaconda)開發,TensorFlow版本是:

tf.__version__複製程式碼

'0.12.0-rc1'

載入資料

MNIST資料集大約有12MB,如果給定的地址裡沒有檔案,它將自動下載。

from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets("data/MNIST/", one_hot=True)複製程式碼

Extracting data/MNIST/train-images-idx3-ubyte.gz
Extracting data/MNIST/train-labels-idx1-ubyte.gz
Extracting data/MNIST/t10k-images-idx3-ubyte.gz
Extracting data/MNIST/t10k-labels-idx1-ubyte.gz

現在已經載入了MNIST資料集,它由70,000張影象和對應的標籤(比如影象的類別)組成。資料集分成三份互相獨立的子集。我們在教程中只用訓練集和測試集。

print("Size of:")
print("- Training-set:\t\t{}".format(len(data.train.labels)))
print("- Test-set:\t\t{}".format(len(data.test.labels)))
print("- Validation-set:\t{}".format(len(data.validation.labels)))複製程式碼

Size of:

  • Training-set: 55000
  • Test-set: 10000
  • Validation-set: 5000

One-Hot 編碼

資料集以一種稱為One-Hot編碼的方式載入。這意味著標籤從一個單獨的數字轉換成一個長度等於所有可能類別數量的向量。向量中除了第$i$個元素是1,其他元素都是0,這代表著它的類別是$i$'。比如,前面五張影象標籤的One-Hot編碼為:

data.test.labels[0:5, :]複製程式碼

array([[ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])

在不同的比較和度量效能時,我們也需要用單獨的數字表示類別,因此我們通過取最大元素的索引,將One-Hot編碼的向量轉換成一個單獨的數字。需注意的是'class'在Python中是一個關鍵字,所以我們用'cls'代替它。

data.test.cls = np.array([label.argmax() for label in data.test.labels])複製程式碼

現在我們可以看到測試集中前面五張影象的類別。將這些與上面的One-Hot編碼的向量進行比較。例如,第一張影象的類別是7,對應的在One-Hot編碼向量中,除了第7個元素其他都為零。

data.test.cls[0:5]複製程式碼

array([7, 2, 1, 0, 4])

資料維度

在下面的原始碼中,有很多地方用到了資料維度。在計算機程式設計中,通常來說最好使用變數和常量,而不是在每次使用數值時寫硬程式碼。這意味著數字只需要在一個地方改動就行。這些最好能從讀取的資料中獲取,但這裡我們直接寫上數值。

# We know that MNIST images are 28 pixels in each dimension.
img_size = 28

# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size

# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)

# Number of classes, one class for each of 10 digits.
num_classes = 10複製程式碼

用來繪製影象的幫助函式

這個函式用來在3x3的柵格中畫9張影象,然後在每張影象下面寫出真實的和預測的類別。

def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9

    # Create figure with 3x3 sub-plots.
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Plot image.
        ax.imshow(images[i].reshape(img_shape), cmap='binary')

        # Show true and predicted classes.
        if cls_pred is None:
            xlabel = "True: {0}".format(cls_true[i])
        else:
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])

        ax.set_xlabel(xlabel)

        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])複製程式碼

繪製幾張影象來看看資料是否正確

# Get the first images from the test-set.
images = data.test.images[0:9]

# Get the true classes for those images.
cls_true = data.test.cls[0:9]

# Plot the images and labels using our helper-function above.
plot_images(images=images, cls_true=cls_true)複製程式碼

TensorFlow圖

TensorFlow的全部目的就是使用一個稱之為計算圖(computational graph)的東西,它會比直接在Python中進行相同計算量要高效得多。TensorFlow比Numpy更高效,因為TensorFlow瞭解整個需要執行的計算圖,然而Numpy只知道某個時間點上唯一的數學運算。

TensorFlow也能夠自動地計算需要優化的變數的梯度,使得模型有更好的表現。這是由於Graph是簡單數學表示式的結合,因此整個圖的梯度可以用鏈式法則推匯出來。

TensorFlow還能利用多核CPU和GPU,Google也為TensorFlow製造了稱為TPUs(Tensor Processing Units)的特殊晶片,它比GPU更快。

一個TensorFlow圖由下面幾個部分組成,後面會詳細描述:

  • 佔位符變數(Placeholder)用來改變圖的輸入。
  • 模型變數(Model)將會被優化,使得模型表現得更好。
  • 模型本質上就是一些數學函式,它根據Placeholder和模型的輸入變數來計算一些輸出。
  • 一個cost度量用來指導變數的優化。
  • 一個優化策略會更新模型的變數。

另外,TensorFlow圖也包含了一些除錯狀態,比如用TensorBoard列印log資料,本教程不涉及這些。

佔位符 (Placeholder)變數

Placeholder是作為圖的輸入,每次我們執行圖的時候都可能會改變它們。將這個過程稱為feeding placeholder變數,後面將會描述它。

首先我們為輸入影象定義placeholder變數。這讓我們可以改變輸入到TensorFlow圖中的影象。這也是一個張量(tensor),代表一個多維向量或矩陣。資料型別設定為float32,形狀設為[None, img_size_flat]None代表tensor可能儲存著任意數量的影象,每張圖象是一個長度為img_size_flat的向量。

x = tf.placeholder(tf.float32, [None, img_size_flat])複製程式碼

接下來我們為輸入變數x中的影象所對應的真實標籤定義placeholder變數。變數的形狀是[None, num_classes],這代表著它儲存了任意數量的標籤,每個標籤是長度為num_classes的向量,本例中長度為10。

y_true = tf.placeholder(tf.float32, [None, num_classes])複製程式碼

最後我們為變數x中影象的真實類別定義placeholder變數。它們是整形,並且這個變數的維度設為[None],代表placeholder變數是任意長的一維向量。

y_true_cls = tf.placeholder(tf.int64, [None])複製程式碼

需要優化的變數

除了上面定義的那些給模型輸入資料的變數之外,TensorFlow還需要改變一些模型變數,使得訓練資料的表現更好。

第一個需要優化的變數稱為權重weight,TensorFlow變數需要被初始化為零,它的形狀是[img_size_flat, num_classes],因此它是一個img_size_flat行、num_classes列的二維張量(或矩陣)。

weights = tf.Variable(tf.zeros([img_size_flat, num_classes]))複製程式碼

第二個需要優化的是偏差變數biases,它被定義成一個長度為num_classes的1維張量(或向量)。

biases = tf.Variable(tf.zeros([num_classes]))複製程式碼

模型

這個最基本的數學模型將placeholder變數x中的影象與權重weight相乘,然後加上偏差biases

結果是大小為[num_images, num_classes]的一個矩陣,由於x的形狀是[num_images, img_size_flat] 並且 weights的形狀是[img_size_flat, num_classes],因此兩個矩陣乘積的形狀是[num_images, num_classes],然後將biases向量新增到矩陣每一行中。

logits = tf.matmul(x, weights) + biases複製程式碼

現在logits是一個 num_imagesnum_classes列的矩陣,第$i$行第$j$列的那個元素代表著第$i$張輸入影象有多大可能性是第$j$個類別。

然而,這是很粗略的估計並且很難解釋,因為數值可能很小或很大,因此我們想要對它們做歸一化,使得logits矩陣的每一行相加為1,每個元素限制在0到1之間。這是用一個稱為softmax的函式來計算的,結果儲存在y_pred中。

y_pred = tf.nn.softmax(logits)複製程式碼

可以從y_pred矩陣中取每行最大元素的索引值,來得到預測的類別。

y_pred_cls = tf.argmax(y_pred, dimension=1)複製程式碼

優化損失函式

為了使模型更好地對輸入影象進行分類,我們必須改變weightsbiases變數。首先我們需要比較模型的預測輸出y_pred和期望輸出y_true,來了解目前模型的效能如何。

交叉熵(cross-entropy)是一個在分類中使用的效能度量。交叉熵是一個常為正值的連續函式,如果模型的預測值精準地符合期望的輸出,它就等於零。因此,優化的目的就是最小化交叉熵,通過改變模型中weightsbiases的值,使交叉熵越接近零越好。

TensorFlow有一個內建的計算交叉熵的函式。需要注意的是它使用logits的值,因為在它內部也計算了softmax。

cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits,
                                                        labels=y_true)複製程式碼

現在,我們已經為每個影象分類計算了交叉熵,所以有一個當前模型在每張圖上的效能度量。但是為了用交叉熵來指導模型變數的優化,我們需要一個額外的標量值,因此我們簡單地利用所有影象分類交叉熵的均值。

cost = tf.reduce_mean(cross_entropy)複製程式碼

優化方法

現在,我們有一個需要被最小化的損失度量,接著我們可以建立優化器。在這種情況中,用的是梯度下降的基本形式,步長設為0.5。

優化過程並不是在這裡執行。實際上,還沒計算任何東西,我們只是往TensorFlow圖中新增了優化器,以便之後的操作。

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cost)複製程式碼

效能度量

我們需要另外一些效能度量,來向使用者展示這個過程。

這是一個布林值向量,代表預測型別是否等於每張圖片的真實型別。

correct_prediction = tf.equal(y_pred_cls, y_true_cls)複製程式碼

上面先將布林值向量型別轉換成浮點型向量,這樣子False就變成0,True變成1,然後計算這些值的平均數,以此來計算分類的準確度。

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))複製程式碼

執行TensorFlow

建立TensorFlow會話(session)

一旦建立了TensorFlow圖,我們需要建立一個TensorFlow session,用來執行圖。

session = tf.Session()複製程式碼

初始化變數

我們需要在開始優化weightsbiases變數之前對它們進行初始化。

session.run(tf.global_variables_initializer())複製程式碼

用來優化迭代的幫助函式

在訓練集中有50,000張圖。用這些影象計算模型的梯度會花很多時間。因此我們利用隨機梯度下降的方法,它在優化器的每次迭代裡只用到了一小部分的影象。

batch_size = 100複製程式碼

函式執行了多次的優化迭代來逐步地提升模型的weightsbiases。在每次迭代中,從訓練集中選擇一批新的資料,然後TensorFlow用這些訓練樣本來執行優化器。

def optimize(num_iterations):
    for i in range(num_iterations):
        # Get a batch of training examples.
        # x_batch now holds a batch of images and
        # y_true_batch are the true labels for those images.
        x_batch, y_true_batch = data.train.next_batch(batch_size)

        # Put the batch into a dict with the proper names
        # for placeholder variables in the TensorFlow graph.
        # Note that the placeholder for y_true_cls is not set
        # because it is not used during training.
        feed_dict_train = {x: x_batch,
                           y_true: y_true_batch}

        # Run the optimizer using this batch of training data.
        # TensorFlow assigns the variables in feed_dict_train
        # to the placeholder variables and then runs the optimizer.
        session.run(optimizer, feed_dict=feed_dict_train)複製程式碼

展示效能的幫助函式

測試集資料字典被當做TensorFlow圖的輸入。注意,在TensorFlow圖中,placeholder變數必須使用正確的名字。

feed_dict_test = {x: data.test.images,
                  y_true: data.test.labels,
                  y_true_cls: data.test.cls}複製程式碼

用來列印測試集分類準確度的函式。

def print_accuracy():
    # Use TensorFlow to compute the accuracy.
    acc = session.run(accuracy, feed_dict=feed_dict_test)

    # Print the accuracy.
    print("Accuracy on test-set: {0:.1%}".format(acc))複製程式碼

函式用scikit-learn列印並繪製混淆矩陣。

def print_confusion_matrix():
    # Get the true classifications for the test-set.
    cls_true = data.test.cls

    # Get the predicted classifications for the test-set.
    cls_pred = session.run(y_pred_cls, feed_dict=feed_dict_test)

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_true,
                          y_pred=cls_pred)

    # Print the confusion matrix as text.
    print(cm)

    # Plot the confusion matrix as an image.
    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)

    # Make various adjustments to the plot.
    plt.tight_layout()
    plt.colorbar()
    tick_marks = np.arange(num_classes)
    plt.xticks(tick_marks, range(num_classes))
    plt.yticks(tick_marks, range(num_classes))
    plt.xlabel('Predicted')
    plt.ylabel('True')複製程式碼

繪製測試集中誤分類影象的函式。

def plot_example_errors():
    # Use TensorFlow to get a list of boolean values
    # whether each test-image has been correctly classified,
    # and a list for the predicted class of each image.
    correct, cls_pred = session.run([correct_prediction, y_pred_cls],
                                    feed_dict=feed_dict_test)

    # Negate the boolean array.
    incorrect = (correct == False)

    # Get the images from the test-set that have been
    # incorrectly classified.
    images = data.test.images[incorrect]

    # Get the predicted classes for those images.
    cls_pred = cls_pred[incorrect]

    # Get the true classes for those images.
    cls_true = data.test.cls[incorrect]

    # Plot the first 9 images.
    plot_images(images=images[0:9],
                cls_true=cls_true[0:9],
                cls_pred=cls_pred[0:9])複製程式碼

繪製模型權重的幫助函式

這個函式用來繪製模型的權重weights。畫了10張影象,訓練模型所識別出的每個數字對應著一張圖。

def plot_weights():
    # Get the values for the weights from the TensorFlow variable.
    w = session.run(weights)

    # Get the lowest and highest values for the weights.
    # This is used to correct the colour intensity across
    # the images so they can be compared with each other.
    w_min = np.min(w)
    w_max = np.max(w)

    # Create figure with 3x4 sub-plots,
    # where the last 2 sub-plots are unused.
    fig, axes = plt.subplots(3, 4)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Only use the weights for the first 10 sub-plots.
        if i<10:
            # Get the weights for the i'th digit and reshape it.
            # Note that w.shape == (img_size_flat, 10)
            image = w[:, i].reshape(img_shape)

            # Set the label for the sub-plot.
            ax.set_xlabel("Weights: {0}".format(i))

            # Plot the image.
            ax.imshow(image, vmin=w_min, vmax=w_max, cmap='seismic')

        # Remove ticks from each sub-plot.
        ax.set_xticks([])
        ax.set_yticks([])複製程式碼

優化之前的效能

測試集上的準確度是9.8%。這是由於模型只做了初始化,並沒做任何優化,所以它通常將影象預測成數字零,正如下面繪製的影象那樣,剛好測試集中9.8%的影象是數字零。

print_accuracy()複製程式碼

Accuracy on test-set: 9.8%

plot_example_errors()複製程式碼

1次迭代優化後的效能

在完成一次迭代優化之後,模型在測試集上的準確率從9.8%提高到了40.7%。這意味著它大約10次裡面會誤分類6次,正如下面所顯示的。

optimize(num_iterations=1)複製程式碼
print_accuracy()複製程式碼

Accuracy on test-set: 40.7%

plot_example_errors()複製程式碼

下面繪製的是權重。正值為紅色,負值為藍色。這些權重可以直觀地理解為影象濾波器。

例如,權重用來確定一張數字零的影象對圓形影象有正反應(紅色),對圓形影象的中間部分有負反應(藍色)。

類似的,權重也用來確定一張數字一的影象對影象中心垂直線段有正反應(紅色),對線段周圍有負反應(藍色)。

注意到權重大多看起來跟它要識別的數字很像。這是因為只做了一次迭代,即權重只在100張影象上訓練。等經過上千張影象的訓練之後,權重會變得更難分辨,因為它們需要識別出數字的許多種書寫方法。

plot_weights()複製程式碼

10次優化迭代後的效能

# We have already performed 1 iteration.
optimize(num_iterations=9)複製程式碼
print_accuracy()複製程式碼

Accuracy on test-set: 78.2%

plot_example_errors()複製程式碼

plot_weights()複製程式碼

1000次迭代之後的效能

在迭代了1000次之後,模型在十次裡面大約只誤識別了一次。如下圖所示,有些誤識別情有可原,因為即使在人類眼裡,也很難確定影象(的數字),然而有一些影象是很明顯的,好的模型應該能分辨出來。但這個簡單的模型無法達到更好的效能,因此需要更為複雜的模型。

# We have already performed 10 iterations.
optimize(num_iterations=990)複製程式碼
print_accuracy()複製程式碼

Accuracy on test-set: 91.7%

plot_example_errors()複製程式碼

模型經過了1000次迭代訓練,每次迭代用到訓練集裡面的100張影象。由於影象的多樣化,現在權重變得很難辨認,我們可能會懷疑這些權重是否真的理解數字是怎麼由線條組成的,或者模型只是記住了許多不同的畫素。

plot_weights()複製程式碼


我們也可以列印並繪製出混淆矩陣,它讓我們看到誤分類的更多細節。例如,它展示了描繪著數字5的影象有時會被誤分類成其他可能的數字,但大多是3,6或8。

print_confusion_matrix()複製程式碼

[[ 957 0 3 2 0 5 11 1 1 0]
[ 0 1108 2 2 1 2 4 2 14 0]
[ 4 9 914 19 15 5 13 14 35 4]
[ 1 0 16 928 0 28 2 14 13 8]
[ 1 1 3 2 939 0 10 2 6 18]
[ 10 3 3 33 10 784 17 6 19 7]
[ 8 3 3 2 11 14 915 1 1 0]
[ 3 9 21 9 7 1 0 959 2 17]
[ 8 8 8 38 11 40 14 18 825 4]
[ 11 7 1 13 75 13 1 39 4 845]]


現在我們用TensorFlow完成了任務,關閉session,釋放資源。

# This has been commented out in case you want to modify and experiment
# with the Notebook without having to restart it.
# session.close()複製程式碼

練習

下面是一些可能會讓你提升TensorFlow技能的一些建議練習。為了學習如何更合適地使用TensorFlow,實踐經驗是很重要的。

在你對這個Notebook進行修改之前,可能需要先備份一下。

  • 改變優化器的學習率。
  • 改變優化器,比如用AdagradOptimizerAdamOptimizer
  • 將batch-size改為1或1000。
  • 這些改變如何影響效能?
  • 你覺得這些改變對其他分類問題或數學模型有相同的影響嗎?
  • 如果你不改變任何引數,多次執行Notebook,會得到完成一樣的結果嗎?為什麼?
  • 改變plot_example_errors() 函式,使它列印誤分類的 logitsy_pred值。
  • sparse_softmax_cross_entropy_with_logits 代替 softmax_cross_entropy_with_logits。這可能需要改變程式碼的多個地方。探討使用這兩中方法的優缺點。
  • 不看原始碼,自己重寫程式。
  • 向朋友解釋程式如何工作。

相關文章