Theano Multi Layer Perceptron 多层感知机
理论
机器学习技法:https://www.coursera.org/course/ntumltwo
假设上述网址不可用的话,自行度娘找别人做好的种子。或者看这篇讲义也能够:http://www.cnblogs.com/xbf9xbf/p/4712785.html
Theano代码
须要使用我上一篇博客关于逻辑回归的代码:http://blog.csdn.net/yangnanhai93/article/details/50410026
保存成ls_sgd.py 文件,置于同一个文件夹下就可以。
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
'''
This is done by Vincent.Y
mainly modified from deep learning tutorial
'''
import os
import sys
import timeit
import numpy as np
import theano
import theano.tensor as T
from theano import function
from lr_sgd import LogisticRegression ,load_data,plot_decision_boundary
import matplotlib.pyplot as plt
class HiddenLayer():
def __init__(self,rng,X,n_in,n_out,W=None,b=None,activation=T.tanh):
self.X=X
if W is None:
W_value=np.asarray(
rng.uniform(
low=-np.sqrt(6.0/(n_in+n_out)),
high=np.sqrt(6.0/(n_in+n_out)),
size=(n_in,n_out)
),
dtype=theano.config.floatX
)
if activation== theano.tensor.nnet.sigmoid:
W_value*=4
W=theano.shared(value=W_value,name='W',borrow=True)
if b is None:
b_value=np.zeros((n_out,),dtype=theano.config.floatX)
b=theano.shared(value=b_value,name='b',borrow=True)
self.W=W
self.b=b
lin_output=T.dot(X,self.W)+self.b
self.output=(lin_output if activation is None else activation(lin_output))
self.params=[self.W,self.b]
class MLP():
def __init__(self,rng,X,n_in,n_hidden,n_out):
self.hiddenLayer=HiddenLayer(
rng=rng,
X=X,
n_in=n_in,
n_out=n_hidden,
activation=T.tanh
)
self.logisticRegressionLayer=LogisticRegression(
X=self.hiddenLayer.output,
n_in=n_hidden,
n_out=n_out
)
self.L1=(abs(self.hiddenLayer.W).sum()+abs(self.logisticRegressionLayer.W).sum())
self.L2=((self.hiddenLayer.W**2).sum()+(self.logisticRegressionLayer.W**2).sum())
self.negative_log_likelihood=self.logisticRegressionLayer.negative_log_likelihood
self.errors=self.logisticRegressionLayer.errors #this is a function
self.params=self.logisticRegressionLayer.params+self.hiddenLayer.params
self.X=X
self.y_pred=self.logisticRegressionLayer.y_pred
def test_mlp(learning_rate=0.11,L1_reg=0.00,L2_reg=0.0001,n_epochs=6000,n_hidden=10):
datasets=load_data()
train_set_x,train_set_y=datasets[0]
test_set_x,test_set_y=datasets[1]
x=T.matrix('x')
y=T.lvector('y')
rng=np.random.RandomState(218)
classifier=MLP(
rng=rng,
X=x,
n_in=2,
n_out=2,
n_hidden=n_hidden
)
cost=(classifier.negative_log_likelihood(y)+L1_reg*classifier.L1+L2_reg*classifier.L2)
test_model=function(
inputs=[x,y],
outputs=classifier.errors(y)
)
gparams=[T.grad(cost,param) for param in classifier.params]
updates=[
(param,param-learning_rate*gparam)
for param,gparam in zip(classifier.params,gparams)
]
train_model=function(
inputs=[x,y],
outputs=cost,
updates=updates
)
epoch=0
while epoch < n_epochs:
epoch=epoch+1
avg_cost=train_model(train_set_x,train_set_y)
test_cost=test_model(test_set_x,test_set_y)
print "epoch is %d,train error %f, test error %f"%(epoch,avg_cost,test_cost)
predict_model=function(
inputs=[x],
outputs=classifier.logisticRegressionLayer.y_pred
)
plot_decision_boundary(lambda x:predict_model(x),train_set_x,train_set_y)
if __name__=="__main__":
test_mlp()
效果
迭代600次,隐层数量为2
迭代6000次。隐层数量为20
当隐层数量非常少。如2或者1的时候。添加迭代次数,分类超平面依然是一条直线;当隐层数量多,迭代次数过少的时候分类超平面也是一条直线。所以在训练的过程中。总是要依据训练的结果来调整隐层节点的数量以及迭代次数来获取最好的效果,当中迭代次数可用early stopping来控制。
Theano Multi Layer Perceptron 多层感知机的更多相关文章
- DeepLearning学习(1)--多层感知机
想直接学习卷积神经网络,结果发现因为神经网络的基础较弱,学习起来比较困难,所以准备一步步学.并记录下来,其中会有很多摘抄. (一)什么是多层感知器和反向传播 1,单个神经元 神经网络的基本单元就是神经 ...
- 基于theano的多层感知机的实现
1.引言 一个多层感知机(Multi-Layer Perceptron,MLP)可以看做是,在逻辑回归分类器的中间加了非线性转换的隐层,这种转换把数据映射到一个线性可分的空间.一个单隐层的MLP就可以 ...
- Theano3.4-练习之多层感知机
来自http://deeplearning.net/tutorial/mlp.html#mlp Multilayer Perceptron note:这部分假设读者已经通读之前的一个练习 Classi ...
- (数据科学学习手札34)多层感知机原理详解&Python与R实现
一.简介 机器学习分为很多个领域,其中的连接主义指的就是以神经元(neuron)为基本结构的各式各样的神经网络,规范的定义是:由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系 ...
- DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解
本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参 ...
- MLP多层感知机
@author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43221829 转载:http://blog.csdn.net ...
- 动手学习pytorch——(3)多层感知机
多层感知机(multi perceptron,MLP).对于普通的含隐藏层的感知机,由于其全连接层只是对数据做了仿射变换,而多个仿射变换的叠加仍然是一个仿射变换,即使添加更多的隐藏层,这种设计也只能与 ...
- Tensorflow 2.0 深度学习实战 —— 详细介绍损失函数、优化器、激活函数、多层感知机的实现原理
前言 AI 人工智能包含了机器学习与深度学习,在前几篇文章曾经介绍过机器学习的基础知识,包括了监督学习和无监督学习,有兴趣的朋友可以阅读< Python 机器学习实战 >.而深度学习开始只 ...
- (数据科学学习手札44)在Keras中训练多层感知机
一.简介 Keras是有着自主的一套前端控制语法,后端基于tensorflow和theano的深度学习框架,因为其搭建神经网络简单快捷明了的语法风格,可以帮助使用者更快捷的搭建自己的神经网络,堪称深度 ...
随机推荐
- 7.Linux 输入子系统分析
为什么要引入输入子系统? 在前面我们写了一些简单的字符设备的驱动程序,我们是怎么样打开一个设备并操作的呢? 一般都是在执行应用程序时,open一个特定的设备文件,如:/dev/buttons .... ...
- Django快速搭建博客
准备工作: 1.Python 2.Django 3.Git 安装Python: 官网下载 安装Django: #安装最新版本的Django $ pip install django #或者指定安装版本 ...
- 2.1 使用eclipse4.4 搭建 maven简单结构项目。
1.前言 1.本博客面向0基础开发人员. 2.本博客为系列博客.<1.X>系列为服务器数据库相关技术,前几章为简单搭建linux+tomcat+mysql+nginx+redis.< ...
- 2. Dubbo和Zookeeper的关系
转自:https://www.cnblogs.com/hirampeng/p/9540243.html Dubbo建议使用Zookeeper作为服务的注册中心. 1. Zookeeper的作用: ...
- 1.1 Introduction中 Putting the Pieces Together官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Putting the Pieces Together 拼在一起 This comb ...
- IDEA配置svn地址方法及出现的问题的解决办法
1.在IDEA中点击File-Settings里面,如图所示,选择你本地装的svn的exe路径: 2.在如图所示菜单中配置svn地址: 问题1:如果svn路径下没有exe文件,则是装svn的时候没有安 ...
- JAVE 视音频转码
http://blog.csdn.net/qllinhongyu/article/details/29817297
- OC内存管理总结,清晰明了!
<span style="font-size:18px;">OC内存管理 一.基本原理 (一)为什么要进行内存管理. 由于移动设备的内存极其有限.所以每一个APP所占的 ...
- 自己写的Android图表库XCL-Charts一些旧的样例
话说有了灵感就要抓住,来了兴趣就要去研究它. 所以尽管近期非常忙.但我还是没有丢下Android图表实现的研究.最终如今我的图表库基类 基本上已经有点模样了.不在是小打小闹,而是能依传入參数非常灵活的 ...
- Windows 64位下 python3.4.3 安装numpy scipy
Numpy: 1.在开始菜单搜索cmd打开 终端 2.在终端输入python -m pip install -U pip 3.到http://www.lfd.uci.edu/~gohlke/pytho ...