吳恩達老師的公開課,簡單神經網路的作業總結

2021-08-10 10:57:06 字數 2803 閱讀 8615

定義四個變數

train_set_x shape: (209, 64, 64, 3)

train_set_y shape: (1, 209)

test_set_x shape: (50, 64, 64, 3)

test_set_y shape: (1, 50)

分別為訓練集的x,y,測試集的x,y。

這裡有一點需要注意,作業中直接匯入的檔案train_catvnoncat.h5和test_catvnoncat.h5兩個檔案,如果後面需要訓練自己的資料,需要建立資料集

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).t

test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).t

建立了兩個為(64×63×3,209)和(64×64×3,50)這樣的形式,作為兩個新的變數

為什麼上面的語句可以實現這一點,需要繼續學習

初始化定義w,b,其中w為(m,1)的形式,其中m為輸入的列向量的維度

函式命名為initialize_with_zeros(dim)

進行正向傳播和損失函式的計算

m = x.shape[1]

a = sigmoid(np.dot(w.t,x)+b)

cost = -np.sum((np.dot(y,np.log(a).t)+np.dot((1-y),np.log(1-a).t)))/m

dw = np.dot(x,(a-y).t)/m

db = np.sum(a-y)/m

上面為計算公式,下面的為宣告和乙個計算,不是很懂

assert(dw.shape == w.shape)

assert(db.dtype == float)

cost = np.squeeze(cost)

assert(cost.shape == ())

在寫程式時出現了乙個問題:dw = 1/m×np.dot(x,(a-y).t)執行一直為0,不是很懂,但是需要注意

函式命名為propagate(w, b, x, y)

///

反響傳播計算,利用學習率計算w,b的下次迭代的值

costs = 

for i in range(num_iterations):

grads, cost = propagate(w, b, x, y)

dw = grads["dw"]//這裡用來做什麼,不是很懂

db = grads["db"]

w = w - learning_rate*dw

b = b - learning_rate*db

if i % 100 == 0:

if print_cost and i % 100 == 0:

print ("cost after iteration %i: %f"

%(i, cost))

函式命名為optimize(w, b, x, y, num_iterations, learning_rate, print_cost = false)

對結果進行**

m = x

.shape[1]

y_prediction = np.zeros((1,m))

w = w.reshape(x

.shape[0], 1)

a = sigmoid(np.dot(w.t,x)+b)

for i in range(a.shape[1]):

if a[0,i]>=0.5:

y_prediction[0,i] = 1

else:

y_prediction[0,i] = 0

assert(y_prediction.shape == (1, m))

函式命名為predict(w, b, x)

函式命名完成,需要進行訓練,先建立model,然後訓練資料集

w, b = initialize_with_zeros(x_train.shape[0])

parameters, grads, costs = optimize(w, b, x_train, y_train, num_iterations, learning_rate, print_cost)

w = parameters["w"]

b = parameters["b"]

y_prediction_test = predict(w, b, x_test)

y_prediction_train = predict(w, b, x_train)

print("train accuracy: {} %".format(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100))

print("test accuracy: {} %".format(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100))

d =

上面的語句顯示最終的正確率,如何計算得到的,還不是很懂,python接觸時間太短,需要了解

函式命名為model(x_train, y_train, x_test, y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = false)

/ 如果要訓練深度學習,網路深度,模型設計,還有資料集的製作,這些都需要仔細學習一下

吳恩達《卷積神經網路》

一 yolo algorithm 把輸入的分割成3x3個格仔或19x19個格仔,每個格仔進行檢測輸出八個特徵,3x3就輸出3x3x8個向量。yolo演算法的優點是不需要乙個演算法跑很多次。相反這是單次卷積實現,在處理計算時很多步驟都是共享的,而且執行速度非常快可以達到實時識別。物件中點的座標在哪個格...

吳恩達 卷積神經網路

卷積神經網路 卷積操作 設輸入n,filter為f,padding為p,步長 stride 為s 則卷積 不滿足結合律,滿足結合律還需要對filter進行水平和垂直翻轉 之後影象大小為 n 2p f s 1 向下取整 rgb影象卷積操作 同時相乘相加,三個channel輸出乙個值 為什麼cnn可以避...

吳恩達 卷積神經網路

1 灰度影象,邊緣檢測,使用核函式的缺點,影象的向量會不斷的縮小,另外乙個就是邊緣的向量相比於中間的向量被覆蓋的次數會少很多。解決這個的方法就是padding在影象的周圍再新增一圈向量。2 核函式通常是奇數維向量 3 卷積層,池化層 選出某一區域的最大值,另外還有 平均池化,就是求乙個小區域的均值 ...