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])
Here is a relatively straight forward approach using your example:
def overlapping_blocker(tensor,block_size,stride):
blocks = []
n = tensor.get_shape().as_list()[0]
ilo = range(0, n, stride)
ihi = range(block_size, n+1, stride)
ilohi = zip(ilo, ihi).
for ilo, ihi in ilohi:
blocks.append(tensor[ilo:ihi])
return(tf.pack(blocks, 0))
with tf.Session() as sess:
tensor = tf.constant([1., 2., 3., 4., 5., 6., 7.])
block_tensor = overlapping_blocker(tensor, 3, 2)
print(sess.run(block_tensor))
Output:
[[ 1. 2. 3.]
[ 3. 4. 5.]
[ 5. 6. 7.]]