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发送arp欺骗攻击代码分析
Jan 16 Python
python开发之IDEL(Python GUI)的使用方法图文详解
Nov 12 Python
简述Python中的进程、线程、协程
Mar 18 Python
Django在win10下的安装并创建工程
Nov 20 Python
python+django+sql学生信息管理后台开发
Jan 11 Python
使用pandas对两个dataframe进行join的实例
Jun 08 Python
python write无法写入文件的解决方法
Jan 23 Python
django 类视图的使用方法详解
Jul 24 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
Aug 28 Python
pytorch查看torch.Tensor和model是否在CUDA上的实例
Jan 03 Python
如何利用python正则表达式匹配版本信息
Dec 09 Python
python opencv实现直线检测并测出倾斜角度(附源码+注释)
Dec 31 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 cli换行示例
2014/04/22 PHP
php获取QQ头像并显示的方法
2014/12/23 PHP
CodeIgniter读写分离实现方法详解
2016/01/20 PHP
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
2019/05/09 PHP
JavaScript入门教程(12) js对象化编程
2009/01/31 Javascript
两个select之间option的互相添加操作(jquery实现)
2009/11/12 Javascript
javascript的函数、创建对象、封装、属性和方法、继承
2011/03/10 Javascript
JavaScript XML和string相互转化实现代码
2011/07/04 Javascript
JS创建自定义表格具体实现
2014/02/11 Javascript
简介JavaScript中setUTCSeconds()方法的使用
2015/06/12 Javascript
jquery衣服颜色选取插件效果代码分享
2015/08/28 Javascript
jquery判断密码强度的验证代码
2020/04/22 Javascript
JS实现一个简单的日历
2017/02/22 Javascript
如何抽象一个Vue公共组件
2017/10/17 Javascript
vue-cli配置环境变量的方法
2018/07/09 Javascript
浅谈vue引用静态资源需要注意的事项
2018/09/28 Javascript
Vue框架TypeScript装饰器使用指南小结
2019/02/18 Javascript
vue实现滑动切换效果(仅在手机模式下可用)
2020/06/29 Javascript
vue webpack build资源相对路径的问题及解决方法
2020/06/04 Javascript
[01:10]为家乡而战!完美世界城市挑战赛全国总决赛花絮
2019/07/25 DOTA
python中快速进行多个字符替换的方法小结
2016/12/15 Python
django-crontab 定时执行任务方法的实现
2019/09/06 Python
python str字符串转uuid实例
2020/03/03 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
2020/03/03 Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
2020/05/17 Python
Python基于gevent实现文件字符串查找器
2020/08/11 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
毕业生机械建模求职信
2013/10/14 职场文书
工商学院毕业生自荐信
2013/11/12 职场文书
会计专业自我鉴定
2014/02/10 职场文书
先进事迹演讲稿
2014/09/01 职场文书
大学生自我评价200字(4篇)
2014/09/17 职场文书
违纪开除通知书
2015/04/25 职场文书
党员身份证明材料
2015/06/19 职场文书
创业计划书之家政服务
2019/09/18 职场文书
MySQL8.0的WITH查询详情
2021/08/30 MySQL