Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slicing a tensor by using indices in Tensorflow

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.

like image 951
Qubix Avatar asked Jan 18 '17 09:01

Qubix


People also ask

Can you slice tensors?

You can use tf. slice on higher dimensional tensors as well. You can also use tf. strided_slice to extract slices of tensors by 'striding' over the tensor dimensions.

What is a slice in TensorFlow?

Used in the notebooksThis operation extracts a slice of size size from a tensor input_ starting at the location specified by begin . The slice size is represented as a tensor shape, where size[i] is the number of elements of the 'i'th dimension of input_ that you want to slice.

How do you flatten a tensor in TensorFlow?

To flatten the tensor, we're going to use the TensorFlow reshape operation. So tf. reshape, we pass in our tensor currently represented by tf_initial_tensor_constant, and then the shape that we're going to give it is a -1 inside of a Python list.

What is ragged tensor?

Ragged tensors are the TensorFlow equivalent of nested variable-length lists. They make it easy to store and process data with non-uniform shapes, including: Variable-length features, such as the set of actors in a movie. Batches of variable-length sequential inputs, such as sentences or video clips.


2 Answers

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

result = noise_spec[:rows, :cols]
like image 180
mrry Avatar answered Oct 11 '22 10:10

mrry


found out, it's

tf.slice(noise_spec, [0,0],[rows, cols])
like image 7
Qubix Avatar answered Oct 11 '22 10:10

Qubix