【问题标题】:Tensorflow: how to batch with a dataset constructed with numpy arrays?Tensorflow:如何使用 numpy 数组构建的数据集进行批处理?
【发布时间】: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 函数应用于数据集,Xyshapes 都是相同的。
  • 更改我输入占位符的形状(例如,将 None 替换为 batch_size)也不会改变行为。

感谢您的建议/更正等。

【问题讨论】:

    标签: python tensorflow shapes tensorflow-datasets


    【解决方案1】:

    为了考虑批量大小,您需要更改以下内容

    dataset.batch(batch_size)
    

    dataset = dataset.batch(batch_size)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 2020-09-25
      • 2019-03-24
      • 2018-09-03
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多