用 theano 求解 Logistic Regression (SGD 优化算法)
1. model
这里待求解的是一个 binary logistic regression,它是一个分类模型,参数是权值矩阵 W 和偏置向量 b。该模型所要估计的是概率 P(Y=1|x),简记为 p,表示样本 x 属于类别 y=1 的概率:
当然最终的目标是求解在整个样本集 D={(x(i),y(i)),0<i≤N} 的对数概率(关于 W和 b):
- 这里的取均值是为了解耦后续的正则化系数,以及 SGD 时的步长的选择;
当然也可对 W 进行二范数约束(F范数约束,全部项的平方和):
2. theano 的使用
实现 theano 下的最小化问题的求解,涉及如下的四个流程:
(1)声明符号变量;
import numpy
import theano.tensor as T
from theano import shared, function x = T.matrix()
y = T.lvector()
w = shared(numpy.random.randn(100))
b = shared(numpy.zeros(()))
print 'step 1, initial mode: '
print w.get_value(), b.get_value()(2)使用这些变量构建符号表达式图(symbolic expression graph)
# hypothesis p_1 = 1/(1+T.exp(-T.dot(x, w)-b))
xent = -y*T.log(p_1)-(1-y)*T.log(1-p_1)
cost = xent.mean() + 0.01*(w**2).sum()
gw, gb = T.grad(cost, [w, b]);
prediction = p_1 > .5(3)编译 Theano functions;
train = function(inputs=[x, y], outputs=[predication, xent], updates={w:w-0.1*gw, b:b-0.1*gb}) predict = function(inputs=[x], outputs=predication)(4)调用编译好的函数来执行数值计算;
N = 4
feats = 100
D = (numpy.random.randn(N, feats), numpy.random.randi(low=0, high=2, size=(N,)))
training_epochs = 10
for _ in range(training_epochs):
pred, err = train(D[0], D[1])
print 'final model: '
print 'target values for D', D[1]
print 'predication on D', predict(D[0])
用 theano 求解 Logistic Regression (SGD 优化算法)的更多相关文章
- paper 8:支持向量机系列五:Numerical Optimization —— 简要介绍求解求解 SVM 的数值优化算法。
作为支持向量机系列的基本篇的最后一篇文章,我在这里打算简单地介绍一下用于优化 dual 问题的 Sequential Minimal Optimization (SMO) 方法.确确实实只是简单介绍一 ...
- logistic regression二分类算法推导
- 原创:logistic regression实战(一):SGD Without lasso
logistic regression是分类算法中非常重要的算法,也是非常基础的算法.logistic regression从整体上考虑样本预测的精度,用判别学习模型的条件似然进行参数估计,假设样本遵 ...
- 线性模型(3):Logistic Regression
此笔记源于台湾大学林轩田老师<机器学习基石><机器学习技法> (一)Logistic Regression 原理 对于分类问题,假设我们想得到的结果不是(x属于某一类)这种形式 ...
- 【 Logistic Regression 】林轩田机器学习基石
这里提出Logistic Regression的角度是Soft Binary Classification.输出限定在0~1之间,用于表示可能发生positive的概率. 具体的做法是在Linear ...
- [OpenCV] Samples 06: [ML] logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- [OpenCV] Samples 06: logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
http://blog.csdn.net/zouxy09/article/details/20319673 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression) z ...
- 逻辑回归(Logistic Regression)算法小结
一.逻辑回归简述: 回顾线性回归算法,对于给定的一些n维特征(x1,x2,x3,......xn),我们想通过对这些特征进行加权求和汇总的方法来描绘出事物的最终运算结果.从而衍生出我们线性回归的计算公 ...
随机推荐
- Mac OS X Kernel Basic User Credentials
User Credentials In order to understand security in OS X, it is important to understand that there a ...
- Android Studio2.0 Beta 2版本号更新说明及注意事项
我们刚刚向canary channel推送了Android Studio2.0 Beta 2版本号 老毕译注: ---------- canary channel: 金丝雀版本号,平均1到2周就会更新 ...
- angular模块详解
原文: https://www.jianshu.com/p/819421ff955a 大纲 1.angular应用是模块化的 2.对模块(Module)的认识 3.模块的分类:根模块和特性模块 4.N ...
- 使用ToolRunner运行Hadoop程序基本原理分析 分类: A1_HADOOP 2014-08-22 11:03 3462人阅读 评论(1) 收藏
为了简化命令行方式运行作业,Hadoop自带了一些辅助类.GenericOptionsParser是一个类,用来解释常用的Hadoop命令行选项,并根据需要,为Configuration对象设置相应的 ...
- 【Nutch2.2.1基础教程之1】nutch相关异常 分类: H3_NUTCH 2014-08-08 21:46 1549人阅读 评论(2) 收藏
1.在任务一开始运行,注入Url时即出现以下错误. InjectorJob: Injecting urlDir: urls InjectorJob: Using class org.apache.go ...
- php实现 统计输入中各种字符的个数
php实现 统计输入中各种字符的个数 一.总结 一句话总结:谋而后动,想清楚,会非常节约编写代码的时间. 1.对结果可能是0的变量,记得初始化? 4 $len=0; 5 $len=strlen($st ...
- javascript合并数组并且删除第二项
var m1 = [5, 6, 2]; var m2 = [4, 2, 6]; var m3 = new Array(); m1 = m1.concat(m2); for ( ...
- [React Unit Testing] React unit testing demo
import React from 'react' const Release = React.createClass({ render() { const { title, artist, outO ...
- [CSS] Change the auto-placement behaviour of grid items with grid-auto-flow
We can change the automatic behaviour of what order our grid items appear. We can even re-order the ...
- source insight 添加自定义macro
打开C:\Documents and Settings\xxxx\My Documents\Source Insight\Projects\Base文件夹下的em文件,可以看到都是由macro定义的一 ...