如何打印`tf.data.Dataset.from_tensor_slices`的结果? [英] How to print the result of `tf.data.Dataset.from_tensor_slices`?
问题描述
我是 tensorflow 的新手,所以我尝试了官方文档中出现的每一个命令.
如何正确打印结果dataset
?这是我的例子:
将 tensorflow 导入为 tf将 numpy 导入为 np
sess = tf.Session()X = tf.constant([[[1, 2, 3], [3, 4, 5]], [[3, 4, 5], [5, 6, 7]]])Y = tf.constant([[[11]], [[12]]])数据集 = tf.data.Dataset.from_tensor_slices((X, Y))数据集打印类型(数据集)# 打印帮助(数据集)# 打印 dataset.output_classes# 打印 dataset.output_shapes
默认情况下,TensorFlow 构建一个图形而不是立即执行操作.如果您想要文字值,请尝试 tf.enable_eager_execution()
:
请注意,在 TensorFlow 2.x 中 tf.enable_eager_execution()
是默认行为,并且该符号不存在;你可以把那条线去掉.
在 TensorFlow 1.x 中构建图形时,您需要创建一个 Session
并运行图形以获取文字值:
I'm a new to tensorflow, so I try every single command appeared in the official document.
How can I properly print the result dataset
? Here is my example:
import tensorflow as tf
import numpy as np
sess = tf.Session()
X = tf.constant([[[1, 2, 3], [3, 4, 5]], [[3, 4, 5], [5, 6, 7]]])
Y = tf.constant([[[11]], [[12]]])
dataset = tf.data.Dataset.from_tensor_slices((X, Y))
dataset
print type(dataset)
# print help(dataset)
# print dataset.output_classes
# print dataset.output_shapes
By default TensorFlow builds up a graph rather than executing operations immediately. If you'd like literal values, try tf.enable_eager_execution()
:
>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> X = tf.constant([[[1,2,3],[3,4,5]],[[3,4,5],[5,6,7]]])
>>> Y = tf.constant([[[11]],[[12]]])
>>> dataset = tf.data.Dataset.from_tensor_slices((X, Y))
>>> for x, y in dataset:
... print(x, y)
...
tf.Tensor(
[[1 2 3]
[3 4 5]], shape=(2, 3), dtype=int32) tf.Tensor([[11]], shape=(1, 1), dtype=int32)
tf.Tensor(
[[3 4 5]
[5 6 7]], shape=(2, 3), dtype=int32) tf.Tensor([[12]], shape=(1, 1), dtype=int32)
Note that in TensorFlow 2.x tf.enable_eager_execution()
is the default behavior and the symbol doesn't exist; you can just take that line out.
When graph building in TensorFlow 1.x, you need to create a Session
and run the graph to get literal values:
>>> import tensorflow as tf
>>> X = tf.constant([[[1,2,3],[3,4,5]],[[3,4,5],[5,6,7]]])
>>> Y = tf.constant([[[11]],[[12]]])
>>> dataset = tf.data.Dataset.from_tensor_slices((X, Y))
>>> tensor = dataset.make_one_shot_iterator().get_next()
>>> with tf.Session() as session:
... print(session.run(tensor))
...
(array([[1, 2, 3],
[3, 4, 5]], dtype=int32), array([[11]], dtype=int32))
这篇关于如何打印`tf.data.Dataset.from_tensor_slices`的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!