吴恩达机器学习练习3——多元分类与神经网络
神经网络——手写数字识别
网络模型
输入层:400个神经元;隐藏层:26个神经元;输出层:10个神经元
ex3_nn.m
%% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks % Instructions % ------------ % % This file contains code that helps you get started on the % linear exercise. You will need to complete the following functions % in this exericse: % % lrCostFunction.m (logistic regression cost function) % oneVsAll.m % predictOneVsAll.m % predict.m % % For this exercise, you will not need to change any code in this file, % or any other files other than those mentioned above. % %% Initialization clear ; close all; clc %% Setup the parameters you will use for this exercise input_layer_size = 400; % 20x20 Input Images of Digits hidden_layer_size = 25; % 25 hidden units num_labels = 10; % 10 labels, from 1 to 10 % (note that we have mapped "0" to label 10) %% =========== Part 1: Loading and Visualizing Data ============= % We start the exercise by first loading and visualizing the dataset. % You will be working with a dataset that contains handwritten digits. % % Load Training Data fprintf(Loading and Visualizing Data ... ) load(ex3data1.mat); m = size(X, 1);%5000 % Randomly select 100 data points to display sel = randperm(size(X, 1)); sel = sel(1:100); displayData(X(sel, :)); fprintf(Program paused. Press enter to continue. ); pause; %% ================ Part 2: Loading Pameters ================ % In this part of the exercise, we load some pre-initialized % neural network parameters. fprintf( Loading Saved Neural Network Parameters ... ) % Load the weights into variables Theta1 and Theta2 load(ex3weights.mat); %% ================= 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 % you compute the training set accuracy. pred = predict(Theta1, Theta2, X); fprintf( Training Set Accuracy: %f , mean(double(pred == y)) * 100); fprintf(Program paused. Press enter to continue. ); pause; % To give you an idea of the networks output, you can also run % through the examples one at the a time to see what it is predicting. % Randomly permute examples rp = randperm(m); for i = 1:m % Display fprintf( Displaying Example Image ); displayData(X(rp(i), :)); pred = predict(Theta1, Theta2, X(rp(i),:)); fprintf( Neural Network Prediction: %d (digit %d) , pred, mod(pred, 10)); % Pause with quit option s = input(Paused - press enter to continue, q to exit:,s); if s == q break end end
预测
function p = predict(Theta1, Theta2, X) m = size(X, 1); num_labels = size(Theta2, 1); p = zeros(size(X, 1), 1); X = [ones(size(X,1),1),X];%5000*201 a2 = sigmoid(X*Theta1); a2 = [ones(size(a2,1),1),a2]; a3 = sigmoid(a2*Theta2); [r,p] = max(a3,[],2); end
上一篇:
通过多线程提高代码的执行效率例子
下一篇:
签名密钥和加密密钥区别