深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
1 Vectorization 简述
2 Linear Regression的Vectorization
function [f,g] = linear_regression(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The target value for each example. y(j) is the target for example j.
% m=size(X,2);
n=size(X,1); f=0;
g=zeros(size(theta)); %
% TODO: Compute the linear regression objective by looping over the examples in X.
% Store the objective function value in 'f'.
%
% TODO: Compute the gradient of the objective with respect to theta by looping over
% the examples in X and adding up the gradient for each example. Store the
% computed gradient in 'g'. %%% YOUR CODE HERE %%% % Step 1 : Compute f cost function
for i = 1:m
f = f + (theta' * X(:,i) - y(i))^2;
end f = 1/2*f; % Step 2: Compute gradient for j = 1:n
for i = 1:m
g(j) = g(j) + X(j,i)*(theta' * X(:,i) - y(i));
end end
再来看Vectorization的方法:
function [f,g] = linear_regression_vec(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The target value for each example. y(j) is the target for example j.
%
m=size(X,2); % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); %
% TODO: Compute the linear regression objective function and gradient
% using vectorized code. (It will be just a few lines of code!)
% Store the objective function value in 'f', and the gradient in 'g'.
%
%%% YOUR CODE HERE %%%
f = 1/2*sum((theta'*X - y).^2); g = X*(theta'*X - y)';
能够看到。这里仅仅须要一条语句就搞定了。
g为nx1,而theta’xX-y为1xm,为了和X相乘。必须转置为mx1,从而nxmxmx1 = nx1.
function [f,g] = logistic_regression_vec(theta, X,y)
%
% Arguments:
% theta - A column vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The label for each example. y(j) is the j'th example's label.
%
m=size(X,2); % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); %
% TODO: Compute the logistic regression objective function and gradient
% using vectorized code. (It will be just a few lines of code!)
% Store the objective function value in 'f', and the gradient in 'g'.
%
%%% YOUR CODE HERE %%%
f = -sum(y.*log(sigmoid(theta'*X)) + (1-y).*log(1 - sigmoid(theta'*X)));
g = X*(sigmoid(theta'*X) - y)';
得到的结果一样,但速度变快非常多
Training accuracy: 100.0%
Test accuracy: 100.0%
深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization的更多相关文章
- 深度学习 Deep Learning UFLDL 最新 Tutorial 学习笔记 1:Linear Regression
1 前言 Andrew Ng的UFLDL在2014年9月底更新了. 对于開始研究Deep Learning的童鞋们来说这真的是极大的好消息! 新的Tutorial相比旧的Tutorial添加了Conv ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression
Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
1 Gradient Checking 说明 前面我们已经实现了Linear Regression和Logistic Regression.关键在于代价函数Cost Function和其梯度Gradi ...
- 【深度学习Deep Learning】资料大全
最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books by Yoshua Bengio, Ian Goodfellow and Aaron C ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...
- 机器学习——深度学习(Deep Learning)
Deep Learning是机器学习中一个非常接近AI的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,近期研究了机器学习中一些深度学习的相关知识,本文给出一些非常实用的资料和心得. Key W ...
- (转)深度学习(Deep Learning, DL)的相关资料总结
from:http://blog.sciencenet.cn/blog-830496-679604.html 深度学习(Deep Learning,DL)的相关资料总结 有人认为DL是人工智能的一场革 ...
随机推荐
- 1.Apache Axis配置文件WSDD详解
转自:https://jyao.iteye.com/blog/1285516 1. Aapche Axis的Web Service Deployment Descriptor(WSDD)文件参考文档. ...
- Python(一) 基本类型
前言: 什么是代码? 代码是现实世界事物在计算机世界中的映射. 什么事写代码? 写代码是将现实世界中的事物用计算机语言来描述. 一.数字:整形与浮点型 整型:int 浮点型:float (没有单精度和 ...
- win7防火墙里开启端口的图文教程
转载于:http://www.cnblogs.com/vipsoft/archive/2012/05/02/2478847.html 开启端口:打开“控制面板”中的“Windows防火墙”,点击左侧的 ...
- canvas中遇到的理解问题
1.lineDashOffset ctx.lineDashOffset = number 描述: setLineDash 用于设置开始绘制虚线的偏移量. 数字的正负表示左右偏移. 2.createLi ...
- Debian9 ifconfig命令找不到解决办法
Debian9 ifconfig命令找不到解决办法 ifconfig.route.arp和netstat等命令行工具(它们统称为net-tools),管理和排查各种网络配置.这类工具原先起源于BSD ...
- Unity容器实现自动注册
如何创建Unity容器? 首先NuGet搜索Unity, 该示例中使用的版本为4.0.1 新建控制台程序 示例中使用常规操作, 创建一个IPay接口, 分别有两个实现类: ApplePay.Huawe ...
- 使用IntelliJ IDEA开发前的基本设置,有助于提高开发效率
2.界面字体大小设置 File菜单->Settings->Appearance->Override default fonts by(not recommended): Name:宋 ...
- js 图片轮转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- BZOJ 1571 DP
思路: 预处理出在能力值为i的时候 滑雪一次的最小时间 f[i][j]表示i时间 j的能力值 最多的滑雪次数 我先用vector 把课程按起点push进去 1. for(int k=0;k<ve ...
- GridView1.DataKeys[e.RowIndex].Value 是什么含义?
https://zhidao.baidu.com/question/88518619.html举个例子来说吧 你将一个student表绑定到grid上 这个表里有一些字段 包括id 姓名 学号 等等等 ...