Slicing a tensor by using indices in Tensorflow

26,839

Solution 1

Indeed, TensorFlow now has better support for slicing, so you can use the exact same syntax as NumPy:

result = noise_spec[:rows, :cols]

Solution 2

found out, it's

tf.slice(noise_spec, [0,0],[rows, cols])
Share:
26,839
Qubix
Author by

Qubix

Updated on July 09, 2022

Comments

  • Qubix
    Qubix almost 2 years

    Basically I have a 2d array and I want to do this nice numpy-like thing

    noise_spec[:rows,:cols]
    

    in Tensorflow. Here rows and cols are just two integers.

  • sachinruk
    sachinruk almost 6 years
    unfortunately u = U[idx_i,:] doesn't work on tf 1.8.0. idx_i are random indices of rows I wanted to choose. tf.nn.embedding_lookup worked.