How to print value of tensorflow.python.framework.ops.Tensor in Tensorflow 2.0?

℡╲_俬逩灬. 提交于 2020-03-24 00:13:30

问题


I have a few tensors in my code and and need to get the values of those tensors. This is one them. How to print the values of tensor OA?

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

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

I have tried all the available functions like tf.print(), eval(), tensor.numpy(). None of them worked for me in Tensorflow 2.0. It seems they work only for 'EagerTensor' and not for 'ops.Tensor'.

1) OA.eval(session=sess) Error: ValueError: Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.

2) tf.print(OA) Output:

3) print (OA.numpy()) Output: AttributeError: 'Tensor' object has no attribute 'numpy'

Is there any way to convert ops.Tensor to EagerTensor to try the above functions? Or is there any other option to print the values of ops.Tensor. Please advise.

--Adding the minimal code to reproduce the example ops.Tensor in TF2.0.

!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)

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

type(HQ)

Output: tensorflow.python.framework.ops.Tensor

How to check the actual values of this tensor?


回答1:


You are trying to print a symbolic tensor. Symbolic tensors are different, in that no explicit values are required to define the tensor, and this has implications in terms of building neural networks with TensorFlow 2.0, which now uses Keras as the default API.

Let's consider a simple example.

a = tf.Variable(5, name="a")
b = tf.Variable(7, name="b")
c = (b**2 - a**3)**5
print(c)

The output is as follows:

tf.Tensor(1759441920, shape=(), dtype=int32)

For the above, the values are specifically defined in tf.Variable format, and the output is in Tensor format. However, the tensor must contain a value in order to be considered as such.

Here is an example of a Sequential neural network:

from tensorflow.keras import models
from tensorflow.keras import layers

model = models.Sequential()
model.add(layers.Dense(8, activation='relu', input_shape=(4,)))
model.add(layers.Dense(1, activation='sigmoid'))

print(model)

The output is as follows:

<tensorflow.python.keras.engine.sequential.Sequential object at 0x7f7af8bd4ac8>

This is a symbolically defined model, as no values are explicitly being defined in the network. Rather, a framework is created for the input variables to be read by the network, and then generate predictions.



来源:https://stackoverflow.com/questions/60338842/how-to-print-value-of-tensorflow-python-framework-ops-tensor-in-tensorflow-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!