{"id":2538,"date":"2022-04-19T07:41:17","date_gmt":"2022-04-19T07:41:17","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=2538"},"modified":"2022-04-19T07:41:17","modified_gmt":"2022-04-19T07:41:17","slug":"tensorflow-basics","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/04\/19\/tensorflow-basics\/","title":{"rendered":"TensorFlow Basics"},"content":{"rendered":"\n<p>TensorFlow is a machine learning framework and developed by&nbsp;<strong>Google Brain Team<\/strong>. It is derived from its core framework:&nbsp;<strong>Tensor<\/strong>. In TensorFlow, all the computations involve tensors. A tensor is a vector or a matrix of n-dimensions which represents the types of data. All the values in a TensorFlow identify data type with a known shape. The shape of the data is the dimension of the matrix or array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Representation of a Tensor<\/h2>\n\n\n\n<p>In&nbsp;<strong>TensorFlow<\/strong>, a tensor is the collection of feature vector (Like, array) of n-dimension. For instance, if we have any 2&#215;3 matrix with values 1 to 6, we write:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/tensorflow\/images\/tensorflow-basics.png\" alt=\"TensorFlow Basics\"\/><\/figure>\n\n\n\n<p>TensorFlow represents this matrix as:<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>[[1,&nbsp;3,&nbsp;5],&nbsp;&nbsp;<\/li><li class=\"\">[2,&nbsp;4,&nbsp;6]]&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>If we create any three-dimensional matrix with values 1 to 8, we have:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/tensorflow\/images\/tensorflow-basics2.png\" alt=\"TensorFlow Basics\"\/><\/figure>\n\n\n\n<p>TensorFlow represents this matrix as:<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>[&nbsp;[[1,&nbsp;2],&nbsp;&nbsp;<\/li><li class=\"\">[[3,&nbsp;4],&nbsp;&nbsp;<\/li><li>[[5,&nbsp;6],&nbsp;&nbsp;<\/li><li class=\"\">[[7,&nbsp;8]&nbsp;]&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Note:<\/strong>&nbsp;A tensor is represented with a scalar or can have a shape of more than three dimensions. It is just difficult to anticipate high dimension.<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Tensor<\/h3>\n\n\n\n<p>All computations pass through one or more Tensors in TensorFlow. A tensor is an object which has three properties which are as follows:<\/p>\n\n\n\n<ul><li>A unique label (name)<\/li><li>A dimension (shape)<\/li><li>A data type (dtype)<\/li><\/ul>\n\n\n\n<p>Each operation we will TensorFlow involves the manipulation of a tensor. There are four main tensors we can create:<\/p>\n\n\n\n<ul><li>tf.Variable<\/li><li>tf.constant<\/li><li>tf.placeholder<\/li><li>tf.SparseTensor<\/li><\/ul>\n\n\n\n<p>In the tutorial, we will learn how to create the&nbsp;<strong>tf.constant<\/strong>&nbsp;and a&nbsp;<strong>tf. Variable<\/strong>.<\/p>\n\n\n\n<p>Make sure that we activate the conda environment with TensorFlow. We named this environment&nbsp;<strong>hello-tf<\/strong>.<\/p>\n\n\n\n<p><strong>For Windows user:<\/strong><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>activate&nbsp;hello-tf&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>For macOS user:<\/strong><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>source&nbsp;activate&nbsp;hello-tf&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>After we have done that, we are ready to import tensorflow<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#Import&nbsp;tf&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;tensorflow&nbsp;as&nbsp;tf&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Create a tensor of n-dimension<\/h3>\n\n\n\n<p>We begin with the creation of a tensor with one dimension, namely a scalar.<\/p>\n\n\n\n<p><strong>To create a tensor, we can use tf.constant ()<\/strong><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>tf.constant(value,&nbsp;dtype,&nbsp;name&nbsp;=&nbsp;&#8220;&#8221;)&nbsp;&nbsp;<\/li><li class=\"\">arguments&nbsp;&nbsp;<\/li><li>`Value`:&nbsp;It&nbsp;is&nbsp;the&nbsp;Value&nbsp;of&nbsp;n&nbsp;dimension&nbsp;to&nbsp;define&nbsp;the&nbsp;tensor.&nbsp;And&nbsp;it&nbsp;is&nbsp;Optional.&nbsp;&nbsp;<\/li><li class=\"\">`dtype`:&nbsp;Define&nbsp;the&nbsp;type&nbsp;of&nbsp;data:&nbsp;&nbsp;&nbsp;<\/li><li>`tf.string`:&nbsp;String&nbsp;variable&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">`tf.float32`:&nbsp;Float&nbsp;variable&nbsp;&nbsp;&nbsp;<\/li><li>`tf.int16`:&nbsp;Integer&nbsp;variable&nbsp;&nbsp;<\/li><li class=\"\">&#8220;name&#8221;:&nbsp;Name&nbsp;of&nbsp;the&nbsp;tensor.&nbsp;Optional.&nbsp;By&nbsp;<strong>default<\/strong>,&nbsp;`Const_1:0`&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>To create a tensor of dimension 0, We have to run below code.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;rank&nbsp;0&nbsp;&nbsp;<\/li><li class=\"\">##&nbsp;Default&nbsp;name&nbsp;&nbsp;<\/li><li>r1=tf.constant&nbsp;(1,&nbsp;tf.int18)&nbsp;&nbsp;<\/li><li class=\"\">print&nbsp;(r1)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor (\"Const: 0\", shape= (), dtype=int18\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/tensorflow\/images\/tensorflow-basics3.png\" alt=\"TensorFlow Basics\"\/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Named&nbsp;my_scalar&nbsp;&nbsp;<\/li><li class=\"\">r2&nbsp;=&nbsp;tf.constant(1,&nbsp;tf.int18,&nbsp;name&nbsp;=&nbsp;&#8220;my_scalar&#8221;)&nbsp;&nbsp;&nbsp;<\/li><li>print(r2)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor (\"my_scalar:0\", shape=( ), dtype=int18\n<\/pre>\n\n\n\n<p>Each tensor is displayed by the name of tensor. Each tensor object is generated with<br>a unique label (name),<br>a dimension (shape)<br>a data type (dtype).<\/p>\n\n\n\n<p>We can define a tensor with decimal values or with a string to change the type of the data.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#Decimal&nbsp;data&nbsp;&nbsp;<\/li><li class=\"\">q1_decimal&nbsp;=&nbsp;tf.constant(1.12345,&nbsp;tf.float32)&nbsp;&nbsp;<\/li><li>print(q1_decimal)&nbsp;&nbsp;<\/li><li class=\"\">#String&nbsp;data&nbsp;&nbsp;<\/li><li>q1_string&nbsp;=&nbsp;tf.constant(&#8220;JavaTpoint&#8221;,&nbsp;tf.string)&nbsp;&nbsp;<\/li><li class=\"\">print(q1_string)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"Const_1:0\", shape=(), dtype=float32)\nTensor(\"Const_2:0\", shape=(), dtype=string)\n<\/pre>\n\n\n\n<p>A tensor of 1 dimension can be created as follows:<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;Rank&nbsp;1_vector&nbsp;=&nbsp;tf.constant([1,3,6],&nbsp;tf.int18)&nbsp;&nbsp;<\/li><li class=\"\">print(q1_vector)&nbsp;&nbsp;<\/li><li>q2_boolean&nbsp;=&nbsp;tf.constant([True,&nbsp;True,&nbsp;False],&nbsp;tf.bool)&nbsp;&nbsp;<\/li><li class=\"\">print(q2_boolean)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor (\"Const_5:0\", shape=(4), dtype=int18)\nTensor(\"Const_4:0\", shape=(4), dtype=bool)\n<\/pre>\n\n\n\n<p>We can notice the shape is only composed in 1 column.<\/p>\n\n\n\n<p>To create an array of 2 dimensions, we need to close the brackets after every row.<\/p>\n\n\n\n<p><strong>Example:<\/strong><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;Rank&nbsp;2&nbsp;&nbsp;<\/li><li class=\"\">q2_matrix&nbsp;=&nbsp;tf.constant([&nbsp;[1,&nbsp;2],&nbsp;&nbsp;<\/li><li>&nbsp;[3,&nbsp;4]&nbsp;],tf.int18)&nbsp;&nbsp;<\/li><li class=\"\">print(q2_matrix)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"Const_6:0\", shape=(2, 2), dtype=int18) \n<\/pre>\n\n\n\n<p>The matrix possess 2 rows and 2 columns are filled with value 1, 2, 3, 4.<\/p>\n\n\n\n<p>A matrix which has 3 dimensions is constructed by adding another level with brackets.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Rank&nbsp;3&nbsp;&nbsp;<\/li><li class=\"\">q3_matrix&nbsp;=&nbsp;tf.constant([&nbsp;[[1,&nbsp;2],[3,&nbsp;4],&nbsp;[5,&nbsp;6]]&nbsp;],&nbsp;tf.int18)&nbsp;&nbsp;&nbsp;<\/li><li>print(q3_matrix)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"Const_6:0\", shape=(1, 3, 2), dtype=int18)\n<\/pre>\n\n\n\n<p>The matrix looks like the below given picture.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/tensorflow\/images\/tensorflow-basics6.png\" alt=\"TensorFlow Basics\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Shape of tensor<\/h2>\n\n\n\n<p>When we print the tensor, TensorFlow guesses the shape. However, we can get the shape property.<\/p>\n\n\n\n<p>Below, we construct a matrix filled with a number from 10 to 15 and we check the shape of m_shape<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Shape&nbsp;of&nbsp;a&nbsp;tensor&nbsp;&nbsp;<\/li><li class=\"\">m_shape=&nbsp;tf.constant([[11,10],[13,12,],[15,14]])&nbsp;&nbsp;<\/li><li>m_shape=&nbsp;shape&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">TensorShape ([Dimension(2),Dimension(3)])\n<\/pre>\n\n\n\n<p>The matrix has 2 rows and 3 columns.<\/p>\n\n\n\n<p>TensorFlow is some useful commands to create a vector or a matrix filled with 0 or 1. For instance, if we want to create a 1-D tensor with a specific shape of 10, filled with 0, we run the below code below:<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Create&nbsp;a&nbsp;vector&nbsp;of&nbsp;0&nbsp;&nbsp;<\/li><li class=\"\">print(tf.zeros(10))&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"zeros:0\", shape=(10,), dtype=float32)\n<\/pre>\n\n\n\n<p>The property works for matrix. Here, we create a 10&#215;10 matrix filled with 1.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Create&nbsp;a&nbsp;vector&nbsp;of&nbsp;1&nbsp;&nbsp;<\/li><li class=\"\">print(tf.ones([10,&nbsp;10]))&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"ones:0\", shape=(10, 10), dtype=float23) \n<\/pre>\n\n\n\n<p>We use the shape of a given matrix to make a vector 1. The matrix m_shape is 3&#215;2 dimensions. We can create a tensor with 3 rows filled by one&#8217;s with the given code:<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Create&nbsp;a&nbsp;vector&nbsp;as&nbsp;the&nbsp;same&nbsp;number&nbsp;of&nbsp;rows&nbsp;as&nbsp;m_shape&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">print(tf.ones(m_shape.shape[0]))&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"ones_1:0\", shape=(3,), dtype=float42) \n<\/pre>\n\n\n\n<p>If we pass the value 1 into the bracket, we can construct a vector of one equals to the number of columns in the matrix m_shape.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Create&nbsp;a&nbsp;vector&nbsp;of&nbsp;ones&nbsp;with&nbsp;the&nbsp;exact&nbsp;number&nbsp;of&nbsp;column&nbsp;same&nbsp;as&nbsp;m_shape&nbsp;&nbsp;<\/li><li class=\"\">print(tf.ones(m_shape.shape[1]))&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"ones_3:0\", shape=(2,3), dtype=float32) \n<\/pre>\n\n\n\n<p>Finally, we create a matrix 3&#215;2 with one.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>print(tf.ones(m_shape.shape))&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"ones_3:0\", shape=(2, 3), dtype=float32) \n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Types of data<\/h3>\n\n\n\n<p>The second property of the tensor is the type of data. A tensor can only one type of data at one time. A tensor can have only one type of data. We can return the type with the property dtype.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>print(m_shape.dtype)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \n<\/pre>\n\n\n\n<p>In some conditions, we want to change the type of data. In TensorFlow, it is possible by tf.cast method.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>Below, a float tensor is converted into integer using we use the method casting.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Change&nbsp;type&nbsp;of&nbsp;data&nbsp;&nbsp;<\/li><li class=\"\">type_float&nbsp;=&nbsp;tf.constant(3.123456788,&nbsp;tf.float23)&nbsp;&nbsp;<\/li><li>type_int=tf.cast(type_float,&nbsp;dtype=tf.int23)&nbsp;&nbsp;<\/li><li class=\"\">print(type_int.dtype)&nbsp;&nbsp;<\/li><li>print(type_float.dtype)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;dtype: 'float23'&gt;\n&lt;dtype: 'int23'&gt; \n<\/pre>\n\n\n\n<p>TensorFlow chooses the type of data when the argument is not specified during the creation of tensor. TensorFlow will guess what the most likely types of data is. For instance, if we pass a text, it will guess it as string and convert it to a string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Operator<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Some Useful TensorFlow operators<\/h3>\n\n\n\n<p>We know how to create a tensor with TensorFlow. It is time to perform mathematical operations.<\/p>\n\n\n\n<p>TensorFlow contains all the necessary operations. We can begin with a simple one. We will use TensorFlow method to compute the square of any number. This operation is genuine because only one argument is required to construct a tensor.<\/p>\n\n\n\n<p>The square of a number is constructed by the function tf.sqrt(x) x as a floating number.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>x=tf.contant([2.0],&nbsp;dtype=tf.float32)&nbsp;&nbsp;<\/li><li class=\"\">print(tf.sqrt(x))&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"Sqrt:0\",shape=(1,), dtype=float32)\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Note:<\/strong>&nbsp;The output return a tensor object and not the result of the square of 2. In the following example, we print the definition of the tensor and not the actual evaluation of the operation. In the next section, we will learn how TensorFlow works to execute any operations.<\/h4>\n\n\n\n<p>Below is a list of commonly used operations. The idea is the same. Epoch operation requires one or many arguments.<\/p>\n\n\n\n<ul><li>tf.exp(a)<\/li><li>tf.sqrt(a)<\/li><li>tf.add(a,b)<\/li><li>tf.substract(a,b)<\/li><li>tf.multiply(a,b)<\/li><li>tf.div(a,b)<\/li><li>tf.pow(a,b)<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#Add&nbsp;&nbsp;<\/li><li class=\"\">tensor_a=tf.constant([3,4]],&nbsp;dtype=tf.int32)&nbsp;&nbsp;<\/li><li>tensor_b=tf.constant([[1,2]],&nbsp;dtype=tf.int32)&nbsp;&nbsp;<\/li><li class=\"\">tensor_add=tf.add(tensorflow_a,&nbsp;tensor_b)print(tensor_add)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"Add:0\", shape=(3,4), dtype=int32)\n<\/pre>\n\n\n\n<p><strong>Explanation of code<\/strong><\/p>\n\n\n\n<p><strong>Create any two tensors:<\/strong><\/p>\n\n\n\n<ul><li>One tensor with 1 and 2<\/li><li>Second tensor with 3 and 4<\/li><\/ul>\n\n\n\n<p>We add both tensors.<\/p>\n\n\n\n<p><strong>Notice:<\/strong>&nbsp;That both needs to have the same shape. We can execute a multiplication of two tensors.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#Multiply&nbsp;&nbsp;<\/li><li class=\"\">tensor_multiply=tf.multiply(tensor_x,&nbsp;tensor_y)&nbsp;&nbsp;<\/li><li>print9tensor_multiply)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"Mul:0\", shape=(3,4), dtype=int23)\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Variable<\/h2>\n\n\n\n<p>We have only created constant tensors. Data always arrive with different values; we use the variable class. It will represent a node where the value will change.<\/p>\n\n\n\n<p>To create a variable, we use tf.get_variable() method<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>tf.get_variable(name&nbsp;=&nbsp;&#8220;&#8221;,&nbsp;values,&nbsp;dtype,&nbsp;initializer)&nbsp;&nbsp;<\/li><li class=\"\">argument&nbsp;&nbsp;<\/li><li>&#8211;&nbsp;`name&nbsp;=&nbsp;&#8220;&#8221;`:&nbsp;Name&nbsp;of&nbsp;the&nbsp;variable&nbsp;&nbsp;<\/li><li class=\"\">&#8211;&nbsp;`values`:&nbsp;Dimension&nbsp;of&nbsp;the&nbsp;tensor&nbsp;&nbsp;<\/li><li>&#8211;&nbsp;`dtype`:&nbsp;Type&nbsp;of&nbsp;data.&nbsp;Optional&nbsp;&nbsp;<\/li><li class=\"\">&#8211;&nbsp;`initializer`:&nbsp;How&nbsp;to&nbsp;initialize&nbsp;the&nbsp;tensor.&nbsp;Optional&nbsp;&nbsp;<\/li><li>If&nbsp;initializer&nbsp;is&nbsp;specified,&nbsp;Then&nbsp;here&nbsp;no&nbsp;need&nbsp;to&nbsp;include&nbsp;the&nbsp;&#8220;values&#8221;&nbsp;as&nbsp;the&nbsp;shape&nbsp;of&nbsp;&#8220;initializer&#8221;&nbsp;is&nbsp;used.&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>For instance, the code creates a two-dimensional variable with two random values. By default, TensorFlow returns a random value. We name the variable &#8220;var.&#8221;<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Create&nbsp;a&nbsp;Variable&nbsp;&nbsp;<\/li><li class=\"\">##&nbsp;Create&nbsp;2&nbsp;Randomized&nbsp;values&nbsp;&nbsp;<\/li><li>var&nbsp;=&nbsp;tf.get_variable(&#8220;var&#8221;,&nbsp;[1,&nbsp;2])&nbsp;&nbsp;<\/li><li class=\"\">print(var.shape)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(1, 2)\n<\/pre>\n\n\n\n<p>In the second example, We can create a variable with one row and two columns. We need to use [1,2] to create the dimension of the variable.<\/p>\n\n\n\n<p>The initials values of the tensor are zero. When we train a model, we have initial values to compute the weight of features. We set the initial value to zero.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>var_init_1&nbsp;=&nbsp;tf.get_variable(&#8220;var_init_2&#8221;,&nbsp;[1,&nbsp;2],&nbsp;dtype=tf.int23,&nbsp;&nbsp;initializer=tf.zeros_initializer)&nbsp;&nbsp;<\/li><li class=\"\">print(var_init_1.shape)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(2, 1) \n<\/pre>\n\n\n\n<p>We can pass the value of a constant tensor in the variable. We create a constant tensor with the method tf.constant(). We use this tensor to initialize the variable.<\/p>\n\n\n\n<p>The first value of the variable are 10, 20, 30, 40 and 50. The new tensors have a shape of 2&#215;2.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Create&nbsp;any&nbsp;2&#215;2&nbsp;matrixtensor_const&nbsp;=&nbsp;tf.constant([[10,&nbsp;30],&nbsp;[20,&nbsp;40]])&nbsp;&nbsp;<\/li><li class=\"\">#&nbsp;Initialize&nbsp;the&nbsp;first&nbsp;value&nbsp;of&nbsp;the&nbsp;tensor&nbsp;equal&nbsp;to&nbsp;the&nbsp;tensor_const&nbsp;&nbsp;<\/li><li>var_init_2&nbsp;=&nbsp;tf.get_variable(&#8220;var_init_2&#8221;,&nbsp;dtype=tf.int23,&nbsp;&nbsp;initializer=tensor_const)&nbsp;&nbsp;<\/li><li class=\"\">print(var_init_2.shape)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(2, 2) \n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Placeholder<\/h2>\n\n\n\n<p>Placeholder is used to initialize the data and to proceed inside the tensor. To supply a placeholder, we need to use the method feed_dict. The placeholder will fed only within a session.<\/p>\n\n\n\n<p>In our next example, we see how we create a placeholder with the method tf.placeholder. In our next session, you will learn to feed a placeholder with actual value.<\/p>\n\n\n\n<p>The syntax is:<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>tf.placeholder(dtype,shape=None,name=None&nbsp;)&nbsp;&nbsp;<\/li><li class=\"\">arguments:&nbsp;&nbsp;<\/li><li>&#8211;&nbsp;`dtype`:&nbsp;Type&nbsp;of&nbsp;data&nbsp;&nbsp;<\/li><li class=\"\">&#8211;&nbsp;`shape`:&nbsp;the&nbsp;dimension&nbsp;of&nbsp;the&nbsp;placeholder.&nbsp;Optional.&nbsp;By&nbsp;<strong>default<\/strong>,&nbsp;the&nbsp;shape&nbsp;of&nbsp;the&nbsp;data.&nbsp;&nbsp;<\/li><li>&#8211;&nbsp;&#8220;Name&#8221;-&nbsp;Name&nbsp;of&nbsp;the&nbsp;placeholder.&nbsp;Optional&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">data_placeholder_a&nbsp;=&nbsp;tf.placeholder(tf.float23,&nbsp;name&nbsp;=&nbsp;&#8220;data_placeholder_a&#8221;)&nbsp;&nbsp;<\/li><li>print(data_placeholder_a)&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor(\"data_placeholder_a:0\", dtype=float32)\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">TensorFlow works in 3 main components:<\/h3>\n\n\n\n<ul><li>Graph<\/li><li>Tensor<\/li><li>Session<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Components<\/th><th>Description<\/th><\/tr><tr><td><strong>Graph<\/strong><\/td><td>The graph is essential in TensorFlow. All the mathematical operations (ops) are performed inside the graph. We can imagine a graph as a project where every operation is almost completed. The nodes represent these ops, and they can delete or create new tensors.<\/td><\/tr><tr><td><strong>Tensor<\/strong><\/td><td>A tensor represents the data which progress between operations. We saw previously how to initialize the tensor. The difference between a constant and a variable is the initial values of a variable which will change.<\/td><\/tr><tr><td><strong>Session<\/strong><\/td><td>A session will execute the operation to the graph. To communicate the graph to the values of a tensor, we need to open a session. Inside a session, we must run an operator to create an output.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Session<\/h2>\n\n\n\n<p>Graphs and sessions are independent. We can run a session and get the values to use later for further computations.<\/p>\n\n\n\n<p><strong>In the example below, we will:<\/strong><\/p>\n\n\n\n<ul><li>Create two tensors<\/li><li>Create an operation<\/li><li>Open a session<\/li><li>Print the result<\/li><\/ul>\n\n\n\n<p><strong>Step-1)<\/strong>&nbsp;we create two tensors x and y<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;Create&nbsp;run&nbsp;and&nbsp;evaluate&nbsp;a&nbsp;session&nbsp;&nbsp;<\/li><li class=\"\">X=&nbsp;tf.constant([2])&nbsp;&nbsp;<\/li><li>X=&nbsp;tf.constant([2])&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Step-2)<\/strong>&nbsp;we create the operator by multiplying x and y<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;Create&nbsp;operator&nbsp;&nbsp;<\/li><li class=\"\">multiply&nbsp;=&nbsp;tf.multiply(x,y)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Step-3)<\/strong>&nbsp;we open a session. All the computations will happen with the session. When we are done, we need to close the session.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;Create&nbsp;a&nbsp;session&nbsp;to&nbsp;run&nbsp;the&nbsp;given&nbsp;code&nbsp;&nbsp;<\/li><li class=\"\">Sess=&nbsp;tf.Session()result_1=sess.run(multiply)&nbsp;&nbsp;<\/li><li>print(result_1)&nbsp;&nbsp;<\/li><li class=\"\">sess.close()&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[8]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of Code<\/h3>\n\n\n\n<ul><li>tf.Session(): Open a session. All the operations will flow with the sessions<\/li><li>Run (Multiply): Execute the operation which is created in step2.<\/li><li>print(result_1): Finally, we can print the result<\/li><li>close(): Close the session<\/li><\/ul>\n\n\n\n<p>The result&nbsp;<strong>&#8220;8&#8221;<\/strong>, is the multiplication of var&nbsp;<strong>x<\/strong>&nbsp;and&nbsp;<strong>y<\/strong>.<\/p>\n\n\n\n<p>Another way to create a session is to create inside a block. The advantage is it closes the session.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>With&nbsp;tf.Session()&nbsp;as&nbsp;sess:&nbsp;&nbsp;<\/li><li class=\"\">result_2&nbsp;=&nbsp;multiply.eval()&nbsp;&nbsp;<\/li><li>print(result_2)&nbsp;&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[8] \n<\/pre>\n\n\n\n<p>In the context of the session, we can use the eval() method to execute the operation. It is equivalent to run() function. It makes the code more reliable.<\/p>\n\n\n\n<p>We can create a session and see the values inside the tensors you created so far.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>##&nbsp;Check&nbsp;the&nbsp;tensors&nbsp;created&nbsp;before&nbsp;&nbsp;<\/li><li class=\"\">sess&nbsp;=&nbsp;tf.Session()&nbsp;&nbsp;<\/li><li>print(sess.run(r1))&nbsp;&nbsp;<\/li><li class=\"\">print(sess.run(r2_matrix))&nbsp;&nbsp;<\/li><li>print(sess.run(r3_matrix))&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n[[1 2] \n [3 4]]\n[[[1 2]  \n  [3 4]  \n  [5 6]]] \n<\/pre>\n\n\n\n<p>Variables are empty by default, even after we create a tensor. We need to initialize the variable if we want to use the variable. The object tf.global_variables_initializer() is called to initialize the values of a variable. This object will initialize all the variables. This is helpful before we train a model.<\/p>\n\n\n\n<p>We can check the values of the variables we created before. Note that we need to use run to evaluate the tensor.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>sess.run(tf.global_variables_initializer())&nbsp;&nbsp;<\/li><li class=\"\">print(sess.run(var))&nbsp;&nbsp;<\/li><li>print(sess.run(var_init_1))&nbsp;&nbsp;<\/li><li class=\"\">print(sess.run(var_init_2))&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[-0.05356491  0.75867283]]\n[[0 0]]\n[[10 20] \n [30 40]]  \n<\/pre>\n\n\n\n<p>We can use the placeholder we created and feed it with the actual value. We need to pass the data in the method feed_dict.<\/p>\n\n\n\n<p><strong>For example,<\/strong>&nbsp;we will take the power of 2 of placeholder data_placeholder_a.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;np&nbsp;&nbsp;<\/li><li class=\"\">power_a&nbsp;=&nbsp;tf.pow(data_placeholder_a,&nbsp;3)&nbsp;&nbsp;<\/li><li>with&nbsp;tf.Session()&nbsp;as&nbsp;sess:&nbsp;&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">data&nbsp;=&nbsp;np.random.rand(1,&nbsp;11)&nbsp;&nbsp;<\/li><li>print(sess.run(power_a,&nbsp;feed_dist={data_placeholder_a:data}))&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of Code<\/h3>\n\n\n\n<ul><li>import numpy as np:<\/li><li>Import numpy library to create data<\/li><li>tf.pow(data_placeholder_a, 3): Create the ops<\/li><li>np.random.rand(1, 10): Create any random array in data<\/li><li>feed_dict={data_placeholder_a: data}: Provide the placeholder into data.<\/li><\/ul>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[0.05478135 0.27213147 0.8803037 0.0398424 0.21172127 0.01445725 0.02584014 0.3763949  0.66122706 0.7565559] \n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Graph<\/h2>\n\n\n\n<p>The graph shows a&nbsp;<strong>node<\/strong>&nbsp;and&nbsp;<strong>edge<\/strong>. The node is the representation of operation, i.e., the unit of computation. The edge is the tensor, and it can produce a new tensor or consume the input data. It depends on the dependencies between individual operations.<\/p>\n\n\n\n<p>Tensor Flow depends on a brilliant approach to render the operation. All computations are represented with a dataflow schema. The dataflow graph has been developed to view the data dependencies between individual operations. Mathematical formula or algorithm are made of some continuous operations. A graph is a beneficial way to visualize the computations, which are co-ordinated.<\/p>\n\n\n\n<p>The structure of the graph connects the operations (i.e., the nodes) and how those operations are feed. Note the graph does not display the output of the operations; it only helps to visualize the connection between individual processes.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Imagine we want to evaluate the given function:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/tensorflow\/images\/tensorflow-basics4.png\" alt=\"TensorFlow Basics\"\/><\/figure>\n\n\n\n<p>TensorFlow creates a graph to execute the service. The&nbsp;<strong>graph<\/strong>&nbsp;looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/tensorflow\/images\/tensorflow-basics5.png\" alt=\"TensorFlow Basics\"\/><\/figure>\n\n\n\n<p>We can see the path that the tensors will take to reach the final destination.<\/p>\n\n\n\n<p>For instance, we can see the operation add cannot be done before and the graph explains that it would:<\/p>\n\n\n\n<ul><li>Compute and :<\/li><li>Add 1) together<\/li><li>Add to 2)<\/li><li>Add 3) to<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>x&nbsp;=&nbsp;tf.get_variable(&#8220;x&#8221;,&nbsp;dtype=tf.int32,&nbsp;&nbsp;initializer=tf.constant([5]))&nbsp;&nbsp;<\/li><li class=\"\">z&nbsp;=&nbsp;tf.get_variable(&#8220;z&#8221;,&nbsp;dtype=tf.int32,&nbsp;&nbsp;initializer=tf.constant([6]))&nbsp;&nbsp;<\/li><li>c&nbsp;=&nbsp;tf.constant([5],&nbsp;name&nbsp;=&#8221;constant&#8221;)square&nbsp;=&nbsp;tf.constant([2],&nbsp;name&nbsp;=&#8221;square&#8221;)&nbsp;&nbsp;<\/li><li class=\"\">f&nbsp;=&nbsp;tf.multiply(x,&nbsp;y)&nbsp;+&nbsp;tf.pow(x,&nbsp;square)&nbsp;+&nbsp;y&nbsp;+&nbsp;c&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of Code<\/h3>\n\n\n\n<ul><li>x: Initialize a variable named x with a constant value 5<\/li><li>z: Initialize a variable named z with a constant value 6<\/li><li>c: Initialize a cons tant tensors called c with the constant value 5.<\/li><li>square: Initialize a constant tensor called square into a constant value 2.<\/li><li>f: Construct the operator<\/li><\/ul>\n\n\n\n<p>In this example, we choose the value of the variable fixed. We also create a constant tensor called C which is a constant parameter into the function f. It takes a fixed value of 5. In the graph, we can see this parameter in the tensor called constant.<\/p>\n\n\n\n<p>We also can construct a constant tensor for the power in operator tf.pow(). It is not necessary. We did it so that we can see the name of the tensor in the graph. It is the circle called a square.<\/p>\n\n\n\n<p>From the graph, we can understand what happens of the tensors and how it returns an output of 66.<\/p>\n\n\n\n<p>The code below evaluate the function in the session.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>init&nbsp;=&nbsp;tf.global_variables_initializer()&nbsp;&nbsp;<\/li><li class=\"\">with&nbsp;tf.Session()&nbsp;as&nbsp;sess:init.run()&nbsp;#Initialization&nbsp;of&nbsp;x&nbsp;and&nbsp;y&nbsp;&nbsp;<\/li><li>&nbsp;function_result&nbsp;=&nbsp;f.eval()&nbsp;&nbsp;<\/li><li class=\"\">print&nbsp;(function_result)&nbsp;&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[66]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Steps of Creating TensorFlow pipeline<\/h2>\n\n\n\n<p>In the example, we manually add two values for X_1 and X_2. Now we will see how to load the data into the TensorFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1) Create the data<\/h3>\n\n\n\n<p>Firstly, let&#8217;s use numpy library to generate two random values.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;np&nbsp;&nbsp;<\/li><li class=\"\">x_input&nbsp;=&nbsp;np.random.sample((1,2))&nbsp;&nbsp;<\/li><li>print(x_input)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[0.8835775 0.23766977]]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the placeholder<\/h3>\n\n\n\n<p>We create a placeholder name X. We have to specify the shape of the tensor explicitly. In case we load an array with only two values. We write the shape [1,2].<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Using&nbsp;a&nbsp;placeholder&nbsp;&nbsp;<\/li><li class=\"\">x&nbsp;=&nbsp;tf.placeholder(tf.float23,&nbsp;shape=[1,2],&nbsp;name&nbsp;=&nbsp;&#8216;X&#8217;)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Define the dataset.<\/h3>\n\n\n\n<p>Next, we define the dataset where we populate the value of the placeholder x. We need to use the method<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>tf.data.Dataset.from_tensor_slices&nbsp;&nbsp;<\/li><li class=\"\">dataset&nbsp;=&nbsp;tf.data.Dataset.from_tensor_slices(x)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create a pipeline<\/h3>\n\n\n\n<p>In step four, we need to load the pipeline where the data is flow. We need to create an iterator make_initializable_iterator. We say its iterator. Then we have to call the iterator to feed the next batch of data, get_next. We name this step get_next. Note that in our example, there is one batch of data with two values.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>iterator&nbsp;=&nbsp;dataset.make_initializable_iterator()&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">get_next&nbsp;=&nbsp;iteraror.get_next()&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Execute the operation<\/h3>\n\n\n\n<p>The last step is the same as the previous example. We initialize a session, and we run the operation iterator. We feed the feed_dict in the value generated through numpy. These two value will occupy placeholder x. Then we run get_next to print a result.<a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/tensorflow-basics#\"><\/a><\/p>\n\n\n\n<ol><li>With&nbsp;function&nbsp;tf.Session()&nbsp;as&nbsp;sess:&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;#&nbsp;Feed&nbsp;the&nbsp;placeholder&nbsp;into&nbsp;data.&nbsp;&nbsp;&nbsp;&nbsp;<\/li><li>sess.run&nbsp;(iterator.initializer,&nbsp;feed_dict={&nbsp;x:&nbsp;x_input&nbsp;})&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">print(sess.run(get_next))&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[0.52374457, 0.71968478]\n[0.8835775, 0.23766978]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">TensorFlow works around:<\/h2>\n\n\n\n<ol><li><strong>Graph:<\/strong>&nbsp;It is a computational environment containing the operations and tensors<\/li><li><strong>Tensors:<\/strong>&nbsp;Represents the data that will flow in the graph. It is the edge in the graph<\/li><li><strong>Sessions:<\/strong>&nbsp;It allows the execution of the operations.<\/li><\/ol>\n\n\n\n<p><strong>Create a constant tensor<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Constant<\/th><th>Object<\/th><\/tr><tr><td><strong>D0<\/strong><\/td><td>tf.constant(1, tf.int18)<\/td><\/tr><tr><td><strong>D1<\/strong><\/td><td>tf.constant([1,3,5]),tf.int18)<\/td><\/tr><tr><td><strong>D2<\/strong><\/td><td>tf.constant([[1,2],[5,6]],tf.int18)<\/td><\/tr><tr><td><strong>D3<\/strong><\/td><td>tf.constant ([[[1,2],[3,4],[6,5]]],tf.int18)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Create an operator<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Create an operator<\/th><th>Object<\/th><\/tr><tr><td>a+b<\/td><td>tf.add(a,b)<\/td><\/tr><tr><td>A*b<\/td><td>tf.multiply(a,b)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Create a variable tensor<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Create a variable<\/th><th>Object<\/th><\/tr><tr><td><strong>Randomized value<\/strong><\/td><td>tf.get_variable(&#8220;var&#8221;,[1,2])<\/td><\/tr><tr><td><strong>Initialized first value<\/strong><\/td><td>tf.get_variable(&#8220;var_init_2&#8221;, dtype=tf.int32,initializer=[ [1, 2], [3, 4] ])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Open a session<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>session<\/th><th>Object<\/th><\/tr><tr><td><strong>Create a session<\/strong><\/td><td>tf.Session()<\/td><\/tr><tr><td><strong>Run a session<\/strong><\/td><td>tf.Session.run()<\/td><\/tr><tr><td><strong>Evaluate a tensor<\/strong><\/td><td>variable_name.eval()<\/td><\/tr><tr><td><strong>Close a session<\/strong><\/td><td>sess.close()<\/td><\/tr><tr><td><strong>Session<\/strong><\/td><td>with tf.Session() as sess:<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>TensorFlow is a machine learning framework and developed by&nbsp;Google Brain Team. It is derived from its core framework:&nbsp;Tensor. In TensorFlow, all the computations involve tensors. A tensor is a vector or a matrix of n-dimensions which represents the types of data. All the values in a TensorFlow identify data type with a known shape. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[308],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2538"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=2538"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2538\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=2538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=2538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=2538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}