How can I access tenor elements in tensorflow Tensor
using tensor indices as follows:
import tensorflow as tf
import numpy as np
# indexing in numpy [Working]
matrix = np.random.randint(0, 10, [100, 100])
indices = np.random.randint(0, 100, [1000, 100])
elements = matrix[indices[:, 0], indices[:, 1]]
# indexing in tensorflow [Not working]
tf_matrix = tf.constant(matrix, dtype=tf.int32)
tf_indices = tf.constant(indices, dtype=tf.int32)
tf_elements = tf_matrix[tf_indices[:, 0], tf_indices[:, 1]] # Error
session = tf.Session()
session.run(tf_elements)
I get these errors:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice_2' (op: 'StridedSlice') with input shapes: [100,100], [2,1000], [2,1000], [2].
ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_2' (op: 'StridedSlice') with input shapes: [100,100], [2,1000], [2,1000], [2].
tf_elements = tf.gather_nd(tf_matrix, tf_indices[:, 0:2])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.