理论

机器学习技法: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 多层感知机的更多相关文章

  1. DeepLearning学习(1)--多层感知机

    想直接学习卷积神经网络,结果发现因为神经网络的基础较弱,学习起来比较困难,所以准备一步步学.并记录下来,其中会有很多摘抄. (一)什么是多层感知器和反向传播 1,单个神经元 神经网络的基本单元就是神经 ...

  2. 基于theano的多层感知机的实现

    1.引言 一个多层感知机(Multi-Layer Perceptron,MLP)可以看做是,在逻辑回归分类器的中间加了非线性转换的隐层,这种转换把数据映射到一个线性可分的空间.一个单隐层的MLP就可以 ...

  3. Theano3.4-练习之多层感知机

    来自http://deeplearning.net/tutorial/mlp.html#mlp Multilayer Perceptron note:这部分假设读者已经通读之前的一个练习 Classi ...

  4. (数据科学学习手札34)多层感知机原理详解&Python与R实现

    一.简介 机器学习分为很多个领域,其中的连接主义指的就是以神经元(neuron)为基本结构的各式各样的神经网络,规范的定义是:由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系 ...

  5. DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解

    本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参 ...

  6. MLP多层感知机

    @author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43221829 转载:http://blog.csdn.net ...

  7. 动手学习pytorch——(3)多层感知机

    多层感知机(multi perceptron,MLP).对于普通的含隐藏层的感知机,由于其全连接层只是对数据做了仿射变换,而多个仿射变换的叠加仍然是一个仿射变换,即使添加更多的隐藏层,这种设计也只能与 ...

  8. Tensorflow 2.0 深度学习实战 —— 详细介绍损失函数、优化器、激活函数、多层感知机的实现原理

    前言 AI 人工智能包含了机器学习与深度学习,在前几篇文章曾经介绍过机器学习的基础知识,包括了监督学习和无监督学习,有兴趣的朋友可以阅读< Python 机器学习实战 >.而深度学习开始只 ...

  9. (数据科学学习手札44)在Keras中训练多层感知机

    一.简介 Keras是有着自主的一套前端控制语法,后端基于tensorflow和theano的深度学习框架,因为其搭建神经网络简单快捷明了的语法风格,可以帮助使用者更快捷的搭建自己的神经网络,堪称深度 ...

随机推荐

  1. JS怎么判断数组类型?

    1.判断对象的constructor是否指向Array,接着判断特殊的属性length,splice等.[应用的是constructor的定义:返回对象所对应的构造函数.] eg: [].constr ...

  2. amazeui学习笔记一(开始使用1)--主干

    amazeui学习笔记一(开始使用1)--主干 一.总结 1.英语:学好英语,编程轻松很多 2. layouts compatibility change log web app collection ...

  3. Java exception handling best practices--转载

    原文地址:http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/ This post is anothe ...

  4. HttpWatch--time chart分析

    这是一个IE的插件,下载可以点这里.下载后解压如下图所示,一共有4个文件.HttpWatch Professional是单独软件,可以单独使用. 解压后有四个文件 插件安装时,只需运行httpwatc ...

  5. 【习题 3-12 UVA - 11809】Floating-Point Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] \(A*10^B = temp[M]*2^{2^E-1}\) 两边取一下对数 得到 \(lg_A+B = lg_{temp[M]} ...

  6. 【Codeforces Round #301 (Div. 2) A】 Combination Lock

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟水题 [代码] #include <bits/stdc++.h> using namespace std; cons ...

  7. 仿oschina 主界面的实现(二) -------Toolbar + DrawerLayout

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  8. git 配置用户名email地址,设置密码

  9. C#DateTime与Unix时间戳的转换

    /// <summary> /// Unix时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp" ...

  10. vue 用 :style动态修改带中划线的样式属性

    今天在项目中遇到要用:style动态设置margin-top值,直接写发现报错.后来改成驼峰就成功了,记录一下 错误示范: <div class="testLeft ulData&qu ...