答案 0 :(得分:10)
默认情况下,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))
>>> iterator = dataset.make_one_shot_iterator()
>>> for x, y in iterator:
... 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)
构建图表时,您需要创建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))