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

          

          

  • 相关阅读:
    我真的没读野鸡大学!是他们不好好起名字!
    Request.Cookies和Response.Cookies
    深受理科生喜欢的10大专业
    如何玩转“互联网+教育”?
    js调试工具Console命令详解
    XSS获取cookie并利用
    257. Binary Tree Paths
    EXEC sp_executesql with multiple parameters
    235. Lowest Common Ancestor of a Binary Search Tree
    226. Invert Binary Tree
  • 原文地址:https://www.cnblogs.com/573177885qq/p/4783465.html
Copyright © 2011-2022 走看看