![](/img/trans.png)
[英]How to get a windowed dataset in tensorflow 2 from an array of numpy arrays?
[英]Tensorflow: how to batch with a dataset constructed with numpy arrays?
我正在嘗試了解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
都相同。 batch_size
替換None
)也不會更改行為。 感謝您的建議/更正等。
為了考慮批次大小,您需要更改以下內容
dataset.batch(batch_size)
至
dataset = dataset.batch(batch_size)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.