How to cut off a part of a tensor?

I want to cut [3.0, 33.0]. I tried to access this snippet by following the code. I misunderstand the tf.slice command. I am not that clear about the start and size mentioned in the document about this command. Can someone please understand it easily.

batch = tf.constant([
  [#First image
    [[0.0,10.0],[1.0,11.0]],
    [[3.0,33.0],[4.0,44.0]]
  ],
  [#Second image
    [[5.0,55.0],[6.0,66.0]],
    [[7.0,77.0],[8.0,88.0]]
  ]
])
slice1 = tf.slice(batch,[0,0,0,0], [0,0,1,0]) 
sess = tf.InteractiveSEssion()
sess.run(tf.initialize_all_variables())
print slice1.eval()

      

+3


source to share


1 answer


I will explain your code with examples, so I created a few cases, but first of all I will explain to you the tf.slice (input, start, size) parameters:

  • input

    is a tensor reference.
  • begin

    the index starts from the slice.
  • size

    - cutoff offset.

So, it tf.slice

works by selecting from a input

subtensor starting with an index begin

and ending with begin + size

, processing begin

and size

as index vectors. The example below will clarify the following:

batch = tf.constant([
        [#First image
            [
                [0.0,10.0],
                [1.0,11.0]
            ],
            [
                [3.0,33.0],
                [4.0,44.0]
            ]
        ],
        [#Second image
            [
                [5.0,55.0],
                [6.0,66.0]
            ],
            [
                [7.0,77.0],
                [8.0,88.0]
            ]
        ]
    ])
slice1 = tf.slice(batch,[0,0,0,0], [1,1,1,1]) 
slice2 = tf.slice(batch,[0,1,0,0], [1,1,2,2]) 
slice3 = tf.slice(batch,[1,1,1,0], [1,1,1,2]) 
slice4 = tf.slice(batch,[0,0,0,0], [2,2,2,2]) 
sess = tf.InteractiveSession()
print("slice1: \n" + str(slice1.eval()) + "\n")
print("slice2: \n" + str(slice2.eval()) + "\n")
print("slice3: \n" + str(slice3.eval()) + "\n")
print("slice4: \n" + str(slice4.eval()) + "\n")

      



Outputs in this case:

slice1: 
[[[[ 0.]]]]

slice2: 
[[[[  3.  33.]
   [  4.  44.]]]]

slice3: 
[[[[  8.  88.]]]]

slice4: 
[[[[  0.  10.]
   [  1.  11.]]

  [[  3.  33.]
   [  4.  44.]]]


 [[[  5.  55.]
   [  6.  66.]]

  [[  7.  77.]
   [  8.  88.]]]]

      

  • slice1

    selects the first element of the tensor because it starts with [0,0,0,0]

    and selects only one element.
  • slice2

    selects the first element of the tensor because it starts with [0,1,0,0]

    and selects 1 element in the first two dimensions and 2 in three and four dimensions.
  • slice3

    selects the first element of the tensor because it starts with [1,1,1,0]

    and selects only one element in the first three dimensions and 2 in the last.
  • slice4

    selects the entire tensor element because it starts with [0,0,0,0]

    and is two elements in size, so it covers all Tensor

Note that the number of dimensions is the same across all slides. If you only remove dimensions with one item, you can use tf.squeeze .

+3


source







All Articles