In this article, we’re going to look at some examples of TensorFlow, and see how easy it is to define tensors and do math with tensors, and I’ll do some other examples of machine learning.

What is TensorFlow?

TensorFlow is a library Google developed to solve complex math problems that take too long.

In fact, TensorFlow can do many things. Such as:

  • Solve complex mathematical expressions
  • Machine learning technology. You put in a sample of data for training, and then you give another sample of data to predict the outcome based on the training data. This is artificial intelligence!
  • Support the GPU. You can use a GPU (image processing unit) instead of a CPU for faster processing. TensorFlow is available in two versions: CPU version and GPU version.

Before you start writing examples, you need to know the basics.

What is a tensor?

Tensor tensor is the main block of data that TensorFlow uses, it’s like a variable, TensorFlow uses it to process data. Tensors have properties of dimension and type.

Dimensions refer to the number of rows and columns of a tensor, and as you will see later on, we can define one-dimensional tensors, two-dimensional tensors, and three-dimensional tensors.

Type refers to the data type of a tensor element.

Define a one-dimensional tensor

A tensor can be defined by creating a NumPy array. The NumPy system is an open source numerical extension of Python. It contains a powerful N-dimensional Array object, Array, for storing and manipulating large matrices) or a Python list, which you then use the tf_convert_to_tensor function to translate into tensors.

We can create an array using NumPy as follows:

Import numpy as NP arr = Np. array([1, 5.5, 3, 15, 20]) arr = Np. array([1, 5.5, 3, 15, 20])Copy the code

The results show the dimensions and shapes of the array.

Import numpy as NP arr = Np. array([1, 5.5, 3, 15, 20]) print(arr) print(arr.ndim) print(arr.shape) print(arr.dtype)Copy the code

It’s much like a Python list, except that there are no commas between elements.

Now we use the Tf_convert_to_tensor function to transform this array into tensors.

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)Copy the code

This run shows what tensors mean, but not the tensor elements.

To see the tensor element, run a session like this:

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]))Copy the code

Define a two-dimensional tensor

We define two-dimensional tensors in the same way we define one-dimensional tensors, but we define arrays like this:

Arr = np. Array ([(1, 5.5, 3, 15, 20), (10, 20, 30, 40, 50), (60, 70, 80, 90, 100)])Copy the code

This is then converted to a tensor:

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))Copy the code

Now you should know how to define tensors, so how do you do the math between tensors?

You do the math on tensors

Suppose we have the following two arrays:

Arr1 = np. Array ([(1, 2, 3), (4 and 6)]) arr2 = np, array ([,8,9 (7), the final three (10)])Copy the code

With TenserFlow, you can do a lot of math. Now we need to sum these two arrays.

Use the addition function to sum:

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)Copy the code

We can also multiply arrays:

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)Copy the code

Now you know.

The three dimensional tensor

Now that we know how to use one-dimensional and two-dimensional tensors, let’s look at three-dimensional tensors, but instead of using numbers, let’s use an RGB image. In this image, each pixel is represented by a combination of x, y and z.

These combinations form the image’s width, height, and color depth.

Start by importing an image using the Matplotlib library. If you don’t have Matplotlib on your system, you can install it using PIP.

Place the image in the same directory as the Python file, then import the image using Matplotlib:

import matplotlib.image as img
myfile = "likegeeks.png"
myimage = img.imread(myfile)
print(myimage.ndim)
print(myimage.shape)

Copy the code

From the results, you should see that the 3d image has a width of 150, a height of 150, and a color depth of 3.

You can also check out this image:

import matplotlib.image as img
import matplotlib.pyplot as plot
myfile = "likegeeks.png"
myimage = img.imread(myfile)
plot.imshow(myimage)
plot.show()

Copy the code

That’s cool!

So how do you use TensorFlow to process images? Super easy.

Use TensorFlow to generate or crop images

First, assign to a placeholder:

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

Copy the code

Crop the image using the crop operation:

Cropped = tf. Slice (myimage.png, [0, 10], [16, 1, 1])Copy the code

Finally, run the session:

result = sess.run(cropped, feed\_dict={slice: myimage})

Copy the code

You can then see the image processed using Matplotlib.

Here’s the whole code:

import tensorflow as tf import matplotlib.image as img import matplotlib.pyplot as plot myfile = "likegeeks.png" myimage Tf.placeholder ("int32",[None,None,3]) cropped = tf.placeholder(myimage,[10,0,0],[16,-1,-1]) sess  = tf.Session() result = sess.run(cropped, feed_dict={slice: myimage}) plot.imshow(result) plot.show()Copy the code

Isn’t that amazing?

Change the image using TensorFlow

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

First, specify the image to be processed and initialize the value of the TensorFlow variable:

myfile = "likegeeks.png"
myimage = img.imread(myfile)
image = tf.Variable(myimage,name='image')
vars = tf.global_variables_initializer()

Copy the code

It then calls the transpose function, which flips the 0 and 1 axes of the input grid.

Sess = tf.session () flipped = tf.transpose(image, perm=[1,0,2]) sess.run(vars) result=sess.run(flipped)Copy the code

Then you can see the image processed using Matplotlib.

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()Copy the code

The examples above all show you how easy it is to use TensorFlow.


Via: www.codementor.io/likegeeks/d…

LikeGeeks by GHSGZ And proofread by WXY

This article is originally compiled by LCTT and released in Linux China