tf.gather with tf.Session() as sess:
# python Python 2.7.12 (default, Oct 8 2019, 14:14:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> import tensorflow as tf >>> a = np.arange(12).reshape(2,2,3) >>> a array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]]) >>> b = tf.constant([2, 1, 0]) >>> b <tf.Tensor 'Const:0' shape=(3,) dtype=int32> >>> a = tf.gather(a, b , axis=1) >>> a <tf.Tensor 'GatherV2:0' shape=(2, 3, 3) dtype=int64> >>> with tf.Session() as sess: ... print sess.run(a) ... ... 2020-06-16 09:54:51.947755: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFl ow binary was not compiled to use: AVX2 FMA 2020-06-16 09:54:52.243377: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had n egative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-06-16 09:54:52.247393: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x5f3a2a0 executing computations on plat form CUDA. Devices: 2020-06-16 09:54:52.247432: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): Tesla V100-SXM2-32GB-LS , Compute Capability 7.0 2020-06-16 09:54:52.250564: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2195105000 Hz 2020-06-16 09:54:52.259420: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x60f6f30 executing computations on plat form Host. Devices: 2020-06-16 09:54:52.259465: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined > 2020-06-16 09:54:52.261153: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: name: Tesla V100-SXM2-32GB-LS major: 7 minor: 0 memoryClockRate(GHz): 1.44 pciBusID: 0000:00:0c.0 totalMemory: 31.72GiB freeMemory: 31.27GiB 2020-06-16 09:54:52.261191: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0 2020-06-16 09:54:52.263624: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with streng th 1 edge matrix: 2020-06-16 09:54:52.263657: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0 2020-06-16 09:54:52.263668: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0: N 2020-06-16 09:54:52.265156: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/rep lica:0/task:0/device:GPU:0 with 30417 MB memory) -> physical GPU (device: 0, name: Tesla V100-SXM2-32GB-LS, pci bus id: 0000:00:0c .0, compute capability: 7.0) [[[ 0 0 0] [ 3 4 5] [ 0 1 2]] [[ 0 0 0] [ 9 10 11] [ 6 7 8]]]