前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow: 打印内存中的变量

tensorflow: 打印内存中的变量

作者头像
JNingWei
发布2018-09-28 15:40:04
1.8K0
发布2018-09-28 15:40:04
举报
文章被收录于专栏:JNing的专栏JNing的专栏

法一:

循环打印

模板

代码语言:javascript
复制
for (x, y) in zip(tf.global_variables(), sess.run(tf.global_variables())):
    print '\n', x, y

实例

代码语言:javascript
复制
# 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()
代码语言:javascript
复制
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

法二:

指定变量名打印

模板

代码语言:javascript
复制
print 'my/BatchNorm/beta:0', (sess.run('my/BatchNorm/beta:0'))

实例

代码语言:javascript
复制
# 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()
代码语言:javascript
复制
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


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 法一:
    • 模板
      • 实例
      • 法二:
        • 模板
          • 实例
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档