如何打印`tf.data.Dataset.from_tensor_slices`的结果? [英] How to print the result of `tf.data.Dataset.from_tensor_slices`?

查看:78
本文介绍了如何打印`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():

<预><代码>>>>将张量流导入为 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]]])>>>数据集 = tf.data.Dataset.from_tensor_slices((X, Y))>>>对于数据集中的 x, y:...打印(x,y)...tf.张量([[1 2 3][3 4 5]], shape=(2, 3), dtype=int32) tf.Tensor([[11]], shape=(1, 1), dtype=int32)tf.张量([[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 并运行图形以获取文字值:

<预><代码>>>>将张量流导入为 tf>>>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.make_one_shot_iterator().get_next()>>>使用 tf.Session() 作为会话:...打印(会话.运行(张量))...(数组([[1, 2, 3],[3, 4, 5]], dtype=int32), 数组([[11]], dtype=int32))

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆