gpt4 book ai didi

tensorflow - 如何打印 `tf.data.Dataset.from_tensor_slices` 的结果?

转载 作者:行者123 更新时间:2023-12-04 03:03:14 25 4
gpt4 key购买 nike

我是 tensorflow 的新手,所以我尝试了官方文档中出现的每一个命令。

如何正确打印结果 dataset ?这是我的例子:

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

最佳答案

默认情况下,TensorFlow 会构建一个图而不是立即执行操作。如果您想要文字值,请尝试 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)

请注意,在 TensorFlow 2.x 中 tf.enable_eager_execution()是默认行为,符号不存在;你可以把那条线拿出来。

在 TensorFlow 1.x 中构建图时,您需要创建一个 Session并运行图形以获取文字值:
>>> 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))

关于tensorflow - 如何打印 `tf.data.Dataset.from_tensor_slices` 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49856880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com