TensorFlow深度學習框架學習(二):TensorFlow實現線性支持向量機(SVM)

SVM的原理可以參考李航的《統計學習方法》
具體代碼如下,代碼都有註釋的

#1、導入必要的庫
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets

#2、創建一個計算圖會話,加載需要的數據集。
sess = tf.Session()
iris = datasets.load_iris()
#在iris中的data這個字段裏面,取第一列(其爲花萼長度)和第四列(爲花萼寬度)的特徵變量
x_vals = np.array([[x[0], x[3]] for x in iris.data])
#iris中的target字段裏面有3個數值:0,1,2;其中將0(山鳶尾花)都設置爲1,其他都設置爲-1
y_vals = np.array([1 if y==0 else -1 for y in iris.target])

#3、將數據集分割爲:訓練集和測試集;
#訓練集的x和y分別爲x_vals_train 和 y_vals_train
#測試集的x和y分別爲x_vals_test 和 y_vals_test
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]

#4、設置批量大小,佔位符,模型變量
batch_size = 100

x_data = tf.placeholder(shape=[None, 2], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

#這裏的A,其實就是ω
A = tf.Variable(tf.random_normal(shape=[2,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

#5、聲明模型輸出
model_output = tf.subtract(tf.matmul(x_data, A), b)

#6、聲明最大間隔損失函數,這裏的損失函數是特定的一個損失函數,詳見《TensorFlow機器學習指南》68頁
#定義一個函數來計算向量的L2範數。
l2_norm = tf.reduce_sum(tf.square(A))

#增加間隔參數α= 0.1
alpha = tf.constant([0.01])

classification_term = tf.reduce_mean(tf.maximum(0., tf.subtract(1., tf.multiply(model_output, y_target))))

loss = tf.add(classification_term, tf.multiply(alpha, l2_norm))

#7、聲明預測函數和準確度函數,用來評估訓練集和測試集訓練的準確度:
prediction = tf.sign(model_output)
accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, y_target), tf.float32))

#8、聲明優化器函數
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)
#初始化模型代碼
init = tf.global_variables_initializer()
sess.run(init)

#9、遍歷迭代訓練模型,記錄訓練集和測試集訓練的損失和準確度
loss_vec = []
train_accuracy = []
test_accuracy = []
for i in range(1000):
    rand_index = np.random.choice(len(x_vals_train), size=batch_size)
    rand_x = x_vals_train[rand_index]
    rand_y = np.transpose([y_vals_train[rand_index]])
    sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})

    temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
    loss_vec.append(temp_loss)

    train_acc_temp = sess.run(accuracy, feed_dict={x_data: x_vals_train, y_target:np.transpose([y_vals_train])}) 
    train_accuracy.append(train_acc_temp)

    test_acc_temp = sess.run(accuracy, feed_dict={x_data: x_vals_test, y_target:np.transpose([y_vals_test])})
    test_accuracy.append(test_acc_temp)

    '''
    if(i+1)%100==0:
        print('Step #'+ str(i+1) + ' A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))
        print('Loss = ' + str(temp_loss))
    '''

#10、抽取數據
[[a1], [a2]] = sess.run(A)
[ [b] ] = sess.run(b)
#slope是斜率的意思,intercept是截距的意思
slope = -a2/a1
y_intercept = b/a1

#下面這句話的意思是將x_vals中的第二列單獨取出賦值給x1_vals
x1_vals = [d[1] for d in x_vals]

#best_fit這個best_fit是根據x1_vals計算出超平面上的x0_vals值
#超平面是a1 * x0_vals + a2 * x1_vals - b = 0 所以 x0_vals = -a2/a1 * x1_vals + b/a1 ; 
best_fit = []
for i in x1_vals:
    best_fit.append(slope*i+y_intercept)

#setosa表示山鳶尾花,enumerate表示枚舉
#爲什麼底下用enumerate?因爲i表示的是x_vals的索引值,而使用enumerate可以直接獲得x_vals的索引值
setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==1]
setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==1]
#not_setosa表示不是山鳶尾花
not_setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==-1]
not_setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==-1]

#12、使用Python代碼繪製數據的線性分類器、準確度和損失圖
#首先,先畫出將樣本分類的圖
plt.plot(setosa_x, setosa_y, 'o', label='setosa')
plt.plot(not_setosa_x, not_setosa_y, 'x', label='not-setosa')
plt.plot(x1_vals, best_fit, 'r-', label='Linear Separator', linewidth=3)
plt.ylim([0, 10])
plt.legend(loc='lower right')
plt.title('Sepal Length vs Pedal Width')
plt.xlabel('Pedal Width')
plt.ylabel('Sepal Length')
plt.show()

#訓練集和測試集迭代的準確度
plt.plot(train_accuracy, 'k-', label='Training Accuracy')
plt.plot(test_accuracy, 'r--', label='Test Accuracy')
plt.title('Train and Test Set Accuracise')
plt.xlabel('Generation')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()

#最大間隔圖
plt.plot(loss_vec, 'k-')
plt.title('Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('Loss')
plt.show()

最後得到的結果如下:
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

發佈了50 篇原創文章 · 獲贊 45 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章