UFLDL 教程学习笔记(一)

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

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

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

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

      接下来是logistic regression和向量化,没什么好说的:http://blog.csdn.net/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