How to split a tensor into different parts with Tensorflow?

I'm using Tensorflow 1 and want to know how I can split a tensor into different parts. I want something like griddy splitting.

 

I want to slice a feature map and concatenate all parts together by another axis.

tensorflowfeature-mapslice
4 votesLP197.00
2 Answers
NN160.00
3

Tensorflow has a slice function, which lets you split your tensor into a different part, by using the beginning and the size of the parts. 

tf.slice(input_tensor, begin, size)
# begin is in or array of ints
# size is int or array of ints
t = tf.constant([[[1, 1, 1], [2, 2, 2]],
                 [[3, 3, 3], [4, 4, 4]],
                 [[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 1, 3])  # [[[3, 3, 3]]]
tf.slice(t, [1, 0, 0], [1, 2, 3])  # [[[3, 3, 3],
                                   #   [4, 4, 4]]]
tf.slice(t, [1, 0, 0], [2, 1, 3])  # [[[3, 3, 3]],
                                   #  [[5, 5, 5]]]

Read this doc if you need more.

Reply
JO224.00
2

You can do numpy-like slicing. 

res = tensor[:, start:end, start:end, start:end]  # First channel is the batch

Remember it will create another op in your graph for every slicing action.

Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?