Obtain arbitrary channel in tf.Variable

Hi,
suppose I have a tf.Variable, whose size is (3,3,16,32)
How can I obtain the parameter in the position of

(:, :, :, [1,2,3])

resulting in a tensor with size of (3,3,16,3)

Hi @Shawn_Zhuang, You can achieve this in 2 ways. one way is to get the array using .numpy() and can apply slicing. For example,

import tensorflow as tf
variable = tf.Variable(tf.random.normal([3, 3, 16, 32]))
sliced_array=variable.numpy()[:, :, :, [1, 2, 3]]
sliced_array.shape #output: (3, 3, 16, 3)

the other is using the tf.gather

sliced_variable = tf.gather(variable, indices=[1, 2, 3], axis=-1)
sliced_variable.shape #output:TensorShape([3, 3, 16, 3])

please refer to this gist for working code example. Thank You.