zoukankan      html  css  js  c++  java
  • 【Course】Machine learning:Week 2-Lecture1/2/3/4/5

    Machine learning - Week 2

    这是机器学习第二周的课程笔记。

    一、课程中编程环境配置

    二、Multivariate linear regression

    1、Week2的梯度下降问题由单一变量转变成了多变量:

    图片名称
    相应的公式如下:
    图片名称
    $$ egin{array}{l}{ ext { repeat until convergence: }{} \ { heta_{j}:= heta_{j}-alpha frac{1}{m} sum_{i=1}^{m}left(h hetaleft(x^{(i)} ight)-y^{(i)} ight) cdot x_{j}^{(i)} quad ext { for } j:=0 ldots n} \ {}}end{array} $$ 也就是:

    [egin{array}{l}{ ext { repeat until convergence: }{} \ { heta_{0}:= heta_{0}-alpha frac{1}{m} sum_{i=1}^{m}left(h_{ heta}left(x^{(i)} ight)-y^{(i)} ight) cdot x_{0}^{(i)}} \ { heta_{1}:= heta_{1}-alpha frac{1}{m} sum_{i=1}^{m}left(h_{ heta}left(x^{(i)} ight)-y^{(i)} ight) cdot x_{1}^{(i)}} \ { heta_{2}:= heta_{2}-alpha frac{1}{m} sum_{i=1}^{m}left(h_{ heta}left(x^{(i)} ight)-y^{(i)} ight) cdot x_{2}^{(i)}} \ {cdots} \ {}^{cdots}}end{array} ]

    ( heta_{0})( heta_{1})( heta_{2})...这些参数要同时更新


    2、Feature Scaling

    在本例中,(x_{1})(x_{2})分别代表房屋总面积和房间数,面积是几千平米,房间数是几个,数量级差别太大。如果特征的数量级差别太大,会导致在收敛时的收敛效率特别差。因此需要对数据进行处理:

    • Feature Scaling

    特征缩放是将输入值除以输入变量的范围(即最大值减去最小值)

    • mean normalization

    均值标准化是从输入变量的值中减去输入变量的平均值,再除以输入变量的范围。

    [egin{array}{l}{x_{i}:=frac{x_{i}-mu_{i}}{s_{i}}} \ { ext { Where } mu_{i} ext { is the average of all the values for feature (i) and } s_{i} ext { is }} \ { ext { the range of values }(max -min ) ext { , or } s_{i} ext { is the standard deviation. }}end{array} ]

    3、Learning Rate

    如何确认梯度下降的计算是正确的

    • Debugging gradient descent:画损失函数--训练次数的二维图

    • Automatic convergence test. 如果在一个迭代中,J(θ)减少小于E,那就说明梯度下降没有正常工作。E是较小值,例如0.001。但是在实际应用中,很难选择这个阈值。

    此外,(alpha)的选择也有技巧:一般可以这样选择:0.001 0.003 0.01 0.03 0.1 0.3 1

    图片名称

    4、Features and Polynomial Regression

    我们可以通过几种不同的方式来改进我们的特征和假设函数的形式。

    下面这个房价示例中,问题原来用到两个特征,房子的长和宽,但是可以将这两个特征转化为一个,即面积=长X宽。

    图片名称
    还可以通过将假设函数变成二次函数、三次函数或平方根函数(或任何其他形式)来改变其行为或曲线。(quadratic, cubic or square root)
    图片名称

    注意:要调节size、size^2等特征的范围!

    三、Computing Parameters Analytically(计量参数分析)

    1、Normal Equation

    梯度下降法给出了一种最小化损失函数J的方法,让我们来讨论第二种方法,这一次显式地执行最小化,而不用求助于迭代算法。"Normal Equation" 方法:

    [ heta=left(X^{T} X ight)^{-1} X^{T} y ]

    示例如下:

    图片名称

    Normal Equation和梯度下降法的对比如下:

    图片名称

    2、Normal Equation Non-invertibility

    在octave中我们使用pinv函数,而不是inv函数,因为pinv函数可以在(X^{T}X)不可逆时也可以计算。

    • (X^{T}X)不可逆的原因:
    图片名称

    四、Submitting Programming Assignments

    这部分列举了许多提交作业时应该注意的和出现的错误:Programming tips from Mentors

    1、提交作业时的注意点

    1. 提交作业时只需在 Octave/Matlab command line中键入:submit,而不是submit()

    2. 评分时会用不同的数据测试作业是否正确,数据的尺寸会变化,提交的代码应该适应各种尺寸,做到以下两点:

    • 不使用固定的数组下标
    • 不使用固定长度的数组和矩阵
    1. 评分者不希望出现额外的输出,所以代码的每一行末尾要加上分号。

    2. 每个编程作业在论坛中都有一个“讨论区”。在这一节中,您经常可以找到“单元测试”。这些是附加的测试用例,它给您一个命令来输入,并提供预期的结果。在提交给评分员之前,最好使用单元测试来测试您的功能。

    2、提交作业时的错误

    • This section will need to be supplemented with info appropriate to the new submission system. If you run the submit script and get a message that your identity can't be verified, be sure that you have logged-in using your Coursera account email and your Programming Assignment submission password.

    • If you get the message "submit undefined", first check that you are in the working directory where you extracted the files from the ZIP archive. Use "cd" to get there if necessary.

    • If the "submit undefined" error persists, or any other "function undefined" messages appear, try using the "addpath(pwd)" command to add your present working directory (pwd) to the Octave execution path.

    • If the submit script crashes with an error message, please see the thread "Mentor tips for submitting your work" under "All Course Discussions".

    • The submit script does not ask for what part of the exercise you want to submit. It automatically grades any function you have modified.

    2、提交作业示例:

    图片名称
    图片名称
    作者:张清博

    -------------------------------------------

    个性签名:半途而废

    本文如有帮助,记得在右下角点个“推荐”哦,在此感谢!

  • 相关阅读:
    Oracle 创建dblink
    好的博客
    Java项目导出war包 security alert:integrity check error”
    tomcat7.0 处理问题
    项目支持Servlet3.0的新特性
    oracle replace函数
    JavaWeb项目连接Oracle数据库
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
  • 原文地址:https://www.cnblogs.com/Ireland/p/12337580.html
Copyright © 2011-2022 走看看