研究生 HW1

2019-09-30  本文已影响0人  不存在的里皮

HW1.1

题目

参见 textbook p4-12。完成以下任务:
(1) 生成正弦序列 s(n);
(2) 使用噪声函数对正弦序列加噪 x(n)=s(n)+w(n);
(3) 使用多项式回归模型对 x(n)进行拟合,并分析过拟合和欠拟合情况

问题分析

效果

当M=3时:



当M=10时:



当M=20时:



效果分析

HW1.2

1.Generate n = 2,000 points uniformly at random in the two-dimensional unit square. Which point do you expect the centroid to be?
(0.5, 0.5)
2.What objective does the centroid of the points optimize?
应当让中心点与所有点的欧式距离最小
\min{cost(x,y)}=\sum_{i}{\sqrt{(x-x_i)^2+(y-y_i)^2}}
化为代码即为

def cost(c, all_points):
    return sum(sum((c - all_points) ** 2, axis=1) ** 0.5)

3.Apply gradient descent (GD) to find the centroid.
先根据损失函数, 写出求导公式
\frac{\partial cost(x,y)}{\partial x}=\sum_{i}{\frac{x-x_i}{\sqrt{(x-x_i)^2+(y-y_i)^2}}}
\frac{\partial cost(x,y)}{\partial y}=\sum_{i}{\frac{y-y_i}{\sqrt{(x-x_i)^2+(y-y_i)^2}}}
该公式会基于全部点的误差进行迭代。
每次迭代都计算全局误差和梯度,将梯度置于中心点,更新中心点。
重复迭代,直到更新步长小于某个定值为止。
具体见代码。

4.Apply stochastic gradient descent (SGD) to find the centroid. Can you say in simple words, what the algorithm is doing?
需要改变损失函数和迭代逻辑。



可见,使用SGD后,点的更新变得十分曲折。

上一篇 下一篇

猜你喜欢

热点阅读