{"id":191,"date":"2023-04-03T18:42:10","date_gmt":"2023-04-03T18:42:10","guid":{"rendered":"https:\/\/blog.amalgamcs.com\/?p=191"},"modified":"2023-04-03T18:42:11","modified_gmt":"2023-04-03T18:42:11","slug":"pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks","status":"publish","type":"post","link":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/","title":{"rendered":"PyTorch 101: A Beginner&#8217;s Guide to Tensors, Autograd, and Neural Networks"},"content":{"rendered":"\n<p>PyTorch is an open-source machine learning library for Python that is widely used in research and industry. It was developed by Facebook&#8217;s AI Research group and provides a flexible and efficient platform for building and training deep neural networks. In this overview, we will cover the basics of PyTorch and provide code examples to demonstrate its capabilities.<\/p>\n\n\n\n<h2>Tensors<\/h2>\n\n\n\n<p>Tensors are the basic data structure in PyTorch, similar to NumPy arrays. They are multidimensional arrays that can be used to store and manipulate data for machine learning tasks. Here is an example of creating a tensor in PyTorch:<\/p>\n\n\n\n<pre class=\"wp-block-code has-green-color has-black-background-color has-text-color has-background\"><code>import torch\r\n\r\n# Create a tensor of size 2x3 with random values\r\nx = torch.rand(2, 3)\r\nprint(x)\r<\/code><\/pre>\n\n\n\n<p>This will create a tensor with size 2&#215;3 and fill it with random values. We can access individual elements of the tensor using indexing:<\/p>\n\n\n\n<pre class=\"wp-block-code has-green-color has-black-background-color has-text-color has-background\"><code># Access the element at row 1, column 2\r\nprint(x&#91;1, 2])\r<\/code><\/pre>\n\n\n\n<h2>Autograd<\/h2>\n\n\n\n<p>PyTorch&#8217;s autograd package provides automatic differentiation for tensors, allowing gradients to be computed automatically for backpropagation. Here is an example of using autograd to compute the gradient of a function:<\/p>\n\n\n\n<pre class=\"wp-block-code has-green-color has-black-background-color has-text-color has-background\"><code>import torch\r\n\r\n# Define a function\r\ndef f(x):\r\n    return x ** 2 + 2 * x + 1\r\n\r\n# Create a tensor and set requires_grad=True to track computation with it\r\nx = torch.tensor(2.0, requires_grad=True)\r\n\r\n# Compute the function\r\ny = f(x)\r\n\r\n# Compute the gradient of y with respect to x\r\ny.backward()\r\n\r\n# Print the gradient\r\nprint(x.grad)\r<\/code><\/pre>\n\n\n\n<p>This will compute the gradient of the function <code>f(x) = x ** 2 + 2 * x + 1<\/code> with respect to x at x=2, using PyTorch&#8217;s autograd package. The gradient will be printed as a tensor with value 6.<\/p>\n\n\n\n<h2>Neural Networks<\/h2>\n\n\n\n<p>PyTorch provides a simple and flexible API for building and training neural networks. Here is an example of defining a simple neural network using PyTorch:<\/p>\n\n\n\n<pre class=\"wp-block-code has-green-color has-black-background-color has-text-color has-background\"><code>import torch\r\nimport torch.nn as nn\r\n\r\n# Define a neural network with one hidden layer\r\nclass Net(nn.Module):\r\n    def __init__(self):\r\n        super(Net, self).__init__()\r\n        self.fc1 = nn.Linear(10, 5)\r\n        self.fc2 = nn.Linear(5, 1)\r\n\r\n    def forward(self, x):\r\n        x = torch.relu(self.fc1(x))\r\n        x = self.fc2(x)\r\n        return x\r\n\r\n# Create a random input tensor\r\nx = torch.randn(1, 10)\r\n\r\n# Create an instance of the network\r\nnet = Net()\r\n\r\n# Compute the output of the network\r\noutput = net(x)\r\n\r\nprint(output)\r<\/code><\/pre>\n\n\n\n<p>This will define a neural network with one hidden layer and print its output for a random input tensor. The network consists of two fully connected layers (<code>fc1<\/code> and <code>fc2<\/code>) with ReLU activation in between.<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/amalgamcs.com\/\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"378\" src=\"http:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/Original-Logo-1024x378.png\" alt=\"AmalgamCS Logo\" class=\"wp-image-76\" srcset=\"https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/Original-Logo-1024x378.png 1024w, https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/Original-Logo-300x111.png 300w, https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/Original-Logo-768x284.png 768w, https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/Original-Logo-1536x567.png 1536w, https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/Original-Logo-2048x756.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/amalgamcs.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/amalgamcs.com\/<\/a><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>PyTorch is an open-source machine learning library for Python that is widely used in research and industry. It was developed by Facebook&#8217;s AI Research group and provides a flexible and efficient platform for building and training deep neural networks. In this overview, we will cover the basics of PyTorch and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PyTorch 101: A Beginner&#039;s Guide to Tensors, Autograd, and Neural Networks - AmalgamCS Tech Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PyTorch 101: A Beginner&#039;s Guide to Tensors, Autograd, and Neural Networks - AmalgamCS Tech Blog\" \/>\n<meta property=\"og:description\" content=\"PyTorch is an open-source machine learning library for Python that is widely used in research and industry. It was developed by Facebook&#8217;s AI Research group and provides a flexible and efficient platform for building and training deep neural networks. In this overview, we will cover the basics of PyTorch and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\" \/>\n<meta property=\"og:site_name\" content=\"AmalgamCS Tech Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-03T18:42:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-03T18:42:11+00:00\" \/>\n<meta name=\"author\" content=\"Garrik Hoyt\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Garrik Hoyt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\"},\"author\":{\"name\":\"Garrik Hoyt\",\"@id\":\"https:\/\/blog.amalgamcs.com\/#\/schema\/person\/97a98f183f3f756243c26dbed73f8922\"},\"headline\":\"PyTorch 101: A Beginner&#8217;s Guide to Tensors, Autograd, and Neural Networks\",\"datePublished\":\"2023-04-03T18:42:10+00:00\",\"dateModified\":\"2023-04-03T18:42:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\"},\"wordCount\":250,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/#organization\"},\"articleSection\":[\"A.I.\/M.L.\/Data Science\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\",\"url\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\",\"name\":\"PyTorch 101: A Beginner's Guide to Tensors, Autograd, and Neural Networks - AmalgamCS Tech Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/#website\"},\"datePublished\":\"2023-04-03T18:42:10+00:00\",\"dateModified\":\"2023-04-03T18:42:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.amalgamcs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PyTorch 101: A Beginner&#8217;s Guide to Tensors, Autograd, and Neural Networks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.amalgamcs.com\/#website\",\"url\":\"https:\/\/blog.amalgamcs.com\/\",\"name\":\"AmalgamCS Tech Blog\",\"description\":\"Curated information on the latest in tech\",\"publisher\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.amalgamcs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/blog.amalgamcs.com\/#organization\",\"name\":\"AmalgamCS\",\"url\":\"https:\/\/blog.amalgamcs.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.amalgamcs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/cropped-cropped-Transparent-Logo.png\",\"contentUrl\":\"https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/cropped-cropped-Transparent-Logo.png\",\"width\":2493,\"height\":485,\"caption\":\"AmalgamCS\"},\"image\":{\"@id\":\"https:\/\/blog.amalgamcs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.amalgamcs.com\/#\/schema\/person\/97a98f183f3f756243c26dbed73f8922\",\"name\":\"Garrik Hoyt\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.amalgamcs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/91f854d9f252604310ae9cef7d5ab86d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/91f854d9f252604310ae9cef7d5ab86d?s=96&d=mm&r=g\",\"caption\":\"Garrik Hoyt\"},\"sameAs\":[\"http:\/\/blog.amalgamcs.com\"],\"url\":\"https:\/\/blog.amalgamcs.com\/index.php\/author\/amalgamdvlpmnt\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PyTorch 101: A Beginner's Guide to Tensors, Autograd, and Neural Networks - AmalgamCS Tech Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/","og_locale":"en_US","og_type":"article","og_title":"PyTorch 101: A Beginner's Guide to Tensors, Autograd, and Neural Networks - AmalgamCS Tech Blog","og_description":"PyTorch is an open-source machine learning library for Python that is widely used in research and industry. It was developed by Facebook&#8217;s AI Research group and provides a flexible and efficient platform for building and training deep neural networks. In this overview, we will cover the basics of PyTorch and [&hellip;]","og_url":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/","og_site_name":"AmalgamCS Tech Blog","article_published_time":"2023-04-03T18:42:10+00:00","article_modified_time":"2023-04-03T18:42:11+00:00","author":"Garrik Hoyt","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Garrik Hoyt","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#article","isPartOf":{"@id":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/"},"author":{"name":"Garrik Hoyt","@id":"https:\/\/blog.amalgamcs.com\/#\/schema\/person\/97a98f183f3f756243c26dbed73f8922"},"headline":"PyTorch 101: A Beginner&#8217;s Guide to Tensors, Autograd, and Neural Networks","datePublished":"2023-04-03T18:42:10+00:00","dateModified":"2023-04-03T18:42:11+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/"},"wordCount":250,"commentCount":0,"publisher":{"@id":"https:\/\/blog.amalgamcs.com\/#organization"},"articleSection":["A.I.\/M.L.\/Data Science"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/","url":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/","name":"PyTorch 101: A Beginner's Guide to Tensors, Autograd, and Neural Networks - AmalgamCS Tech Blog","isPartOf":{"@id":"https:\/\/blog.amalgamcs.com\/#website"},"datePublished":"2023-04-03T18:42:10+00:00","dateModified":"2023-04-03T18:42:11+00:00","breadcrumb":{"@id":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.amalgamcs.com\/index.php\/2023\/04\/03\/pytorch-101-a-beginners-guide-to-tensors-autograd-and-neural-networks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.amalgamcs.com\/"},{"@type":"ListItem","position":2,"name":"PyTorch 101: A Beginner&#8217;s Guide to Tensors, Autograd, and Neural Networks"}]},{"@type":"WebSite","@id":"https:\/\/blog.amalgamcs.com\/#website","url":"https:\/\/blog.amalgamcs.com\/","name":"AmalgamCS Tech Blog","description":"Curated information on the latest in tech","publisher":{"@id":"https:\/\/blog.amalgamcs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.amalgamcs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/blog.amalgamcs.com\/#organization","name":"AmalgamCS","url":"https:\/\/blog.amalgamcs.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.amalgamcs.com\/#\/schema\/logo\/image\/","url":"https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/cropped-cropped-Transparent-Logo.png","contentUrl":"https:\/\/blog.amalgamcs.com\/wp-content\/uploads\/2023\/03\/cropped-cropped-Transparent-Logo.png","width":2493,"height":485,"caption":"AmalgamCS"},"image":{"@id":"https:\/\/blog.amalgamcs.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/blog.amalgamcs.com\/#\/schema\/person\/97a98f183f3f756243c26dbed73f8922","name":"Garrik Hoyt","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.amalgamcs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/91f854d9f252604310ae9cef7d5ab86d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/91f854d9f252604310ae9cef7d5ab86d?s=96&d=mm&r=g","caption":"Garrik Hoyt"},"sameAs":["http:\/\/blog.amalgamcs.com"],"url":"https:\/\/blog.amalgamcs.com\/index.php\/author\/amalgamdvlpmnt\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/posts\/191"}],"collection":[{"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":1,"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/posts\/191\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.amalgamcs.com\/index.php\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}