tensorflow 打印内存中的变量方法

 更新时间:2018年07月30日 09:45:42   作者:JNingWei  
今天小编就为大家分享一篇tensorflow 打印内存中的变量方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

法一:

循环打印

模板

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 中Django验证码功能的实现代码

    Python 中Django验证码功能的实现代码

    验证码是一种区分用户是计算机还是人的公共全自动程序,很多用户登录和注册系统都提供了图形验证码功能。这篇文章主要介绍了Python 中Django验证码功能的实现代码,需要的朋友可以参考下
    2019-06-06
  • Python使用pydub实现M4A转MP3转换器

    Python使用pydub实现M4A转MP3转换器

    这篇文章主要介绍了如何使用 wxPython 创建一个图形用户界面(GUI)应用程序,能够将 .m4a 文件转换为 .mp3 文件,感兴趣的可以了解下
    2024-11-11
  • pytorch tensor合并与分割方式

    pytorch tensor合并与分割方式

    这篇文章主要介绍了pytorch tensor合并与分割方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • PyTorch中torch.manual_seed()的用法实例详解

    PyTorch中torch.manual_seed()的用法实例详解

    在Pytorch中可以通过相关随机数来生成张量,并且可以指定生成随机数的分布函数等,下面这篇文章主要给大家介绍了关于PyTorch中torch.manual_seed()用法的相关资料,需要的朋友可以参考下
    2022-06-06
  • Python实现图片格式转换

    Python实现图片格式转换

    经常会遇到图片格式需要转换的情况,这篇文章主要为大家详细介绍了Python实现图片格式转换,文中示例代码介绍的非常详细、实用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Python实现透明数字时钟效果

    Python实现透明数字时钟效果

    这篇文章主要为大家详细介绍了一个使用 Python 和 Tkinter 库实现的透明数字时钟应用,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2025-02-02
  • 解决python 输出是省略号的问题

    解决python 输出是省略号的问题

    下面小编就为大家分享一篇解决python 输出是省略号的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 全面了解Python的getattr(),setattr(),delattr(),hasattr()

    全面了解Python的getattr(),setattr(),delattr(),hasattr()

    下面小编就为大家带来一篇全面了解Python的getattr(),setattr(),delattr(),hasattr()。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • python列表去重的二种方法

    python列表去重的二种方法

    这篇文章主要介绍了python列表去重的二种方法,第二种方法无法保持原有顺序,需要的朋友可以参考下
    2014-02-02
  • 图文详解python安装Scrapy框架步骤

    图文详解python安装Scrapy框架步骤

    在本篇内容中我们给大家整理了关于python安装Scrapy框架的图文详细步骤,需要的朋友们跟着学习下。
    2019-05-05

最新评论