窗口多维 Tensorflow 数据集 [英] Window Multidimensional Tensorflow Dataset

查看:53
本文介绍了窗口多维 Tensorflow 数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有形状为 m by n 的二维数据,我想将其大小为 w 沿第一个轴窗口化为数据集mw 个二维数组,每个数组的大小为 wn.例如,如果数据是:

I have 2-dimensional data with shape m by n that I want to window with size w along the first axis into a dataset of m-w many two-dimensional arrays each of size w by n. For instance if the data is:

[[0,  1,  2 ], 
 [3,  4,  5 ], 
 [6,  7,  8 ],
 [9,  10, 11]]

然后我想把它窗口化

[[[0, 1 , 2 ], 
  [3, 4 , 5 ], 
  [6, 7 , 8 ]],
 [[3, 4 , 5 ],
  [6, 7 , 8 ],
  [9, 10, 11]]]

我可以将数据组合成正确的集合:

I can window the data together into the right sets:

dataset = tf.data.Dataset.from_tensor_slices(np.arange(5*3).reshape(5,3))
dataset = dataset.window(size=3,shift=1,drop_remainder=True)
for window in dataset : print(list(window.as_numpy_iterator()))
>>>[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
>>>[array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
>>>[array([6, 7, 8]), array([ 9, 10, 11]), array([12, 13, 14])]

但我不知道如何将数据重新恢复为堆叠形状.我想也许是 tf.stack,但没有骰子.有人知道如何完成吗?

but I can't figure out how to get the data back into the stacked shape again. I thought maybe tf.stack, but no dice on that. Does anybody know how to finish this?

推荐答案

我找到了答案

I found the answer here actually. I don't know why it works, but it does:

dataset = tf.data.Dataset.from_tensor_slices(np.arange(5*3).reshape(5,3))
dataset = dataset.window(size=3,shift=1)
dataset = dataset.flat_map(lambda x : x.batch(3))
for d in dataset : print(d)

使


tf.Tensor(
[[0 1 2]
 [3 4 5]
 [6 7 8]], shape=(3, 3), dtype=int64)
tf.Tensor(
[[ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]], shape=(3, 3), dtype=int64)
tf.Tensor(
[[ 6  7  8]
 [ 9 10 11]
 [12 13 14]], shape=(3, 3), dtype=int64)
tf.Tensor(
[[ 9 10 11]
 [12 13 14]], shape=(2, 3), dtype=int64)
tf.Tensor([[12 13 14]], shape=(1, 3), dtype=int64)

这篇关于窗口多维 Tensorflow 数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆