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使用cookielib库示例分享
Mar 03 Python
Python自定义scrapy中间模块避免重复采集的方法
Apr 07 Python
python 统计列表中不同元素的数量方法
Jun 29 Python
python 反向输出字符串的方法
Jul 16 Python
利用python实现简易版的贪吃蛇游戏(面向python小白)
Dec 30 Python
详解Python装饰器
Mar 25 Python
django中SMTP发送邮件配置详解
Jul 19 Python
python使用pip安装SciPy、SymPy、matplotlib教程
Nov 20 Python
如何让PyQt5中QWebEngineView与JavaScript交互
Oct 21 Python
Python实现哲学家就餐问题实例代码
Nov 09 Python
基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
Feb 20 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Apr 09 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/05/04 PHP
php去除重复字的实现代码
2011/09/16 PHP
数据库中排序的对比及使用条件详解
2012/02/23 PHP
php使用百度天气接口示例
2014/04/22 PHP
Yii实现多数据库主从读写分离的方法
2014/12/29 PHP
浅谈php错误提示及查错方法
2015/07/14 PHP
Laravel 对某一列进行筛选然后求和sum()的例子
2019/10/10 PHP
JavaScript通过RegExp实现客户端验证处理程序
2013/05/07 Javascript
多个jquery.datatable共存,checkbox全选异常的快速解决方法
2013/12/10 Javascript
Javascript基础教程之while语句
2015/01/18 Javascript
JS实现一个按钮的方法
2015/02/05 Javascript
使用JS正则表达式 替换括号,尖括号等
2016/11/29 Javascript
简单实现bootstrap导航效果
2017/02/07 Javascript
Flask中获取小程序Request数据的两种方法
2017/05/12 Javascript
fetch 如何实现请求数据
2018/12/20 Javascript
ES6使用新特性Proxy实现的数据绑定功能实例
2020/05/11 Javascript
八种Vue组件间通讯方式合集(推荐)
2020/08/18 Javascript
使用Python实现一个简单的项目监控
2015/03/31 Python
python实现m3u8格式转换为mp4视频格式
2018/02/28 Python
Python中的Numpy矩阵操作
2018/08/12 Python
在Python中字典根据多项规则排序的方法
2019/01/21 Python
基于树莓派的语音对话机器人
2019/06/17 Python
python使用itchat模块给心爱的人每天发天气预报
2019/11/25 Python
python 实现图片上传接口开发 并生成可以访问的图片url
2019/12/18 Python
Python flask框架端口失效解决方案
2020/06/04 Python
python 通过exifread读取照片信息
2020/12/24 Python
Probikekit日本:自行车套件,跑步和铁人三项装备
2017/04/03 全球购物
啤酒销售实习自我鉴定
2013/09/24 职场文书
生物医学工程专业学生求职信范文分享
2013/12/14 职场文书
心得体会怎么写
2013/12/30 职场文书
招聘与培训专员岗位职责
2014/01/30 职场文书
党员目标管理责任书
2014/07/25 职场文书
党员弘扬焦裕禄精神思想汇报
2014/09/10 职场文书
师范生见习报告
2014/10/31 职场文书
个人先进事迹材料范文
2014/12/29 职场文书
Android Studio实现带三角函数对数运算功能的高级计算器
2022/05/20 Java/Android