{"id":2514,"date":"2022-04-19T07:00:51","date_gmt":"2022-04-19T07:00:51","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=2514"},"modified":"2022-04-19T07:00:51","modified_gmt":"2022-04-19T07:00:51","slug":"pytorch-basics","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/04\/19\/pytorch-basics\/","title":{"rendered":"PyTorch Basics"},"content":{"rendered":"\n<p>It is essential to understand all the basic concepts which are required to work with PyTorch. PyTorch is completely based on Tensors. Tensor has operations to perform. Apart from these, there are lots of other concepts which are required to perform the task.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<p>Now, understand all the concepts one by one to gain deep knowledge of PyTorch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Matrices or Tensors<\/h2>\n\n\n\n<p>Tensors are the key components of Pytorch. We can say PyTorch is completely based on the Tensors. In mathematical term, a rectangular array of number is called a metrics. In&nbsp;<strong>the Numpy<\/strong>&nbsp;library, these metrics called&nbsp;<strong>ndaaray<\/strong>. In PyTorch, it is known as&nbsp;<strong>Tensor<\/strong>. A Tensor is an n-dimensional data container. For example, In PyTorch, 1d-Tensor is a vector, 2d-Tensor is a metrics, 3d- Tensor is a cube, and 4d-Tensor is a cube vector.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch2.jpg\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<p><strong>Above matrics represent 2D-Tensor with three rows and two columns.<\/strong><\/p>\n\n\n\n<p>There are three ways to create Tensor. Each one has a different way to create Tensor. Tensors are created as:<\/p>\n\n\n\n<ol><li>Create PyTorch Tensor an array<\/li><li>Create a Tensor with all ones and random number<\/li><li>Create Tensor from numpy array<\/li><\/ol>\n\n\n\n<p>Let see how Tensors are created<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a PyTorch Tensor as an array<\/h3>\n\n\n\n<p>In this, you have first to define the array and then pass that array in your Tensor method of the torch as an argument.<\/p>\n\n\n\n<p><strong>For example<\/strong><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li class=\"\">arr&nbsp;=&nbsp;[[3,&nbsp;4],&nbsp;[8,&nbsp;5]]&nbsp;&nbsp;&nbsp;<\/li><li>pyTensor&nbsp;=&nbsp;torch.Tensor(arr)&nbsp;&nbsp;<\/li><li class=\"\">print(pyTensor)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tensor ([[3., 4.],[8., 5.]])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch3.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Tensor with the random number and all one<\/h3>\n\n\n\n<p>To create a random number Tensor, you have to use rand() method and to create a Tensor with all ones you have to use ones() of the torch. To generate random number one more method of the torch will be used with the rand, i.e., manual_seed with 0 parameters.<\/p>\n\n\n\n<p><strong>For example<\/strong><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li class=\"\">ones_t&nbsp;=&nbsp;torch.ones((2,&nbsp;2))&nbsp;&nbsp;&nbsp;<\/li><li>torch.manual_seed(0)&nbsp;&nbsp;\/\/to&nbsp;have&nbsp;same&nbsp;values&nbsp;for&nbsp;random&nbsp;generation&nbsp;&nbsp;<\/li><li class=\"\">rand_t&nbsp;=&nbsp;torch.rand((2,&nbsp;2))&nbsp;&nbsp;<\/li><li>print(ones_t)&nbsp;&nbsp;<\/li><li class=\"\">print(rand_t)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Tensor ([[1., 1.],[1., 1.]])\ntensor ([[0.4963, 0.7682],[0.0885, 0.1320]])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch4.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Tensor from numpy array<\/h3>\n\n\n\n<p>To create a Tensor from the numpy array, we have to create a numpy array. Once your numpy array is created, we have to pass it in from_numpy() as an argument. This method converts the numpy array to Tensor.<\/p>\n\n\n\n<p><strong>For example<\/strong><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;np1&nbsp;&nbsp;<\/li><li>numpy_arr&nbsp;=&nbsp;np1.ones((2,&nbsp;2))&nbsp;&nbsp;<\/li><li class=\"\">pyTensor&nbsp;=&nbsp;torch.from_numpy(numpy_arr)&nbsp;&nbsp;<\/li><li>np1_arr_from_Tensor&nbsp;=&nbsp;pyTensor.numpy()&nbsp;&nbsp;<\/li><li class=\"\">print(np1_arr_from_Tensor)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1. 1.] [1. 1.]]\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch5.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Tensors Operations<\/h2>\n\n\n\n<p>Tensors are similar to an array, so all the operation which we are performing on an array can also apply for Tensor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch6.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1) Resizing a Tensor<\/h3>\n\n\n\n<p>We can resize the Tensor using the size property of Tensor. We use Tensor.view() for resizing a Tensor. Resizing a Tensor means the conversion of 2*2 dimensional Tensor to 4*1 or 4*4 dimensional Tensor to 16*1 and so on. To print the Tensor size, we use Tensor.size() method.<\/p>\n\n\n\n<p>Let see an example of resizing a Tensor.<a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li class=\"\">pyt_Tensor&nbsp;=&nbsp;torch.ones((2,&nbsp;2))&nbsp;&nbsp;<\/li><li>print(pyt_Tensor.size())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;shows&nbsp;the&nbsp;size&nbsp;of&nbsp;<strong>this<\/strong>&nbsp;Tensor&nbsp;&nbsp;<\/li><li class=\"\">pyt_Tensor&nbsp;=&nbsp;pyt_Tensor.view(4)&nbsp;#&nbsp;resizing&nbsp;2&#215;2&nbsp;Tensor&nbsp;to&nbsp;4&#215;1&nbsp;&nbsp;<\/li><li>print(pyt_Tensor)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">torch.Size ([2, 2])\ntensor ([1., 1., 1., 1.])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch7.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Mathematical Operations<\/h3>\n\n\n\n<p>All the mathematical operation such as addition, subtraction, division, and multiplication can be performed on Tensor. The torch can do the mathematical operation. We use a torch.add(), torch.sub(), torch.mul() and torch.div() to perform operations on Tensors.<\/p>\n\n\n\n<p>Let see an example how mathematical operations are performed:<a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;np&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li>Tensor_a&nbsp;=&nbsp;torch.ones((2,&nbsp;2))&nbsp;&nbsp;<\/li><li class=\"\">Tensor_b&nbsp;=&nbsp;torch.ones((2,&nbsp;2))&nbsp;&nbsp;<\/li><li>result=Tensor_a+Tensor_b&nbsp;&nbsp;<\/li><li class=\"\">result1&nbsp;=&nbsp;torch.add(Tensor_a,&nbsp;Tensor_b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/another&nbsp;way&nbsp;of&nbsp;addidtion&nbsp;&nbsp;<\/li><li>Tensor_a.add_(Tensor_b)&nbsp;\/\/&nbsp;In-place&nbsp;addition&nbsp;&nbsp;<\/li><li class=\"\">print(result)&nbsp;&nbsp;<\/li><li>print(result1)&nbsp;&nbsp;<\/li><li class=\"\">print(Tensor_a)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tensor ([[2., 2.], [2., 2.]])\ntensor ([[2., 2.], [2., 2.]])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch8.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3) Mean and Standard deviation<\/h3>\n\n\n\n<p>We can calculate the standard deviation of Tensor either for one dimensional or multi-dimensional. In our mathematical calculation, we have first to calculate mean, and then we apply the following formula on the given data with mean.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch9.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<p>But in Tensor, we can use Tensor.mean() and Tensor.std() to find the deviation and mean of the given Tensor.<\/p>\n\n\n\n<p>Let see an example of how it performed.<a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li class=\"\">pyTensor&nbsp;=&nbsp;torch.Tensor([1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5])&nbsp;&nbsp;<\/li><li>mean&nbsp;=&nbsp;pyt_Tensor.mean(dim=0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/if&nbsp;multiple&nbsp;rows&nbsp;then&nbsp;dim&nbsp;=&nbsp;1&nbsp;&nbsp;<\/li><li class=\"\">std_dev&nbsp;=&nbsp;pyTensor.std(dim=0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;if&nbsp;multiple&nbsp;rows&nbsp;then&nbsp;dim&nbsp;=&nbsp;1&nbsp;&nbsp;<\/li><li>print(mean)&nbsp;&nbsp;<\/li><li class=\"\">print(std_dev)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tensor (3.)\ntensor (1.5811)\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch10.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Variables and Gradient<\/h2>\n\n\n\n<p>The central class of the package is&nbsp;<strong>autograd.variable<\/strong>. Its main task is to wrap a&nbsp;<strong>Tensor<\/strong>. It supports nearly all of the operations defined on it. You can call&nbsp;<strong>.backword()<\/strong>&nbsp;and have all the gradient computed only when you finish your computation.<\/p>\n\n\n\n<p>Through&nbsp;<strong>.data<\/strong>&nbsp;attribute, you can access the row Tensor, while the gradient for this variable is accumulated into&nbsp;<strong>.grad<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch11.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<p>In Deep learning, gradient calculation is the key point. Variables are used to calculate the gradient in PyTorch. In simple words, variables are just a wrapper around Tensors with gradient calculation functionality.<\/p>\n\n\n\n<p>Below is the python code which is used to manage variables.<a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;np&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li>from&nbsp;torch.autograd&nbsp;<strong>import<\/strong>&nbsp;Variable&nbsp;&nbsp;<\/li><li class=\"\">pyt_var&nbsp;=&nbsp;Variable(torch.ones((2,&nbsp;2)),&nbsp;requires_grad&nbsp;=&nbsp;True)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Above code behaves the same as Tensors, so that we can apply all operations in the same way.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch12.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n\n\n\n<p>Let see how we can calculate the gradient in PyTorch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/basics-of-pytorch#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;np&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;torch&nbsp;&nbsp;<\/li><li>from&nbsp;torch.autograd&nbsp;<strong>import<\/strong>&nbsp;Variable&nbsp;&nbsp;&nbsp;&nbsp;<\/li><li class=\"\">\/\/&nbsp;let&#8217;s&nbsp;consider&nbsp;the&nbsp;following&nbsp;equation&nbsp;&nbsp;<\/li><li>\/\/&nbsp;y&nbsp;=&nbsp;5(x&nbsp;+&nbsp;1)^2&nbsp;&nbsp;<\/li><li class=\"\">x&nbsp;=&nbsp;Variable&nbsp;(torch.ones(1),&nbsp;requires_grad&nbsp;=&nbsp;True)&nbsp;&nbsp;<\/li><li>y&nbsp;=&nbsp;5&nbsp;*&nbsp;(x&nbsp;+&nbsp;1)&nbsp;**&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/implementing&nbsp;the&nbsp;equation.&nbsp;&nbsp;<\/li><li class=\"\">y.backward()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;calculate&nbsp;gradient&nbsp;&nbsp;<\/li><li>print(x.grad)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;get&nbsp;the&nbsp;gradient&nbsp;of&nbsp;variable&nbsp;x&nbsp;&nbsp;<\/li><li class=\"\">#&nbsp;differentiating&nbsp;the&nbsp;above&nbsp;mentioned&nbsp;equation&nbsp;&nbsp;<\/li><li>\/\/&nbsp;=&gt;&nbsp;5(x&nbsp;+&nbsp;1)^2&nbsp;=&nbsp;10(x&nbsp;+&nbsp;1)&nbsp;=&nbsp;10(2)&nbsp;=&nbsp;20&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tensor([20.])\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/pytorch\/images\/basic-of-pytorch13.png\" alt=\"Basics of PyTorch\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>It is essential to understand all the basic concepts which are required to work with PyTorch. PyTorch is completely based on Tensors. Tensor has operations to perform. Apart from these, there are lots of other concepts which are required to perform the task. Now, understand all the concepts one by one to gain deep knowledge [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[374],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2514"}],"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=2514"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2514\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=2514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=2514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=2514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}