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 打印内存中的变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Tensorflow 查看变量的值方法

    定义一个变量,直接输出会输出变量的属性,并不能输出变量值.那么怎么输出变量值呢?请看下面得意 import tensorflow as tf biases=tf.Variable(tf.zeros([2,3]))#定义一个2x3的全0矩阵 sess=tf.InteractiveSession()#使用InteractiveSession函数 biases.initializer.run()#使用初始化器 initializer op 的 run() 方法初始化 'biases' print(se

  • 详解TensorFlow查看ckpt中变量的几种方法

    查看TensorFlow中checkpoint内变量的几种方法 查看ckpt中变量的方法有三种: 在有model的情况下,使用tf.train.Saver进行restore 使用tf.train.NewCheckpointReader直接读取ckpt文件,这种方法不需要model. 使用tools里的freeze_graph来读取ckpt 注意: 如果模型保存为.ckpt的文件,则使用该文件就可以查看.ckpt文件里的变量.ckpt路径为 model.ckpt 如果模型保存为.ckpt-xxx-

  • tensorflow 获取变量&打印权值的实例讲解

    在使用tensorflow中,我们常常需要获取某个变量的值,比如:打印某一层的权重,通常我们可以直接利用变量的name属性来获取,但是当我们利用一些第三方的库来构造神经网络的layer时,存在一种情况:就是我们自己无法定义该层的变量,因为是自动进行定义的. 比如用tensorflow的slim库时: <span style="font-size:14px;">def resnet_stack(images, output_shape, hparams, scope=None

  • 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):

  • 查看TensorFlow checkpoint文件中的变量名和对应值方法

    实例如下所示: from tensorflow.python import pywrap_tensorflow checkpoint_path = os.path.join(model_dir, "model.ckpt") reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() for key in var_

  • 打印tensorflow恢复模型中所有变量与操作节点方式

    我就废话不多说了,大家还是直接看代码吧! #参数恢复 self.sess=tf.Session() saver = tf.train.import_meta_graph(os.path.join(model_fullpath,'model.ckpt-7.meta')) module_file = tf.train.latest_checkpoint(model_fullpath) saver.restore(self.sess, module_file) variable_names = [v.

  • TensorFlow打印tensor值的实现方法

    最近一直在用TF做CNN的图像分类,当softmax层得到预测结果后,我希望能够看到预测结果,以便和标签之间进行比较.特此补上,以便自己记忆. 我现在通过softmax层得到变量train_logits,如果我直接执行print(train_logits)时,得到的结果如下(因为我是134类分类,所以结果是(1,134)维): 这貌似什么都看不出来. 其实tensorflow提供输出中间值方法方便debug. 这个函数就是[tf.Print]. tf.Print( input_, data, m

  • TensorFlow 输出checkpoint 中的变量名与变量值方式

    废话不多说,直接看代码吧! import os from tensorflow.python import pywrap_tensorflow model_dir="/xxxxxxxxx/model.ckpt" #checkpoint的文件位置 # Read data from checkpoint file reader = pywrap_tensorflow.NewCheckpointReader(model_dir) var_to_shape_map = reader.get_v

  • tensorflow 获取checkpoint中的变量列表实例

    方式1:静态获取,通过直接解析checkpoint文件获取变量名及变量值 通过 reader = tf.train.NewCheckpointReader(model_path) 或者通过: from tensorflow.python import pywrap_tensorflow reader = pywrap_tensorflow.NewCheckpointReader(model_path) 代码: model_path = "./checkpoints/model.ckpt-7500

  • 浅谈java+内存分配及变量存储位置的区别

    Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分配方面的知识.一般Java在内存分配时会涉及到以下区域: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中(new 出来的对象) ◆堆:存放用new产生的数据 ◆静态域:存放在对象中用static定义的静态成员 ◆常量池:存放常量 ◆非RAM存储:硬盘等永久

  • 浅谈Java 类中各成分加载顺序和内存中的存放位置

    一.什么时候会加载类? 使用到类中的内容时加载:有三种情况 1.创建对象:new StaticCode(); 2.使用类中的静态成员:StaticCode.num=9;  StaticCode.show(); 3.在命令行中运行:java StaticCodeDemo 二.类所有内容加载顺序和内存中的存放位置 利用语句进行分析: 1.Person p=new Person("zhangsan",20); 该句话所做的事情: 1.在栈内存中,开辟main函数的空间,建立main函数的变量

  • C语言编程数据在内存中的存储详解

    目录 变量在计算机中有三种表示方式,原码反码,补码 原码 反码 补码 总结一下 浮点数在内存的储存 C语言中,有几种基本内置类型. int unsigned int signed int char unsigned char signed char long unsigned long signed long float double 在内存中创建变量,会在内存中开辟空间,并为其赋值. int a=10; 在计算机中,所有数据都是以二进制的形式存储在内存中. 变量在计算机中有三种表示方式,原码反

随机推荐