在 TensorFlow.js 中切片 3d 张量

Slice a 3d tensor in TensorFlow.js

如何将以下 TensorFlow Python 代码翻译成 TensorFlow.js?

New_tensor = A_3D_tensor[:, :seq_len, :]

您可以使用 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]]]"
*/