【发布时间】:2019-06-05 16:41:38
【问题描述】:
考虑以下示例:
import tensorflow as tf
import numpy as np
X = np.arange(4).reshape(4, 1) + (np.arange(3) / 10).reshape(1, 3)
batch = tf.data.Dataset.from_tensor_slices(X) \
.batch(2).make_one_shot_iterator().get_next()
def foo(x):
return x + 1
tensor = foo(batch)
现在,我正在寻找一种能够在每次 session.run() 调用中多次采样 tensor 的方法,即:
def bar(x):
return x - 1
result1 = bar(tensor)
with tf.control_dependencies([result1]):
op = <create operation to sample from dataset into `tensor` again>
with tf.control_dependencies([op]):
result2 = bar(tensor)
sess = tf.Session()
print(*sess.run([result1, result2]), sep='\n\n')
应该输出:
[[0. 0.1 0.2]
[1. 1.1 1.2]]
[[2. 2.1 2.2]
[3. 3.1 3.2]]
这可能吗?我知道可以多次调用get_next() 以获取不同张量对象中的多个数据集样本,但是一个样本可以进入相同张量对象吗?
对我来说,用例是这样的,该代码的 foo 和 bar 部分是分开的,而 foo 部分不知道每次运行需要多少次样本。
附: 我正在使用 tf 1.12。 1.13 也是一个选项,但不是 tf 2。
【问题讨论】:
标签: python tensorflow tensorflow-datasets