[机器学习]week4编程作业:Multi-class Classification and Neural Networks
来源:互联网 发布:js点击缓慢移动 编辑:程序博客网 时间:2024/06/05 05:37
lrCostFunction.m
function [J, grad] = lrCostFunction(theta, X, y, lambda)%LRCOSTFUNCTION Compute cost and gradient for logistic regression with %regularization% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using% theta as the parameter for regularized logistic regression and the% gradient of the cost w.r.t. to the parameters. % Initialize some useful valuesm = length(y); % number of training examples% You need to return the following variables correctly J = 0;grad = zeros(size(theta));% ====================== YOUR CODE HERE ======================% Instructions: Compute the cost of a particular choice of theta.% You should set J to the cost.% Compute the partial derivatives and set grad to the partial% derivatives of the cost w.r.t. each parameter in theta%% Hint: The computation of the cost function and gradients can be% efficiently vectorized. For example, consider the computation%% sigmoid(X * theta)%% Each row of the resulting matrix will contain the value of the% prediction for that example. You can make use of this to vectorize% the cost function and gradient computations. %% Hint: When computing the gradient of the regularized cost function, % there're many possible vectorized solutions, but one solution% looks like:% grad = (unregularized gradient for logistic regression)% temp = theta; % temp(1) = 0; % because we don't add anything for j = 0 % grad = grad + YOUR_CODE_HERE (using the temp variable)%h = 1./(1+e.^(-X*theta));J = -(1/m) * (sum( y.*log(h) +(1-y).*log(1-h)));J = J + lambda/ (2*m) *(sum (theta.^2) -theta(1).^2);grad = 1/m * X' * (h - y);grad = grad + lambda / m *theta;grad(1) = grad(1) - lambda / m *theta(1);% =============================================================grad = grad(:);end
oneVsAll.m
function [all_theta] = oneVsAll(X, y, num_labels, lambda)%ONEVSALL trains multiple logistic regression classifiers and returns all%the classifiers in a matrix all_theta, where the i-th row of all_theta %corresponds to the classifier for label i% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels% logistic regression classifiers and returns each of these classifiers% in a matrix all_theta, where the i-th row of all_theta corresponds % to the classifier for label i% Some useful variablesm = size(X, 1);n = size(X, 2);% You need to return the following variables correctly all_theta = zeros(num_labels, n + 1);% Add ones to the X data matrixX = [ones(m, 1) X];% ====================== YOUR CODE HERE ======================% Instructions: You should complete the following code to train num_labels% logistic regression classifiers with regularization% parameter lambda. %% Hint: theta(:) will return a column vector.%% Hint: You can use y == c to obtain a vector of 1's and 0's that tell you% whether the ground truth is true/false for this class.%% Note: For this assignment, we recommend using fmincg to optimize the cost% function. It is okay to use a for-loop (for c = 1:num_labels) to% loop over the different classes.%% fmincg works similarly to fminunc, but is more efficient when we% are dealing with large number of parameters.%% Example Code for fmincg:%% % Set Initial theta% initial_theta = zeros(n + 1, 1);% % % Set options for fminunc% options = optimset('GradObj', 'on', 'MaxIter', 50);% % % Run fmincg to obtain the optimal theta% % This function will return theta and the cost % [theta] = ...% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...% initial_theta, options);% initial_theta = zeros(n+1,1); options = optimset('GradObj','on','MaxIter',50);for c = 1:num_labels [theta] = fmincg(@(t)(lrCostFunction(t,X, (y==c),lambda)),... initial_theta, options); all_theta(c,:) = theta';end% =========================================================================end
predictOneVsAll.m
function p = predictOneVsAll(all_theta, X)%PREDICT Predict the label for a trained one-vs-all classifier. The labels %are in the range 1..K, where K = size(all_theta, 1). % p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions% for each example in the matrix X. Note that X contains the examples in% rows. all_theta is a matrix where the i-th row is a trained logistic% regression theta vector for the i-th class. You should set p to a vector% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2% for 4 examples) m = size(X, 1);num_labels = size(all_theta, 1);% You need to return the following variables correctly p = zeros(size(X, 1), 1);% Add ones to the X data matrixX = [ones(m, 1) X];% ====================== YOUR CODE HERE ======================% Instructions: Complete the following code to make predictions using% your learned logistic regression parameters (one-vs-all).% You should set p to a vector of predictions (from 1 to% num_labels).%% Hint: This code can be done all vectorized using the max function.% In particular, the max function can also return the index of the % max element, for more information see 'help max'. If your examples % are in rows, then, you can use max(A, [], 2) to obtain the max % for each row.% [a,b] = max(sigmoid(X * all_theta'),[],2);p = b;% =========================================================================end
predict.m
function p = predict(Theta1, Theta2, X)%PREDICT Predict the label of an input given a trained neural network% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the% trained weights of a neural network (Theta1, Theta2)% Useful valuesm = size(X, 1);num_labels = size(Theta2, 1);% You need to return the following variables correctly p = zeros(size(X, 1), 1);% ====================== YOUR CODE HERE ======================% Instructions: Complete the following code to make predictions using% your learned neural network. You should set p to a % vector containing labels between 1 to num_labels.%% Hint: The max function might come in useful. In particular, the max% function can also return the index of the max element, for more% information see 'help max'. If your examples are in rows, then, you% can use max(A, [], 2) to obtain the max for each row.%X= [ ones(m,1) X];a = sigmoid (Theta1*X');a = a';a = [ ones(size(a,1),1) a];b = sigmoid(Theta2*a');[c p] = max(b',[],2);% =========================================================================end
阅读全文
0 0
- [机器学习]week4编程作业:Multi-class Classification and Neural Networks
- 机器学习作业之 Multi-class Classification and Neural Networks(week4)
- Stanford 机器学习 Week4 作业 Multi-class Classification and Neural Networks
- [Coursera机器学习]Multi-class Classication and Neural Networks WEEK4编程作业
- 斯坦福机器学习实验之3-多分类和神经网络(Multi-class Classification and Neural Networks)
- Stanford ex3: Multi-class Classification and Neural Networks
- Andrew Ng机器学习week4(Neural Networks: Representation)编程习题
- (三)Multi-class Classification and Neural Networks[多分类问题和神经网络]
- Machine Learning week 4 quiz: programming assignment-Multi-class Classification and Neural Networks
- Programming Exercise 3: Multi-class Classification and Neural Networks Machine Learning
- Machine Learning - Neural Networks for Multi-class Classification
- Stanford 机器学习笔记 Week4 Neural Networks: Representation
- 斯坦福机器学习视频笔记 Week4&Week5 神经网络 Neural Networks
- [Coursera机器学习]Neural Networks Learning WEEK5编程作业
- Coursera Machine Learning 第四周week4编程 ex3Multi-class Classication and Neural Networks编程全套满分题目+注释
- Andrew NG 机器学习 练习3-Multiclass Classification and Neural Networks
- 机器学习 笔记(二)Multi-class classification
- Coursera机器学习 week4 神经网络的表示 编程作业代码
- hibernate懒加载导致多表联合查询失败
- 读取文件,判断是否含有固定字符串
- ConcurrentHashMap
- Android 判断网络状态
- 一维的Haar小波变换
- [机器学习]week4编程作业:Multi-class Classification and Neural Networks
- 蛇形填数
- oracle杀回话不释放的问题
- 计算机网络原理知识点(第四章:网络层)
- 太复杂了么,秒懂jdbc的用法
- jsp文件中的注释
- 如何做到优化引擎搜索SEO之第八篇:懒加载图片(调整翻译)
- 修改z-stack节点深度和数量
- 3.1 决策树的基本思想