基于python中theano库的线性回归


Posted in Python onAugust 31, 2018

theano库是做deep learning重要的一部分,其最吸引人的地方之一是你给出符号化的公式之后,能自动生成导数。本文使用梯度下降的方法,进行数据拟合,现在把代码贴在下方

代码块

import numpy as np 
import theano.tensor as T 
import theano 
import time 

class Linear_Reg(object): 
  def __init__(self,x): 
    self.a = theano.shared(value = np.zeros((1,), dtype=theano.config.floatX),name = 'a') 
    self.b = theano.shared(value = np.zeros((1,), 
dtype=theano.config.floatX),name = 'b') 
    self.result = self.a * x + self.b 
    self.params = [self.a,self.b] 
  def msl(self,y): 
    return T.mean((y - self.result)**2) 

def regrun(rate,data,labels): 

  X = theano.shared(np.asarray(data, 
                 dtype=theano.config.floatX),borrow = True) 
  Y = theano.shared(np.asarray(labels, 
                 dtype=theano.config.floatX),borrow = True) 

  index = T.lscalar() #定义符号化的公式
  x = T.dscalar('x')  #定义符号化的公式
  y = T.dscalar('y')  #定义符号化的公式

  reg = Linear_Reg(x = x) 
  cost = reg.msl(y) 


  a_g = T.grad(cost = cost,wrt = reg.a) #计算梯度 
  b_g = T.grad(cost = cost, wrt = reg.b) #计算梯度

  updates=[(reg.a,reg.a - rate * a_g),(reg.b,reg.b - rate * b_g)] #更新参数
  train_model = theano.function(inputs=[index], outputs = reg.msl(y),updates = updates,givens = {x:X[index], y:Y[index]}) 

  done = True 
  err = 0.0 
  count = 0 
  last = 0.0 
  start_time = time.clock() 
  while done: 
    #err_s = [train_model(i) for i in xrange(data.shape[0])] 
    for i in xxx:
      err_s = [train_model(i) ]
      err = np.mean(err_s)  

    #print err 
    count = count + 1 
    if count > 10000 or err <0.1: 
      done = False 
    last = err 
  end_time = time.clock() 
  print 'Total time is :',end_time -start_time,' s' # 5.12s 
  print 'last error :',err 
  print 'a value : ',reg.a.get_value() # [ 2.92394467]  
  print 'b value : ',reg.b.get_value() # [ 1.81334458] 

if __name__ == '__main__':  
  rate = 0.01 
  data = np.linspace(1,10,10) 
  labels = data * 3 + np.ones(data.shape[0],dtype=np.float64) +np.random.rand(data.shape[0])
  regrun(rate,data,labels)

其基本思想是随机梯度下降。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
分析在Python中何种情况下需要使用断言
Apr 01 Python
python字符串的常用操作方法小结
May 21 Python
python中requests爬去网页内容出现乱码问题解决方法介绍
Oct 25 Python
python定时利用QQ邮件发送天气预报的实例
Nov 17 Python
python登录并爬取淘宝信息代码示例
Dec 09 Python
Tesserocr库的正确安装方式
Oct 19 Python
使用PIL(Python-Imaging)反转图像的颜色方法
Jan 24 Python
详解Python用三种方式统计词频的方法
Jul 29 Python
python中的逆序遍历实例
Dec 25 Python
Django admin 实现search_fields精确查询实例
Mar 30 Python
Python greenlet和gevent使用代码示例解析
Apr 01 Python
sklearn和keras的数据切分与交叉验证的实例详解
Jun 19 Python
基于随机梯度下降的矩阵分解推荐算法(python)
Aug 31 #Python
python实现梯度下降算法
Mar 24 #Python
wtfPython—Python中一组有趣微妙的代码【收藏】
Aug 31 #Python
opencv python 图像去噪的实现方法
Aug 31 #Python
python+numpy+matplotalib实现梯度下降法
Aug 31 #Python
python实现随机梯度下降法
Mar 24 #Python
python实现决策树分类(2)
Aug 30 #Python
You might like
php设计模式 Factory(工厂模式)
2011/06/26 PHP
有道搜索和IP138的IP的API接口(PHP应用)
2012/11/29 PHP
使用php检测用户当前使用的浏览器是否为IE浏览器
2013/12/03 PHP
php利用cookies实现购物车的方法
2014/12/10 PHP
php读取本地json文件的实例
2018/03/07 PHP
PHP基于面向对象封装的分页类示例
2019/03/15 PHP
滚动图片效果 jquery实现回旋滚动效果
2013/01/08 Javascript
js中点击空白区域时文本框与隐藏层的显示与影藏问题
2013/08/26 Javascript
jquery实现左右无缝轮播图
2020/07/31 Javascript
解读Bootstrap v4 sass设计
2016/05/29 Javascript
AngularJS通过$http和服务器通信详解
2016/09/21 Javascript
seajs学习之模块的依赖加载及模块API的导出
2016/10/20 Javascript
详解node HTTP请求客户端 - Request
2017/05/05 Javascript
React Native验证码倒计时工具类分享
2017/10/24 Javascript
关于HTTP传输中gzip压缩的秘密探索分析
2018/01/12 Javascript
jQuery实现碰到边缘反弹的动画效果
2018/02/24 jQuery
JS canvas绘制五子棋的棋盘
2020/05/28 Javascript
[38:51]2014 DOTA2国际邀请赛中国区预选赛 Orenda VS LGD-CDEC
2014/05/22 DOTA
Python的Django中将文件上传至七牛云存储的代码分享
2016/06/03 Python
python 实现数组list 添加、修改、删除的方法
2018/04/04 Python
TensorFlow 合并/连接数组的方法
2018/07/27 Python
Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例
2018/07/27 Python
python3爬虫获取html内容及各属性值的方法
2018/12/17 Python
Djang的model创建的字段和参数详解
2019/07/27 Python
基于python实现matlab filter函数过程详解
2020/06/08 Python
PyTorch如何搭建一个简单的网络
2020/08/24 Python
CSS3制作气泡对话框的实例教程
2016/05/10 HTML / CSS
酒吧员工的岗位职责
2013/11/26 职场文书
园林毕业生自我鉴定范文
2013/12/29 职场文书
遗体告别仪式答谢词
2014/01/23 职场文书
网络编辑岗位职责范本
2014/02/10 职场文书
公司总经理岗位职责
2014/03/15 职场文书
员工手册编写范本
2015/05/14 职场文书
家长会开场白和结束语
2015/05/29 职场文书
时尚女魔头观后感
2015/06/04 职场文书
2019请假条的基本格式及范文!
2019/07/05 职场文书