In this post, we are going to see some TensorFlow examples and see how it’s easy to define tensors, perform math operations using tensors, and other machine learning examples.

Table of Contents

## What is TensorFlow?

TensorFlow is a library which was developed by Google for solving complicated mathematical problems which takes much time.

Actually, TensorFlow can do many things like:

- Solving complex mathematical expressions.
- Machine learning techniques, where you give it a sample of data for training, then you give another sample of data to predict the result based on the training data. This is the artificial intelligence!!
- GPU support. You can use GPU (Graphical Processing Unit) instead of CPU for faster processing. There are two versions of TensorFlow, CPU version and GPU version.

Before we start working with TensorFlow examples, we need to know some basics.

## What is a Tensor?

The tensor is the main blocks of data that TensorFlow uses, it’s like the variables that TensorFlow uses to work with data. Each tensor has a dimension and a type.

The dimension is the rows and columns of the tensor, you can define one-dimensional tensor, two-dimensional tensor, and three-dimensional tensor as we will see later.

The type is the data type for the elements of the tensor.

## Define one-dimensional Tensor

To define a tensor, we will create a NumPy array or a Python list and convert it to a tensor using the tf_convert_to_tensor function.

We will use NumPy to create an array like this:

1 2 3 | import numpy as np arr = np.array([1, 5.5, 3, 15, 20]) |

You can see from the results the dimension and shape of the array.

1 2 3 4 5 6 7 8 9 10 11 | import numpy as np arr = np.array([1, 5.5, 3, 15, 20]) print(arr) print (arr.ndim) print (arr.shape) print (arr.dtype) |

It looks like the Python list but here there is no comma between the items.

Now we will convert this array to a tensor using tf_convert_to_tensor function.

1 2 3 4 5 6 7 8 9 | import numpy as np import tensorflow as tf arr = np.array([1, 5.5, 3, 15, 20]) tensor = tf.convert_to_tensor(arr,tf.float64) print(tensor) |

From the results, you can see the tensor definition, but you can’t see the tensor elements.

Well, to see the tensor elements, you can run a session like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np import tensorflow as tf arr = np.array([1, 5.5, 3, 15, 20]) tensor = tf.convert_to_tensor(arr,tf.float64) sess = tf.Session() print(sess.run(tensor)) print(sess.run(tensor[1])) |

## Define Two-dimensional Tensor

The same way as the one-dimensional array, but this time we will define the array like this:

arr = np.array([(1, 5.5, 3, 15, 20),(10, 20, 30, 40, 50), (60, 70, 80, 90, 100)])

And you can convert it to a tensor like this:

1 2 3 4 5 6 7 8 9 10 11 | import numpy as np import tensorflow as tf arr = np.array([(1, 5.5, 3, 15, 20),(10, 20, 30, 40, 50), (60, 70, 80, 90, 100)]) tensor = tf.convert_to_tensor(arr) sess = tf.Session() print(sess.run(tensor)) |

Now you know how to define tensors, what about performing some math operations between them?

## Performing Math on Tensors

Suppose that we have 2 arrays like this:

1 2 3 | arr1 = np.array([(1,2,3),(4,5,6)]) arr2 = np.array([(7,8,9),(10,11,12)]) |

We need to get the sum of them. You can perform many math operations using TensorFlow.

You can use the add function like this:

arr3 = tf.add(arr1,arr2)

So the whole code will be like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import numpy as np import tensorflow as tf arr1 = np.array([(1,2,3),(4,5,6)]) arr2 = np.array([(7,8,9),(10,11,12)]) arr3 = tf.add(arr1,arr2) sess = tf.Session() tensor = sess.run(arr3) print(tensor) |

You can multiply arrays like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import numpy as np import tensorflow as tf arr1 = np.array([(1,2,3),(4,5,6)]) arr2 = np.array([(7,8,9),(10,11,12)]) arr3 = tf.multiply(arr1,arr2) sess = tf.Session() tensor = sess.run(arr3) print(tensor) |

Now you got the idea.

## Three-dimensional Tensor

We saw how to work with one and two-dimensional tensors, now we will see the three-dimensional tensors, but this time we won’t use numbers, we will use an RGB image where each piece of the image is specified by x, y, and z coordinates.

These coordinates are the width, height, and color depth.

First, let’s import the image using matplotlib. You can install matplotlib using pip if it’s not installed on your system.

Now, put your file in the same directory with your Python file and import the image using matplotlib like this:

1 2 3 4 5 6 7 8 9 | import matplotlib.image as img myfile = "likegeeks.png" myimage = img.imread(myfile) print(myimage.ndim) print(myimage.shape) |

As you can see, it’s a three-dimensional image where the width is 150 and the height is 150 and the color depth is 3.

You can view the image like this:

1 2 3 4 5 6 7 8 9 10 11 | import matplotlib.image as img import matplotlib.pyplot as plot myfile = "likegeeks.png" myimage = img.imread(myfile) plot.imshow(myimage) plot.show() |

Cool!!

What about manipulating the image using TensorFlow? Super easy.

## Crop Or Slice Image Using TensorFlow

First, we put the values on a placeholder like this:

myimage = tf.placeholder("int32",[None,None,3])

To slice the image, we will use the slice operator like this:

cropped = tf.slice(myimage,[10,0,0],[16,-1,-1])

Finally, run the session:

result = sess.run(cropped, feed_dict={slice: myimage})

Then you can see the result image using matplotlib.

So the whole code will be like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import tensorflow as tf import matplotlib.image as img import matplotlib.pyplot as plot myfile = "likegeeks.png" myimage = img.imread(myfile) slice = tf.placeholder("int32",[None,None,3]) cropped = tf.slice(myimage,[10,0,0],[16,-1,-1]) sess = tf.Session() result = sess.run(cropped, feed_dict={slice: myimage}) plot.imshow(result) plot.show() |

Awesome!!

## Transpose Images using TensorFlow

In this TensorFlow example, we will do a simple transformation using TensorFlow.

First, specify the input image and initialize TensorFlow variables:

1 2 3 4 5 6 7 | myfile = "likegeeks.png" myimage = img.imread(myfile) image = tf.Variable(myimage,name='image') vars = tf.global_variables_initializer() |

Then we will use the transpose function which flips the 0 and 1 axes of the input grid:

1 2 3 4 5 6 7 | sess = tf.Session() flipped = tf.transpose(image, perm=[1,0,2]) sess.run(vars) result=sess.run(flipped) |

Then you can show the resulting image using matplotlib.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import tensorflow as tf import matplotlib.image as img import matplotlib.pyplot as plot myfile = "likegeeks.png" myimage = img.imread(myfile) image = tf.Variable(myimage,name='image') vars = tf.global_variables_initializer() sess = tf.Session() flipped = tf.transpose(image, perm=[1,0,2]) sess.run(vars) result=sess.run(flipped) plot.imshow(result) plot.show() |

All these TensorFlow examples show you how easy it’s to work with TensorFlow.

I hope you find the post useful. Keep coming back.

Thank you, what you writen is very useful to me.

I am very new to tensorflow, and I am finding ways to convert my images to tensors,

and I have a label file(.csv), it contain 4 columns of features(float32),

Could you explain more about how to import them into tensorflow data structure and use them to train model please? thank you very much!!!

Thanks for your interest.

To read from CSV file, you can use TextLineReader like this:

files = tf.train.string_input_producer([“file0.csv”, “file1.csv”])

reader = tf.TextLineReader()

key, value = reader.read(files)

Then you can set your default data to your 4 columns like this:

record_defaults = [[1], [1], [1], [1]]

col1, col2, col3, col4 = tf.decode_csv(value, record_defaults=record_defaults)

features = tf.stack([col1, col2, col3, col4])

And finally, you can train your model:

with tf.Session() as sess:

coord = tf.train.Coordinator()

threads = tf.train.start_queue_runners(coord=coord)

I think this should be OK. Hope you find your missing puzzle.

Regards,

Thank you for your reply.

I have try this way, but there are still some questions,

1.This step just read data into structure, why should we us

coord = tf.train.Coordinator()

threads = tf.train.start_queue_runners(coord=coord)

what are these two lines mean?

2.While I try to print(features), It’s only a 1-D (4,) array,

but it should be a 2-D (1440, 4) array, because my two files are all contains 720 lines,

and 4 columns of float data each row.

Or it should be a 1-D array used to training?

3.Sometimes this code read “file0.csv” first, and sometimes “file1.csv” first,

but I need to read files in sequence, how should I do?

Tjank you very much, again.

Regards,

Try the code according to your needs.

Regarding the sequence, you can try to turn the shuffle OFF like this:

tf.train.string_input_producer([“file0.csv”, “file1.csv”], shuffle=False)

Hope that helps.