Theano入门神经网络(三)
附录一个:Keras学习随笔 http://blog.csdn.net/niuwei22007/article/details/49045909
参考 《Python Machine Learning》这本书的第13章
Theano是Bengio大神课题组弄得。
一、Theano初步
Theano编程三个步骤: 初始化、编译和执行,就是定义符号(变量对象)、编译代码和执行代码
举个例子:计算 z=x1*w1+wo 代码如下:
# -*- coding: utf-8 -*-
__author__ = 'Administrator' import theano
import theano.tensor as T
import random
import numpy as np
from itertools import izip
import matplotlib.pyplot as plt #初始化
x1 = T.scalar()
w1 = T.scalar()
w0 = T.scalar()
z = w1*x1+w0 #编译 net_input = theano.function(
inputs=[w1, x1, w0],
outputs=z
) #执行 print 'net input %.2f'% (net_input(2.0, 1.0 ,0.5))
在写Theano代码时,要注意变量的类型 dtype ,要分清楚我们要使用 64或者32为的int or floats
二、 配置Theano
一般来说 采用CPU时,我们设置float64 ,采用GPU时,需要设置float32
print (theano.config.floatX)
print (theano.config.device)
在window中设置CPU or GPU模式,是在运行cmd后出现的路径下,新建一个.theanorc.txt的文件。文件内容如下图所示
三、使用array结构
一个简单的求和例子
#初始化
x = T.fmatrix(name ='x')
x_sum = T.sum(x, axis=1) # 0 是按列 1 是按行 #编译
calc_sum = theano.function(
inputs=[x],
outputs=x_sum
) #执行
ary = np.array([[1,2,4],[1,2,3]],dtype=theano.config.floatX)
print calc_sum(ary)
shared variable 和 权值更新
#初始化
x = T.fmatrix('x')
w = theano.shared(np.asarray([[0.0,0.0,0.0]],dtype=theano.config.floatX)) z = T.dot(x,w.T) # 这个就是两个矩阵相乘 w是1*3 x是1*3 update = [[w, w+1.0]] # 编译
net_input = theano.function(
inputs=[x],
updates=update,
outputs=z
) # 执行 data = np.array([[1,2,3]],dtype=theano.config.floatX)
for i in range(5):
print w.get_value()
print '%d %.2f' % (i,net_input(data))
可以提取指定数据,代码如下
#初始化
data = np.array([[1,2,3]],dtype=theano.config.floatX)
x = T.fmatrix('x')
w = theano.shared(np.asarray([[0.0,0.0,0.0]],dtype=theano.config.floatX)) z = T.dot(x,w.T) # 这个就是两个矩阵相乘 w是1*3 x是1*3 update = [[w, w+1.0]] # 编译
net_input = theano.function(
inputs=[],
updates=update,
givens={x : data},
outputs=z
) # 执行 for i in range(5):
print w.get_value()
print '%d %.2f' % (i,net_input())
四、 一个线性回归的例子
X_train = np.asarray(
[ [0,0] , [1, 0],
[2,0] , [3, 0],
[4,0] , [5, 0],
[6,0] , [7, 0],
[8,0] , [9, 0]],
dtype= theano.config.floatX
) y_train = np.asarray(
[ 1.0,1.3,3.1,2.0,
5.0,6.3,6.6,7.4,
8.0,9.0],
dtype= theano.config.floatX
) y_train_new = np.asarray(
[ [1.0],[1.3],[3.1],[2.0],
[5.0],[6.3],[6.6],[7.4],
[8.0],[9.0]],
dtype= theano.config.floatX
)
print X_train.shape[1] # shape 0 获取行 shape 1 获取列 def train_linreg(X_train, y_train ,eta, epochs):
costs = []
#初始化array0
eta0 = T.fscalar('eta0')
y = T.fvector(name='y')
X = T.fmatrix(name='X')
w = theano.shared(np.zeros(
shape=(X_train.shape[1] +1 ),dtype= theano.config.floatX), name = 'w') #计算损失函数
net_input = T.dot(X, w[1:]) + w[0] # w[1:] 从第二个到最后一个数据
errors = y - net_input
cost = T.sum(T.pow(errors,2)) #梯度更新
gradient = T.grad(cost,wrt=w)
update = [(w, w- eta0* gradient)] #定义模型
train = theano .function(inputs=[eta0],
outputs=cost,
updates=update,
givens={X:X_train,
y: y_train}) for i in range(epochs):
costs.append(train(eta))
print w.get_value()
return costs, w def predict_linreg(X,w):
Xt = T.matrix(name='X')
net_input = T.dot(Xt, w[1:])+w[0]
predict = theano.function(inputs=[Xt],
givens={w:w},
outputs= net_input)
return predict(X) costs,w= train_linreg(X_train,y_train,eta=0.001,epochs=10)
plt.plot(range(1,len(costs)+1),costs)
plt.tight_layout()
plt.xlabel("epoch")
plt.ylabel('cost')
plt.show()
#
#plt.scatter(X_train, y_train ,marker='s',s=50)
plt.plot(range(X_train.shape[0]),predict_linreg(X_train,w),color='gray',marker='o',markersize=4,linewidth=3)
plt.xlabel("x")
plt.ylabel('y')
plt.show()
结果图:
Theano入门神经网络(三)的更多相关文章
- Theano入门神经网络(一)
Theano是一个Python库,专门用于定义.优化.求值数学表达式,效率高,适用于多维数组.特别适合做机器学习.一般来说,使用时需要安装python和numpy. 首先回顾一下机器学习的东西,定义一 ...
- Theano入门神经网络(四)
这一部分主要介绍用Theano 实现softmax函数. 在多分类任务中经常用到softmax函数,首先上几个投影片解释一下 假设目标输出是y_hat ,激活函数后的Relu y 一个是1.2 一个是 ...
- Theano入门神经网络(二) 实现一个XOR门
与非门的图片如下 示意图 详细解释: 1 定义变量的代码,包括了输入.权值.输出等.其中激活函数采用的是sigmod函数 # -*- coding: utf-8 -*- __author__ = 'A ...
- Theano入门——CIFAR-10和CIFAR-100数据集
Theano入门——CIFAR-10和CIFAR-100数据集 1.CIFAR-10数据集介绍 CIFAR-10数据集包含60000个32*32的彩色图像,共有10类.有50000个训练图像和1000 ...
- 无废话ExtJs 入门教程三[窗体:Window组件]
无废话ExtJs 入门教程三[窗体:Window组件] extjs技术交流,欢迎加群(201926085) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3 ...
- RequireJS入门(三)转
这篇来写一个具有依赖的事件模块event.event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.da ...
- RequireJS入门(三)
这篇来写一个具有依赖的事件模块event.event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.da ...
- 【SSRS】入门篇(三) -- 为报表定义数据集
原文:[SSRS]入门篇(三) -- 为报表定义数据集 通过前两篇文件 [SSRS]入门篇(一) -- 创建SSRS项目 和 [SSRS]入门篇(二) -- 建立数据源 后, 我们建立了一个SSRS项 ...
- Bootstrap入门(三十)JS插件7:警告框
Bootstrap入门(三十)JS插件7:警告框 通过这个插件可以为警告信息添加点击以及消失的功能. 当使用一个.close按钮,它必须是第一个子元素.alert-dismissible,并没有文字内 ...
随机推荐
- Error
错误解决的方案: armv7错误: 或者: 友情提示:为了避免不必要的错误,私有的还是放在.m 中引用.h c does not support default arguments: 还没解决方案,待 ...
- wpf下datagrid使用过程中需要注意的几点(一)
MainWindow.xaml中的代码如下: <DataGrid CanUserAddRows="False" ItemsSource="{Binding}&quo ...
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
好长时间没有用过Spring了. 突然拿起书.我都发现自己对AOP都不熟悉了. 其实AOP的意思就是面向切面编程. OO注重的是我们解决问题的方法(封装成Method),而AOP注重的是许多解决解决问 ...
- 使用NPOI将数据导出为word格式里的table
开发环境:VS2013+MySQL5.5+EF6+NPOI2.0.6 格式:WinForm+CodeFirst PS:vs2013的CodeFirst很方便了啊 CodeFirst方式就不再赘述了. ...
- Linux 学习笔记(一) 入门
Shell 显示Shell类型 $ps 切换Shell $[Shell 名称] ex. $tcsh 快捷键 Ctrl + Z:挂起,可用jobs查看到,fg恢复运行 Ctrl + W:删除单词 Ct ...
- centos install kafka and zookeeper
1.安装zookeeper ZooKeeper is a distributed, open-source coordination service for distributed applicati ...
- 开始VS 2012中LightSwitch系列的第5部分:我可以使用用户权限来控制访问权吗?
[原文发表地址] Beginning LightSwitch in VS 2012 Part 5: May I? Controlling Access with User Permissions [ ...
- 【vscode】如何在vscode 中配置:TypeScript开发node环境
入门流程,大神绕行. 安装环境 这就不多说了,安装开发的环境. 安装vscode 下载地址:https://code.visualstudio.com/ 安装Nodejs 下载地址:https://n ...
- 设计模式之美:Visitor(访问者)
索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Visitor 模式结构样式代码. 实现方式(二):使用 Visitor 模式解构设计. 实现方式(三):使用 Acyclic ...
- 详解SQL集合运算
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 ...