代码1:(求雅可比矩阵, jacobian矩阵求解)

import theano
from theano import tensor # Creating a vector
x = tensor.dvector('x') # Creating 'y' expression
y = (2 * x ** 3) # Computing derivative
Output, updates = theano.scan(lambda i, y, x : tensor.grad(y[i], x),\
sequences=tensor.arange(y.shape[0]),\
non_sequences=[y, x]) # Creating function
# fun = theano.function([x], Output, updates=updates)
fun = theano.function([x], Output) # Calling function
print( fun([3,3]) )

运行结果:

代码2:(求黑森矩阵, Hession矩阵求解)

import theano
from theano import tensor # Creating a vector
x = tensor.dvector('x') # Creating 'y' expression
y = (2 * x ** 3) # Calculating cost
cost = y.sum() # Computing derivative
derivative = tensor.grad(cost, x)
output, updates = theano.scan(lambda i, derivative,x : \
tensor.grad(derivative[i], x),\
sequences=tensor.arange(derivative.shape[0]),\
non_sequences=[derivative, x]) # Creating function
# fun = theano.function([x], output, updates=updates)
fun = theano.function([x], output) # Calling function
print( fun([3,3]) )

运行结果:

代码3:(theano自定义列表List类型)

import theano.typed_list

# Creating typedlist
f1 = theano.typed_list.TypedListType(theano.tensor.fvector)() # Creating a vector
f2 = theano.tensor.fvector() # Appending 'f1' and 'f2'
f3 = theano.typed_list.append(f1, f2) # Creating function which takes two vectors 'f1' and 'f2' as input and gives 'f3' as output
fun = theano.function([f1, f2], f3) # Calling function
print( fun([[1,2]], [2]) )
print( type(fun([[1,2]], [2])) )

运行结果:

代码4:(theano的switch选择函数)

import theano
from theano import tensor # Creating two scalars
x, y = tensor.scalars('x', 'y')
xx, yy = tensor.vectors('xx', 'yy') # switch expression
switch_expression = tensor.switch(tensor.gt(x, y), x, y)
switch_expression2 = tensor.switch(tensor.gt(xx, yy), xx, yy) # Creating function
fun = theano.function([x, y], switch_expression, mode=theano.compile.mode.Mode(linker='vm'))
fun2 = theano.function([xx, yy], switch_expression2, mode=theano.compile.mode.Mode(linker='vm')) # Calling function fun(12,11)
print( fun(12,11) ) print( fun2([2, 2, 2], [1, 1, 1]) ) print( fun2([2, 0, 2], [1, 3, 1]) )

运行结果:

代码5:

import theano

# Creating a theano variable 'x' with value 10
x = theano.shared(10, 'xxx') #This will just print the variable x
print(x) # Eval function
print(x.eval()) #This will print its actual value

运行结果:

代码6:(和概率推断框架pymc3联合使用)(PyMC3框架联合使用)

import theano
from theano import tensor
import pymc3 as pm # Creating pymc3 model
model = pm.Model() # Creating tensor variable
mu = tensor.scalar('mu') # Log-normal distribution
distribution = pm.Normal.dist(0, 1).logp(mu) # Creating function
fun = theano.function([mu], distribution) # Calling function
print( fun(4) )

运行结果:

代码7:

import theano
from theano import tensor
from theano.ifelse import ifelse # Creating variables
# Input neuron
x = tensor.vector('x')
# Weight
w = tensor.vector('w')
# Bias
b = tensor.scalar('b') # Creating expression:
z = tensor.dot(x,w)+b # Output neuron
o = ifelse(tensor.lt(z,0),0,1)
fun_neural_network = theano.function([x,w,b],o) # Defining Inputs, Weights and bias
inputs = [ [0, 0], [0, 1], [1, 0], [1, 1] ]
weights = [ 1, 1]
bias = 0 # Iterate through all inputs and find outputs:
for ip in range(len(inputs)):
m = inputs[ip]
out = fun_neural_network(m,weights,bias)
print('The output for x1 = {} & x2 = {} is {}'.format(m[0],m[1],out))

运行结果:

代码8:

import theano
from theano import tensor
from theano.ifelse import ifelse import numpy as np
from random import random # Creating variables:
x = tensor.matrix('x') #Input neurons w1 = theano.shared(np.array([random(),random()])) #Random generation of weights
w2 = theano.shared(np.array([random(),random()]))
w3 = theano.shared(np.array([random(),random()]))
b1 = theano.shared(1.) #Bias
b2 = theano.shared(1.) rate_of_learning = 0.01 # Learning rate
y1 = 1/(1+tensor.exp(-tensor.dot(x,w1)-b1))
y2 = 1/(1+tensor.exp(-tensor.dot(x,w2)-b1))
x2 = tensor.stack([y1,y2],axis=1)
y3 = 1/(1+tensor.exp(-tensor.dot(x2,w3)-b2))
actual = tensor.vector('actual') #Actual output
cost = -(actual*tensor.log(y3) + (1-actual)*tensor.log(1-y3)).sum()
dervw1,dervw2,dervw3,dervb1,dervb2 = tensor.grad(cost,[w1,w2,w3,b1,b2]) # Model training
model_train = theano.function( inputs = [x, actual],\
outputs = [y3, cost],\
updates = [ [w1, w1-rate_of_learning*dervw1],\
[w2, w2-rate_of_learning*dervw2],\
[w3, w3-rate_of_learning*dervw3],\
[b1, b1-rate_of_learning*dervb1],\
[b2, b2-rate_of_learning*dervb2] ] ) inputs = [ [0, 0], [0, 1], [1, 0], [1, 1] ]
outputs = [0,1,0,1] # Iterate through all inputs and find outputs:
cost = []
for i in range(100000):
pred, cost_iteration = model_train(inputs, outputs)
cost.append(cost_iteration) # Output
print('The outputs of the Neural network are => ') for i in range(len(inputs)):
print('The output for x1 = {} | x2 = {} => {}'.format(inputs[i][0],inputs[i][1],pred[i]))

运行结果:

代码9:(矩阵拼接)

import theano
from theano import tensor # Creating two matrices
a, b = tensor.matrices('a', 'b') # Using concatenate function
merge_c = tensor.concatenate([a, b]) # Creating function
cancat_function = theano.function([a, b], merge_c) # Calling function
print( cancat_function([[1,2]], [[1,2], [3,4]]) )

运行结果:

深度学习的始祖框架,grandfather级别的框架 —— Theano —— 示例代码学习(5)的更多相关文章

  1. iView学习笔记(一):Table基本操作(包含前后端示例代码)

    iView表格操作 1.前端准备工作 首先新建一个项目,然后引入iView插件,配置好router npm安装iView npm install iview --save cnpm install i ...

  2. 【深度学习系列3】 Mariana CNN并行框架与图像识别

    [深度学习系列3] Mariana CNN并行框架与图像识别 本文是腾讯深度学习系列文章的第三篇,聚焦于腾讯深度学习平台Mariana中深度卷积神经网络Deep CNNs的多GPU模型并行和数据并行框 ...

  3. [源码解析] 深度学习分布式训练框架 horovod (5) --- 融合框架

    [源码解析] 深度学习分布式训练框架 horovod (5) --- 融合框架 目录 [源码解析] 深度学习分布式训练框架 horovod (5) --- 融合框架 0x00 摘要 0x01 架构图 ...

  4. 深度神经网络DNN的多GPU数据并行框架 及其在语音识别的应用

    深度神经网络(Deep Neural Networks, 简称DNN)是近年来机器学习领域中的研究热点,产生了广泛的应用.DNN具有深层结构.数千万参数需要学习,导致训练非常耗时.GPU有强大的计算能 ...

  5. iOS学习——iOS 整体框架及类继承框架图

    整理自:IOS 整体框架类图值得收藏 一 整体框架 在iOS开发过程中,对iOS的整理框架的了解和学习是必不可少的一个环节,今天我们就好好来了解一下iOS的整体框架.首先贴一个关于iOS的框架介绍:i ...

  6. SpringBoot学习笔记(13):日志框架

    SpringBoot学习笔记(13):日志框架——SL4J 快速开始 说明 SpringBoot底层选用SLF4J和LogBack日志框架. SLF4J的使用 SpringBoot的底层依赖关系 1. ...

  7. Duilib学习笔记《01》— duilib整体框架认识

    从GoogleCode上下载的duilib工程中附带的一副总体设计图(如下所示),可以先整体了解一下,有个初步的认识,对后续进一步深入了解学习会很有帮助. 通过设计图有了一个初步认识后,接下来开始进一 ...

  8. Linux学习笔记之Linux启动级别

    对于绝大多数Linux程序员来说,进入Linux系统后一般看到的是黑乎乎的界面(开发模式),因为系统如果启动选择开发模式,会减少启动时间,优化内存等.但是通常我们刚安装完Linux系统,然后进去以后是 ...

  9. Android新技术学习——阿里巴巴免Root无侵入AOP框架Dexposed

    阿里巴巴无线事业部近期开源的Android平台下的无侵入运行期AOP框架Dexposed,该框架基于AOP思想,支持经典的AOP使用场景.可应用于日志记录,性能统计,安全控制.事务处理.异常处理等方面 ...

  10. 微信开发学习日记(六):weiphp框架

    最近重点在看weiphp这个开源的第三方微信公众平台框架. 在网上找微信资料,找到了这个.很早之前,就初步学习了Thinkphp和Onethink2个开源框架,当看到weiphp是用这2个框架开发的时 ...

随机推荐

  1. kettle从入门到精通 第二十九课 job 循环 检验字段的值

    1.平常我们在用kettle设计job的时候,会用到循环来处理一些业务逻辑,比如循环检测某个表中的数据条数等.这个时候就会用到一个特别重要的步骤:检验字段的值. 下图是一个的通过初始化DNOE变量为f ...

  2. 算法金 | LSTM 原作者带队,一个强大的算法模型杀回来了

    大侠幸会,在下全网同名「算法金」 0 基础转 AI 上岸,多个算法赛 Top 「日更万日,让更多人享受智能乐趣」 时间拉回 2019 年,有「计算机界诺贝尔奖」之称图灵奖获得者公布,深度学习三巨头:Y ...

  3. The solution of P5339

    problem 容斥好题,结果题解里面一堆 \(\text{NTT}\). 如果我们去掉有多少个人喜欢什么东西的条件,那么这个题就直接枚举有 \(i\) 组同学会一起讨论蔡徐坤.这一个问题十分容易. ...

  4. 使用edge浏览器时,怎么让alt+tab不切换他的子标签页而只在程序间切换?

    使用搜索按钮(WIN+Q),搜索"多任务设置",在弹出的窗口中看到"alt+tab"相关设置.选择"仅打开的窗口",ok搞定.

  5. WebStorm 中自定义文档注释模板

    WebStorm 中自定义文档注释模板 前提 使用WebStrom写HTML,JavaScript,进行头部注释. 减少重复劳动 养成良好的代码习惯,规范化代码,规范的注释便于后续维护. 头部注释内容 ...

  6. .Net Core5.0中Autofac依赖注入整合多层,项目中可直接用

    一.配置Autofac替换内置DI 1.安装Nuget包:Autofac,Autofac.Extensions.DependencyInjection 2.Program.cs中CreateHostB ...

  7. 开源的网络瑞士军刀「GitHub 热点速览」

    上周的开源热搜项目可谓是精彩纷呈,主打的就一个方便快捷.开箱即用!这款无需安装.点开就用的网络瑞士军刀 CyberChef,试用后你就会感叹它的功能齐全和干净的界面.不喜欢 GitHub 的英文界面? ...

  8. Scrapy爬取知名技术文章网站

    scrapy安装以及目录结构介绍 创建有python3的虚拟环境 mkvirtualenv mkvirtualenv py3env 安装scrapy 进入虚拟环境py3env,把pip的源设置为豆瓣源 ...

  9. 未能加载文件或程序集“netstandard,Version=2.0.0.0, Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”或它的某一个依赖项 解决

    未能加载文件或程序集"netstandard,Version=2.0.0.0, Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51"或它 ...

  10. arm linux 移植 ffmpeg 库 + x264 + x265

    背景 Ffmpeg 中带有h264的解码,没有编码,需要添加x264.libx264是一个自由的H.264编码库,是x264项目的一部分,使用广泛,ffmpeg的H.264实现就是用的libx264. ...