【发布时间】:2019-05-23 12:53:10
【问题描述】:
我试图了解Dataset.batch 的行为。这是我用来尝试通过基于numpy 数组的Dataset 对批处理数据设置迭代器的代码。
## experiment with a numpy dataset
sample_size = 100000
ncols = 15
batch_size = 1000
xarr = np.ones([sample_size, ncols]) * [i for i in range(ncols)]
xarr = xarr + np.random.normal(scale = 0.5, size = xarr.shape)
yarr = np.sum(xarr, axis = 1)
self.x_placeholder = tf.placeholder(xarr.dtype, [None, ncols])
self.y_placeholder = tf.placeholder(yarr.dtype, [None, 1])
dataset = tf.data.Dataset.from_tensor_slices((self.x_placeholder, self.y_placeholder))
dataset.batch(batch_size)
self.iterator = dataset.make_initializable_iterator()
X, y = self.iterator.get_next()
但是,当我检查 X 和 y 的形状时,它们是
(Pdb) X.shape
TensorShape([Dimension(15)])
(Pdb) y.shape
TensorShape([Dimension(1)])
这让我很困惑,因为我的批量大小似乎没有被考虑在内。它还会在构建模型时导致下游问题,因为我希望 X 和 y 有两个维度,第一个维度是批次中的示例数。
问题:为什么迭代器的输出是一维的?我应该如何正确批处理?
这是我尝试过的:
- 无论我是否将
batch函数应用于数据集,X和y的shapes都是相同的。 - 更改我输入占位符的形状(例如,将
None替换为batch_size)也不会改变行为。
感谢您的建议/更正等。
【问题讨论】:
标签: python tensorflow shapes tensorflow-datasets