Slice a 3d tensor in TensorFlow.js

Viewed 519

How to translate the following TensorFlow Python codes into TensorFlow.js?

New_tensor = A_3D_tensor[:, :seq_len, :]
1 Answers

You can do that with the slice method (or, for more complex cases, with stridedSlice):

const t = tf.range(0, 36, 1).as3D(2, 6, 3);
const seq_len = 4;
const t_sliced = t.slice([0, 0, 0], [-1, seq_len, -1]);
t_sliced.print();
/*
"Tensor
    [[[0 , 1 , 2 ],
      [3 , 4 , 5 ],
      [6 , 7 , 8 ],
      [9 , 10, 11]],

     [[18, 19, 20],
      [21, 22, 23],
      [24, 25, 26],
      [27, 28, 29]]]"
*/
Related