Get Tensor Values By Indices in Tensorflow – TensorFlow Tutorial

By | March 17, 2021

Can we get values of a tensor by indices in tensorflow? It means can we operate tensorflow tensor like python list. In this tutorial, we will discuss this topic.

Python List

We can read, write and slice elements in a python list easily, here is an example:

>>> lx = [1, 2, 3, 4, 5]
>>> lx[2] # read by index
3
>>> lx[2:] #slice
[3, 4, 5]
>>> lx[2]= 6 #write a new value
>>> lx
[1, 2, 6, 4, 5]
>>>

Can we read, write and slice elements in a tensor in tensorflow?

TensorFlow tensor operation

We will use some examples to show you this answer in tensorflow 1.10 and python 3.6.

You can check your tensorflow version.

Print TensorFlow Version for Beginners – TensorFlow Tutorial

Read value by index

Look at this example:

import numpy as np
import tensorflow as tf
a = np.array(range(50))
aa = tf.convert_to_tensor(a, tf.float32)
aa = tf.reshape(aa, [5,10])

a1 = aa[2]
a2 = aa[2][2]
print(a1)
print(a2)

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    np.set_printoptions(precision=4, suppress=True)
    print(sess.run([a1, a2])

We have created a tensor with the shape 5 * 10, we will read two values form the indices are [2] and [2][2].

Run this code, we will get the result:

Tensor("strided_slice:0", shape=(10,), dtype=float32)
Tensor("strided_slice_2:0", shape=(), dtype=float32)
array([20., 21., 22., 23., 24., 25., 26., 27., 28., 29.], dtype=float32), 22.0

We can find we can read values from a tensor by its index. The data type is tensor strided_slice.

We also know that tf.nn.embedding_lookup() also allows us to read values from a tensor by its id. Here is the tutorial:

Understand tf.nn.embedding_lookup(): Pick Up Elements by Ids – TensorFlow Tutorial

We will compare them.

a4 = tf.nn.embedding_lookup(aa, 2)
print(a4)

Run this code, we will get the ouput.

Tensor("embedding_lookup/Identity:0", shape=(10,), dtype=float32)
array([20., 21., 22., 23., 24., 25., 26., 27., 28., 29.], dtype=float32)

The value of \(a4\) is the same to \(a1\), however, the data type of them are different.

Slice a tensor by index

We can use index to slice a python list, can we slice a tensor by its index? Look at this example:

a3 = aa[1:]
print(a3)

Run this code, we will get the result:

Tensor("strided_slice_3:0", shape=(4, 10), dtype=float32)
array([[10., 11., 12., 13., 14., 15., 16., 17., 18., 19.],
       [20., 21., 22., 23., 24., 25., 26., 27., 28., 29.],
       [30., 31., 32., 33., 34., 35., 36., 37., 38., 39.],
       [40., 41., 42., 43., 44., 45., 46., 47., 48., 49.]], dtype=float32)

We can find: we can use tensor index to slice a tensor and we will get a tensorflow strided_slice object.

Meanwhile, we also can use tf.split() to split a tensor. Here is the tutorial:

TensorFlow tf.split(): Splits a Tensor into Sub Tensors – TensorFlow Tutorial

Write tensorflow by index

Can we update value in a tensor by its index? We will use an example to explain it.

Look at this example:

d = aa[3][2]
aa[2][2] = d

Run this code, we will get this error:

aa[2][2] = d
TypeError: ‘Tensor’ object does not support item assignment

It means we can not update the tensor value by its index.

Leave a Reply