# You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') data = scio.loadmat('ex3data1.mat') X = data['X'] y = data['y'].flatten() m = y.size # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) selected = X[rand_indices[0:100], :] dd.display_data(selected) input('Program paused. Press ENTER to continue') # ===================== 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])
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') data = scio.loadmat('ex3data1.mat') X = data['X'] y = data['y'].flatten() m = y.size # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) selected = X[rand_indices[0:100], :] dd.display_data(selected) input('Program paused. Press ENTER to continue') # ===================== Part 2: Loading Parameters ===================== # In this part of the exercise, we load some pre-initiated # neural network parameters print('Loading Saved Neural Network Parameters ...') data = scio.loadmat('ex3weights.mat') theta1 = data['Theta1'] theta2 = data['Theta2'] # ===================== Part 3: Implement Predict ===================== # After training the neural network, we would like to use it to predict
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') data = scio.loadmat('ex4data1.mat') X = data['X'] y = data['y'].flatten() m = y.size # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) selected = X[rand_indices[0:100], :] dd.display_data(selected) input('Program paused. Press ENTER to continue') # ===================== Part 2: Loading Parameters ===================== # In this part of the exercise, we load some pre-initiated # neural network parameters print('Loading Saved Neural Network Parameters ...') data = scio.loadmat('ex4weights.mat') theta1 = data['Theta1'] theta2 = data['Theta2'] nn_params = np.concatenate([theta1.flatten(), theta2.flatten()])
hidden_layer_size = 25 # 10 labels, from 1 to 10 num_labels = 10 # =========== Part 1: Loading and Visualizing Data ============= data = load_mat_data("../ex3data1.mat") X = data['X'] y = data['y'] m = len(y) # Load Training Data print('Loading and Visualizing Data ...\n') # 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 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized neural network parameters. print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2 theta1_2 = load_mat_data('ex3weights.mat') Theta1 = theta1_2['Theta1'] Theta2 = theta1_2['Theta2'] # ================= Part 3: Implement Predict ================= # After training the neural network, we would like to use it to predict # the labels. You will now implement the "predict" function to use the # neural network to predict the labels of the training set. This lets
rk.draw_line(X_norm[i], X_rec[i]) plt.axis([-4, 3, -4, 3]) plt.show() input('Program paused. Press ENTER to continue') # ===================== Part 4: Loading and Visualizing Face Data ===================== # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print('Loading face dataset.') # Load Face dataset data = scio.loadmat('ex7faces.mat') X = data['X'] disp.display_data(X[0:100]) input('Program paused. Press ENTER to continue') # ===================== Part 5: PCA on Face Data: Eigenfaces ===================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print('Running PCA on face dataset.\n(this might take a minute or two ...)') # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature X_norm, mu, sigma = fn.feature_normalize(X) # Run PCA U, S = pca.pca(X_norm)
print(data.keys()) print(data['X'].shape) print(data['y'].shape) X = data['X'] #提取输入特征 5000*400的矩阵 5000个训练样本 每个样本特征维度为400 一行代表一个训练样本 # print(X) y = data['y'].flatten() #提取标签 data['y']是一个5000*1的2维数组 利用flatten()将其转换为有5000个元素的一维数组 #y中 数字0对应的标签是10 print(y) m = y.size #训练样本的数量 # 随机抽取100个训练样本 进行可视化 rand_indices = np.random.permutation(range(m)) #获取0-4999 5000个无序随机索引 selected = X[rand_indices[0:100], :] #获取前100个随机索引对应的整条数据的输入特征 dd.display_data(selected) #调用可视化函数 进行可视化 input('Program paused. Press ENTER to continue') '''第2-1部分 编写逻辑回归的代价函数(正则化),做简单的测试''' # 逻辑回归(正则化)代价函数的测试用例 print('Testing lrCostFunction()') theta_t = np.array([-2, -1, 1, 2]) #初始化假设函数的参数 假设有4个参数 X_t = np.c_[np.ones(5), np.arange(1, 16).reshape((3, 5)).T/10] #输入特征矩阵 5个训练样本 每个样本3个输入特征,前面添加一列特征=1 y_t = np.array([1, 0, 1, 0, 1]) #标签 做2分类 lmda_t = 3 #正则化惩罚性系数 cost, grad = lCF.lr_cost_function(theta_t, X_t, y_t, lmda_t) #传入代价函数
# 25 hidden units hidden_layer_size = 25 # 10 labels, from 1 to 10 num_labels = 10 # =========== Part 1: Loading and Visualizing Data ============= data = load_mat_data("ex4data1.mat") X = data['X'] y = data['y'] m = len(y) # Load Training Data print('Loading and Visualizing Data ...\n') # 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 2: Loading Parameters ================ # In this part of the exercise, we load some pre-initialized neural network parameters. print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2 theta1_2 = load_mat_data('ex4weights.mat') Theta1 = theta1_2['Theta1'] Theta2 = theta1_2['Theta2'] nn_params = np.append(np.ravel(Theta1, order='F'), np.ravel(Theta2, order='F'), axis=0) # ================ Part 3: Compute Cost (Feedforward) ================
hidden_layer_size = 25 num_labels = 10 # =========== Part 1: Loading and Visualizing Data ============= # Load Data print('Loading and Visualizing Data ...\n') data = sio.loadmat('ex4data1.mat') X, y = data['X'], data['y'].flatten() # flatten is important! m = X.shape[0] # Randomly select 100 data points to display sel = np.random.permutation(range(m)) sel = sel[:100] display_data(X[sel, :]) plt.show() input('Program paused. Press enter to continue.\n') # ================ Part 2: Loading Parameters ================ print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2 weights = sio.loadmat('ex4weights.mat') Theta1, Theta2 = weights['Theta1'], weights['Theta2'] # Unroll parameters nn_params = np.r_[Theta1.flatten(), Theta2.flatten()] # ================ Part 3: Compute Cost (Feedforward) ================
# Obtain theta1 and theta2 back from nn_params print(res) theta1, theta2 = deserialize_t(res.x, input_layer_size, hidden_layer_size, num_labels) input('Program paused. Press ENTER to continue') # ===================== Part 10: Visualize Weights ===================== # You can now 'visualize' what the neural network is learning by # displaying the hidden units to see what features they are capturing in # the data print('Visualizing Neural Network...') dd.display_data(theta1[:, 1:]) plt.show() input('Program paused. Press ENTER to continue') # ===================== Part 11: Implement Predict ===================== # After the training the neural network, we would like to use it to predict # the labels. You will now implement the 'predict' function to use the # neural network to predict the labels of the training set. This lets # you compute the training set accuracy. pred = pd.predict(theta1, theta2, X) print('Training set accuracy: {}'.format(np.mean(pred == y) * 100)) input('ex4 Finished. Press ENTER to exit')
X_rec[0, 0], X_rec[0, 1])) print('(this value should be about -1.047419 -1.047419)') # Draw lines connecting the projected points to the original points plt.scatter(X_rec[:, 0], X_rec[:, 1], facecolors='none', edgecolors='r', s=20) for i in range(X_norm.shape[0]): draw_line(X_norm[i, :], X_rec[i, :]) input('Program paused. Press enter to continue.\n') # =============== Part 4: Loading and Visualizing Face Data ============= print("\nLoading face dataset.\n\n") data = loadmat('ex7faces.mat') X = data['X'] # (5000,1024) # display display_data(X[:100, :]) input('Program paused. Press enter to continue.\n') # =========== Part 5: PCA on Face Data: Eigenfaces =================== print('Running PCA on face dataset.') print('(this might take a minute or two ...)\n\n') ''' % Before running PCA, it is important to first normalize X by subtracting % the mean value from each feature ''' X_norm, mu, sigma = feature_normalize(X) # Run PCA U, S, V = pca(X_norm)
# 20x20 Input Imc c.reshape(2,8,order='F')= a.ravel(order='F')ages of Digits input_layer_size = 400 # 10 labels, from 1 to 10 num_labels = 10 # =========== Part 1: Loading and Visualizing Data ============= data = load_mat_data("ex3data1.mat") X = data['X'] y = data['y'] m = len(y) # Load Training Data print('Loading and Visualizing Data ...\n') # 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()
input_layer_size = 400 hidden_layer_size = 25 num_labels = 10 print('Loading and Visualizing Data ...\n') data = sio.loadmat('ex3data1.mat') m = data['X'].shape[0] X, y = data['X'], data['y'].flatten() # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[:100], :] display_data(sel) plt.show() input('Program paused. Press enter to continue.\n') # ================ Part 2: Loading Pameters ================ print('\nLoading Saved Neural Network Parameters ...\n') weights = sio.loadmat('ex3weights.mat') Theta1, Theta2 = weights['Theta1'], weights['Theta2'] # print(Theta1.shape) # (25,401) # print(Theta2.shape) # (10,26) # ================= Part 3: Implement Predict ================= pred = predict(Theta1, Theta2, X) print('\nTraining Set Accuracy: {:.1f}%\n'.format(np.mean(pred == y) * 100))