Regression 手动实现Gradient Descent
import numpy as np
import matplotlib.pyplot as plt x_data = [338.,333.,328.,207.,226.,25.,179.,60.,208.,606.]
y_data = [640.,633.,619.,393.,428.,27.,193.,66.,226.,1591.]
#y_data = w*x_data + b x = np.arange(-200,-100,1)#bias
y = np.arange(-5,5,0.1)#weight
Z = np.zeros((len(x),len(y)))
X,Y = np.meshgrid(x,y)
for i in range(len(x)):
for j in range(len(y)):
b = x[i]
w = y[j]
Z[j][i] = 0
for n in range(len(x_data)):
Z[j][i] = Z[j][i] + (y_data[n] - b - w*x_data[n])**2
Z[j][i] = Z[j][i]/len(x_data) b = -120 #初始化b
w = -4 #初始化w
lr = 0.0000001 #learning rate
iteration = 100000 #作图保留
b_history = [b]
w_history = [w] for i in range(iteration):
b_grad = 0.0
w_grad = 0.0
for n in range(len(x_data)):#求导的和
b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0
w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n] #update
b = b - lr*b_grad
w = w - lr*w_grad #store for plotting
b_history.append(b)
w_history.append(w) #plot
plt.contourf(x,y,Z,50,alpha=0.5,cmap=plt.get_cmap('jet'))
plt.plot([-188.4],[2.67],'x',ms=12,markeredgewidth=3,color='orange')
plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black')
plt.xlim(-200,-100) plt.ylim(-5,5)
plt.xlabel(r'$b$', fontsize=16)
plt.ylabel(r'$w$', fontsize=16) plt.show()

_
显然没有搞好
用adaGrad
import numpy as np
import matplotlib.pyplot as plt x_data = [338.,333.,328.,207.,226.,25.,179.,60.,208.,606.]
y_data = [640.,633.,619.,393.,428.,27.,193.,66.,226.,1591.]
#y_data = w*x_data + b
#Z是整个data的loss值
x = np.arange(-200,-100,1)#bias
y = np.arange(-5,5,0.1)#weight
Z = np.zeros((len(x),len(y)))
X,Y = np.meshgrid(x,y)
for i in range(len(x)):
for j in range(len(y)):
b = x[i]
w = y[j]
Z[j][i] = 0
for n in range(len(x_data)):
Z[j][i] = Z[j][i] + (y_data[n] - b - w*x_data[n])**2
Z[j][i] = Z[j][i]/len(x_data) b = -120 #初始化b
w = -4 #初始化w
lr = 1 #learning rate
iteration = 100000 #作图保留
b_history = [b]
w_history = [w] lr_b = 0
lr_w = 0 for i in range(iteration):
b_grad = 0.0
w_grad = 0.0
for n in range(len(x_data)):#求导的和
b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0
w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n] lr_b = lr_b + b_grad ** 2
lr_w = lr_w + w_grad ** 2 #update
b = b - lr/np.sqrt(lr_b)*b_grad
w = w - lr/np.sqrt(lr_w)*w_grad #store for plotting
b_history.append(b)
w_history.append(w) #plot
plt.contourf(x,y,Z,50,alpha=0.5,cmap=plt.get_cmap('jet'))
plt.plot([-188.4],[2.67],'x',ms=12,markeredgewidth=3,color='orange')
plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black')
plt.xlim(-200,-100) plt.ylim(-5,5)
plt.xlabel(r'$b$', fontsize=16)
plt.ylabel(r'$w$', fontsize=16) plt.show()

ok
Regression 手动实现Gradient Descent的更多相关文章
- 线性回归、梯度下降(Linear Regression、Gradient Descent)
转载请注明出自BYRans博客:http://www.cnblogs.com/BYRans/ 实例 首先举个例子,假设我们有一个二手房交易记录的数据集,已知房屋面积.卧室数量和房屋的交易价格,如下表: ...
- Logistic Regression and Gradient Descent
Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...
- 斯坦福机器学习视频笔记 Week1 Linear Regression and Gradient Descent
最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...
- Logistic Regression Using Gradient Descent -- Binary Classification 代码实现
1. 原理 Cost function Theta 2. Python # -*- coding:utf8 -*- import numpy as np import matplotlib.pyplo ...
- Linear Regression Using Gradient Descent 代码实现
参考吴恩达<机器学习>, 进行 Octave, Python(Numpy), C++(Eigen) 的原理实现, 同时用 scikit-learn, TensorFlow, dlib 进行 ...
- 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent
最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...
- flink 批量梯度下降算法线性回归参数求解(Linear Regression with BGD(batch gradient descent) )
1.线性回归 假设线性函数如下: 假设我们有10个样本x1,y1),(x2,y2).....(x10,y10),求解目标就是根据多个样本求解theta0和theta1的最优值. 什么样的θ最好的呢?最 ...
- machine learning (7)---normal equation相对于gradient descent而言求解linear regression问题的另一种方式
Normal equation: 一种用来linear regression问题的求解Θ的方法,另一种可以是gradient descent 仅适用于linear regression问题的求解,对其 ...
- machine learning(10) -- classification:logistic regression cost function 和 使用 gradient descent to minimize cost function
logistic regression cost function(single example) 图像分布 logistic regression cost function(m examples) ...
随机推荐
- python - 常用模块 os, sys
常用模块: os(处理文件和目录), sys(sys 模块包含了与 Python 解释器和它的环境有关的函数.) sys.argv 变量是一个字符串的 列表.特别地,sys.argv 包含了 命令行参 ...
- XDU 1022 (数论筛法+前缀和)
解法一:数论筛法+前缀和 //其实题目中f[n]的值可理解为存在多少个整数对使a*b<=n #include<cstdio> #define N 1007 #define maxn ...
- 服务器抓包命令:tcpdump详解
官网地址:http://www.tcpdump.org/tcpdump_man.html 简介: tcpdump,就是:dump the traffic on a network,根据使用者的定义对网 ...
- GoEasyWeb实时推送
GoEasyWeb实时推送,轻松实现实时消息推送. Web页面订阅(约5行代码),服务器端推送(2行代码)就可以轻松实现,而且在高并发时消息推送稳定. 自己完全可以只花五分钟写出属于自己的第一个实时推 ...
- JavaScript&jQuery获取url参数方法
JavaScript&jQuery获取url参数方法 function getUrlParam(name){ var reg = new RegExp("(^|&)" ...
- 20145211《网络渗透》Adobe阅读器渗透攻击
20145211<网络渗透>Adobe阅读器渗透攻击 实验准备 1.用了一个kali,一个English Winxp3,并保证能相互ping通 2.开启显示隐藏文件 实验步骤: 1.开启m ...
- cookie注入原理
cookie注入原理-->红客联盟 http://www.2cto.com/article/201202/118837.html 前言: document.cookie:表示当前浏览器中的coo ...
- Vmware 设置桥接模式
在搭建VMware虚拟机的时候要配置网络 可以看到一共主要就3种 1.桥接模式(Bridge) 虚拟系统的IP可设置成与本机系统在同一网段,虚拟系统相当于网络内的一台.独立的机器,与本机共同插在一个H ...
- JAVA基础补漏--泛型通配符
泛型通配符只能用于方法的参数 不能用对象定义 public class Test { public static void main(String[] args) { ArrayList<Str ...
- 永久更改hostname主机名
vim /etc/sysconfig/network NETWORKING=yesHOSTNAME=keepalived-nginx1GATEWAY=192.168.33.2 vim /etc/hos ...