x[m,n]是通過numpy庫引用數組或矩陣中的某一段數據集的一種寫法,
m代表第m維,n代表m維中取第幾段特征數據。
通常用法:
x[:,n]或者x[n,:]
1.x[:,n]表示在全部數組(維)中取第n個數據,直觀來說,x[:,n]就是取所有集合的第n個數據,
1 import numpy as np 2 3 X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]]) 4 print X[:,0]
X[:,0]輸出結果是:
1 import numpy as np 2 3 X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]]) 4 print X[:,1]
2. x[n,:]表示在n個數組(維)中取全部數據,直觀來說,x[n,:]就是取第n集合的所有數據,
X[1,:]即取第一維中下標為1的元素的所有值,輸出結果:
X[:, m:n],即取所有數據的第m到n-1列數據,含左不含右
例:輸出X數組中所有行第1到2列數據
X = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14],[15,16,17],[18,19,20]])
print X[:,1:3]
以下 numpy.random.shuffle() 簡稱 shuffle,numpy.random.permutation() 簡稱 permutation。
shuffle 的參數只能是 array_like,而 permutation 除了 array_like 還可以是 int 類型,如果是 int 類型,那就隨機打亂 numpy.arange(int)。
shuffle 返回 None,這點尤其要注意,也就是說沒有返回值,而 permutation 則返回打亂后的 array。
def permutation(self, object x): '''這里都是幫助文檔,我就省略了''' if isinstance(x, (int, long, np.integer)): arr = np.arange(x) else: arr = np.array(x) self.shuffle(arr) return arr
所以permutation內部調用的也是shuffle
1.獲取mini-batch:
def random_mini_batches(X,Y,mini_batch_size=64,seed=0):
第一,打亂順序
permutation = list(np.random.permutation(m)) #它會返回一個長度為m的隨機數組,且里面的數是0到m-1 shuffled_X = X[:,permutation] #將每一列的數據按permutation的順序來重新排列。 shuffled_Y = Y[:,permutation].reshape((1,m))
X - 輸入數據,維度為(輸入節點數量,樣本的數量) Y - 對應的是X的標簽,【1 | 0】(藍|紅),維度為(1,樣本的數量)
這一步就相當於對X的維度=1隨機取,等於被打亂了(因為那個位置對應的都是樣本)
第二.分割
num_complete_minibatches = math.floor(m / mini_batch_size) #把你的訓練集分割成多少份,請注意,如果值是99.99,那么返回值是99,剩下的0.99會被舍棄 for k in range(0,num_complete_minibatches): mini_batch_X = shuffled_X[:,k * mini_batch_size:(k+1)*mini_batch_size] mini_batch_Y = shuffled_Y[:,k * mini_batch_size:(k+1)*mini_batch_size]
類似於
a = np.array([[1,2,3,4,5,6,7,8,9],
[9,8,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,8,9]])
k=1
mini_batch_size=3
print(a[:,1*3:(1+1)*3]) #從第4列到第6列
'''
[[4 5 6]
[6 5 4]
[4 5 6]]
'''
k=2
print(a[:,2*3:(2+1)*3]) #從第7列到第9列
'''
[[7 8 9]
[3 2 1]
[7 8 9]]
'''
包含動量的梯度下降:
這里的V還是指的dw與db的值
def initialize_velocity(parameters): """ 初始化速度,velocity是一個字典: - keys: "dW1", "db1", ..., "dWL", "dbL" - values:與相應的梯度/參數維度相同的值為零的矩陣。 參數: parameters - 一個字典,包含了以下參數: parameters["W" + str(l)] = Wl parameters["b" + str(l)] = bl 返回: v - 一個字典變量,包含了以下參數: v["dW" + str(l)] = dWl的速度 v["db" + str(l)] = dbl的速度 """ L = len(parameters) // 2 #神經網絡的層數 v = {} for l in range(L): v["dW" + str(l + 1)] = np.zeros_like(parameters["W" + str(l + 1)]) v["db" + str(l + 1)] = np.zeros_like(parameters["b" + str(l + 1)]) return v
def update_parameters_with_momentun(parameters,grads,v,beta,learning_rate): """ 使用動量更新參數 參數: parameters - 一個字典類型的變量,包含了以下字段: parameters["W" + str(l)] = Wl parameters["b" + str(l)] = bl grads - 一個包含梯度值的字典變量,具有以下字段: grads["dW" + str(l)] = dWl grads["db" + str(l)] = dbl v - 包含當前速度的字典變量,具有以下字段: v["dW" + str(l)] = ... v["db" + str(l)] = ... beta - 超參數,動量,實數 learning_rate - 學習率,實數 返回: parameters - 更新后的參數字典 v - 包含了更新后的速度變量 """ L = len(parameters) // 2 for l in range(L): #計算速度 v["dW" + str(l + 1)] = beta * v["dW" + str(l + 1)] + (1 - beta) * grads["dW" + str(l + 1)] v["db" + str(l + 1)] = beta * v["db" + str(l + 1)] + (1 - beta) * grads["db" + str(l + 1)] #更新參數 parameters["W" + str(l + 1)] = parameters["W" + str(l + 1)] - learning_rate * v["dW" + str(l + 1)] parameters["b" + str(l + 1)] = parameters["b" + str(l + 1)] - learning_rate * v["db" + str(l + 1)] return parameters,v
Adam算法
def initialize_adam(parameters): """ 初始化v和s,它們都是字典類型的變量,都包含了以下字段: - keys: "dW1", "db1", ..., "dWL", "dbL" - values:與對應的梯度/參數相同維度的值為零的numpy矩陣 參數: parameters - 包含了以下參數的字典變量: parameters["W" + str(l)] = Wl parameters["b" + str(l)] = bl 返回: v - 包含梯度的指數加權平均值,字段如下: v["dW" + str(l)] = ... v["db" + str(l)] = ... s - 包含平方梯度的指數加權平均值,字段如下: s["dW" + str(l)] = ... s["db" + str(l)] = ... """ L = len(parameters) // 2 v = {} s = {} for l in range(L): v["dW" + str(l + 1)] = np.zeros_like(parameters["W" + str(l + 1)]) v["db" + str(l + 1)] = np.zeros_like(parameters["b" + str(l + 1)]) s["dW" + str(l + 1)] = np.zeros_like(parameters["W" + str(l + 1)]) s["db" + str(l + 1)] = np.zeros_like(parameters["b" + str(l + 1)]) return (v,s)
np.arange(24).reshape(4,6)
def update_parameters_with_adam(parameters,grads,v,s,t,learning_rate=0.01,beta1=0.9,beta2=0.999,epsilon=1e-8): """ 使用Adam更新參數 參數: parameters - 包含了以下字段的字典: parameters['W' + str(l)] = Wl parameters['b' + str(l)] = bl grads - 包含了梯度值的字典,有以下key值: grads['dW' + str(l)] = dWl grads['db' + str(l)] = dbl v - Adam的變量,第一個梯度的移動平均值,是一個字典類型的變量 s - Adam的變量,平方梯度的移動平均值,是一個字典類型的變量 t - 當前迭代的次數 learning_rate - 學習率 beta1 - 動量,超參數,用於第一階段,使得曲線的Y值不從0開始(參見天氣數據的那個圖) beta2 - RMSprop的一個參數,超參數 epsilon - 防止除零操作(分母為0) 返回: parameters - 更新后的參數 v - 第一個梯度的移動平均值,是一個字典類型的變量 s - 平方梯度的移動平均值,是一個字典類型的變量 """ L = len(parameters) // 2 v_corrected = {} #偏差修正后的值 s_corrected = {} #偏差修正后的值 for l in range(L): #梯度的移動平均值,輸入:"v , grads , beta1",輸出:" v " v["dW" + str(l + 1)] = beta1 * v["dW" + str(l + 1)] + (1 - beta1) * grads["dW" + str(l + 1)] v["db" + str(l + 1)] = beta1 * v["db" + str(l + 1)] + (1 - beta1) * grads["db" + str(l + 1)] #計算第一階段的偏差修正后的估計值,輸入"v , beta1 , t" , 輸出:"v_corrected" v_corrected["dW" + str(l + 1)] = v["dW" + str(l + 1)] / (1 - np.power(beta1,t)) v_corrected["db" + str(l + 1)] = v["db" + str(l + 1)] / (1 - np.power(beta1,t)) #計算平方梯度的移動平均值,輸入:"s, grads , beta2",輸出:"s" s["dW" + str(l + 1)] = beta2 * s["dW" + str(l + 1)] + (1 - beta2) * np.square(grads["dW" + str(l + 1)]) s["db" + str(l + 1)] = beta2 * s["db" + str(l + 1)] + (1 - beta2) * np.square(grads["db" + str(l + 1)]) #計算第二階段的偏差修正后的估計值,輸入:"s , beta2 , t",輸出:"s_corrected" s_corrected["dW" + str(l + 1)] = s["dW" + str(l + 1)] / (1 - np.power(beta2,t)) s_corrected["db" + str(l + 1)] = s["db" + str(l + 1)] / (1 - np.power(beta2,t)) #更新參數,輸入: "parameters, learning_rate, v_corrected, s_corrected, epsilon". 輸出: "parameters". parameters["W" + str(l + 1)] = parameters["W" + str(l + 1)] - learning_rate * (v_corrected["dW" + str(l + 1)] / np.sqrt(s_corrected["dW" + str(l + 1)] + epsilon)) parameters["b" + str(l + 1)] = parameters["b" + str(l + 1)] - learning_rate * (v_corrected["db" + str(l + 1)] / np.sqrt(s_corrected["db" + str(l + 1)] + epsilon)) return (parameters,v,s)
現在用類似之前的模型來寫個程序
def model(X,Y,layers_dims,optimizer,learning_rate=0.0007, mini_batch_size=64,beta=0.9,beta1=0.9,beta2=0.999, epsilon=1e-8,num_epochs=10000,print_cost=True,is_plot=True): """ 可以運行在不同優化器模式下的3層神經網絡模型。 參數: X - 輸入數據,維度為(2,輸入的數據集里面樣本數量) Y - 與X對應的標簽 layers_dims - 包含層數和節點數量的列表 optimizer - 字符串類型的參數,用於選擇優化類型,【 "gd" | "momentum" | "adam" 】 learning_rate - 學習率 mini_batch_size - 每個小批量數據集的大小 beta - 用於動量優化的一個超參數 beta1 - 用於計算梯度后的指數衰減的估計的超參數 beta1 - 用於計算平方梯度后的指數衰減的估計的超參數 epsilon - 用於在Adam中避免除零操作的超參數,一般不更改 num_epochs - 整個訓練集的遍歷次數,(視頻2.9學習率衰減,1分55秒處,視頻中稱作“代”),相當於之前的num_iteration print_cost - 是否打印誤差值,每遍歷1000次數據集打印一次,但是每100次記錄一個誤差值,又稱每1000代打印一次 is_plot - 是否繪制出曲線圖 返回: parameters - 包含了學習后的參數 """ L = len(layers_dims) costs = [] t = 0 #每學習完一個minibatch就增加1 seed = 10 #隨機種子 #初始化參數 parameters = opt_utils.initialize_parameters(layers_dims) #選擇優化器 注意注意,重點操作!優化用的是啥就指望這個呢!這里是初始化V與S
if optimizer == "gd": pass #不使用任何優化器,直接使用梯度下降法 elif optimizer == "momentum": v = initialize_velocity(parameters) #使用動量 elif optimizer == "adam": v, s = initialize_adam(parameters)#使用Adam優化 else: print("optimizer參數錯誤,程序退出。") exit(1) #開始學習 for i in range(num_epochs): #定義隨機 minibatches,我們在每次遍歷數據集之后增加種子以重新排列數據集,使每次數據的順序都不同 seed = seed + 1 minibatches = random_mini_batches(X,Y,mini_batch_size,seed) for minibatch in minibatches: #選擇一個minibatch (minibatch_X,minibatch_Y) = minibatch #前向傳播 A3 , cache = opt_utils.forward_propagation(minibatch_X,parameters) #計算誤差 cost = opt_utils.compute_cost(A3 , minibatch_Y) #反向傳播 grads = opt_utils.backward_propagation(minibatch_X,minibatch_Y,cache) #更新參數 注意注意 重點操作第二步,這里頭是更新不同的參數 if optimizer == "gd": parameters = update_parameters_with_gd(parameters,grads,learning_rate) elif optimizer == "momentum": parameters, v = update_parameters_with_momentun(parameters,grads,v,beta,learning_rate) elif optimizer == "adam": t = t + 1 #這個t是用來修正v和s的corrected的 parameters , v , s = update_parameters_with_adam(parameters,grads,v,s,t,learning_rate,beta1,beta2,epsilon) #記錄誤差值 if i % 100 == 0: costs.append(cost) #是否打印誤差值 if print_cost and i % 1000 == 0: print("第" + str(i) + "次遍歷整個數據集,當前誤差值:" + str(cost)) #是否繪制曲線圖 if is_plot: plt.plot(costs) plt.ylabel('cost') plt.xlabel('epochs (per 100)') plt.title("Learning rate = " + str(learning_rate)) plt.show() return parameters
普通
#使用普通的梯度下降 layers_dims = [train_X.shape[0],5,2,1] parameters = model(train_X, train_Y, layers_dims, optimizer="gd",is_plot=True)
adam優化后
layers_dims = [train_X.shape[0], 5, 2, 1] #使用Adam優化的梯度下降 parameters = model(train_X, train_Y, layers_dims, optimizer="adam",is_plot=True)
預測(這里一直不是很懂,回頭再看看opt_utils的代碼研究研究)
#預測 preditions = opt_utils.predict(train_X,train_Y,parameters) #繪制分類圖 plt.title("Model with Adam optimization") axes = plt.gca() axes.set_xlim([-1.5, 2.5]) axes.set_ylim([-1, 1.5]) opt_utils.plot_decision_boundary(lambda x: opt_utils.predict_dec(parameters, x.T), train_X, train_Y)
opt_utils的代碼

1 # -*- coding: utf-8 -*- 2 3 #opt_utils.py 4 5 import numpy as np 6 import matplotlib.pyplot as plt 7 import sklearn 8 import sklearn.datasets 9 10 def sigmoid(x): 11 """ 12 Compute the sigmoid of x 13 14 Arguments: 15 x -- A scalar or numpy array of any size. 16 17 Return: 18 s -- sigmoid(x) 19 """ 20 s = 1/(1+np.exp(-x)) 21 return s 22 23 def relu(x): 24 """ 25 Compute the relu of x 26 27 Arguments: 28 x -- A scalar or numpy array of any size. 29 30 Return: 31 s -- relu(x) 32 """ 33 s = np.maximum(0,x) 34 35 return s 36 37 38 def load_params_and_grads(seed=1): 39 np.random.seed(seed) 40 W1 = np.random.randn(2,3) 41 b1 = np.random.randn(2,1) 42 W2 = np.random.randn(3,3) 43 b2 = np.random.randn(3,1) 44 45 dW1 = np.random.randn(2,3) 46 db1 = np.random.randn(2,1) 47 dW2 = np.random.randn(3,3) 48 db2 = np.random.randn(3,1) 49 50 return W1, b1, W2, b2, dW1, db1, dW2, db2 51 52 def initialize_parameters(layer_dims): 53 """ 54 Arguments: 55 layer_dims -- python array (list) containing the dimensions of each layer in our network 56 57 Returns: 58 parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL": 59 W1 -- weight matrix of shape (layer_dims[l], layer_dims[l-1]) 60 b1 -- bias vector of shape (layer_dims[l], 1) 61 Wl -- weight matrix of shape (layer_dims[l-1], layer_dims[l]) 62 bl -- bias vector of shape (1, layer_dims[l]) 63 64 Tips: 65 - For example: the layer_dims for the "Planar Data classification model" would have been [2,2,1]. 66 This means W1's shape was (2,2), b1 was (1,2), W2 was (2,1) and b2 was (1,1). Now you have to generalize it! 67 - In the for loop, use parameters['W' + str(l)] to access Wl, where l is the iterative integer. 68 """ 69 70 np.random.seed(3) 71 parameters = {} 72 L = len(layer_dims) # number of layers in the network 73 74 for l in range(1, L): 75 parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1])* np.sqrt(2 / layer_dims[l-1]) 76 parameters['b' + str(l)] = np.zeros((layer_dims[l], 1)) 77 78 assert(parameters['W' + str(l)].shape == layer_dims[l], layer_dims[l-1]) 79 assert(parameters['W' + str(l)].shape == layer_dims[l], 1) 80 81 return parameters 82 83 def forward_propagation(X, parameters): 84 """ 85 Implements the forward propagation (and computes the loss) presented in Figure 2. 86 87 Arguments: 88 X -- input dataset, of shape (input size, number of examples) 89 parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3": 90 W1 -- weight matrix of shape () 91 b1 -- bias vector of shape () 92 W2 -- weight matrix of shape () 93 b2 -- bias vector of shape () 94 W3 -- weight matrix of shape () 95 b3 -- bias vector of shape () 96 97 Returns: 98 loss -- the loss function (vanilla logistic loss) 99 """ 100 101 # retrieve parameters 102 W1 = parameters["W1"] 103 b1 = parameters["b1"] 104 W2 = parameters["W2"] 105 b2 = parameters["b2"] 106 W3 = parameters["W3"] 107 b3 = parameters["b3"] 108 109 # LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID 110 z1 = np.dot(W1, X) + b1 111 a1 = relu(z1) 112 z2 = np.dot(W2, a1) + b2 113 a2 = relu(z2) 114 z3 = np.dot(W3, a2) + b3 115 a3 = sigmoid(z3) 116 117 cache = (z1, a1, W1, b1, z2, a2, W2, b2, z3, a3, W3, b3) 118 119 return a3, cache 120 121 def backward_propagation(X, Y, cache): 122 """ 123 Implement the backward propagation presented in figure 2. 124 125 Arguments: 126 X -- input dataset, of shape (input size, number of examples) 127 Y -- true "label" vector (containing 0 if cat, 1 if non-cat) 128 cache -- cache output from forward_propagation() 129 130 Returns: 131 gradients -- A dictionary with the gradients with respect to each parameter, activation and pre-activation variables 132 """ 133 m = X.shape[1] 134 (z1, a1, W1, b1, z2, a2, W2, b2, z3, a3, W3, b3) = cache 135 136 dz3 = 1./m * (a3 - Y) 137 dW3 = np.dot(dz3, a2.T) 138 db3 = np.sum(dz3, axis=1, keepdims = True) 139 140 da2 = np.dot(W3.T, dz3) 141 dz2 = np.multiply(da2, np.int64(a2 > 0)) 142 dW2 = np.dot(dz2, a1.T) 143 db2 = np.sum(dz2, axis=1, keepdims = True) 144 145 da1 = np.dot(W2.T, dz2) 146 dz1 = np.multiply(da1, np.int64(a1 > 0)) 147 dW1 = np.dot(dz1, X.T) 148 db1 = np.sum(dz1, axis=1, keepdims = True) 149 150 gradients = {"dz3": dz3, "dW3": dW3, "db3": db3, 151 "da2": da2, "dz2": dz2, "dW2": dW2, "db2": db2, 152 "da1": da1, "dz1": dz1, "dW1": dW1, "db1": db1} 153 154 return gradients 155 156 def compute_cost(a3, Y): 157 158 """ 159 Implement the cost function 160 161 Arguments: 162 a3 -- post-activation, output of forward propagation 163 Y -- "true" labels vector, same shape as a3 164 165 Returns: 166 cost - value of the cost function 167 """ 168 m = Y.shape[1] 169 170 logprobs = np.multiply(-np.log(a3),Y) + np.multiply(-np.log(1 - a3), 1 - Y) 171 cost = 1./m * np.sum(logprobs) 172 173 return cost 174 175 def predict(X, y, parameters): 176 """ 177 This function is used to predict the results of a n-layer neural network. 178 179 Arguments: 180 X -- data set of examples you would like to label 181 parameters -- parameters of the trained model 182 183 Returns: 184 p -- predictions for the given dataset X 185 """ 186 187 m = X.shape[1] 188 p = np.zeros((1,m), dtype = np.int) 189 190 # Forward propagation 191 a3, caches = forward_propagation(X, parameters) 192 193 # convert probas to 0/1 predictions 194 for i in range(0, a3.shape[1]): 195 if a3[0,i] > 0.5: 196 p[0,i] = 1 197 else: 198 p[0,i] = 0 199 200 # print results 201 202 #print ("predictions: " + str(p[0,:])) 203 #print ("true labels: " + str(y[0,:])) 204 print("Accuracy: " + str(np.mean((p[0,:] == y[0,:])))) 205 206 return p 207 208 def predict_dec(parameters, X): 209 """ 210 Used for plotting decision boundary. 211 212 Arguments: 213 parameters -- python dictionary containing your parameters 214 X -- input data of size (m, K) 215 216 Returns 217 predictions -- vector of predictions of our model (red: 0 / blue: 1) 218 """ 219 220 # Predict using forward propagation and a classification threshold of 0.5 221 a3, cache = forward_propagation(X, parameters) 222 predictions = (a3 > 0.5) 223 return predictions 224 225 def plot_decision_boundary(model, X, y): 226 # Set min and max values and give it some padding 227 x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1 228 y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1 229 h = 0.01 230 # Generate a grid of points with distance h between them 231 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) 232 # Predict the function value for the whole grid 233 Z = model(np.c_[xx.ravel(), yy.ravel()]) 234 Z = Z.reshape(xx.shape) 235 # Plot the contour and training examples 236 plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral) 237 plt.ylabel('x2') 238 plt.xlabel('x1') 239 plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral) 240 plt.show() 241 242 def load_dataset(is_plot = True): 243 np.random.seed(3) 244 train_X, train_Y = sklearn.datasets.make_moons(n_samples=300, noise=.2) #300 #0.2 245 # Visualize the data 246 if is_plot: 247 plt.scatter(train_X[:, 0], train_X[:, 1], c=train_Y, s=40, cmap=plt.cm.Spectral); 248 train_X = train_X.T 249 train_Y = train_Y.reshape((1, train_Y.shape[0])) 250 251 return train_X, train_Y