1. 代码实现

#!/usr/bin/env python
#! _*_ coding:UTF-8 _*_

import numpy as np
import theano.tensor as T
import theano

if __name__ == "__main__":

    # 用一个累加器来测试共享变量
    state = theano.shared(np.array(0, dtype=np.float64), 'state')

    inc = T.scalar('inc', dtype=state.dtype)

    accumulator = theano.function([inc], state, updates=[
        (state, state + inc)
    ])

    print state.get_value()
    accumulator(10)
    print state.get_value()
    # 这里不宜直接用print accumulaot(10)进行取值

    # 设置共享变量的值
    state.set_value(-1)
    accumulator(3)
    print state.get_value()

    # 使用另一个变量暂时代替共享变量进行赋值
    tmp_function = state * 2 + inc
    a = T.scalar(dtype=state.dtype)
    skip_shared = theano.function([inc, a], tmp_function, givens=[
        (state, a)
    ])

    print skip_shared(2, 3)
    print state.get_value()

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/theano_day3/shared_value.py
0.0
10.0
2.0
8.0
2.0

Process finished with exit code 0

莫烦theano学习自修第三天【共享变量】的更多相关文章

  1. 莫烦theano学习自修第九天【过拟合问题与正规化】

    如下图所示(回归的过拟合问题):如果机器学习得到的回归为下图中的直线则是比较好的结果,但是如果进一步控制减少误差,导致机器学习到了下图中的曲线,则100%正确的学习了训练数据,看似较好,但是如果换成另 ...

  2. 莫烦keras学习自修第三天【回归问题】

    1. 代码实战 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ import numpy as np # 这句话不知道是什么意思 np.random.seed ...

  3. 莫烦theano学习自修第十天【保存神经网络及加载神经网络】

    1. 为何保存神经网络 保存神经网络指的是保存神经网络的权重W及偏置b,权重W,和偏置b本身是一个列表,将这两个列表的值写到列表或者字典的数据结构中,使用pickle的数据结构将列表或者字典写入到文件 ...

  4. 莫烦theano学习自修第八天【分类问题】

    1. 代码实现 from __future__ import print_function import numpy as np import theano import theano.tensor ...

  5. 莫烦theano学习自修第七天【回归结果可视化】

    1.代码实现 from __future__ import print_function import theano import theano.tensor as T import numpy as ...

  6. 莫烦theano学习自修第六天【回归】

    1. 代码实现 from __future__ import print_function import theano import theano.tensor as T import numpy a ...

  7. 莫烦theano学习自修第五天【定义神经层】

    1. 代码如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T ...

  8. 莫烦theano学习自修第二天【激励函数】

    1. 代码如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np import theano.tensor as T ...

  9. 莫烦theano学习自修第一天【常量和矩阵的运算】

    1. 代码实现如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ # 导入numpy模块,因为numpy是常用的计算模块 import numpy as ...

随机推荐

  1. 第23章 Spring MVC初体验

    23.1 鸟瞰Spring MVC 粗略的介绍了SpringMVC的主要组成部分,SpringMVC作为一个Web层的框架,最大的作用是把我从繁重的web.xml文件编写中解救出来,再也不要不停的添加 ...

  2. windows server 2012 安装 VC14(VC2015) 安装失败解决方案

    原文地址:https://www.cnblogs.com/huoniao/articles/6186021.html 系统环境如下:cmd命令行-输入 systeminfo 如下图 - The VC1 ...

  3. nginx 配sorry page - error page

    include conf.d/*.conf; server { listen 9999; server_name 127.0.0.1; location / { root html; index er ...

  4. JS判断当前设备是 PC IOS Andriod

    JS判断当前设备是 PC IOS Andriod <script > window.onload = function(){ var isPc = IsPC(); var isAndroi ...

  5. spark 2.3 导致driver OOM的一个SparkPlanGraphWrapper源码的bug

    背景 长话短说,我们部门一个同事找到我,说他的spark 2.3 structured streaming程序频繁报OOM,从来没有坚持过超过三四天的,叫帮看一下. 这种事情一般我是不愿意看的,因为大 ...

  6. js实现活动倒计时

    let startTime = 1527647143949; // 开始时间 var time = new Countdown('timer',startTime); function Countdo ...

  7. koa2实现session的两种方式(基于Redis 和MySQL)

    一.基于MySQL的实现方式 这种方式需要安装koa-session-minimal和koa-mysql-session两个依赖. 执行 npm install koa-session-minimal ...

  8. c++入门之再次探讨类属性

    精辟博文:https://blog.csdn.net/msdnwolaile/article/details/51923859(转载,仅供学习|!)

  9. Rikka with Subset HDU - 6092 (DP+组合数)

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  10. Python_内置函数之max

    源码: def max(*args, key=None): # known special case of max """ max(iterable, *[, defau ...