Theano是一个Python库,专门用于定义、优化、求值数学表达式,效率高,适用于多维数组。特别适合做机器学习。一般来说,使用时需要安装python和numpy.

首先回顾一下机器学习的东西,定义一个模型(函数)f(x;w) x为输入,w为模型参数,然后定义一个损失函数c(f),通过数据驱动在一堆模型函数中选择最优的函数就是训练training的过程,在机器学习中训练一般采用梯度下降法gradient descent.

使用theano来搭建机器学习(深度学习)框架,有以下优点:

1、 theano能够自动计算梯度

2、只需要两步骤就能搭建框架,定义函数和计算梯度。

一、 定义函数

步骤 0    宣告使用theano   import theano
步骤 1 定义输入 x=theano.tensor.scalar()
步骤 2 定义输出 y=2*x
步骤3 定义fuction f = theano.function([x],y)
步骤 4 调用函数 print f(-2)

步骤1 定义输入变量

a = theano.tensor.scalar()

b =theano.tensor.matrix()

简化  import  theano.tensor as T

步骤2 定义输出变量 需要和输入变量的关系

x1=T.matrix()

x2=T.matrix()

y1=x1*x2

y2=T.dot(x1,x2) #矩阵乘法

步骤3 申明函数

f= theano.function([x],y)

函数输入必须是list 带[]

example:

 import theano
import theano.tensor as T a= T.matrix()
b= T.matrix()
c = a*b
d = T.dot(a,b)
F1= theano.function([a,b],c)
F2= theano.function([a,b],d)
A=[[1,2],[3,4]]
B=[[2,4],[6,8]] #2*2矩阵
C=[[1,2],[3,4],[5,6]] #3*2矩阵
print F1(A,B)
print F2(C,B)

二、计算梯度

计算 dy/dx ,直接调用g=T.grad(y,x) y必须是一个标量 scalar

和梯度有关的三个例子:

example1 :标量对标量的导数

 x= T.scalar('x')
y = x**2
g = T.grad(y,x)
f= theano.function([x],y)
f_prime=theano.function([x],g)
print f(-2)
print f_prime(-2)

example2 : 标量对向量的导数

x1= T.scalar()
x2= T.scalar()
y = x1*x2
g = T.grad(y,[x1,x2])
f= theano.function([x1,x2],y)
f_prime=theano.function([x1,x2],g)
print f(2,4)
print f_prime(2,4)

example3 : 标量对矩阵的导数

A= T.matrix()
B= T.matrix()
C=A*B #不是矩阵乘法,是对于位置相乘
D=T.sum(C)
g=T.grad(D,A) #注意D是求和 所以肯定是一个标量 但g是一个矩阵
y_prime=theano.function([A,B],g)
A=[[1,2],[3,4]]
B=[[2,4],[6,8]]
print y_prime(A,B)

搭建神经网络

1 单个神经元

假设w b 已知。y=neuron(x;w,b)

 import theano
import theano.tensor as T
import random
import numpy as np x = T.vector()
w = T.vector()
b = T.scalar() z= T.dot(w,x)+b
y= 1/(1+T.exp(-z)) neuron =theano.function(
inputs=[x,w,b],
outputs=[y]
) w = [-1,1]
b=0
for i in range(100):
x = [random.random(),random.random()]
print x
print neuron(x,w,b)

但是运行出现错误 'TensorType(float32, vector) cannot store accurately value [0.4079584242156499, 0.7781482896772725], it would be represented as [ 0.40795842  0.77814829]. If you do not mind this precision loss, you can: 1) explicitly convert your data to a numpy array of dtype float32, or 2) set "allow_input_downcast=True" when calling "function".',

因此我们按照第一种方法,转换成a numpy array of dtype float32,将上述21行代码替换如下:

x=np.asarray([random.random(),random.random()], dtype = np.float32)

运行结果如下

[array(0.3996952772140503, dtype=float32)]
[ 0.12659253 0.45289889]
[array(0.5808603763580322, dtype=float32)]
[ 0.96148008 0.70698273]
[array(0.43671688437461853, dtype=float32)]

w,b应该也是参数 ,上述函数改为neuron(x),model 参数 wb 应该用shared variables,改进的代码
 import theano
import theano.tensor as T
import random
import numpy as np x = T.vector()
# share variables 参数!有值
w = theano.shared(np.array([1.,1.]))
b = theano.shared(0.) z= T.dot(w,x)+b
y= 1/(1+T.exp(-z)) neuron =theano.function(
inputs=[x], # x 作为输入
outputs=y
) w.set_value([0.1, 0.1]) #修改值
for i in range(100):
#x = [random.random(),random.random()]
x=np.asarray([random.random(),random.random()], dtype = np.float32)
print x
print w.get_value() #得到值
print neuron(x)

2  训练 training

定义一个损失函数C  计算C对每一个wi的偏导数 和b的偏导数

梯度下降 w1 = w1 -n*dc/dw1 

常规:
dw, db =gradient(x,y_hat)
w.set_value(w.get_value()-0.1*dw)
b.set_value(b.get_value()-0.1*db)
改进:
gradient = theano.function(
inputs=[x,y_hat],
updates=[(w,w-0.1*dw),(b,b-0.1*db)]

Theano入门神经网络(一)的更多相关文章

  1. Theano入门神经网络(四)

    这一部分主要介绍用Theano 实现softmax函数. 在多分类任务中经常用到softmax函数,首先上几个投影片解释一下 假设目标输出是y_hat ,激活函数后的Relu y 一个是1.2 一个是 ...

  2. Theano入门神经网络(三)

    附录一个:Keras学习随笔 http://blog.csdn.net/niuwei22007/article/details/49045909 参考 <Python Machine Learn ...

  3. Theano入门神经网络(二) 实现一个XOR门

    与非门的图片如下 示意图 详细解释: 1 定义变量的代码,包括了输入.权值.输出等.其中激活函数采用的是sigmod函数 # -*- coding: utf-8 -*- __author__ = 'A ...

  4. Theano入门——CIFAR-10和CIFAR-100数据集

    Theano入门——CIFAR-10和CIFAR-100数据集 1.CIFAR-10数据集介绍 CIFAR-10数据集包含60000个32*32的彩色图像,共有10类.有50000个训练图像和1000 ...

  5. Theano入门笔记2:scan函数等

    1.Theano中的scan函数 目前先弱弱的认为:相当于symbolic的for循环吧,或者说计算图上的for循环,也可以用来替代repeat-until. 与scan相比,scan_checkpo ...

  6. Theano入门

    由于自己的一个小项目需要Theano部分的开源代码,所以学习一下并记录入门的经典网站. 入门中文博客:https://blog.csdn.net/hjimce/article/details/4680 ...

  7. Theano入门笔记1:Theano中的Graph Structure

    译自:http://deeplearning.net/software/theano/extending/graphstructures.html#graphstructures 理解Theano计算 ...

  8. Theano深度学习结构分析

    Reference:Theano入门三部曲 http://deeplearning.net/tutorial/logreg.html  (Softmax回归) http://deeplearning. ...

  9. 关于深度学习框架 TensorFlow、Theano 和 Keras

    [TensorFlow] ——( https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/) 1.TensorFlow是啥 ...

随机推荐

  1. C# Retry重试操作解决方案(附源码)

    一.前言 (1)对于Thread的Abort方法,如果线程当前正在执行的是一段非托管代码,那么CLR就不会抛出ThreadAbortException,只有当代码继续回到CLR中时,才会引发Threa ...

  2. anyncTask的3个参数

    AnyncTask异步处理数据并将数据应用到视图的操作场合 一  其中包含这几个方法 1 onPreExcute() 初始化控件,例如进度条2 doInBackground() 具体的执行动作请求数据 ...

  3. 使用WPF动态生成Code 39条形码

    最近在看些条形码方面相关的资料,而如果只是看的话,效果似乎并不怎么好,所以决定动手做点Demo,以增强对相关知识的记忆. 这里是一个我编写的使用WPF生成Code 39的例子,Code 39的编码很简 ...

  4. 深入理解CSS弹性盒模型flex

    × 目录 [1]版本更迭 [2]display [3]基本概念[4]伸缩容器[5]伸缩项目 前面的话 CSS3引入了一种新的布局模型——flex布局.flex是flexible box的缩写,一般称之 ...

  5. Win10 UWP开发中的重复性静态UI绘制小技巧 1

    介绍 在Windows 10 UWP界面实现的过程中,有时会遇到一些重复性的.静态的界面设计.比如:画许多等距的线条,画一圈时钟型的刻度线,同特别的策略排布元素,等等. 读者可能觉得这些需求十分简单, ...

  6. 今天心情好,一起探讨下《送给大家的200兆SVN代码服务器》怎么管理我们的VS代码?

    前几天给大家免费送了个200兆SVN代码服务器(今天心情好,给各位免费呈上200兆SVN代码服务器一枚,不谢!),还木有领取的速度戳链接哦! 好几位园友拿到SVN服务器都对其赞不绝口,我也用这个服务器 ...

  7. drag & resize元素的jQuery实现

    有时项目中会遇到需要拖动元素.拖拽调整元素大小的需求.大部分时候都不想自己写一遍,因为已经有很多现成的例子了.例如jqueryui提供的drag和resize.但是仅仅是为了这么小一个功能就引入一个库 ...

  8. [ZigBee] 11、ZigBee之睡眠定时器二

    1.前言 上一节讲了Zigbee的睡眠定时器利用外部按键使系统从休眠态唤醒到工作态,其核心在于: 61 void SysPowerMode(uchar mode) 62 { 63 if(mode &g ...

  9. Spring-Context之九:在bean定义中使用继承

    定义bean时有个abstract属性,可以设置为true或false,默认为false. 1 2 3 4 <bean id="animal" class="Ani ...

  10. ASP.NET将原始图片按照指定尺寸等比例缩放显示图片

    网站上可能会有很多图片,比如产品图片等,而且他们可能大小不一,宽度和高度也不一定一样,有的很大有的很小.如果放在一张网页上,可能会破坏版面,但是如果强制让他们按照指定的宽度和高度显示,因为比例不同还会 ...