问题
When I work with Python, I am used to work in a notebook environment (either Jupyter Notebook locally or Google Colab, where my examples below are tested).
I am sometimes omitting the print()
command when I am interested in a variable. Example:
for each in ['a','b','c']:
print(each)
This prints a b c
as expected.
However,
for each in ['a','b','c']:
each
doesn't print anything. If later I type in a new cell each
, the notebook prints 'c'
, as expected. If I write print(each)
, I get c
as output.
I have a tensorflow dataset, obtained as follows (relying on this):
import tensorflow_datasets as tfds
import tensorflow as tf
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True,
as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
If later I do in a cell:
for each in [each for each in train_dataset.take(5)]:
print(each[1])
Output is:
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
If I do:
for each in [each for each in train_dataset.take(5)]:
each[1]
Then nothing is printed. Following this, running:
print(each[1])
Gives:
tf.Tensor(1, shape=(), dtype=int64)
While:
each[1]
Gives:
<tf.Tensor: shape=(), dtype=int64, numpy=1>
type(each[1])
is:
tensorflow.python.framework.ops.EagerTensor
What does a notebook print out when it is not explicitly told so to print out a something? I think the value which should be printed out when print()
is called is stored within the tensorflow.python.framework.ops.EagerTensor
object. If this is true, how to access that value without printing it out?
来源:https://stackoverflow.com/questions/62882061/print-or-not-to-print-a-tensorflow-python-framework-ops-eagertensor-differ