用于框架滑动的张量流变换(numpy stride_tricks)

Pet*_*mit 5 reshape tensorflow

我在张量流图中的输入是一个包含多个重叠窗口的向量。如何仅使用张量流操作创建这个数组?

input = [1,2,3,4,5,6,7,8]
shift = 2
window_width = 4
count = (len(input) - window_width) // 2 + 1 = 3

output = [[1,2,3,4],
          [3,4,5,6],
          [5,6,7,8]]
Run Code Online (Sandbox Code Playgroud)

在 numpy 中,我会使用 stride_tricks,但类似的东西在张量流中不可用。我应该如何处理这个问题?

kev*_*man 4

TensorFlow 没有 stride_tricks。以下内容适用于您的特定用例。

>>> b=tf.concat(0, [tf.reshape(input[i:i+4], [1, window_width]) for i in range(0, len(input) - window_width + 1, shift)])
>>> with tf.Session(""): b.eval()
... 
array([[1, 2, 3, 4],
       [3, 4, 5, 6],
       [5, 6, 7, 8]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

如果您的输入很大,您可能还需要查看slice_input_ Producer