I have a 1D tensor that I wish to partition into overlapping blocks. I\'m thinking of something like:
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7])
You can achieve the same using tf.extract_image_patches
.
tensor = tf.placeholder(tf.int32, [None])
def overlapping_blocker(tensor,block_size=3,stride=2):
return tf.squeeze(tf.extract_image_patches(tensor[None,...,None, None], ksizes=[1, block_size, 1, 1], strides=[1, stride, 1, 1], rates=[1, 1, 1, 1], padding='VALID'))
result = overlapping_blocker(tensor,block_size=3,stride=2)
sess = tf.InteractiveSession()
print(result.eval({tensor:np.array([1, 2, 3, 4, 5, 6, 7], np.int32)}))
#[[1 2 3]
#[3 4 5]
#[5 6 7]]