Strain Gauge with Tensor

I am trying to use the "advanced", numpy style that was added to this PR , however I am facing the same problem as the user here :

ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_15' (op: 'StridedSlice') with input shapes: [3,2], [1,2], [1,2], [1].

      

Namely, I would like to do the equivalent of this numpy operation (works in numpy):

A = np.array([[1,2],[3,4],[5,6]]) 
id_rows = np.array([0,2])
A[id_rows]

      

however this doesn't work in TF for the error above:

A = tf.constant([[1,2],[3,4],[5,6]])
id_rows = tf.constant([0,2])
A[id_rows]

      

+3


source to share


2 answers


you are looking for something like this:



A = tf.constant([[1,2],[3,4],[5,6]])
id_rows = tf.constant([[0],[2]]) #Notice the brackets
out = tf.gather_nd(A,id_rows)

      

0


source


You can slice the tensor as shown below.



A = tf.constant([[1,2],[3,4],[5,6]])
id_rows = tf.constant(np.array([0, 2]).reshape(-1, 1))
out = tf.gather_nd(A,id_rows)
with tf.Session() as session: 
    print(session.run(out))

      

0


source







All Articles