def one_vs_all(X, y, num_labels, lmd): # Some useful variables (m, n) = X.shape # You need to return the following variables correctly all_theta = np.zeros((num_labels, n + 1)) # Add ones to the X data 2D-array X = np.c_[np.ones(m), X] for i in range(num_labels): print('Optimizing for handwritten number {}...'.format(i)) # ===================== Your Code Here ===================== # Instructions : You should complete the following code to train num_labels # logistic regression classifiers with regularization # parameter lambda # # # Hint: you can use y == c to obtain a vector of True(1)'s and False(0)'s that tell you # whether the ground truth is true/false for this class # # Note: For this assignment, we recommend using opt.fmin_cg to optimize the cost # function. It is okay to use a for-loop (for c in range(num_labels) to # loop over the different classes # iclass = i if i else 10 y_i = np.array([1 if x == iclass else 0 for x in y]) cost_func = lambda t : lCF.lr_cost_function(t, X, y_i, lmd)[0] grad_func = lambda t: lCF.lr_cost_function(t, X, y_i, lmd)[1] theta = opt.fmin_cg(f=cost_func, x0=all_theta[i].T, maxiter=100, fprime=grad_func, disp=True) all_theta[i] = theta # ============================================================ print('Done') return all_theta
# ===================== Part 2-a: Vectorize Logistic Regression ===================== # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. Your task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset # # Test case for lrCostFunction print('Testing lrCostFunction()') theta_t = np.array([-2, -1, 1, 2]) X_t = np.c_[np.ones(5), np.arange(1, 16).reshape((3, 5)).T/10] y_t = np.array([1, 0, 1, 0, 1]) lmda_t = 3 cost, grad = lCF.lr_cost_function(theta_t, X_t, y_t, lmda_t) np.set_printoptions(formatter={'float': '{: 0.6f}'.format}) print('Cost: {:0.7f}'.format(cost)) print('Expected cost: 2.534819') print('Gradients:\n{}'.format(grad)) print('Expected gradients:\n[ 0.146561 -0.548558 0.724722 1.398003]') input('Program paused. Press ENTER to continue') # ===================== Part 2-b: One-vs-All Training ===================== print('Training One-vs-All Logistic Regression ...') lmd = 0.1
def grad_func(t): return lCF.lr_cost_function(t, X, y_i, lmd)[1]
def cost_func(t): return lCF.lr_cost_function(t, X, y_i, lmd)[0]
def grad_func(t): return lr_cost_function(X,y,t,lmd)[1]
def cost_func(t): return lr_cost_function(X,y,t,lmd)[0]
# Randomly select 100 data points to display shuffle_100_X = np.arange(0, m, 1, dtype=int) np.random.shuffle(shuffle_100_X) sel = X[shuffle_100_X[0:100], :] display_data(sel) print('Program paused. Press enter to continue.\n') # pause_func() # ============ Part 2a: Vectorize Logistic Regression ============ # Test case for lrCostFunction print('\nTesting lrCostFunction() with regularization') theta_t = np.array([[-2], [-1], [1], [2]]) X_t = np.append(np.ones((5, 1)), np.arange(1, 16).reshape(5, 3, order='F') / 10, axis=1) y_t = np.array([[1], [0], [1], [0], [1]]) lambda_t = 3 J, grad = lr_cost_function(theta_t, X_t, y_t, lambda_t) print('\nCost: \n', J, '\nExpected cost: 2.534819\n') print('Gradients:\n', grad, '\nExpected gradients:\n', ' 0.146561\n -0.548558\n 0.724722\n 1.398003\n') print('Program paused. Press enter to continue.\n') # pause_func() # ============ Part 2b: One-vs-All Training ============ print('\nTraining One-vs-All Logistic Regression...\n') ova_lambda = 0.1 all_theta = one_vs_all(X, y, num_labels, ova_lambda) print('Program paused. Press enter to continue.\n') # pause_func() # ================ Part 3: Predict for One-Vs-All ================ pred = predict_one_vs_all(all_theta, X) + 1 print('\nTraining Set Accuracy: \n', np.mean((pred == y) * 100))