Theano学习笔记(二)——逻辑回归函数解析
2015-12-02 22:56
169 查看
有了前面的准备,可以用Theano实现一个逻辑回归程序,逻辑回归是典型的有监督学习。
为了形象,这里我们假设分类任务是区分人与狗的照片。
首先是生成随机数对象
[python] view plaincopy
importnumpy
importtheano
importtheano.tensor as T
rng= numpy.random
数据初始化
有400张照片,这些照片不是人的就是狗的。
每张照片是28*28=784的维度。
D[0]是训练集,是个400*784的矩阵,每一行都是一张照片。
D[1]是每张照片对应的标签,用来记录这张照片是人还是狗。
training_steps是迭代上限。
[python] view plaincopy
N= 400
feats= 784
D= (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps= 10000
[python] view plaincopy
#Declare Theano symbolic variables
x= T.matrix("x")
y= T.vector("y")
w= theano.shared(rng.randn(feats), name="w")
b= theano.shared(0., name="b")
print"Initial model:"
printw.get_value(), b.get_value()
x是输入的训练集,是个矩阵,把D[0]赋值给它。
y是标签,是个列向量,400个样本所以有400维。把D[1]赋给它。
w是权重列向量,维数为图像的尺寸784维。
b是偏倚项向量,初始值都是0,这里没写成向量是因为之后要广播形式。
[python] view plaincopy
#Construct Theano expression graph
p_1= 1 / (1 + T.exp(-T.dot(x, w) - b)) #Probability that target = 1
prediction= p_1 > 0.5 # Theprediction thresholded
xent= -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost= xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw,gb = T.grad(cost, [w, b]) #Compute the gradient of the cost
# (we shall return to this in a
#following section of this tutorial)
这里是函数的主干部分,涉及到3个公式
1.判定函数
![](http://latex.codecogs.com/gif.latex?{h_\theta%20}\left(%20x%20\right)%20=%20\frac{1}{{I%20+%20{e^{%20-%20{\theta%20^T}X}}}})
2.代价函数
![](http://latex.codecogs.com/gif.latex?Cost\left(%20{{h_\theta%20}\left(%20x%20\right),y}%20\right)%20=%20\left\{%20\begin{array}{r}\begin{array}{*{20}{c}}{%20-%20\log%20\left(%20{{h_\theta%20}\left(%20x%20\right)}%20\right)}&{\begin{array}{*{20}{c}}{if}&{y%20=%201}\end{array}}\end{array}\\\begin{array}{*{20}{c}}{%20-%20\log%20\left(%20{1%20-%20{h_\theta%20}\left(%20x%20\right)}%20\right)}&{\begin{array}{*{20}{c}}{if}&{y%20=%200}\end{array}}\end{array}\end{array}%20\right.)
3.总目标函数
![](http://latex.codecogs.com/gif.latex?J\left(%20\theta%20%20\right)%20=%20%20-%20\frac{1}{m}\left[%20{\sum\limits_{i%20=%201}^m%20{\left(%20{{y^{\left(%20i%20\right)}}\log%20{h_\theta%20}\left(%20{{x^{\left(%20i%20\right)}}}%20\right)%20+%20\left(%20{1%20-%20{y^{\left(%20i%20\right)}}}%20\right)\log%20\left(%20{1%20-%20{h_\theta%20}\left(%20{{x^{\left(%20i%20\right)}}}%20\right)}%20\right)}%20\right)}%20}%20\right]%20+%20\frac{\lambda%20}{2}\sum\limits_{i%20=%201}^m%20{W_i^2})
第二项是权重衰减项,减小权重的幅度,用来防止过拟合的。
[python] view plaincopy
#Compile
train= theano.function(
inputs=[x,y],
outputs=[prediction, xent],
updates=((w, w - 0.1 * gw), (b, b -0.1 * gb)))
predict= theano.function(inputs=[x], outputs=prediction)
构造预测和训练函数。
[python] view plaincopy
#Train
fori in range(training_steps):
pred,err = train(D[0], D[1])
print"Final model:"
printw.get_value(), b.get_value()
print"target values for D:", D[1]
print"prediction on D:", predict(D[0])
这里算过之后发现,经过10000次训练,预测结果与标签已经完全相同了。
为了形象,这里我们假设分类任务是区分人与狗的照片。
首先是生成随机数对象
[python] view plaincopy
importnumpy
importtheano
importtheano.tensor as T
rng= numpy.random
数据初始化
有400张照片,这些照片不是人的就是狗的。
每张照片是28*28=784的维度。
D[0]是训练集,是个400*784的矩阵,每一行都是一张照片。
D[1]是每张照片对应的标签,用来记录这张照片是人还是狗。
training_steps是迭代上限。
[python] view plaincopy
N= 400
feats= 784
D= (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps= 10000
[python] view plaincopy
#Declare Theano symbolic variables
x= T.matrix("x")
y= T.vector("y")
w= theano.shared(rng.randn(feats), name="w")
b= theano.shared(0., name="b")
print"Initial model:"
printw.get_value(), b.get_value()
x是输入的训练集,是个矩阵,把D[0]赋值给它。
y是标签,是个列向量,400个样本所以有400维。把D[1]赋给它。
w是权重列向量,维数为图像的尺寸784维。
b是偏倚项向量,初始值都是0,这里没写成向量是因为之后要广播形式。
[python] view plaincopy
#Construct Theano expression graph
p_1= 1 / (1 + T.exp(-T.dot(x, w) - b)) #Probability that target = 1
prediction= p_1 > 0.5 # Theprediction thresholded
xent= -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost= xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw,gb = T.grad(cost, [w, b]) #Compute the gradient of the cost
# (we shall return to this in a
#following section of this tutorial)
这里是函数的主干部分,涉及到3个公式
1.判定函数
2.代价函数
3.总目标函数
第二项是权重衰减项,减小权重的幅度,用来防止过拟合的。
[python] view plaincopy
#Compile
train= theano.function(
inputs=[x,y],
outputs=[prediction, xent],
updates=((w, w - 0.1 * gw), (b, b -0.1 * gb)))
predict= theano.function(inputs=[x], outputs=prediction)
构造预测和训练函数。
[python] view plaincopy
#Train
fori in range(training_steps):
pred,err = train(D[0], D[1])
print"Final model:"
printw.get_value(), b.get_value()
print"target values for D:", D[1]
print"prediction on D:", predict(D[0])
这里算过之后发现,经过10000次训练,预测结果与标签已经完全相同了。
Theano学习笔记(二)——逻辑回归函数解析
相关文章推荐
- SpringMVC文件上传、拦截器、与Spring的整合以及与Struts2的比较
- 【c++】用c++实现Date类
- 实验三 进程调度模拟程序
- ssh 免密码登录
- CSS 尺寸单位
- C++学习网站
- 手册下载
- theano 模块 MLP示例
- 运算符重载
- 关于Handler 使用This Handler class should be static or leaks might occur 警告处理
- 华为的模式
- UI基础-XML、JSON 数据结构解析
- 什么是性能测试?
- Oracle ROWID详解
- MFC CDocument子类中的Serialize(CArchive& ar)方法,在打开文档时调用一次之后,再点击“打开”菜不被调用,解决。
- thrift中的超时(timeout)坑
- 一步一步教你Socket编程
- 多线程编程-线程的创建和终止
- 初识RMAN2:12C中数据文件和归档日志的保留策略
- LeetCode 38 : Count and Say (Java)