數據集為:
代碼為:
1 # encoding: utf-8 2 3 import tensorflow as tf 4 import numpy as np 5 import seaborn as sns 6 import matplotlib.pyplot as plt 7 from sklearn.datasets import make_moons 8 # from sklearn.datasets import make_circles 9 from sklearn.model_selection import train_test_split 10 11 N_SAMPLES = 2000 # 采樣點數 12 TEST_SIZE = 0.3 # 測試數量比率 13 14 # 產生一個簡單的樣本數據集,半環形圖,類似的有make_circles,環形數據 15 X, y = make_moons(n_samples=N_SAMPLES, noise=0.2, random_state=100) # (2000, 2),(2000, 1) 16 # X, y = make_circles(n_samples = N_SAMPLES, noise=0.2, random_state=100) 17 # 將矩陣隨機划分訓練集和測試集 (1400,2),(600,2),(1400,1),(600,1) 18 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=TEST_SIZE, random_state=42) 19 print(X.shape, y.shape) 20 21 # 繪制數據集分布,X為2D坐標,y為數據點標簽 22 23 24 def make_plot(X, y, plot_name=None, XX=None, YY=None, preds=None, dark=False): 25 if dark: 26 plt.style.use('dark_background') 27 else: 28 sns.set_style('whitegrid') 29 plt.figure(figsize=(16, 12)) 30 axes = plt.gca() 31 axes.set(xlabel="$x_l$", ylabel="$x_2$") 32 plt.title(plot_name, fontsize=30) 33 plt.subplots_adjust(left=0.20) # 調整邊距和子圖間距,子圖的左側 34 plt.subplots_adjust(right=0.80) 35 if XX is not None and YY is not None and preds is not None: 36 plt.contourf(XX, YY, preds.shape(XX.shape), 25, alpha=1, cmap=plt.cm.Spectral) 37 plt.contour(XX, YY, preds.reshape(XX.shape), levels=[1.5], cmap="Greys", vmin=0, vmax=.6) 38 # 根據標簽區分顏色 39 plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), s=40, cmap=plt.cm.Spectral, edgecolors='none') 40 41 plt.savefig('data_set.png') 42 plt.close() 43 44 45 make_plot(X, y, "Classification DataSet Visualization") 46 plt.show() 47 48 49 class Layer: 50 # 全連接層網絡 51 def __init__(self, n_input, n_neurons, activation=None, weights=None, bias=None): 52 """ 53 : int n_input: 輸入節點數 54 :int n_neurons: 輸出節點數 55 :str activation: 激活函數類型 56 : weights: 權值張量,內部生成 57 : bias: 偏置,內部生成 58 """ 59 # 通過正態分布生成初始化的參數 60 self.weights \ 61 = weights if weights is not None else \ 62 np.random.randn(n_input, n_neurons) * np.sqrt(1/n_neurons) 63 self.bias \ 64 = bias if bias is not None else \ 65 np.random.randn(n_neurons) * 0.1 66 self.activation = activation 67 self.last_activation = None 68 self.error = None 69 self.delta = None 70 71 # 網絡的前向傳播 72 def activate(self, x): 73 r = np.dot(x, self.weights) + self.bias # X@w + b 74 self.last_activation = self._apply_activation(r) # 激活函數 75 return self.last_activation 76 77 # 不同類型的激活函數 78 def _apply_activation(self, r): 79 if self.activation is None: 80 return r 81 elif self.activation == 'relu': 82 return np.maximum(r, 0) 83 elif self.activation == 'tanh': 84 return np.tanh(r) 85 elif self.activation == 'sigmoid': 86 return 1 / (1 + np.exp(-r)) 87 return r 88 89 # 不同類型激活函數的導數實現 90 def apply_activation_derivation(self, r): 91 if self.activation is None: 92 return np.ones_like(r) 93 elif self.activation == 'relu': 94 grad = np.array(r, copy=True) 95 grad[r > 0] = 1. 96 grad[r <= 0] = 0. 97 return grad 98 elif self.activation == 'tanh': 99 return 1 - r**2 100 elif self.activation == 'sigmoid': 101 return r * (1 - r) 102 return r 103 104 105 # 神經網絡模型 106 class NeuralNetwork: 107 def __init__(self): # 需要實例化后對屬性賦值 108 self._layers = [] # 網絡層對象列表 109 110 def add_layer(self, layer): # 追加網絡層 111 self._layers.append(layer) 112 113 # 前向傳播只需要循環調用各網絡層對象的前向計算函數 114 def feed_forward(self, X): 115 for layer in self._layers: 116 X = layer.activate(X) 117 return X 118 119 # 網絡模型的反向傳播 120 def backpropagation(self, X, y, learning_rate): 121 output = self.feed_forward(X) 122 # 反向循環 123 for i in reversed(range(len(self._layers))): 124 layer = self._layers[i] # 得到當前層對象 125 if layer == self._layers[-1]: #如果是輸出層 126 layer.error = y - output 127 layer.delta = layer.error * layer.apply_activation_derivation(output) 128 else: # 計算隱藏層 129 next_layer = self._layers[i + 1] # 得到下一層對象 130 layer.error = np.dot(next_layer.weights, next_layer.delta) # 矩陣乘法 131 layer.delta = layer.error *\ 132 layer.apply_activation_derivation(layer.last_activation) 133 134 for i in range(len(self._layers)): 135 layer = self._layers[i] 136 # o_i為上一層網絡輸出 137 o_i = np.atleast_2d(X if i == 0 else self._layers[i - 1].last_activation) # 將數據視為2維數據 138 layer.weights += layer.delta * o_i.T * learning_rate # .T是轉置 139 140 # 網絡的訓練 141 def train(self, X_train, X_test, y_train, y_test, learning_rate, max_epochs): 142 temp1 = y_train.shape[0] 143 y_onehot = np.zeros((temp1, 2)) 144 temp2 = np.arange(y_train.shape[0]) # 線性 0 - 1399 145 y_onehot[temp2, y_train] = 1 146 mses = [] 147 accuracy = [] 148 for i in range(max_epochs): 149 for j in range(len(X_train)): # 一次訓練一個樣本 150 self.backpropagation(X_train[j], y_onehot[j], learning_rate) 151 if i % 10 == 0: 152 mse = np.mean(np.square(y_onehot - self.feed_forward(X_train))) 153 mses.append(mse) 154 print('Epoch: #%s, MSE: %f' % (i, float(mse))) 155 acc = self.accuracy(self.predict(X_test), y_test.flatten()) 156 print('Accuracy: %.2f%%' % (acc * 100)) 157 accuracy.append(acc*100) 158 return mses, accuracy 159 160 def accuracy(self, y_output, y_test): 161 return np.mean((np.argmax(y_output, axis=1) == y_test)) 162 163 def predict(self, X_test): 164 return self.feed_forward(X_test) 165 166 167 # 4層全連接網絡 實例化訓練和預測 168 nn = NeuralNetwork() # 實列化網絡 169 nn.add_layer(Layer(2, 25, 'sigmoid')) # 2 --> 25 170 nn.add_layer(Layer(25, 50, 'sigmoid')) # 25 --> 50 171 nn.add_layer(Layer(50, 25, 'sigmoid')) # 50 --> 25 172 nn.add_layer(Layer(25, 2, 'sigmoid')) # 25 --> 2 173 learning_rate = 0.01 174 max_epochs = 1000 175 mses, accuracy = nn.train(X_train, X_test, y_train, y_test, learning_rate, max_epochs) 176 177 plt.figure() 178 plt.plot(mses, 'b', label='MSE Loss') 179 plt.xlabel('Epoch') 180 plt.ylabel('MSE') 181 plt.legend() 182 plt.savefig('exam5.2 MSE Loss.png') 183 plt.show() 184 185 plt.figure() 186 plt.plot(accuracy, 'r', label='Accuracy rate') 187 plt.xlabel('Epoch') 188 plt.ylabel('Accuracy') 189 plt.legend() 190 plt.savefig('exam5.2 Accuracy.png') 191 plt.show()
誤差為:
准確率為:
這個例子的目的是為讓讀者更進一步了解反向傳播,包括數學上的理解和代碼上的理解。
大體上還是能理解文中的含義,只是細節上要自己動手去算,故使用tensorflow封裝好的函數,會簡化很多代碼,
會使學習者的成就感增加,否者的話,看到這么多數學公式以及代碼的實現,早就放棄了!
下一次,我想更新關於tensorboard可視化的一些學習代碼和感興趣的東西。
但是下一次更新也不知道是好久,因為要做Geant4模擬,還有模擬內容相關的圖像重建算法研究,
所以不知道什么時候可以繼續學習tensorflow,但是也不能放棄,一定要把這本書過一遍!
最近solidorks的學習也遇到困難了,也不知道下一次更新是什么時候,可能2019年的更新就這些內容了!
不過對於我來說,也算開了個頭!