{"id":5512,"date":"2020-10-12T16:01:07","date_gmt":"2020-10-12T10:31:07","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=5512"},"modified":"2024-06-07T13:26:48","modified_gmt":"2024-06-07T07:56:48","slug":"the-basics-of-tensorflow-understanding-tensors-graphs-sessions-and-operations","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/the-basics-of-tensorflow-understanding-tensors-graphs-sessions-and-operations\/","title":{"rendered":"The Basics of TensorFlow: Understanding Tensors, Graphs, Sessions, and Operations"},"content":{"rendered":"\n<p>When it comes to building AI systems, TensorFlow has become ubiquitous. It has grown to become the library of choice for most machine learning and deep learning projects.&nbsp; Researchers, programmers, and data scientists even predict that the library will gain even more traction in the coming years.&nbsp;<\/p>\n\n\n\n<p>But truth be told, TensorFlow can get complex when you attempt to go beyond ready-made examples and try out things from scratch. Without using APIs, applying the key concepts and methods of TensorFlow to build models can be slightly advanced. Without a doubt, you must have a firm understanding of the basics of TensorFlow before you can begin to build models with fresh ideas.&nbsp;<\/p>\n\n\n\n<p>&nbsp;And that\u2019s why in this tutorial, we shall explain the very basics of TensorFlow. We shall go bare to explain when Tensors are and how to navigate the TensorFlow library to do the basic stuff. By the end of this tutorial, you will learn:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>What is a tensor\u00a0<\/li>\n\n\n\n<li>How tensors are represented\u00a0<\/li>\n\n\n\n<li>The components of s TensorFlow program<\/li>\n\n\n\n<li>Creating tensors in TensorFlow<\/li>\n\n\n\n<li>Creating special tensors\u00a0<\/li>\n\n\n\n<li>The shape and datatype of a tensor\u00a0<\/li>\n\n\n\n<li>How to change the shape of a tensor\u00a0<\/li>\n\n\n\n<li>How to change the datatype of a tensor<\/li>\n\n\n\n<li>Running operations in TensorFlow with tf.Session()<\/li>\n\n\n\n<li>Mathematical operations in TensorFlow<\/li>\n<\/ol>\n\n\n\n<p>Sure, you\u2019re excited to get on this journey. Let\u2019s get started.&nbsp;<\/p>\n\n\n\n<p>We\u2019ll begin by understanding what Tensors are.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a Tensor?<\/strong><\/h2>\n\n\n\n<p>We start with this because the name TensorFlow is derived from the word tensor. A tensor is simply a matrix of any given dimension which represents all forms of data. A tensor can be defined by its shape, which is the number of sides in the tensor. The shape of a tensor is called its dimensionality.&nbsp;<\/p>\n\n\n\n<p>Putting it differently, we can define a tensor as a multidimensional array. A tensor could be zero-dimensional (0-D) as we have in scalar values. It could be one-dimension (1-D) as in a line or vector, it could be two-dimensional (2-D) as in a matrix, it could be three-dimensional (3-D) as we have in the color in images, and so on.&nbsp;<\/p>\n\n\n\n<p>The TensorFlow library represents all data structures as tensors. In other words, whatever form of data you feed into the TensorFlow library is represented as a tensor. The library is called TensorFlow is what really happens in the flowing of tensors in a computational graph. A computational graph is a representation that indicates how the computation takes place. It shows how every operation (called the op node) is linked to each other. We will get to discuss graphs in more detail momentarily.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How are Tensors Represented<\/h2>\n\n\n\n<p>Tensors are represented in TensorFlow as a collection of arrays in a given dimension. Say, we have a 4 by 2 matrix given below. <img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/reUY-mmjMQ2PHXjOHP50cATzL2LH1RWYAiuYQh-3_oGyNvIVDTg-0tTgVtxwqA2eDBxOTdXqtBJZKiAM3i2dXEK4uxZ1jTAUMeL9FkYVObIpdZwC12YWigPex5zzsfkHYlr18TesexivCZjWzQ\" width=\"12\" height=\"19\" alt=\"\" title=\"\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/reUY-mmjMQ2PHXjOHP50cATzL2LH1RWYAiuYQh-3_oGyNvIVDTg-0tTgVtxwqA2eDBxOTdXqtBJZKiAM3i2dXEK4uxZ1jTAUMeL9FkYVObIpdZwC12YWigPex5zzsfkHYlr18TesexivCZjWzQ\" width=\"12\" height=\"19\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/reUY-mmjMQ2PHXjOHP50cATzL2LH1RWYAiuYQh-3_oGyNvIVDTg-0tTgVtxwqA2eDBxOTdXqtBJZKiAM3i2dXEK4uxZ1jTAUMeL9FkYVObIpdZwC12YWigPex5zzsfkHYlr18TesexivCZjWzQ\" width=\"12\" height=\"19\" alt=\"\" title=\"\">1 2 3 4 5 6 7 8&nbsp;<\/p>\n\n\n\n<p>In TensorFlow, it is written as&nbsp;<\/p>\n\n\n\n<p>[[1, 2],<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;[3, 4],<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;[5, 6],<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;[7, 8]]<\/p>\n\n\n\n<p>If it were are 3 by 3 matrix which can be visualized in a vector space like this<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/iFyJf6HOxclkcZwYKrfNY9FOMiaKbqkgHKYeCbx7ZBmQnHjHGWG9rXWg_gQs_hZz-yn4LRDuWrnai8MmkKUon8WmL6hKIjFztljqWf165nwjBtFgqmqbObWZ8a53dbnlfRzRaEGM5CSrhet1Og\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>It is written in TensorFlow as&nbsp;<\/p>\n\n\n\n<p>[ [[1, 2],&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[3, 4],&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[5, 6],&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[7,8] ]<\/p>\n\n\n\n<p>Soon enough, we will discuss how to create and store these tensors using TensorFlow. Note that tensors with zero dimension (a scaler) or dimensions greater than 3 are difficult to visualize since we as humans work in a 3-dimensional space. TensorFlow can, however, work with tensors of higher dimensions.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Components of a TensorFlow Program<\/strong><\/h2>\n\n\n\n<p>A TensorFlow program can be split into 3 main components.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Computational Graph: The computational graph can be seen as where all mathematical operations are performed. A computational graph comprises nodes and edges. The nodes in a graph indicate where the operations on tensors are carried out to return new tensors.\u00a0<\/li>\n\n\n\n<li>Tensor: The data returned after each operation is called the tensors. As various operations progress, new tensors are outputted. The edges of a computational graph are the tensors.\u00a0<\/li>\n\n\n\n<li>Session: The session runs the defined operations from the computational graph. Inside a session, you can either print a tensor or run operations on an operation to return an output tensor.\u00a0<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s go get into coding with TensorFlow.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Tensors in TensorFlow<\/strong><\/h2>\n\n\n\n<p>As mentioned earlier, TensorFlow carries out all its computations using tensors. When defining a tensor in TensorFlow, 3 properties are fundamental.&nbsp;<\/p>\n\n\n\n<p>First, the name of the tensor (name). Second, the dimension (shape). And lastly, the data type (dtype). There are four types of tensors you can create with TensorFlow.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>tf.constant<\/li>\n\n\n\n<li>tf.Variable or tf.get_variable<\/li>\n\n\n\n<li>tf.placeholder<\/li>\n\n\n\n<li>tf.SparseTensor<\/li>\n<\/ol>\n\n\n\n<p>Before we go on to discuss how to create these tensors in TensorFlow, we will start by importing the library. You import TensorFlow with the following import statement.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#import TensorFlow library\nimport tensorflow as tf\n<\/code><\/pre>\n\n\n\n<p>\u2018tf\u2019 is called an alias. It is not compulsory to use an alias when working with <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-libraries-for-data-visualization\/\">python libraries<\/a>, but it is recommended. It is particularly useful when the library\u2019s name is a lengthy word. It can be replaced with shorter words, which makes your code cleaner. Note that you are not constrained to use \u2018tf\u2019 as \u2018tensorflow\u2019 alias but since it is widely used, we will stick to it throughout the course of this tutorial.&nbsp;<\/p>\n\n\n\n<p>If the import statement above gives a \u2018ModuleNotFound\u2019 error, it means that you do not have TensorFlow installed on your machine. You can refer to our previous tutorial where we discussed how to download and install TensorFlow on your machine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Tensors in TensorFlow with tf.constant<\/strong><\/h2>\n\n\n\n<p>You can create a constant tensor of n-dimension using the tf.constant() method. As mentioned earlier, the method takes in 3 key parameters, the value of the tensor, the data type, and the name of the tensor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tf.constant(\n&nbsp;&nbsp;&nbsp;&nbsp;value,\n&nbsp;&nbsp;&nbsp;&nbsp;dtype=None,\n&nbsp;&nbsp;&nbsp;&nbsp;shape=None,\n&nbsp;&nbsp;&nbsp;&nbsp;name='Const',\n&nbsp;&nbsp;&nbsp;&nbsp;verify_shape=False,\n)\nDocstring:\nCreates a constant tensor.\n\nThe resulting tensor <strong>is<\/strong> populated <strong>with<\/strong> values of type `dtype`, <strong>as<\/strong>\nspecified by arguments `value` <strong>and<\/strong> (optionally) `shape`&nbsp;<\/pre>\n\n\n\n<p>It is important to state the value of the tensor. The other parameters are given default values by TensorFlow. Let\u2019s say you want to create a one-dimensional tensor populated with numbers 1 to 10. You can write the following code.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#print a rank 1, one-dimensional tensor populated from number 1 to 10\ntf.constant(&#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=tf.float32)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'Const_2:0' shape=(10,) dtype=float32&gt;<\/code><\/pre>\n\n\n\n<p>Note: The output printed is not the tensor but the tensor object, with its properties. This is because the TensorFlow object has not been run yet. We will get to how to run a tensor using the tf.Session() class later in this tutorial.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s take another example. Say this time, you want to create a 3 by 3 matrix populated with a constant value, 2. We can as well use the tf.constant() method. The value argument will be given as 2 while the shape argument will be given as (3, 3). The code to do this is written below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#print a rank 1, 3 by 3 tensor populated with ones\ntf.constant(value=1, shape=(3, 3))<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'Const_3:0' shape=(3, 3) dtype=int32&gt;<\/code><\/pre>\n\n\n\n<p>Another way to go about this is to explicitly pass the matrix as the value argument. See this code below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tf.constant(&#91;&#91;1, 1, 1,],\n            &#91;1, 1, 1],\n            &#91;1, 1, 1]])<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'Const_4:0' shape=(3, 3) dtype=int32&gt;<\/code><\/pre>\n\n\n\n<p>Creating Tensors with String and Boolean Data Types<\/p>\n\n\n\n<p>We have been creating tensors of int and float data types. But you can also create tensors of string or Boolean dtype.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create TensorFlow with Boolean dtype\ntf.constant(&#91;False, True, True, False])<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'Const_7:0' shape=(4,) dtype=bool&gt;<\/code><\/pre>\n\n\n\n<p>As seen in the output, the tensor has a Boolean dtype.&nbsp;<\/p>\n\n\n\n<p>If we want to create a tensor with the string data type, it is as easy as writing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create TensorFlow with string dtype\ntf.constant(&#91;'Tensors'])<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'Const_8:0' shape=(1,) dtype=string&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Special Tensors<\/strong><\/h2>\n\n\n\n<p>Examples of special tensors are identity tensors or tensors filled with ones or zeros.&nbsp; Since this kind of tensor is identified by a particular format, TensorFlow has methods that can be used to create them easily. We are only required to specify the shape of the tensor we want to create.&nbsp;<\/p>\n\n\n\n<p>To create a tensor of ones, we use the tf.ones() method. It takes a compulsory parameter of the shape of the tensor to be created. To create a 1 by 10 tensor, populated with one, see the code below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create tensor populated with ones\ntf.ones(10)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'ones:0' shape=(10,) dtype=float32&gt;<\/code><\/pre>\n\n\n\n<p>If we want to create tensors with more rows and columns greater than 1, we can pass the number of rows and columns as a list. The code below creates a 3 by 3 tensor populated with ones.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a 3 by 3 tensor populated with ones\ntf.ones(&#91;3, 3])<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'ones_1:0' shape=(3, 3) dtype=float32&gt;<\/code><\/pre>\n\n\n\n<p>To create a tensor with all elements populated with zeros, we use the tf.zeros() method. Like the tf.ones() method, this method requires the shape of the tensor passed as an argument.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s see an example. To create a tensor with all elements set to zero, the code below does just that.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a tensor populated with zeros\ntf.zeros(10)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'zeros_1:0' shape=(10,) dtype=float32&gt;<\/code><\/pre>\n\n\n\n<p>Finally, let\u2019s discuss identity tensor. For those who do not know, an identity tensor is a square tensor whose diagonal elements are ones while the other elements are zeros. An important property of an identity tensor is that when an identity tensor is multiplied with a tensor, it returns the tensor itself.<\/p>\n\n\n\n<p>To create an identity tensor in TensorFlow, we use the tf.eye() method. Let\u2019s create a 4 by 4 identity tensor.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a 4 by 4 identity tensor\ntf.eye(4)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Tensor 'eye\/TensorDiag:0' shape=(4, 4) dtype=float32&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Shape and Datatype of a Tensor<\/strong><\/h2>\n\n\n\n<p>We mentioned that a tensor is defined by three properties: its value, shape, and datatype. When you create a tensor of some given value, the shape and datatype of the tensor are guessed by the TensorFlow algorithm.&nbsp;<\/p>\n\n\n\n<p>However, if you wish to print the shape of a tensor, you can do so with the shape attribute of the tensor. Let\u2019s take an example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a 2 by 3 tensor\ntensor_a = tf.constant(&#91;&#91;1, 2, 3],\n                       &#91;4, 5, 6]])\n#print the shape of the tensor\nprint(tensor_a.shape)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TensorShape(&#91;Dimension(2), Dimension(3)])<\/code><\/pre>\n\n\n\n<p>As seen in the output, the tensor has 2 rows and 3 columns.<\/p>\n\n\n\n<p>If you wish to print the datatype for a tensor, on the other hand. You can do this with the dtype attribute of the tensor. Let\u2019s see an example.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a 2 by 3 tensor\ntensor_a = tf.constant(&#91;&#91;1, 2, 3],\n                       &#91;4, 5, 6]])\n#print the datatype of the tensor\nprint(tensor_a.dtype)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dtype: 'int32'&gt;<\/code><\/pre>\n\n\n\n<p>As seen in the result, the datatype of the tensor is int32. But what if we wish to change the shape of data type of an already created tensor. How do we go about this?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Changing the Shape of a Tensor.&nbsp;<\/strong><\/h2>\n\n\n\n<p>There may be situations where you need to change the shape of a tensor. In Convolutional Neural Networks (CNN), you will be required to shape the shape of the input data (image array) to a different shape. To change the shape of a tensor, you use the tf.reshape() method. This method takes 2 important arguments: the tensor you want to reshape and the new shape you want for the tensor. See the example below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a 2 by 3 tensor\ntensor_a = tf.constant(&#91;&#91;1, 2, 3],\n                       &#91;4, 5, 6]])\n#reshape the above tensor into a 3 by 2 tensor\ntensor_a_reshaped = tf.reshape(tensor_a, &#91;3, 2])\n#print the shape of the reshaped tensor\nprint(tensor_a_reshaped.shape)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>(3, 2)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Changing the Datatype of a Tensor<\/strong><\/h2>\n\n\n\n<p>As seen from previous examples, upon creating a tensor, TensorFlow assigns a data type automatically. You can decide to change the datatype to another one using the tf.cast() method. The tf.cast() method takes two important arguments: the tensor to cast and the new dtype. Let\u2019s see an example.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a 2 by 3 tensor\ntensor_a = tf.constant(&#91;&#91;1, 2, 3],\n                       &#91;4, 5, 6]])\n#print datatype for the tensor\nprint('The dtype of the initial tensor is ', tensor_a.dtype)\n\n#change the datatype to float32\ntensor_a_changed_dtype = tf.cast(tensor_a, tf.float32)\n#print the datatype of the cast tensor\nprint('The dtype of the cast tensor is ', tensor_a_changed_dtype.dtype)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The dtype of the initial tensor is  &lt;dtype: 'int32'&gt;\nThe dtype of the cast tensor is  &lt;dtype: 'float32'&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Running Operations in TensorFlow with tf.Session()<\/strong><\/h2>\n\n\n\n<p>Before now, we have not printed the tensors created on the terminal. When we attempt to print the tensors, it returns its properties, i.e. its name, shape, and tensor. To print a tensor or the result of any tensor operation. You need to call the tf.Session() class. The class must first be instantiated. The tensor is printed on the terminal using the run() method of the instantiated class. In the last example, we defined a tensor_a. To print the tensor_a, we write the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a tensor_a\ntensor_a = tf.constant(&#91;&#91;1, 2, 3],\n                       &#91;4, 5, 6]])\n\n#call the Session class\nwith tf.Session() as sess:\n    result =sess.run(tensor_a)\n    #print the result\n    print(result)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;1 2 3] &#91;4 5 6]]<\/code><\/pre>\n\n\n\n<p>Now let\u2019s get back to other methods of creating Tensors<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Tensors Variables<\/strong><\/h2>\n\n\n\n<p>We have looked at creating tensors with tf.constant(). While it is great to use that method if the tensor value would not change, it is uncommon. To create a more dynamic tensor whose values can be used as input to another operation, we create a variable tensor.&nbsp;<\/p>\n\n\n\n<p>You can create a using the tf.Variable() class. Using this method, you must specify some initial value of the tensor of any shape and datatype.&nbsp;<\/p>\n\n\n\n<p>To run operations with the values of the created variable, you must explicitly assign the value of the tensor to the variable itself. This is done by initializing the variable. Creating a variable only store the tensor value to that variable. You can see the initialization operation as calling a variable from a saved file to be ready for use.&nbsp;<\/p>\n\n\n\n<p>To initialize the variable for an operation, the most common method to use is the global_variables_initializer() method. Note that the initializer method must be run in a session. Let\u2019s see an example where we implement this.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a tensor variable\ntensor_a = tf.Variable(&#91;1, 2, -4])\n\n#initialize the variable\ninit_op = tf.global_variables_initializer()\n\n#run a graph in session\nwith tf.Session() as sess:\n    #run the variable initializer in a session.\n    sess.run(init_op)\n    #run the tensor operation\n    result = sess.run(tensor_a)\n    #print the result\n    print(result)<\/code><\/pre>\n\n\n\n<p>You could also choose to create a variable by using the tf.get_variable() method.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tf.get_variable(name = \"\", values, dtype, initializer)\nargument\n- `name = \"\"`: Name of the variable\n- `values`: Dimension of the tensor\n- `dtype`: Type of data. Optional\n- `initializer`: How to initialize the tensor. Optional\nIf the initializer is specified, there is no need to include the `values` as the shape of `initializer` is used.\t\t<\/code><\/pre>\n\n\n\n<p>This method is preferable since it doesn\u2019t necessarily take an initial value and the initializer can be specified when calling the method. The tf.Variable() class always creates a new variable while the get_variable() method checks and gets existing variables with the same parameters. If there are no existing variables with the same parameters, it creates a new variable.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s say we want to create a 2 by 1 variable tensor using the tf.get_variable() method. When we call the method without defining the initializer, TensorFlow creates a tensor of random values with the defined shape.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#creates a 2 by 1 variable tensor\nvar1 = tf.get_variable('var1', &#91;2, 1])\n#print the tensor\nprint(var1)<\/code><\/pre>\n\n\n\n<p>Note: The name argument is the name stored by TensorFlow while the variable name is the name stored by the Python compiler. To avoid confusion, you may use the same names in both places.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;tf.Variable 'var1:0' shape=(2, 1) dtype=float32_ref&gt;<\/code><\/pre>\n\n\n\n<p>If we wish to specify the initial values of the tensor, we can do this with the initializer parameter. For instance, setting the initializer parameter to tf.zers_initializer fills the variable with zeros. You can also pass a constant tensor as the values of the initializer parameter. This will populate the variable tensor with the values of the constant tensor. Note that when the initializer parameter is defined, we do not need to pass the shape of the variable tensor as it takes the shape of the initializer tensor.<\/p>\n\n\n\n<p>&nbsp;Let\u2019s see an example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create 3 by 3 constant tensor\ntensor_a = tf.constant(&#91;1, 2, 3, 4, 5, 6, 7, 8, 9])\ntensor_a = tf.reshape(tensor_a, &#91;3, 3])\n\n#create a variable tensor whose first value is the constant tensor\nvar_tensor = tf.get_variable('var_tensor', initializer=tensor_a)\n#check the shape of the variable tensor\nvar_tensor.shape<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TensorShape(&#91;Dimension(3), Dimension(3)])<\/code><\/pre>\n\n\n\n<p>As seen, even without defining the shape of the variable tensor explicitly, it takes the shape of the constant tensor.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Tensors with Placeholders<\/strong><\/h2>\n\n\n\n<p>The third method of creating a tensor is by using placeholders. This is done with the tf.placeholder() method. Placeholders allow you to feed the data outside of your TensorFlow graph. In other words, the data in a placeholder is not given when creating the TensorFlow graph, but rather, when running the graph in a session.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Signature: tf.placeholder(dtype, shape=None, name=None)\narguments:\n- `dtype`: Type of data\n- `shape`: dimension of the placeholder. Optional. By default, shape of the data\n- `name`: Name of the placeholder. Optional\t\t\t\nReturns: A `Tensor` that may be used as a handle for feeding a value, but not evaluated directly.\n<\/code><\/pre>\n\n\n\n<p>The placeholder() method takes the data type of the data as an important parameter. The name and shape are optional parameters. When the name is not defined, TensorFlow assigns the tensor with a default name. The shape of the tensor can be determined when it is later defined in the session.&nbsp;<\/p>\n\n\n\n<p>When using placeholders, we have mentioned that the data is fed when running the session. Upon calling the session, we pass the data using the feed_dict argument. This is a dictionary whose key is the variable name of the placeholder and values, the data to be received as numbers of arrays.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s take an example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a placeholder tensor_a\ntensor_a = tf.placeholder(tf.float32)\n#create a tensor b\ntensor_b = 2 + tensor_a\n\n#run a session\nwith tf.Session() as sess:\n    #define tensor_a to be a 0-D tensor of 5\n    #run the tensoor_b\n    result = sess.run(tensor_b, feed_dict={tensor_a: 5})\n    #print the result\n    print(result)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>7.0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Mathematical Operations in TensorFlow<\/strong><\/h2>\n\n\n\n<p>TensorFlow can perform all the basic operations in mathematics such as addition, subtraction, multiplication, division, finding squares and square roots, exponents and so on. This is important in data manipulation. Let\u2019s list out some of the common basic operators and the required argument<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>tf.add(a, b): Returns a + b element-wise.<\/li>\n\n\n\n<li>tf.subtract(a, b): Returns a &#8211; b element-wise.<\/li>\n\n\n\n<li>tf.multiply(a, b): Returns a * b element-wise.<\/li>\n\n\n\n<li>tf.div(a, b): Divides a \/ b elementwise<\/li>\n\n\n\n<li>tf.pow(a, b): Computes the power of one value to another.<\/li>\n\n\n\n<li>tf.sqrt(a): Computes square root of \u2018a\u2019 element-wise.<\/li>\n\n\n\n<li>tf.exp(a): Computes exponential of \u2018a\u2019 element-wise.\u00a0 \\\\(b = ea\\\\).<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s take some examples<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a tensor a\ntensor_a = tf.constant(&#91;1, 2, 3])\n#create a tensor b\ntensor_b = tf.constant(&#91;-1, 4, 9])\n\n#perform element-wise addition on tensor a and b\ntensor_c = tf.add(tensor_a, tensor_b)\n#create a session\nwith tf.Session() as sess:\n    #run the session \n    result = sess.run(tensor_c)\n    #print the result\n    print(result)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>[ 0 6 12]<\/p>\n\n\n\n<p>It\u2019s almost the same procedure for other operations. If we want to find the exponential of a tensor, for instance, the code below is an example of how to accomplish that.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#create a tensor a\ntensor_a = tf.constant(&#91;1, 2, 3], dtype=tf.float64)\n\n#perform element-wise addition on tensor a and b\ntensor_b = tf.exp(tensor_a)\n#create a session\nwith tf.Session() as sess:\n    #run the session \n    result = sess.run(tensor_b)\n    #print the result\n    print(result)<\/code><\/pre>\n\n\n\n<p>Note: Since exponents are in decimals, the datatype cannot be set to the default int32 or any int datatype.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 2.71828183 7.3890561 20.08553692]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>To sum it all up, we started by defining what a tensor is and how they are represented in TensorFlow.&nbsp;<\/p>\n\n\n\n<p>We said that a TensorFlow program can be split into two parts, the computational graph, tensor, and session. We went on to explain the different types of ways to define a tensor with TensorFlow.&nbsp;<\/p>\n\n\n\n<p>We said that tf.constant() creates a tensor whose values remain the same. tf.variable() creates a tensor such that its values can be updated after an operation and&nbsp; tf.placeholders() allows you to withdraw the values and even shape of a tensor until you&#8217;re ready to run a session.&nbsp;<\/p>\n\n\n\n<p>We discussed the shape and datatype of tensors and how to change them using the tf.reshape() method and tf.cast() method respectively.&nbsp;<\/p>\n\n\n\n<p>We finally tied it all together by creating a computational graph (initial tensors) and running a session (based on some operation) to produce output results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to building AI systems, TensorFlow has become ubiquitous. It has grown to become the library of choice for most machine learning and deep learning projects.&nbsp; Researchers, programmers, and data scientists even predict that the library will gain even more traction in the coming years.&nbsp; But truth be told, TensorFlow can get complex [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5580,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-5512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=5512"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5512\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/5580"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=5512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=5512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=5512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}