本博客记录吴恩达的《深度学习专项系列课程(Deep Learning Specialization)

编程作业:Multi-class Classi cation and Neural Networks

1 lrCostFunction函数

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 values
m = 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)
%value = X*theta;
J = (-(y' * log(sigmoid(value)) + (1 - y)' * log(1 - sigmoid(value)))+ ((lambda/2) * (theta'*theta - theta(1)^2)))/m;grad = (X'*(sigmoid(value) - y) + lambda*theta)/m;
grad(1) = ((sigmoid(value) - y)' * X(:,1))/m;% =============================================================grad = grad(:);end

2 oneVsAll函数

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 variables
m = 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 matrix
X = [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);
%for c = 1:num_labels% Set Initial thetainitial_theta = zeros(n + 1, 1);%  Set options for fminuncoptions = optimset('GradObj', 'on', 'MaxIter', 50);% Run fmincg to obtain the optimal theta% This function will return theta and the cost  [initial_theta] = fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);all_theta(c,:) = initial_theta';
endfor% =========================================================================end

3 predictOneVsAll函数

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 matrix
X = [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.
%       [p, p] = max(X * all_theta', [], 2);% =========================================================================end

4 predict函数

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 values
m = 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];
X = X * Theta1';
X = [ones(m, 1) sigmoid(X)];
X = X * Theta2';
[p, p] = max(X, [], 2);% =========================================================================end

Machine-learning-ex3相关推荐

  1. 吴恩达ex3_吴恩达Machine Learning Ex3 python实现

    1.Multi-class classification 使用Logistic regression和neural networks来识别手写数字识别(从0到9).在第一部分练习中使用Logistic ...

  2. 吴恩达ex3_[Coursera] Machine Learning ex3 多元分类和神经网络 步骤分析

    第四周的主要内容是神经网络,个人觉得讲得比较跳,所以补充几篇文章加深一下理解: But what *is* a Neural Network? 先提一下,本人设计背景,没学过微积分,这篇只当是笔记,有 ...

  3. machine learning ex3

    本周作业:Multi-class Classification and Neural Networks(多层次分类和神经网络) 对手写数字进行辨别.分类 [*] lrCostFunction.m - ...

  4. Machine Learning week 4 quiz: programming assignment-Multi-class Classification and Neural Networks

    一.ex3.m %% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all% Instructions % --------- ...

  5. bff v2ex_语音备忘录的BFF-如何通过Machine Learning简化Speech2Text

    bff v2ex by Rafael Belchior 通过拉斐尔·贝尔基奥尔(Rafael Belchior) 语音备忘录的BFF-如何通过Machine Learning简化Speech2Text ...

  6. 吴恩达新书《Machine Learning Yearning》完整中文版 PDF 下载!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale资源 推荐人:GithubDaily,Datawhale伙伴 <Mach ...

  7. 吴恩达《Machine Learning》Jupyter Notebook 版笔记发布!图解、公式、习题都有了

    在我很早之前写过的文章<机器学习如何入门>中,就首推过吴恩达在 Coursera 上开设的<Machine Learning>课程.这门课最大的特点就是基本没有复杂的数学理论和 ...

  8. 吴恩达新书《Machine Learning Yearning》完整中文版开源!

    选自Github 来源:机器学习算法与自然语言处理 吴恩达新书<Machine Learning Yearning>完整中文版开源,整理给大家. <Machine Learning ...

  9. Auto Machine Learning 自动化机器学习笔记

    ⭐适读人群:有机器学习算法基础 1. auto-sklearn 能 auto 到什么地步? 在机器学习中的分类模型中: 常规 ML framework 如下图灰色部分:导入数据-数据清洗-特征工程-分 ...

  10. 机器学习与优化基础(Machine Learning and Optimization)

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自|新机器视觉 引用大佬Pedro Domingos的说法: ...

最新文章

  1. 【面向对象编程】(3) 类之间的交互,依赖关系,关联关系
  2. Tomcat 启动错误 org/eclipse/jdt/debug/core/JDIDebug...
  3. 硬核!尽量避免 BUG 手法
  4. 项目范围管理:WBS
  5. PAT甲级 -- 1009 Product of Polynomials (25 分)
  6. linux vim基本操作,vim基本操作笔记
  7. 初一模拟赛总结(6.6 my brother高考前一天,加油!(。・`ω´・。))
  8. java屏蔽编译告警_java-禁止JAXB生成的类上的编译器警告
  9. 201521123062《Java程序设计》第10周学习总结
  10. Workstation-CentOS-XShell-YUM源 JAVA大数据Week5-DAY1-linux
  11. php sqlite存入文件夹,PHP_小文件php+SQLite存储方案,我们草根站长购买的虚拟主机 - phpStudy...
  12. 机器学习算法基础8-Nagel-Schreckenberg交通流模型-公路堵车概率模型
  13. 我拿什么拯救你,混乱的思维?不如试试这3款神器
  14. zabbix内网安装部署_搭建环境tomcat+nginx+keepalived+zabbix
  15. 军用装备产品GJB150A淋雨试验检测机构
  16. linux环境使用c语言获取当前目录下有哪些文件,并打印它们的名字
  17. 实时查看MD文件效果 - 在线Markdown预览
  18. 从一道面试题掌握ES6的综合运用(有彩蛋)
  19. 中国互联网创业者的困境
  20. ffmpeg rtmp推流代码示例

热门文章

  1. 大数据生态圈概要介绍
  2. 喝红茶有什么好处和不好
  3. java企业网站源码 后台springmvc SSM 前台静态引擎 代码生成器
  4. 循环神经网络(RNN)的工作方式(一)
  5. php 浮点数之间比较
  6. mysql查询未讲课教师_SQL数据库查询语言练习
  7. android面试之郑州面试总结
  8. 电脑键盘部分按键失灵_电脑键盘失灵个别字母不灵怎么办 键盘失灵解决方法...
  9. 华为交换机实现mac地址与ip地址一一绑定
  10. 国标GB28181-2016级联测试工具