UFLDL 教程学习笔记(一)

本文提供了一段使用Matlab实现线性回归的教程,从基础概念开始,逐步指导读者如何计算目标函数和梯度,并通过实例演示了如何避免常见错误。教程包括两种实现方式:一种使用向量简化代码,另一种使用循环增强理解。同时,介绍了如何进行逻辑回归和向量化操作。对于不熟悉Matlab的读者,文章提供了详细的注释帮助理解。

     ufdl的新教程,从基础学起。第一节讲的是线性回归。主要目的是熟悉目标函数,计算梯度和优化。

     按着教程写完代码后,总是编译出错,一查是mex的原因,实在不想整了。

     这位博主用的是向量,比较简洁:http://blog.youkuaiyun.com/lingerlanlan/article/details/38377023

      这位博主用的是循环,更好理解:http://www.cnblogs.com/william7neral/p/4448566.html

      接下来是logistic regression和向量化,没什么好说的:http://blog.youkuaiyun.com/lingerlanlan/article/details/38390085

      

      这节主要是完成liner_regression.m,包括计算目标函数f和梯度g,需要注意的是要分清变量是列向量还是行向量或是矩阵。

       对matlab不太熟,所以我稍微注释了下

      

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.行向量
  %
  %size(a,1)求矩阵的行数 size(a,2)求矩阵的列数,相当于length(a)
  %size(a)同时求矩阵的行和列数
  % m=size(X,2);
  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 %%%
h = theta' * X
f = (1/2)* (h-y)' * (h-y)
g = X * (h - y)'
View Code

 

      

 

      

转载于:https://www.cnblogs.com/573177885qq/p/4783465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值