吴恩达深度学习公开课笔记(2)

使用矩阵来简化算法

使用矩阵乘法来减少代码

训练集

size( f e e t 2 feet^2 feet2)Number of badroomsNumber of floorsAge of home(years)Price($1000)
20145145460
14163240232
15343230315
8522136178

预测函数
h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 2 + θ 3 x 3 + θ 4 x 4 h\theta(x)=\theta_{0}+\theta_{1}x_{1}+\theta_{2}x_{2}+\theta_{3}x_{3}+\theta_{4}x_{4} hθ(x)=θ0+θ1x1+θ2x2+θ3x3+θ4x4

使用矩阵

当特征数量为n时
h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 2 + θ 3 x 3 + θ 4 x 4 + . . . . . . + θ n x n h\theta(x)=\theta_{0}+\theta_{1}x_{1}+\theta_{2}x_{2}+\theta_{3}x_{3}+\theta_{4}x_{4}+......+\theta_{n}x_{n} hθ(x)=θ0+θ1x1+θ2x2+θ3x3+θ4x4+......+θnxn

假设一个 x 0 = 1 x_{0}=1 x0=1$

x = [ x 0 x 1 x 2 x 3 x 4 . . . x n ] x=\begin{gathered}\begin{bmatrix}x_{0}\\x_{1}\\x_{2}\\x_{3}\\x_{4}\\...\\x_{n}\end{bmatrix}\quad\end{gathered} x=x0x1x2x3x4...xn θ = [ θ 0 θ 1 θ 2 θ 3 θ 4 . . . θ n ] \theta=\begin{gathered}\begin{bmatrix}\theta_{0}\\\theta_{1}\\\theta_{2}\\\theta_{3}\\\theta_{4}\\...\\\theta_{n}\end{bmatrix}\quad\end{gathered} θ=θ0θ1θ2θ3θ4...θn

预测函数为
h θ ( x ) = θ ⊤ x h\theta(x)=\theta^\top x hθ(x)=θx

各主流语言都有相关的库来优化矩阵计算

梯度下降算法为
θ j = θ j − a 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) \theta_{j}=\theta_{j}-a\frac{1}{m}\sum_{i=1}^{m}(h\theta(x^{(i)})-y^{(i)})x_{j}^{(i)} θj=θjam1i=1m(hθ(x(i))y(i))xj(i)

其中 x j ( i ) x_{j}^{(i)} xj(i)表示第 i i i 组特征的第 j j j 个特征

特征缩放

特征缩放的目的是使特征保持在相似的取值范围,这样梯度下降算法能更快的收敛

x 1 = s i z e ( 0 − 2000   f e e t 2 ) x_{1}=size(0-2000\space feet^2) x1=size(02000 feet2)
x 2 = n u m b e r   o f   b a d r o o m s ( 1 − 5 ) x_{2}=number\space of\space badrooms(1-5) x2=number of badrooms(15)

特征范围差距特别大时,轮廓图将呈现椭圆形

均值归一化

x j = x j − 标 准 差 m a x x_{j}=\frac{x_{j}-标准差}{max} xj=maxxj
列如 x 1 = s i z e − 1000 2000 x_{1}=\frac{size-1000}{2000} x1=2000size1000 x 2 = b a d r o o m s − 2 5 x_{2}=\frac{badrooms-2}{5} x2=5badrooms2
− 0.5 ≤ x 1 ≤ 0.5 -0.5\le x_{1}\le 0.5 0.5x10.5, − 0.5 ≤ x 2 ≤ 0.5 -0.5\le x_{2}\le 0.5 0.5x20.5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值