# Obtain arbitrary channel in tf.Variable

**URL:** <https://discuss.ai.google.dev/t/obtain-arbitrary-channel-in-tf-variable/26135>\
**Category:** General Discussion\
**Tags:** tfvariable, help\_request\
**Created:** [March 16, 2022, 9:34pm UTC](https://discuss.ai.google.dev/t/obtain-arbitrary-channel-in-tf-variable/26135 "2022-03-16T21:34:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawn\_Zhuang](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/shawn_zhuang/32/9326_2.png) [@Shawn\_Zhuang](https://discuss.ai.google.dev/u/Shawn_Zhuang)\
**Post date:** [March 16, 2022, 9:34pm UTC](https://discuss.ai.google.dev/t/obtain-arbitrary-channel-in-tf-variable/26135/1 "2022-03-16T21:34:19Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![Kiran\_Sai\_Ramineni](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/kiran_sai_ramineni/32/8859_2.png) [@Kiran\_Sai\_Ramineni](https://discuss.ai.google.dev/u/Kiran_Sai_Ramineni)\
**Post date:** [July 25, 2024, 6:54am UTC](https://discuss.ai.google.dev/t/obtain-arbitrary-channel-in-tf-variable/26135/3 "2024-07-25T06:54:18Z")

</div>

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,

```auto
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

```auto
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](https://gist.github.com/kiransair/47fa06bc1d32d0b549ede63ec39d8ab7) for working code example. Thank You.
