【问题标题】:How to print value of tensorflow.python.framework.ops.Tensor in Tensorflow 2.0?如何在 Tensorflow 2.0 中打印 tensorflow.python.framework.ops.Tensor 的值?
【发布时间】:2020-06-05 21:28:22
【问题描述】:

我的代码中有一些张量,并且需要获取这些张量的值。这是其中之一。如何打印张量OA的值?

Input:OA
Output: <tf.Tensor 'Sum_1:0' shape=(1, 600) dtype=float32>

Input:type(OA)
Output: tensorflow.python.framework.ops.Tensor

我已经尝试了所有可用的函数,例如 tf.print()、eval()、tensor.numpy()。在 Tensorflow 2.0 中,它们都不适合我。似乎它们仅适用于“EagerTensor”而不适用于“ops.Tensor”。

1) OA.eval(session=sess) 错误:ValueError:无法使用给定的会话来评估张量:张量的图表与会话的图表不同。

2) tf.print(OA) 输出:

3) 打印 (OA.numpy()) 输出:AttributeError: 'Tensor' 对象没有属性 'numpy'

有没有办法将 ops.Tensor 转换为 EagerTensor 来尝试上述功能?或者是否有任何其他选项可以打印 ops.Tensor 的值。请指教。

--在TF2.0中添加最小代码重现示例ops.Tensor。

!pip install tensorflow==2.0.0
tf.__version__

import tensorflow as tf
from keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, Dropout, Input, Embedding, Bidirectional, LSTM
from tensorflow.keras import regularizers

EMBEDDING_DIM = 300
max_length = 120
batch_size = 512
vocab_size = 1000
units = 300

from keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, Dropout, Input, Embedding, Bidirectional, LSTM
from tensorflow.keras import regularizers

input_text = tf.keras.Input(shape= (max_length), batch_size=batch_size)

embedding_layer = tf.keras.layers.Embedding(vocab_size, EMBEDDING_DIM, input_length =max_length, name="Embedding_Layer_1")
embedding_sequence = embedding_layer(input_text)

HQ = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units,recurrent_dropout=0.5,kernel_regularizer=regularizers.l2(0.001),return_sequences=True,name='Bidirectional_1'))(embedding_sequence)
HQ = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units,recurrent_dropout=0.5,kernel_regularizer=regularizers.l2(0.001),name='Bidirectional_2'))(HQ)

print (HQ)

输出:Tensor("bidirectional_3/concat:0", shape=(512, 600), dtype=float32)

类型(总部)

输出:tensorflow.python.framework.ops.Tensor

如何检查这个张量的实际值?

【问题讨论】:

  • 请提供minimal, reproducible example。你如何创建OA
  • @jakub - 请您现在检查一下。我用最少的可重现示例更新了问题。
  • @Raghu - 我也有类似的需求。我想使用以下方法检查常量索引序列的嵌入张量: MyEmbedding = tf.keras.layers.Embedding(tf.constant([[[0], [1], [5], [500]]])) .因此,MyEmbedding 是一个 tensorflow.python.framework.ops.Tensor 对象。我可以打印数组的唯一方法是按照这里的第一个示例:stackoverflow.com/questions/52215711/…,使用 tf.enable_eager_execution() 和 tensor.numpy()。希望对您有所帮助。
  • tf.config.run_functions_eagerly(True) 在开始时执行一次为我解决了类似问题

标签: python tensorflow tensorflow2.0


【解决方案1】:

在您打印 HQ 时,您的图表并不完整。您需要完成模型创建。大概是这样的

output = tf.keras.layers.xyz()(HQ)
model = tf.keras.models.Model(input_text, output)

打印中间层的诀窍是让它成为一个输出。您可以暂时将其作为现有模型的附加输出,或者只是制作一个新模型。

inspection_model = tf.keras.models.Model(input_text, [output, HQ])

现在对您的inspection_model 运行推理以获得中间激活HQ 的值。

print(inspection_model(xyz))

【讨论】:

    【解决方案2】:

    使用.numpy() 属性,如:

    your_tensor.numpy()
    

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 2017-10-08
      • 2016-02-11
      • 1970-01-01
      • 2017-11-10
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多