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创建临时文件夹的方法
Jul 06 Python
将Django框架和遗留的Web应用集成的方法
Jul 24 Python
使用Python保存网页上的图片或者保存页面为截图
Mar 05 Python
Python matplotlib的使用并自定义colormap的方法
Dec 13 Python
对python3中, print横向输出的方法详解
Jan 28 Python
python实现列表中最大最小值输出的示例
Jul 09 Python
matplotlib jupyter notebook 图像可视化 plt show操作
Apr 24 Python
Python发送邮件封装实现过程详解
May 09 Python
Python‘==‘ 及 ‘is‘相关原理解析
Sep 05 Python
python批量修改文件名的示例
Sep 27 Python
2021年最新用于图像处理的Python库总结
Jun 15 Python
Python使用PyYAML库读写yaml文件的方法
Apr 06 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中使用__autoload()自动加载未定义类的实现代码
2013/02/06 PHP
PHP命名空间(Namespace)的使用详解
2013/05/04 PHP
Laravel 5框架学习之向视图传送数据
2015/04/08 PHP
php实现用手机关闭计算机(电脑)的方法
2015/04/22 PHP
PHP内核探索之变量
2015/12/22 PHP
PHP二进制与字符串之间的相互转换教程
2016/10/14 PHP
解决laravel5.4下的group by报错的问题
2019/10/16 PHP
JS弹出窗口代码大全(详细整理)
2012/12/21 Javascript
使用简洁的jQuery方法实现隔行换色功能
2014/01/02 Javascript
JavaScript制作的可折叠弹出式菜单示例
2014/04/04 Javascript
Javascript BOM学习小结(六)
2015/11/26 Javascript
解决ajax不能访问本地文件问题(利用js跨域原理)
2017/01/24 Javascript
webpack-dev-server自动更新页面方法
2018/02/22 Javascript
微信小程序实现之手势锁功能实例代码
2018/07/19 Javascript
Jquery的Ajax技术使用方法
2019/01/21 jQuery
Vue自定义属性实例分析
2019/02/23 Javascript
原生javascript如何实现共享onload事件
2020/07/03 Javascript
Python实现控制台输入密码的方法
2015/05/29 Python
python中in在list和dict中查找效率的对比分析
2018/05/04 Python
如何在VSCode上轻松舒适的配置Python的方法步骤
2019/10/28 Python
使用wxpy实现自动发送微信消息功能
2020/02/28 Python
详解用Pytest+Allure生成漂亮的HTML图形化测试报告
2020/03/31 Python
Python importlib动态导入模块实现代码
2020/04/16 Python
Django项目在pycharm新建的步骤方法
2021/03/02 Python
css3动画鼠标放上图片逐渐变大鼠标离开图片逐渐缩小效果
2021/01/27 HTML / CSS
canvas小画板之平滑曲线的实现
2020/08/12 HTML / CSS
通用求职信范文模板分享
2013/12/27 职场文书
岗位职责怎么写
2014/03/14 职场文书
计生工作先进事迹
2014/08/15 职场文书
2014年领导班子专项整治整改方案
2014/09/28 职场文书
南京导游词
2015/02/03 职场文书
同学聚会通知短信
2015/04/20 职场文书
2015社区个人工作总结范文
2015/05/13 职场文书
2015年乡镇安全生产工作总结
2015/05/19 职场文书
导游词之大雁塔景区
2019/09/17 职场文书
python tkinter模块的简单使用
2021/04/07 Python