{"id":29147,"date":"2022-03-25T05:45:19","date_gmt":"2022-03-25T05:45:19","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=29147"},"modified":"2022-12-23T06:20:16","modified_gmt":"2022-12-23T06:20:16","slug":"what-is-torch-and-how-it-works-an-overview-and-its-use-cases","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-torch-and-how-it-works-an-overview-and-its-use-cases\/","title":{"rendered":"What is Torch and How it works? An Overview and Its Use Cases ?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Torch?<\/h2>\n\n\n\n<p>PyTorch is a part of computer software based on torch library, which is an open-source Machine learning library for Python. It is a deep learning framework that was developed by the Facebook artificial intelligence research group. Torch Browser is a free and unique software that offers you powerful browsing together with built-in media downloading and sharing features. Torch Browser for Windows is based on the Chromium technology platform, giving it fast browsing capabilities. It is used for applications such as Natural Language Processing and Computer Vision.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.christianperone.com\/wp-content\/uploads\/2018\/03\/pytorch_variable-1-700x253.png\" alt=\"\" width=\"841\" height=\"304\" \/><\/figure>\n\n\n\n<p>The Browser&#8217;s built-in torrent manager makes downloading torrent files simple. You can download torrents without the hassle of any additional software, and manage your torrent downloads straight from your browser. Click the Torrent button to get started. You will see all of your active torrents there. It is downloading simplified.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"71ed\">Some of the key advantages of PyTorch are:<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Simplicity:<\/strong>&nbsp;It is very pythonic and integrates easily with the rest of the Python ecosystem. It is easy to learn, use, extend, and debug.<\/li><li><strong>Great API:&nbsp;<\/strong>PyTorch shines in term of usability due to better designed Object Oriented classes which encapsulate all of the important data choices along with the choice of model architecture. The documentation of PyTorch is also very brilliant and helpful for beginners.<\/li><li><strong>Dynamic Graphs:&nbsp;<\/strong>PyTorch implements dynamic computational graphs. Which means that the network can change behavior as it is being run, with little or no overhead. This is extremely helpful for debugging and also for constructing sophisticated models with minimal effort. allowing PyTorch expressions to be automatically differentiated.<\/li><\/ul>\n\n\n\n<p>There is a growing popularity of PyTorch in research. Below plot showing monthly number of mentions of the word \u201cPyTorch\u201d as a percentage of all mentions among other deep learning frameworks. We can see there is an steep upward trend of PyTorch in arXiv in 2019 reaching almost 50%.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*91yyjqA4nrpyl0CTEcgyeg.png\" alt=\"\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"cad8\">Basic Tensor Operations<\/h2>\n\n\n\n<p id=\"8173\">Tensor refers to the generalization of vectors and matrices to an arbitrary number of dimensions. The dimensionality of a tensor coincides with the number of indexes used to refer to scalar values within the tensor. A tensor of order zero (0D tensor) is just a number or a&nbsp;<em>scalar<\/em>. A tensor of order one (1D tensor) is an array of numbers or a&nbsp;<em>vector<\/em>. Similarly a 2nd-order tensor (2D)is an array of vectors or a&nbsp;<em>matrix<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/752\/1*Q8bknFjSyoN-UIzKc89P9g.png\" alt=\"\" \/><\/figure>\n\n\n\n<p id=\"1c9e\">Now let us create a tensor in PyTorch.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/464\/1*6tEnEI_G_PaSt5cQ9nRNYA.png\" alt=\"\" \/><\/figure>\n\n\n\n<p id=\"fe74\">After importing the torch module, we called a function<em>&nbsp;torch.ones<\/em>&nbsp;that creates a (2D) tensor of size 9 filled with the values 1.0.<\/p>\n\n\n\n<p id=\"aec3\">Other ways include using\u00a0<code><strong>t<em>orch.zeros<\/em><\/strong><\/code><em>;<\/em>\u00a0zero filled tensor,\u00a0<code><strong><em>torch.randn<\/em><\/strong><\/code><em>;\u00a0<\/em>from random uniform distribution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-loading-in-pytorch\">Data Loading in PyTorch<\/h2>\n\n\n\n<p>Data loading is one of the first steps in building a Deep Learning pipeline, or training a model. This task becomes more challenging when the complexity of the data increases. In this section, we will learn about the&nbsp;<code>DataLoader<\/code>&nbsp;class in PyTorch that helps us to load and iterate over elements in a dataset. This class is available as&nbsp;<code>DataLoader<\/code>&nbsp;in the&nbsp;<code>torch.utils.data<\/code>&nbsp;module.&nbsp;<code>DataLoader<\/code>&nbsp;can be imported as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span> torch.utils.data <span class=\"hljs-keyword\">import<\/span> DataLoader<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"transforms-and-rescaling-the-data\">Transforms and Rescaling the Data<\/h2>\n\n\n\n<p>PyTorch transforms define simple image transformation techniques that convert the whole dataset into a unique format. For example, consider a dataset containing pictures of different cars in various resolutions. While training, all the images in our train dataset should have the same resolution size. It&#8217;s time-consuming if we manually convert all the images into the required input size, so we can use transforms instead; with a few lines of PyTorch code, all the images in our dataset can be converted to the desired input size and resolution. You can also resize them using the&nbsp;<code>transforms<\/code>&nbsp;module. The few most commonly used operations are&nbsp;<code>transforms.Resize()<\/code>&nbsp;to resize images,&nbsp;<code>transforms.CenterCrop()<\/code>&nbsp;to crop the images from the center, and&nbsp;<code>transforms.RandomResizedCrop()<\/code>&nbsp;to randomly resize all the images in the dataset.<\/p>\n\n\n\n<p>Let\u2019s now load CIFAR10 from&nbsp;<code>torchvision.datasets<\/code>&nbsp;and apply the following transforms:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Resizing all the images to 32\u00d732<\/li><li>Applying a center crop transform to the images<\/li><li>Converting the cropped images to tensors<\/li><li>Normalizing the images<\/li><\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Torch? PyTorch is a part of computer software based on torch library, which is an open-source Machine learning library for Python. It is a deep learning framework that&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-29147","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=29147"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29147\/revisions"}],"predecessor-version":[{"id":29152,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29147\/revisions\/29152"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=29147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=29147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=29147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}