Removal of data /indices values(range) from a tensor tensorflow

Question:

Consider the following tensor

params = tf.constant([
    1.3, 7, 1, 0.5, -2,
    3, -0.033, 0.9, -6.3, 4.1,
    9, 5, 0.25, -6, 0.2])

params

The output of the above tensor is

<tf.Tensor: shape=(15,), dtype=float32, numpy=
array([ 1.3  ,  7.   ,  1.   ,  0.5  , -2.   ,  3.   , -0.033,  0.9  ,
       -6.3  ,  4.1  ,  9.   ,  5.   ,  0.25 , -6.   ,  0.2  ],
      dtype=float32)>

Now I want to remove, lets say the first value , i.e., 1.3, remove values from indices starting from 4 to 6 and from value 0.25 onwards [12:]
The output shall be

<tf.Tensor: shape=(8,), dtype=float32, numpy=
array([  7.   ,  1.   ,  0.5  ,   0.9  ,
       -6.3  ,  4.1  ,  9.   ,  5.],
      dtype=float32)> 

Can it be done?
Thanks in advance

Asked By: CA Khan

||

Answers:

Sure, have a look at tensor slicing. In your case it would be:

import tensorflow as tf

params = tf.constant([
    1.3, 7, 1, 0.5, -2,
    3, -0.033, 0.9, -6.3, 4.1,
    9, 5, 0.25, -6, 0.2])

out = tf.concat([params[1:4], params[7:12]], 0)

print(out)

Output:

tf.Tensor([ 7.   1.   0.5  0.9 -6.3  4.1  9.   5. ], shape=(8,), dtype=float32)
Answered By: ClaudiaR
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.