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 生成不重复的随机数的代码
May 15 Python
python使用点操作符访问字典(dict)数据的方法
Mar 16 Python
python抓取网页中图片并保存到本地
Dec 01 Python
简单谈谈python中的多进程
Nov 06 Python
windows下添加Python环境变量的方法汇总
May 14 Python
python爬虫实例详解
Jun 19 Python
Flask框架配置与调试操作示例
Jul 23 Python
Python实现二叉树的常见遍历操作总结【7种方法】
Mar 06 Python
python调用支付宝支付接口流程
Aug 15 Python
Python的bit_length函数来二进制的位数方法
Aug 27 Python
Pytorch技巧:DataLoader的collate_fn参数使用详解
Jan 08 Python
Python爬虫制作翻译程序的示例代码
Feb 22 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 页面跳转到另一个页面的多种方法方法总结
2009/07/07 PHP
php学习之运算符相关概念
2011/06/09 PHP
PHP图片处理类 phpThumb参数用法介绍
2012/03/11 PHP
PHP实现简单汉字验证码
2015/07/28 PHP
php中get_object_vars()在数组的实例用法
2021/02/22 PHP
JavaScript 高级篇之DOM文档,简单封装及调用、动态添加、删除样式(六)
2012/04/07 Javascript
js实现连续英文字符自动换行兼容ie6 ie7和firefox
2013/09/06 Javascript
JavaScript匿名函数之模仿块级作用域
2015/12/12 Javascript
JavaScript中setter和getter方法介绍
2016/07/11 Javascript
Ajax使用原生态JS验证用户名是否存在
2020/05/26 Javascript
Javascript 数组去重的方法(四种)详解及实例代码
2016/11/24 Javascript
jQuery实现的checkbox级联选择下拉菜单效果示例
2016/12/26 Javascript
jQuery实现拖拽可编辑模块功能代码
2017/01/12 Javascript
JavaScript中EventLoop介绍
2018/01/22 Javascript
Node.js 利用cheerio制作简单的网页爬虫示例
2018/03/01 Javascript
值得收藏的八个常用的js正则表达式
2018/10/19 Javascript
Intellij IDEA搭建vue-cli项目的方法步骤
2018/10/20 Javascript
微信小程序学习笔记之本地数据缓存功能详解
2019/03/29 Javascript
JavaScript实现页面中录音功能的方法
2019/06/04 Javascript
原生js拖拽功能制作滑动条实例代码
2021/02/05 Javascript
用Python实现换行符转换的脚本的教程
2015/04/16 Python
python开发之str.format()用法实例分析
2016/02/22 Python
Python实现简单网页图片抓取完整代码实例
2017/12/15 Python
在Python中实现替换字符串中的子串的示例
2018/10/31 Python
Python for i in range ()用法详解
2020/09/18 Python
解决pyshp UnicodeDecodeError的问题
2019/12/06 Python
Django User 模块之 AbstractUser 扩展详解
2020/03/11 Python
SmartBuyGlasses丹麦:网上购买名牌太阳镜、眼镜和隐形眼镜
2016/10/01 全球购物
2014年公司迎新年活动方案
2014/02/24 职场文书
国庆65周年演讲稿:回首往昔,展望未来
2014/09/21 职场文书
幼儿园小班教学反思
2016/03/03 职场文书
Python+Appium自动化测试的实战
2021/06/30 Python
python中出现invalid syntax报错的几种原因分析
2022/02/12 Python
Python语言内置数据类型
2022/02/24 Python
试用1103暨1103、1101同门大比武 [ DAIWEI ]
2022/04/05 无线电
MySQL GTID复制的具体使用
2022/05/20 MySQL