tensorflow 打印内存中的变量方法


Posted in Python onJuly 30, 2018

法一:

循环打印

模板

for (x, y) in zip(tf.global_variables(), sess.run(tf.global_variables())):
 print '\n', x, y

实例

# coding=utf-8

import tensorflow as tf


def func(in_put, layer_name, is_training=True):
 with tf.variable_scope(layer_name, reuse=tf.AUTO_REUSE):
  bn = tf.contrib.layers.batch_norm(inputs=in_put,
           decay=0.9,
           is_training=is_training,
           updates_collections=None)
 return bn

def main():

 with tf.Graph().as_default():
  # input_x
  input_x = tf.placeholder(dtype=tf.float32, shape=[1, 4, 4, 1])
  import numpy as np
  i_p = np.random.uniform(low=0, high=255, size=[1, 4, 4, 1])
  # outputs
  output = func(input_x, 'my', is_training=True)
  with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())
   t = sess.run(output, feed_dict={input_x:i_p})

   # 法一: 循环打印
   for (x, y) in zip(tf.global_variables(), sess.run(tf.global_variables())):
    print '\n', x, y

if __name__ == "__main__":
 main()
2017-09-29 10:10:22.714213: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1052] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)

<tf.Variable 'my/BatchNorm/beta:0' shape=(1,) dtype=float32_ref> [ 0.]

<tf.Variable 'my/BatchNorm/moving_mean:0' shape=(1,) dtype=float32_ref> [ 13.46412563]

<tf.Variable 'my/BatchNorm/moving_variance:0' shape=(1,) dtype=float32_ref> [ 452.62246704]

Process finished with exit code 0

法二:

指定变量名打印

模板

print 'my/BatchNorm/beta:0', (sess.run('my/BatchNorm/beta:0'))

实例

# coding=utf-8

import tensorflow as tf


def func(in_put, layer_name, is_training=True):
 with tf.variable_scope(layer_name, reuse=tf.AUTO_REUSE):
  bn = tf.contrib.layers.batch_norm(inputs=in_put,
           decay=0.9,
           is_training=is_training,
           updates_collections=None)
 return bn

def main():

 with tf.Graph().as_default():
  # input_x
  input_x = tf.placeholder(dtype=tf.float32, shape=[1, 4, 4, 1])
  import numpy as np
  i_p = np.random.uniform(low=0, high=255, size=[1, 4, 4, 1])
  # outputs
  output = func(input_x, 'my', is_training=True)
  with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())
   t = sess.run(output, feed_dict={input_x:i_p})

   # 法二: 指定变量名打印
   print 'my/BatchNorm/beta:0', (sess.run('my/BatchNorm/beta:0'))
   print 'my/BatchNorm/moving_mean:0', (sess.run('my/BatchNorm/moving_mean:0'))
   print 'my/BatchNorm/moving_variance:0', (sess.run('my/BatchNorm/moving_variance:0'))

if __name__ == "__main__":
 main()
2017-09-29 10:12:41.374055: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1052] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)

my/BatchNorm/beta:0 [ 0.]
my/BatchNorm/moving_mean:0 [ 8.08649635]
my/BatchNorm/moving_variance:0 [ 368.03442383]

Process finished with exit code 0

以上这篇tensorflow 打印内存中的变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python中操作字典之update()方法的使用
May 22 Python
Python3实现并发检验代理池地址的方法
Sep 18 Python
python 动态加载的实现方法
Dec 22 Python
如何用Python合并lmdb文件
Jul 02 Python
Django添加sitemap的方法示例
Aug 06 Python
Django后端接收嵌套Json数据及解析详解
Jul 17 Python
对python3中的RE(正则表达式)-详细总结
Jul 23 Python
Python爬取智联招聘数据分析师岗位相关信息的方法
Aug 13 Python
PyQt5事件处理之定时在控件上显示信息的代码
Mar 25 Python
pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
Apr 24 Python
简单了解python shutil模块原理及使用方法
Apr 28 Python
python如何构建mock接口服务
Jan 28 Python
Python实现的多叉树寻找最短路径算法示例
Jul 30 #Python
tensorflow: variable的值与variable.read_value()的值区别详解
Jul 30 #Python
Tensorflow 实现修改张量特定元素的值方法
Jul 30 #Python
python用BeautifulSoup库简单爬虫实例分析
Jul 30 #Python
对TensorFlow的assign赋值用法详解
Jul 30 #Python
Python双向循环链表实现方法分析
Jul 30 #Python
tensorflow更改变量的值实例
Jul 30 #Python
You might like
php中0,null,empty,空,false,字符串关系的详细介绍
2013/06/20 PHP
正确的PHP匹配UTF-8中文的正则表达式
2015/05/13 PHP
PHP 数组基本操作小结(推荐)
2016/06/13 PHP
php安装php_rar扩展实现rar文件读取和解压的方法
2016/11/17 PHP
php 调用百度sms来发送短信的实现示例
2018/11/02 PHP
用js实现随机返回数组的一个元素
2007/08/13 Javascript
不使用中间变量,交换int型的 a, b两个变量的值。
2010/10/29 Javascript
silverlight线程与基于事件驱动javascript引擎(实现轨迹回放功能)
2011/08/09 Javascript
jQuery中jqGrid分页实现代码
2011/11/04 Javascript
拉动滚动条加载数据的jquery代码
2012/05/03 Javascript
jquery中.add()的使用分析
2013/04/26 Javascript
离开当前页面前使用js判断条件提示是否要离开页面
2014/05/02 Javascript
JavaScript在IE和FF下的兼容性问题
2014/05/19 Javascript
最精简的JavaScript实现鼠标拖动效果的方法
2015/05/11 Javascript
js简单工厂模式用法实例
2015/06/30 Javascript
JavaScript变量的作用域全解析
2015/08/14 Javascript
jQuery简易时光轴实现方法示例
2017/03/13 Javascript
浅谈如何通过node.js对数据进行MD5加密
2018/05/16 Javascript
基于vue循环列表时点击跳转页面的方法
2018/08/31 Javascript
小程序页面动态配置实现方法
2019/02/05 Javascript
小程序使用分包的示例代码
2020/03/23 Javascript
JS如何实现封装列表右滑动删除收藏按钮
2020/07/23 Javascript
10种检测Python程序运行时间、CPU和内存占用的方法
2015/04/01 Python
Python+matplotlib实现华丽的文本框演示代码
2018/01/22 Python
wxPython的安装与使用教程
2018/08/31 Python
django的ORM操作 删除和编辑实现详解
2019/07/24 Python
python线程信号量semaphore使用解析
2019/11/30 Python
ASP.NET中的身份验证有那些
2012/07/13 面试题
机械设计毕业生自荐信
2014/02/02 职场文书
2014年安全生产目标责任书
2014/07/23 职场文书
新闻学专业职业生涯规划范文:我的人生我做主
2014/09/12 职场文书
关于学习的决心书
2015/02/05 职场文书
中班教师个人总结
2015/02/05 职场文书
导游词之泉州崇武古城
2019/12/20 职场文书
Nginx本地目录映射实现代码实例
2021/03/31 Servers
Java时间工具类Date的常用处理方法
2022/05/25 Java/Android