In this TensorFlow tutorial, I will show you how to iterate over tensors in TensorFlow.

While processing data, I created a custom function to normalize the data and fill in the missing values. I wanted to apply this function to each element of my dataset in tensor format.

For that, I needed to iterate over my dataset, so I used a **for-and-while loop **and appl**i**ed the custom function to each element. I successfully preprocessed the dataset.

In this tutorial, you will learn how to create a tensor and iterate over its values individually. Additionally, I have provided solutions to errors that may arise while iterating over the tensor.

## What does Iterate Over mean?

Iterate over is a term used in every programming language. It is the way to visit each element of a collection of items sequentially, one by one. The collection can be any iterable data structure, such as a **dictionary**, **set**, **list**, or **tensor.**

So here, you will learn how to iterate over Tensor. In TensorFlow, a tensor is a multidimensional array that can store different kinds of data.

Lets’ begin

### Iterate Over Tensor in TensorFlow using Python Loop

So you can iterate over tensors using the Python **For loop. **If you want to know how For loop works, visit the Python tutorial For loop vs while loop in Python, and read the For loop section.

To iterate over a tensor, first create a tensor using the **tf.constant()** function.

```
import tensorflow as tf
tensor_data = tf.constant(['USA', 'Canada', 'Brazil', 'Austrailia'])
```

The above code, **tensor_data**, is created and contains the list of string values.

Next, use the **For loop, **as shown in the code below, to iterate over **tensor_data** and print each string value.

```
for data in tensor_data:
print(data)
```

Look at the output. The For loop printed all the elements of tensor **tensor_data**. The code **‘for data in tensor_data’** in this line visits each element of** tenosr_data **one by one, represented by **data** in the for loop.

Next, let’s iterate over the tensor using a for loop.

### Iterate Over Tensor in TensorFlow using While Loop

In the above section, you have used the For loop to iterate over the tensor. Similarly, you can use the While loop of Python.

First, create a tensor using the below code.

`tensor_values = tf.constant([23, 56, 98, 55])`

Now, initialize the two variables **tensor_len** and **data** using the code below.

```
tensor_len = len(tensor_values)
data = 0
```

The above two variables, **tensor_len**, store the length of the **tensor_values** using the **len()** function and the data are initialized with a value of 0.

Use the while loop to iterate over the tensor, as shown below.

```
while data < tensor_len:
print(tensor_values[data])
data+=1
```

Look using the While loop; all the elements of the **tensor_values** are printed on the terminal.

First, the loop checks if the **data < tensor_len**, the initial value of data is **0**, and the **tensor_len** is 4.

then within the while loop, a statement **print(tensor_values[data])**, takes the each values from **tensor_value** variable and prints on the terminal.

Finally, the value variable data is incremented by one in each iteration using **data+=1**.

While iterating over a tensor, you might encounter errors such as **typeerror: cannot iterate over a scalar tensor**.

Here, the error indicates that you can’t iterate over a scalar value. A scalar value is a single value, so you can access one but can’t iterate on it. You can iterate over a collection of values, not on a single value.

Let me show you through code how that error can appear, so use the below code.

```
import tensorflow as tf
tensor=tf.constant(1)
for tensor in tensor:
print("Iterate tensor:",tensor)
```

When you execute the above code, the error shows that you have created a tensor using **tensor=tf.constant(1)**, which contains only a single value 1, so you can iterate on it.

The tensor variable must contain a list of values to iterate over the tensor; you can modify the code below.

```
import tensorflow as tf
tensor=tf.constant([1,7,2,8])
for tensor in tensor:
print("Iterate tensor:",tensor)
```

Look now; the error disappears. Here, a **tensor** is created that contains a list of values **[1,7,2,8]**, and then, using a for loop, each value of a tensor is printed.

You can also get one more error while iterating over a tensor: **‘iterating over a symbolic `tf.tensor` is not allowed.’**

To resolve that error, you can use the code below to examine how the Tensorflow function is used.

```
import tensorflow as tf
tens = tf.random.uniform(shape=(2,50, 60, 400))
result = tf.shape(tens)
tens = [tens[0][m][n] - tf.reduce_mean(tens[0][m][n]) for n in tf.range(result[2]) for m in tf.range(result[1])]
```

In the above code, we have imported the TensorFlow library and then used the **tf.random.uniform()** function. Within this function, the random shape is described. After that, the list comprehension method, the for loop and **tf.reduce_mean()** function is used.

Now, I hope you understand how to iterate over tensors using Python’s for and while loop.

## Conclusion

In this TensorFlow tutorial, you learned how to iterate over tensors. You have used the Python For and While loops to iterate over tensors.

Additionally, you learned how to fix the errors while iterating over tensors such as **typeerror: cannot iterate over a scalar tensor** and **‘iterating over a symbolic `tf.tensor` is not allowed**‘.

You may like to read:

- How to Convert Tensor to Numpy in TensorFlow
- Convert list to tensor TensorFlow
- Python TensorFlow one_hot

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.