zoukankan      html  css  js  c++  java
  • 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

          

          

  • 相关阅读:
    Linux常用命令学习
    LA 6437 Power Plant (prim最小生成树)
    Atitit.提升稳定性-----分析内存泄漏PermGen OOM跟解决之道...java
    hdu 1248 寒冰王座
    数据结构——算法之(031)(将字符串中全部小写字母排在大写字母的前面)
    X-射线:探索原子世界的利器
    关于sql中的with(nolock)
    Netty In Action中文版
    抓包报文分析
    POJ 1562 Oil Deposits
  • 原文地址:https://www.cnblogs.com/573177885qq/p/4783465.html
Copyright © 2011-2022 走看看