Tensorflow: Slicing a Tensor into overlapping blocks

前端 未结 6 1450
我在风中等你
我在风中等你 2021-01-02 06:26

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])



        
6条回答
  •  一向
    一向 (楼主)
    2021-01-02 07:28

    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]]
    

提交回复
热议问题