Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading