Consider the following tensor
JavaScript
x
7
1
params = tf.constant([
2
1.3, 7, 1, 0.5, -2,
3
3, -0.033, 0.9, -6.3, 4.1,
4
9, 5, 0.25, -6, 0.2])
5
6
params
7
The output of the above tensor is
JavaScript
1
5
1
<tf.Tensor: shape=(15,), dtype=float32, numpy=
2
array([ 1.3 , 7. , 1. , 0.5 , -2. , 3. , -0.033, 0.9 ,
3
-6.3 , 4.1 , 9. , 5. , 0.25 , -6. , 0.2 ],
4
dtype=float32)>
5
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
JavaScript
1
5
1
<tf.Tensor: shape=(8,), dtype=float32, numpy=
2
array([ 7. , 1. , 0.5 , 0.9 ,
3
-6.3 , 4.1 , 9. , 5.],
4
dtype=float32)>
5
Can it be done? Thanks in advance
Advertisement
Answer
Sure, have a look at tensor slicing. In your case it would be:
JavaScript
1
11
11
1
import tensorflow as tf
2
3
params = tf.constant([
4
1.3, 7, 1, 0.5, -2,
5
3, -0.033, 0.9, -6.3, 4.1,
6
9, 5, 0.25, -6, 0.2])
7
8
out = tf.concat([params[1:4], params[7:12]], 0)
9
10
print(out)
11
Output:
JavaScript
1
2
1
tf.Tensor([ 7. 1. 0.5 0.9 -6.3 4.1 9. 5. ], shape=(8,), dtype=float32)
2