【问题标题】:Printing values (weights) for all dtype=float32 in Tensorflow在 Tensorflow 中打印所有 dtype=float32 的值(权重)
【发布时间】:2018-01-06 14:06:09
【问题描述】:

没有placeholdersfeed_dict 有没有简单的方法打印dtype=float32 的值?这个过程很尴尬,需要为每个操作单独定义。假设我有具有数百个操作的 inceptionv3 模型:

 op = sess.graph.get_operations()
    for m in op : 
    print(m.values())

因为这些和其中一些是混合的:

...
(<tf.Tensor 'pool_3:0' shape=(?, ?, ?, 2048) dtype=float32>,)
(<tf.Tensor 'pool_3/_reshape/shape:0' shape=(2,) dtype=int32>,)
(<tf.Tensor 'pool_3/_reshape:0' shape=(1, 2048) dtype=float32>,)
(<tf.Tensor 'softmax/weights_quint8_const:0' shape=(2048, 1008) dtype=quint8>,)
(<tf.Tensor 'softmax/weights_min:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/weights_max:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_reshape_dims:0' shape=(1,) dtype=int32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_reduction_dims:0' shape=(1,) dtype=int32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_reshape_pool_3/_reshape:0' shape=(2048,) dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_min_pool_3/_reshape:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_max_pool_3/_reshape:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_pool_3/_reshape:0' shape=(1, 2048) dtype=quint8>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_pool_3/_reshape:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_pool_3/_reshape:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_quantized_bias_add:0' shape=(1, 1008) dtype=qint32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantized_bias_add:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantized_bias_add:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_down:0' shape=(1, 1008) dtype=quint8>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_down:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_down:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/biases_quint8_const:0' shape=(1008,) dtype=quint8>,)
(<tf.Tensor 'softmax/biases_min:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/biases_max:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits_eightbit_quantized_bias_add:0' shape=(1, 1008) dtype=qint32>, <tf.Tensor 'softmax/logits_eightbit_quantized_bias_add:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits_eightbit_quantized_bias_add:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits_eightbit_quantize_down:0' shape=(1, 1008) dtype=quint8>, <tf.Tensor 'softmax/logits_eightbit_quantize_down:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits_eightbit_quantize_down:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits:0' shape=(1, 1008) dtype=float32>,)
(<tf.Tensor [] shape=(1, 1008) dtype=float32>,)

有没有一种简单的方法可以一次打印所有这些浮点类型的值?

【问题讨论】:

    标签: python tensorflow type-conversion


    【解决方案1】:

    你可以使用类似的东西

    from pprint import pprint
    pprint([out
      for op in tf.get_default_graph().get_operations() if op.type != 'Placeholder'
      for out in op.values() if out.dtype == tf.float32])
    

    Feed dicts 是标准的 Python 字典,因此它们不应显示为图形操作。

    编辑

    要获取它们的值,您需要运行它们:

    out_val = sess.run([out
      for op in tf.get_default_graph().get_operations() if op.type != 'Placeholder'
      for out in op.values() if out.dtype == tf.float32], 
      feed_dict=my_feed_dict)
    

    【讨论】:

    • 感谢@user1735003,这将只打印具有dtype=float32 的操作:这是我的输出:... &lt;tf.Tensor 'softmax/logits_eightbit_quantize_down:2' shape=() dtype=float32&gt;, &lt;tf.Tensor 'softmax/logits:0' shape=(1, 1008) dtype=float32&gt;, &lt;tf.Tensor 'softmax:0' shape=(1, 1008) dtype=float32&gt;] 现在,我将如何打印它们的值?
    • @Amir 我更新了我的答案,指出您需要在会话中运行输出以获取它们的值。
    • 非常感谢,@user1735003。现在回到我的第一个问题!我们如何将feed_dict 定义为对所有具有不同形状的不同浮点类型通用:D
    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 2017-07-26
    • 2021-09-03
    • 2017-11-10
    • 1970-01-01
    • 2019-11-08
    • 2019-10-07
    • 1970-01-01
    相关资源
    最近更新 更多