단순하지만 기본이 되는 매우 중요한 과제입니다. 원래 기본이 가장 중요한 것 로지스틱 회귀 모델을 구축하여 고양이 이미지를 인식하는 시스템을 만드는 과제입니다. 기본적인 로지스틱 회귀 모델의 구현과 평가 방법을 익히며, 딥러닝의 기초적인 개념을 이해하고, 실질적인 머신러닝 모델을 만들어 봅니다. Exercise 1 shape를 통해 길이관련 정보에 접근할 수 있다는 것을 알아야 합니다. output을 보면 아시겠지만 트레이닝 이미지가 4차원 배열입니다.. 숫자로 적혀있으니 별 것 아닌것 같지만, 우리가 현실세계에서 4차원 공간을 마주할 일을 없습니다. 그것을 잘 표현해 놓은것이 인터스텔라의 테서렉트입니다(제 인생영화중에 하나라서 괜히 말함) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # YOUR CODE STARTS HERE m_train = train_set_x_orig.shape[0] m_test = test_set_x_orig.shape[0] num_px = train_set_x_orig.shape[1] # YOUR CODE ENDS HERE print ("Number of training examples: m_train = " + str(m_train)) print ("Number of testing examples: m_test = " + str(m_test)) print ("Height/Width of each image: num_px = " + str(num_px)) print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)") print ("train_set_x shape: " + str(train_set_x_orig.shape)) print ("train_set_y shape: " + str(train_set_y.shape)) print ("test_set_x shape: " + str(test_set_x_orig.shape)) print ("test_set_y shape: " + str(test_set_y.shape)) ## output Number of training examples: m_train = 209 Number of testing examples: m_test = 50 Height/Width of each image: num_px = 64 Each image is of size: (64, 64, 3) 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) Exercise 2 reshape에서 -1의 의미를 알아야 합니다. 하나의 인자만 -1로 넘길 수 있는데, -1인 부분은 나머지 값을 통해 추론하겠다라는 의미입니다. 1 2 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 Exercise 3 - sigmoid 1 2 3 def sigmoid(z): s = 1/(1+np.exp(-z)) return s Exercise 4 - initialize_with_zeros 1 2 3 4 def initialize_with_zeros(dim): w = np.zeros((dim, 1)) b = 0.0 return w, b Exercise 5 - propagate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 def propagate(w, b, X, Y): m = X.shape[1] A = cost = dw = db = cost = np.squeeze(np.array(cost)) grads = {"dw": dw, "db": db} return grads, cost Exercise 6 - optimize 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 def optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False): w = copy.deepcopy(w) b = copy.deepcopy(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 # Record the costs if i % 100 == 0: costs.append(cost) if print_cost: print ("Cost after iteration %i: %f" %(i, cost)) params = {"w": w, "b": b} grads = {"dw": dw, "db": db} return params, grads, costs Exercise 7 - predict 1 2 3 4 5 6 7 8 9 10 11 12 13 14 def predict(w, b, X): m = X.shape[1] Y_prediction = np.zeros((1, m)) w = w.reshape(X.shape[0], 1) Z = np.dot(w.T, X) + b A = 1 / (1 + np.exp(-Z)) for i in range(A.shape[1]): if A[0, i] > 0.5 : Y_prediction[0,i] = 1 else: Y_prediction[0,i] = 0 return Y_prediction Exercise 8 - model 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False): w, b = initialize_with_zeros(X_train.shape[0]) params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, True) w = params["w"] b = params["b"] Y_prediction_test = predict(w, b, X_test) Y_prediction_train = predict(w, b, X_train) if print_cost: 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 = {"costs": costs, "Y_prediction_test": Y_prediction_test, "Y_prediction_train" : Y_prediction_train, "w" : w, "b" : b, "learning_rate" : learning_rate, "num_iterations": num_iterations} return d