如何在 Tensorflow 2.0 中打印 tensorflow.python.framework.ops.Tensor 的值?

Rag*_*ghu 10 python tensorflow tensorflow2.0

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

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

Input:type(OA)
Output: tensorflow.python.framework.ops.Tensor
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有可用的函数,如 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)
Run Code Online (Sandbox Code Playgroud)

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

类型(总部)

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

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

Dac*_*hao -1

使用.numpy()如下属性:

your_tensor.numpy()
Run Code Online (Sandbox Code Playgroud)