<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gurkirat Singh</title>
    <description>The latest articles on DEV Community by Gurkirat Singh (@tbhaxor).</description>
    <link>https://dev.to/tbhaxor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F323246%2Fbb3c2644-f2b7-424e-a69b-95af93ff134b.png</url>
      <title>DEV Community: Gurkirat Singh</title>
      <link>https://dev.to/tbhaxor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tbhaxor"/>
    <language>en</language>
    <item>
      <title>Introduction and History of Regular Expression</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Tue, 01 Aug 2023 05:00:00 +0000</pubDate>
      <link>https://dev.to/tbhaxor/motivation-for-inventing-regular-expression-1kib</link>
      <guid>https://dev.to/tbhaxor/motivation-for-inventing-regular-expression-1kib</guid>
      <description>&lt;p&gt;Hello World! Welcome to the "Regular Expressions" series, where we tackle the intimidating syntax that has spawned numerous memes among developers. Don't worry, though! As we move forward, I assure you that you'll gain the confidence to craft your own elegant regular expressions by the end of this journey.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This cover image is taken from Midjourney.ai Discord server.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this series, I will utilise &lt;a href="https://regex101.com" rel="noopener noreferrer"&gt;https://regex101.com&lt;/a&gt; to share patterns with you, avoiding the constraint of a specific programming language that could potentially create barriers for some learners. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;→\to &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 If anywhere coding is required, I will be using C++.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/79WVN-vGllU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Regular Expressions?
&lt;/h2&gt;

&lt;p&gt;Regular expressions are a specific type of &lt;strong&gt;text pattern&lt;/strong&gt; that are used while programming a logic. &lt;/p&gt;

&lt;p&gt;I couldn't think of a single modern application that doesn't make use of it, either directly or indirectly. If you went to a website and entered any gibberish text in the email field, you would have received an invalid email format  message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqkuzu45amkg6hnrj9yc5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqkuzu45amkg6hnrj9yc5.jpg" alt="invalid email" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This image is taken from &lt;a href="https://www.accessguide.io/guide/identify-errors" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Under the hood, your input text is being verified by the following regex pattern.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[a-z0-9.-]@[a-z0-9]{2,}\.[a-z]{2,}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can see how this regex works &lt;a href="https://regex101.com/r/Rd09JD/1" rel="noopener noreferrer"&gt;here&lt;/a&gt;. This is a minimal version of matching simple email addresses. Please bear with me if this appears overwhelming. Such expressions can be created in less than 10 seconds.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why this name after all?
&lt;/h3&gt;

&lt;p&gt;The name "regular expression" originates from the mathematical concept of &lt;a href="https://en.wikipedia.org/wiki/Regular_language" rel="noopener noreferrer"&gt;regular languages&lt;/a&gt;, which were first studied by mathematicians in the field of &lt;a href="https://en.wikipedia.org/wiki/Formal_language" rel="noopener noreferrer"&gt;formal language theory&lt;/a&gt;. Regular expressions are a way to describe and match patterns in strings of characters, hence the name "regular" expressions.&lt;/p&gt;
&lt;h3&gt;
  
  
  Problem that Led Development of RegExp
&lt;/h3&gt;

&lt;p&gt;It was created in the 1950s (far before most of us were born)  to help with &lt;em&gt;text processing&lt;/em&gt; tasks, such as &lt;em&gt;searching&lt;/em&gt; and &lt;em&gt;editing&lt;/em&gt;. Since then, regular expressions have become a standard feature of many programming languages.&lt;/p&gt;

&lt;p&gt;In the early 1960s, &lt;a href="https://en.wikipedia.org/wiki/Ken_Thompson" rel="noopener noreferrer"&gt;Ken Thompson&lt;/a&gt; implemented regular expressions in the QED text editor. This was the first time that regular expressions were used in a practical application.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why You Should Learn Regular Expressions?
&lt;/h3&gt;

&lt;p&gt;Learning regular expressions offers numerous benefits in various fields such as &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;data analysis&lt;/li&gt;
&lt;li&gt;software development&lt;/li&gt;
&lt;li&gt;content management&lt;/li&gt;
&lt;li&gt;scientific research&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Their versatility allows you to define complex patterns for finding specific words or phrases, extracting data from structured text, and performing advanced search and replace operations. &lt;/p&gt;

&lt;p&gt;Additionally, mastering regular expressions helps prevent costly mistakes by providing a precise and controlled approach to text processing. With a solid understanding of regular expressions, you can confidently handle challenging text manipulation tasks, ensuring accuracy, reliability, and improved productivity.&lt;/p&gt;

&lt;p&gt;Since you're here, I'm assuming you're ready to understand and use those unwieldy strings of brackets and question marks in your code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Flavours of RegExp
&lt;/h2&gt;

&lt;p&gt;There is no established standard that specifies which text patterns are and are not regular expressions. There are numerous languages on the market whose creators have various ideas about how regular expressions should look. So we're now stuck with a whole spectrum of regular expression &lt;a href="https://www.regular-expressions.info/tutorial.html#engine" rel="noopener noreferrer"&gt;&lt;em&gt;flavours&lt;/em&gt;&lt;/a&gt; (implementation of RegExp in the programming language). &lt;/p&gt;

&lt;p&gt;But why reinvent the wheel? Instead, every modern regular expression engine may be traced back to the Perl programming language.&lt;/p&gt;

&lt;p&gt;Regular expression flavours are commonly integrated into scripting languages, while other programming languages rely on dedicated libraries for regex support. JavaScript offers built-in support for regular expressions using the syntax &lt;code&gt;/expr/&lt;/code&gt; or the &lt;code&gt;RegExp&lt;/code&gt; object. On the other hand, Python implements regular expressions through its standard library &lt;a href="https://docs.python.org/3/library/re.html" rel="noopener noreferrer"&gt;&lt;code&gt;re&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  References
&lt;/h2&gt;


&lt;div class="ltag__wikipedia--container"&gt;
  &lt;div class="ltag__wikipedia--header"&gt;
    &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fwikipedia-logo-0a3e76624c7b1c3ccdeb9493ea4add6ef5bd82d7e88d102d5ddfd7c981efa2e7.svg" class="ltag__wikipedia--logo" alt="Wikipedia Logo" width="128" height="128"&gt;
    &lt;a href="https://en.wikipedia.org/wiki/Automata_theory" rel="noopener noreferrer"&gt;Automata theory&lt;/a&gt;
  &lt;/div&gt;
  &lt;div class="ltag__wikipedia--extract"&gt;&lt;p&gt;&lt;b&gt;Automata theory&lt;/b&gt; is the study of abstract machines and automata, as well as the computational problems that can be solved using them. It is a theory in theoretical computer science with close connections to mathematical logic. The word &lt;i&gt;automata&lt;/i&gt; comes from the Greek word αὐτόματος, which means "self-acting, self-willed, self-moving". An automaton is an abstract self-propelled computing device which follows a predetermined sequence of operations automatically. An automaton with a finite number of states is called a finite automaton (FA) or finite-state machine (FSM). The figure on the right illustrates a finite-state machine, which is a well-known type of automaton. This automaton consists of states and transitions. As the automaton sees a symbol of input, it makes a transition to another state, according to its transition function, which takes the previous state and current input symbol as its arguments.&lt;/p&gt;&lt;/div&gt;
  &lt;div class="ltag__wikipedia--btn--container"&gt;
      &lt;a class="ltag__wikipedia--btn" href="https://en.wikipedia.org/wiki/Automata_theory" rel="noopener noreferrer"&gt;View on Wikipedia&lt;/a&gt;&amp;gt;
  &lt;/div&gt;
&lt;/div&gt;




&lt;div class="ltag__wikipedia--container"&gt;
  &lt;div class="ltag__wikipedia--header"&gt;
    &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fwikipedia-logo-0a3e76624c7b1c3ccdeb9493ea4add6ef5bd82d7e88d102d5ddfd7c981efa2e7.svg" class="ltag__wikipedia--logo" alt="Wikipedia Logo" width="128" height="128"&gt;
    &lt;a href="https://en.wikipedia.org/wiki/Regular_expression" rel="noopener noreferrer"&gt;Regular expression&lt;/a&gt;
  &lt;/div&gt;
  &lt;div class="ltag__wikipedia--extract"&gt;&lt;p&gt;A &lt;b&gt;regular expression&lt;/b&gt;, sometimes referred to as &lt;b&gt;rational expression&lt;/b&gt;, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regular expression techniques are developed in theoretical computer science and formal language theory.&lt;/p&gt;&lt;/div&gt;
  &lt;div class="ltag__wikipedia--btn--container"&gt;
      &lt;a class="ltag__wikipedia--btn" href="https://en.wikipedia.org/wiki/Regular_expression" rel="noopener noreferrer"&gt;View on Wikipedia&lt;/a&gt;&amp;gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;div class="ltag__wikipedia--container"&gt;
  &lt;div class="ltag__wikipedia--header"&gt;
    &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fwikipedia-logo-0a3e76624c7b1c3ccdeb9493ea4add6ef5bd82d7e88d102d5ddfd7c981efa2e7.svg" class="ltag__wikipedia--logo" alt="Wikipedia Logo" width="128" height="128"&gt;
    &lt;a href="https://en.wikipedia.org/wiki/Formal_language" rel="noopener noreferrer"&gt;Formal language&lt;/a&gt;
  &lt;/div&gt;
  &lt;div class="ltag__wikipedia--extract"&gt;&lt;p&gt;In logic, mathematics, computer science, and linguistics, a &lt;b&gt;formal language&lt;/b&gt; consists of words whose letters are taken from an alphabet and are well-formed according to a specific set of rules called a formal grammar.&lt;/p&gt;&lt;/div&gt;
  &lt;div class="ltag__wikipedia--btn--container"&gt;
      &lt;a class="ltag__wikipedia--btn" href="https://en.wikipedia.org/wiki/Formal_language" rel="noopener noreferrer"&gt;View on Wikipedia&lt;/a&gt;&amp;gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Contact Me
&lt;/h2&gt;

&lt;p&gt;Email: tbhaxor &lt;em&gt;at&lt;/em&gt; proton &lt;em&gt;dot&lt;/em&gt; me&lt;br&gt;
LinkedIn: &lt;code&gt;@tbhaxor&lt;/code&gt;&lt;br&gt;
Twitter: &lt;code&gt;@tbhaxor&lt;/code&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>automata</category>
      <category>cpp</category>
      <category>regex</category>
    </item>
    <item>
      <title>How to Think Before Designing Algorithms</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Sun, 19 Jun 2022 00:02:32 +0000</pubDate>
      <link>https://dev.to/tbhaxor/how-to-think-before-designing-algorithms-gg9</link>
      <guid>https://dev.to/tbhaxor/how-to-think-before-designing-algorithms-gg9</guid>
      <description>&lt;p&gt;Hello everyone! I am back with a new post which most of the new students struggle while learning about algorithms in undergrad courses. In this post, I will discuss the step by step process on how the developers usually undergo while thinking of solution to the problem and then designing algorithms.&lt;/p&gt;

&lt;p&gt;It's the computer scientists who propose and invent new algorithms with improved features (usually time complexity) from the previous ones. For example, just look at searching algorithms. At the end, new constraints or inputs are added to the algorithms. The developers then use this algorithms and fit into the problems to get the things done in less time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Defining Problem
&lt;/h2&gt;

&lt;p&gt;The problem created while discussion with the client and project manager isn't particularly technical, and it needs to be translated into technical terms so that it can be understood by the developers. Nerds!&lt;/p&gt;

&lt;p&gt;For example let's take a simple problem statements. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How many users are coming to the application in a day and going to the checkout page and how many are abandoning it. Consider the timeout to be 300 seconds?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This could be shortened to the following&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Classify the checkout session as completed or abandoned if it is inactive for 300 seconds, and then group by this label starting from 00:00:00 UTC to 23:59:59 UTC every day.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 2: Organising the Steps to Solve
&lt;/h2&gt;

&lt;p&gt;From the problem statements, now it's time to interpret the statement and create the steps involved in the checkout completion or abandonment. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Checkout session create&lt;/li&gt;
&lt;li&gt;Get the status by differentiating the current time with create time&lt;/li&gt;
&lt;li&gt;If the time difference is 300 seconds, then mark it abandoned&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 3: Automate it via an Algorithm
&lt;/h2&gt;

&lt;p&gt;Use any programming and database to find all the checkout sessions created whose create time is less than &lt;em&gt;current time&lt;/em&gt; and greater then &lt;em&gt;current time - 300 seconds&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I have used MongoDB to implement this logic. You can check it on the playground, shared link below. I would recommend you to tinker with it and learn how it is working.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://mongoplayground.net/p/o4kygHi7ncJ" rel="noopener noreferrer"&gt;
      mongoplayground.net
    &lt;/a&gt;
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; This is supposed to be run periodically, therefore we need a cron job like feature in the application which is supposed to be executed at end of the day, everyday (&lt;code&gt;59 59 23 * * *&lt;/code&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 4: Test it and Fix it Iteratively
&lt;/h2&gt;

&lt;p&gt;What if the customer finally completes the checkout after some time has passed and the cron job has not yet been run? In the current scenario, this session will also be marked as &lt;em&gt;abandoned&lt;/em&gt;. &lt;strong&gt;False positive&lt;/strong&gt; is the term for this type of error.&lt;/p&gt;

&lt;p&gt;Let's come to the second error type: What if the checkout session is &lt;em&gt;abandoned&lt;/em&gt; for greater than 300 secs. It should also be counted as the &lt;em&gt;abandoned&lt;/em&gt;, but with current logic, it is counted as &lt;em&gt;completed&lt;/em&gt;. &lt;strong&gt;False negative&lt;/strong&gt; is the term for this type of error.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the fix?
&lt;/h3&gt;

&lt;p&gt;If the checkout is completed, there must be an order id linked to it. Therefore, if the property exists in the document, then the checkout is completed, otherwise it was abandoned by the user.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://mongoplayground.net/p/8asCBuvwMv9" rel="noopener noreferrer"&gt;
      mongoplayground.net
    &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Step 5: Make Sure your Solution is Effective and Efficient
&lt;/h2&gt;

&lt;p&gt;Create a reusable function and try the same algorithm on real data after you've gotten your algorithm to work on the dummy data. This will help you decide whether or not to include it in the code base.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCheckoutStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;startDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;endDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// get all the &lt;/span&gt;
        &lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;$gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;startDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;$lt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;endDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;$addFields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// set status to completed if orderId exists; otherwise, abandon.&lt;/span&gt;
        &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;$cond&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$orderId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;completed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abandoned&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// group and aggregate the count&lt;/span&gt;
        &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$status&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The SQL equivalent of this could be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;completed&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`orderId`&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;abandoned&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nv"&gt;`orderId`&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you get an error in this step, it means you didn't understand the problem to begin with. Return to step 2 and repeat the process.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Comprehensive Guide on File Handling in C++</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Sat, 09 Apr 2022 07:51:26 +0000</pubDate>
      <link>https://dev.to/tbhaxor/a-comprehensive-guide-to-file-handling-in-c-2p1b</link>
      <guid>https://dev.to/tbhaxor/a-comprehensive-guide-to-file-handling-in-c-2p1b</guid>
      <description>&lt;p&gt;Hello World! You have been working with data in the program and struggling with saving its states across multiple restarts. Yeah, I know that is temporary and will be gone as the program exists. In this post, I will teach you how you can use C++ file handling and save not only text data into it but also binary data like Class or Struct.&lt;/p&gt;

&lt;p&gt;You can clone the repository for all the snippets and source code used in this tutorial &lt;a href="https://github.com/tbhaxor/cpp-file-handling-tutorial" rel="noopener noreferrer"&gt;https://github.com/tbhaxor/cpp-file-handling-tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Why do you need File Handling Anyway?&lt;/li&gt;
&lt;li&gt;
Getting Started with File Handling

&lt;ul&gt;
&lt;li&gt;Text File vs Binary File&lt;/li&gt;
&lt;li&gt;What is EOF&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Writing into the File&lt;/li&gt;
&lt;li&gt;
Reading from the File

&lt;ul&gt;
&lt;li&gt;Reading until line end (EOL)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Check Whether the File is Open or Not&lt;/li&gt;
&lt;li&gt;Closing file handle&lt;/li&gt;
&lt;li&gt;Appending into the Existing File&lt;/li&gt;
&lt;li&gt;Storing Class Object in the File&lt;/li&gt;
&lt;li&gt;
Using I/O Streams of the File

&lt;ul&gt;
&lt;li&gt;Read/Write Cursors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why do you need File Handling Anyway?
&lt;/h2&gt;

&lt;p&gt;The program state gets vanished when it is exited or the functions go out of scope during execution. Sometimes it's required to have the states for future execution.&lt;/p&gt;

&lt;p&gt;Take the example of a game, you don't play it continuously. Saving the same and loading it later is very basic. I had first seen this in the Project IGI game and then Age of Empires. Nowadays multiplayer games are getting popular and they use databases to store this information.&lt;/p&gt;

&lt;p&gt;That's a great idea to use databases. So why not use the database, it can be shared easily. Let me tell you that there is no different magic going on there, DBMS uses files to store the data. So it boils down to file handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with File Handling
&lt;/h2&gt;

&lt;p&gt;There are and will be 2 types of operation in the file: Read file will allow you to retrieve the content and write file will allow you to put the content into the file using the current stream. A stream is basically a channel that carries your data from/to the file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.cplusplus.com/reference/fstream/ifstream/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ifstream&lt;/code&gt;&lt;/a&gt; class is used to open the file in reading mode and &lt;a href="https://www.cplusplus.com/reference/fstream/ofstream/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ofstream&lt;/code&gt;&lt;/a&gt; is used to open the file in the write mode.&lt;/p&gt;

&lt;p&gt;These classes provide different methods and properties as an abstraction to deal with files, or I would say even the large files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Text File vs Binary File
&lt;/h3&gt;

&lt;p&gt;There are two types of files: Human readable (or text) and human non-readable (binary).&lt;/p&gt;

&lt;p&gt;It is easy to determine whether a file is a text or binary: If you open a file in a text editor (let's assume notepad or vscode), it will show all the characters and you can pronounce them. They might or might not make sense, but it's not required here. This file is a &lt;strong&gt;text file&lt;/strong&gt;. If you are unable to pronounce any character, then that file is a &lt;strong&gt;binary file&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; A binary file can contain a few readable characters as well, and that is ok.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Text File&lt;/th&gt;
&lt;th&gt;Binary File&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7tg2gbueccf8aps3qaf.png" alt="Text File" width="688" height="286"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fed7q2x2yve3i9t56x702.png" alt="Binary File" width="778" height="252"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  What is EOF
&lt;/h3&gt;

&lt;p&gt;While seeing the file format and the number of total bytes, we can determine that there are 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;nn &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 bytes in the file that can be read, but how does an opened stream will know in general? EOF is also known as &lt;a href="https://en.wikipedia.org/wiki/End-of-file" rel="noopener noreferrer"&gt;End-of-File&lt;/a&gt; in computing. It is a condition or a notification to the file reader that tells no more retrieve more information from the file and seems like it is ended. &lt;/p&gt;

&lt;p&gt;When you try to read a file and it is failed, both &lt;em&gt;failed&lt;/em&gt; and &lt;em&gt;eof&lt;/em&gt; bits are set in the error flags. You can use &lt;a href="https://www.cplusplus.com/reference/ios/ios/eof/" rel="noopener noreferrer"&gt;&lt;code&gt;.eof()&lt;/code&gt;&lt;/a&gt; method to get this information.&lt;/p&gt;

&lt;p&gt;So when does this bit set? An EOF bit is set after the ifstream reader tried to read the contents from the file and fails while the stream is still good (there is no error while performing I/O operations). So at least in the case of an ifstream, it waits for the OS to tell it that there's no more data &lt;sup&gt;&lt;a href="https://www.reddit.com/r/cpp_questions/comments/ty1hiz/comment/i3ppceo/?utm_source=share&amp;amp;utm_medium=web2x&amp;amp;context=3" rel="noopener noreferrer"&gt;read more&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is not only used for files but also streams and sockets&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Programmatically, it will look like the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ifile&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ifile&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;eof&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; 
          &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ifile&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;fail&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// 0 1 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing into the File
&lt;/h2&gt;

&lt;p&gt;Enough talking, let's start by writing a text content into the file using &lt;code&gt;std::ofstream&lt;/code&gt;. Before all of that, you need to instantiate the ofstream and open the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ofstream&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ofstream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can use &lt;code&gt;std::ofstream file; // w/o pointer&lt;/code&gt; as well, but I prefer pointers. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;a href="https://www.cplusplus.com/reference/fstream/ifstream/open/" rel="noopener noreferrer"&gt;&lt;code&gt;.open()&lt;/code&gt;&lt;/a&gt; function, as it says, open the file with name (&lt;em&gt;first argument&lt;/em&gt;) and the mode (&lt;em&gt;second argument&lt;/em&gt;). &lt;/p&gt;

&lt;p&gt;I will discuss different modes with you as we proceed, &lt;code&gt;std::ios::out&lt;/code&gt; mode is used to tell the function, that opens the file in write mode, overwrite the contents of it if it exists or create a new one.&lt;/p&gt;

&lt;p&gt;Now it's time to write into the file stream using the left shift operator &lt;a href="https://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/" rel="noopener noreferrer"&gt;&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;&lt;/a&gt;. This will format all your data into &lt;code&gt;char*&lt;/code&gt; before writing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we are using a pointer variable, therefore it is required to dereference the file handle to access the &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; operator from the &lt;code&gt;std::ofstream&lt;/code&gt; object stored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading from the File
&lt;/h2&gt;

&lt;p&gt;Reading is pretty simple and uses &lt;code&gt;std::ifstream&lt;/code&gt; and &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; operator with &lt;code&gt;std::ios::in&lt;/code&gt; mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ifstream&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ifstream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For reading files in text mode, there is only one option &lt;a href="https://www.cplusplus.com/reference/ios/ios_base/openmode/?kw=openmode" rel="noopener noreferrer"&gt;&lt;code&gt;std::ios::in&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Hello\n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reading until line end (eol)
&lt;/h3&gt;

&lt;p&gt;When you write a file with &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; it formats whitespaces and streams that to the file, but while reading the content, it will stop at either EOF or the first whitespace character hit. &lt;/p&gt;

&lt;p&gt;EOL or End-of-line is defined by the character &lt;code&gt;\n&lt;/code&gt; or &lt;code&gt;\r\n&lt;/code&gt; and is considered as the whitespace. So if there are multiple lines, you can use the &lt;a href="https://www.cplusplus.com/reference/string/string/getline/" rel="noopener noreferrer"&gt;&lt;code&gt;std::getline()&lt;/code&gt;&lt;/a&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Hello World\n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Check Whether the File is Open or Not
&lt;/h2&gt;

&lt;p&gt;Till now we are believing that the file exists and is ready for any I/O operation, but what if the condition is otherwise? What if it failed to open? Will it throw an error?&lt;/p&gt;

&lt;p&gt;The answer to the question is, &lt;strong&gt;NO&lt;/strong&gt;. It will not throw error but the RW will not work. You can use &lt;a href="https://www.cplusplus.com/reference/fstream/ifstream/is_open/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ifstream::is_open()&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://www.cplusplus.com/reference/fstream/ofstream/is_open/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ofstream::is_open()&lt;/code&gt;&lt;/a&gt;. The functions returns boolean value, so if the file is actually opened successfully, it will return true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;is_open&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;// do the rest actions here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Unable to open file&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing file handle
&lt;/h2&gt;

&lt;p&gt;When you are done with the file operations, it is recommended to close the file handle which will dispose of the file stream after writing any pending sequence of characters and clearing the buffer for both input and output streams.&lt;/p&gt;

&lt;p&gt;For this you can use &lt;a href="https://www.cplusplus.com/reference/fstream/ofstream/close/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ofstream::close()&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://www.cplusplus.com/reference/fstream/ifstream/close/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ifstream::close&lt;/code&gt;&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the file is not successfully opened, the function will fail and set the &lt;code&gt;failbit&lt;/code&gt; flag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Appending into the Existing File
&lt;/h2&gt;

&lt;p&gt;What if you want to append the data into the file without losing old content. In the &lt;code&gt;ios::out&lt;/code&gt;, it overwrites the file and reading the content of files, appending in memory and then writing to a file doesn't seem to be a space and time-efficient solution. &lt;/p&gt;

&lt;p&gt;There is another mode of opening the file that you can use in place of &lt;code&gt;ios::out&lt;/code&gt;, which is append mode &lt;code&gt;ios::app&lt;/code&gt;. This will allow you to write from the end of the file to the size of the data. &lt;/p&gt;

&lt;p&gt;When you choose to open the file with append mode, the put cursor of the handle is set at the end of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Storing Class Object in the File
&lt;/h2&gt;

&lt;p&gt;So far you have seen me dealing with the text file, but in the real world, there are more than binary files with the custom format, like Zip archives, PDF files and other document files. &lt;/p&gt;

&lt;p&gt;To open the file in binary mode, you need to specify &lt;code&gt;std::ios::binary&lt;/code&gt; mode in the &lt;code&gt;open()&lt;/code&gt; method of &lt;code&gt;std::ifstream&lt;/code&gt; or &lt;code&gt;std::ofstream&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;in_file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.bin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;out_file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.bin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For reading and writing, you must use &lt;a href="https://www.cplusplus.com/reference/istream/istream/read/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ifstream::read()&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://www.cplusplus.com/reference/ostream/ostream/write/" rel="noopener noreferrer"&gt;&lt;code&gt;std::ofstream::write()&lt;/code&gt;&lt;/a&gt; because the binary file saves the raw bytes and does not perform any kind of formatting.&lt;/p&gt;

&lt;p&gt;With this, you can now store the object of structs or classes directly into the file without serializing it into the textual format. It will be then automatically deserialized when the file is read.&lt;/p&gt;

&lt;p&gt;Let's create a simple class called &lt;code&gt;Student&lt;/code&gt; that will have &lt;em&gt;name&lt;/em&gt; and &lt;em&gt;age&lt;/em&gt; fields and a &lt;em&gt;whoami()&lt;/em&gt; method to return a std::string object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;whoami&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;stringstream&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"I am "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" and "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" years old"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;Student&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Gurkirat Singh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can save this to a file using the following snippet by casing it to &lt;code&gt;char*&lt;/code&gt; because of its definition in the stdc++. &lt;sup&gt;&lt;a href="https://stackoverflow.com/a/45202852/10362396" rel="noopener noreferrer"&gt;read more&lt;/a&gt;&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;out_file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this is successfully saved into the file, you will verify that there are some weird characters present in the file and that is normal, because the string data also stores the information of deserializing the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;mystudent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;in_file&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;mystudent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mystudent&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;mystudent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;whoami&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// I am Gurkirat Singh and 20 years old&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now as a beginner, you must be thinking that the &lt;code&gt;Student&lt;/code&gt; type is different from the &lt;code&gt;char&lt;/code&gt; type. So why does the casting pointer not affect serialization? I would like to tell you that, you are getting confused between pointer type and other data types. For any data type, the pointer type would be 8 bytes or 4 bytes based on the CPU architecture, because it stores the starting address of the memory where that actual data of a type is stored. If the type of data is char, then it will consume 1 byte in the memory otherwise &lt;code&gt;sizeof(T)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using I/O Streams of the File
&lt;/h2&gt;

&lt;p&gt;It is not like you can open a file only in one mode (that is either read or write), using &lt;code&gt;std::fstream&lt;/code&gt; you can perform both input/output operations on the file with the same file handle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Read/Write Cursors
&lt;/h3&gt;

&lt;p&gt;There are two types of cursors in the file &lt;strong&gt;&lt;code&gt;g&lt;/code&gt;&lt;/strong&gt;et and &lt;strong&gt;&lt;code&gt;p&lt;/code&gt;&lt;/strong&gt;ut. The get cursor is responsible for handling reading from the file which is known as &lt;strong&gt;input operation&lt;/strong&gt; and the put cursor is responsible for handling writing into the file which is known as &lt;strong&gt;output operation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To &lt;strong&gt;get&lt;/strong&gt; the current position of the cursors, use the &lt;a href="https://www.cplusplus.com/reference/ostream/ostream/tellp/?kw=tellp" rel="noopener noreferrer"&gt;&lt;code&gt;std::fstream::tellp()&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://www.cplusplus.com/reference/istream/istream/tellg/?kw=tellg" rel="noopener noreferrer"&gt;&lt;code&gt;std::fstream::tellg()&lt;/code&gt;&lt;/a&gt;. You can also set the position of these cursors making (either forward or backward) while the file is open using &lt;a href="https://www.cplusplus.com/reference/ostream/ostream/seekp/?kw=seekp" rel="noopener noreferrer"&gt;&lt;code&gt;std::fstream::seekp()&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://www.cplusplus.com/reference/istream/istream/seekg/?kw=seekg" rel="noopener noreferrer"&gt;&lt;code&gt;std::fstream::seekg()&lt;/code&gt;&lt;/a&gt; functions.&lt;/p&gt;

&lt;p&gt;You can also seek the cursor relatively based on the three types of the &lt;a href="https://www.cplusplus.com/reference/ios/ios_base/seekdir/" rel="noopener noreferrer"&gt;seek directions&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;std::ios::beg&lt;/code&gt; — from the beginning of file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;std::ios::cur&lt;/code&gt; — from the current position of the cursor&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;std::ios::end&lt;/code&gt; — from the ending of the file or where the reader hits EOF&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; You must clear the flags using &lt;a href="https://www.cplusplus.com/reference/ios/ios/clear/" rel="noopener noreferrer"&gt;&lt;code&gt;std::fstream::clear()&lt;/code&gt;&lt;/a&gt; to clear all the error bits before using &lt;code&gt;seekg()&lt;/code&gt; or &lt;code&gt;seekp()&lt;/code&gt;. &lt;sup&gt;&lt;a href="https://stackoverflow.com/questions/16364301/whats-wrong-with-the-ifstream-seekg" rel="noopener noreferrer"&gt;read more&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to Linux for Developers</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Thu, 24 Mar 2022 19:50:44 +0000</pubDate>
      <link>https://dev.to/tbhaxor/introduction-to-linux-for-developers-28df</link>
      <guid>https://dev.to/tbhaxor/introduction-to-linux-for-developers-28df</guid>
      <description>&lt;p&gt;Hello friends! In this post, I will try to explain to you why I prefer Linux-based OS for development. Not only because it's free, but also you have full control over organising and customising the OS as per your requirements, no enforcement from the vendor's end. I will try to explain the working of Linux in brief and try to gain your trust in switching to any Linux-based OS.&lt;/p&gt;

&lt;p&gt;If you are coming from Windows OS, in the starting, it would be hard for you to grasp the knowledge because till now in a closed source operating system everything is pre-built for you. All you need to do is give a certain partition configuration and install it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Linux for Development?
&lt;/h2&gt;

&lt;p&gt;I have seen many developers misunderstand Linux as an operating system and then get puzzled by different distros. Guys hold your pace! Linux is just a kernel, which means it's the core that allows bridging the gap between hardware and the software world. When you have a kernel with a bunch of software that is commonly used in our daily lives, we call it an Operating System.&lt;/p&gt;

&lt;p&gt;Yes but, then why do you need external drivers for communicating new hardware devices? Well, long story short, new devices are coming into the market every day and it's kinda hard for Linux developers to maintain a repository for all of them. Kernel focuses on managing the crucial resources like CPU execution time, memory, disk, and networking in the OS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is it really a synonym of CLI?
&lt;/h3&gt;

&lt;p&gt;Yeah when I was a beginner in OS and encountered my first ever SSH connection back in days while learning Linux in my college, I also made this perception that Linux is all about handling CLI like old school in DOS. Well, this is not true. In Linux, there are several third-party software you can install and start the GUI. It's the choice of the user whether to work like on a terminal or a GUI. In this section, I will guide you about different packages that build up the Graphical Interface on top of Linux Kernel.&lt;/p&gt;

&lt;p&gt;On the ground level, it's a minimal server used for handling the communication between GUI elements like mouse and keyboard by defining a set of protocols for the same. One of the most common examples of this is the X11 server or the Way-land.&lt;/p&gt;

&lt;p&gt;On the top of X Server, window managers are built to handle different windows to open the application. You see the close, minimise and maximise buttons and think how a window is created to show the content of a particular process in it, well which is all handled by these window managers. Apart from basic window management, they offer additional services out of the box like tabs, visual effects, and multiple desktops. An example of this could be KWin and Mutter.&lt;/p&gt;

&lt;p&gt;You might have heard people often debating about KDE vs Gnome, etc. Well, those are examples of what we call Desktop Environments. They are built on top of window managers and managed by specific communities. It consists of all the magical things you see in the GUI, like different panels, widgets, drag-and-drop features of files and similar contents, the configuration of services via GUI-based inputs controls like switches and knobs.&lt;/p&gt;

&lt;p&gt;These all 3 components – X Window System + Window Manager + Desktop Environment work in harmony to provide a full graphical experience in the Linux-based distros. The cool thing is you have full autonomy to decide which of these to use and with what type of customisation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading Man Pages - User manual for Linux
&lt;/h2&gt;

&lt;p&gt;In this journey, documentation is your best friend. You should be able to understand a lot of things from the documentation provided by the project maintainers to build, customize and troubleshoot them. In Linux, you can use man pages for that using man command.&lt;/p&gt;

&lt;p&gt;These manual pages are used to get help regarding any command or library function shipped with the packages. See &lt;a href="https://man7.org/linux/man-pages/man1/man.1.html" rel="noopener noreferrer"&gt;man man&lt;/a&gt; for more information. Sometimes it's hard to remember the function name or the exact thing to look but we have an idea of what we are looking for. So in that case you can use &lt;a href="https://man7.org/linux/man-pages/man1/apropos.1.html" rel="noopener noreferrer"&gt;apropos&lt;/a&gt; the command followed by the text to find the topic and then later use the man page for it.&lt;/p&gt;

&lt;p&gt;For example, I forgot a command to get the memory dump of the program. So in this case I can use the apropos ssh copy command and then it will search from the man page index locally and get the appropriate topic with a brief description about it to differentiate the usage among others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ apropos ssh cop
btrfs-select-super (8) - overwrite primary superblock with a backup copy
cp (1)               - copy files and directories
cpgr (8)             - copy with locking the given file to the password or group file
cpio (1)             - copy files to and from archives
cppw (8)             - copy with locking the given file to the password or group file
dd (1)               - convert and copy a file
debconf-copydb (1)   - copy a debconf database
git-checkout-index (1) - Copy files from the index to the working tree
git-shell (1)        - Restricted login shell for Git-only SSH access
hpcopy (1)           - copy files from an HFS+ volume
install (1)          - copy files and set attributes
mcopy (1)            - copy MSDOS files to/from Unix
ntfscp (8)           - copy file to an NTFS volume.
objcopy (1)          - copy and translate object files
rcp (1)              - OpenSSH secure file copy
scp (1)              - OpenSSH secure file copy
sg_copy_results (8)  - send SCSI RECEIVE COPY RESULTS command (XCOPY related)
sg_dd (8)            - copy data to and from files and devices, especially SCSI devices
sg_xcopy (8)         - copy data to and from files and devices using SCSI EXTENDED COPY (XCOPY)
sgm_dd (8)           - copy data to and from files and devices, especially SCSI devices
sgp_dd (8)           - copy data to and from files and devices, especially SCSI devicesy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have trimmed a lot of output here, but you can add some strict constraints like only getting the entries if it has all the keywords otherwise ignore them. Well, you will have to add -a switch after the apropos command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ apropos -a ssh copy
rcp (1)              - OpenSSH secure file copy
scp (1)              - OpenSSH secure file copy
ssh-copy-id (1)      - use locally available keys to authorise logins on a remote machine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Types of Man Page Categories
&lt;/h3&gt;

&lt;p&gt;As you can see there are different numbers assigned to similar or different entries in the index. What does that mean? Well, there are about 8 different categories of information in Linux and each command or same function are logically grouped into them. The following is copied from the man page of the man using &lt;code&gt;man man&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The table below shows the section numbers of the manual followed by the types of pages they contain.


       1   Executable programs or shell commands
       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
       4   Special files (usually found in /dev)
       5   File formats and conventions, e.g. /etc/passwd
       6   Games
       7   Miscellaneous (including macro packages and conventions),
           e.g. man(7), groff(7)
       8   System administration commands (usually only for root)
       9   Kernel routines [Non standard]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the time you will deal with 1, 2, 4, and 8 sections only. Others are helpful when you are into Linux system development.&lt;/p&gt;

&lt;p&gt;You can get the information of different sections either from apropos search or "SEE ALSO" section on the man page of a specific entry. For example, while reading the man's man page, I found there is one more entry for the same man(7).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC5612AQFA8Xj74x2Lfg%2Farticle-inline_image-shrink_1000_1488%2F0%2F1646846732653%3Fe%3D1653523200%26v%3Dbeta%26t%3D-jh54KBhx9_Yz9h-q8-zscv0XxHzNSg0P5ZntyUJKog" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia-exp1.licdn.com%2Fdms%2Fimage%2FC5612AQFA8Xj74x2Lfg%2Farticle-inline_image-shrink_1000_1488%2F0%2F1646846732653%3Fe%3D1653523200%26v%3Dbeta%26t%3D-jh54KBhx9_Yz9h-q8-zscv0XxHzNSg0P5ZntyUJKog" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default it will try to look into the very first category, you can provide additional argument before the name that specifies a particular category to look into. If the entry does not exist in the man database, it will return an error otherwise open the man page for you. For example, in this case, if you can open the 7th category man page of the man using &lt;strong&gt;&lt;code&gt;man 7 man&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Authentication Best Practices in the Web Applications</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Thu, 10 Mar 2022 05:00:55 +0000</pubDate>
      <link>https://dev.to/tbhaxor/authentication-best-practices-in-the-web-applications-2h52</link>
      <guid>https://dev.to/tbhaxor/authentication-best-practices-in-the-web-applications-2h52</guid>
      <description>&lt;p&gt;Hello World! Web development is one of the most popular fields nowadays and we can see every day new developers are joining this field because of high-paying jobs. Javascript is the most common and loved language among developers because it offers a lot of frameworks available to make the development process look like a piece of cake and its ecosystem of course.&lt;/p&gt;

&lt;p&gt;In my past working with different teams, I have realized that developers (sometimes including me, I am not the perfect guy) do not take the most crucial processes of authentication seriously and because of this, it leaves security vulnerabilities in the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Authentication Anyway?
&lt;/h2&gt;

&lt;p&gt;Let's understand with a very obvious example what does authentication means in the web application. Suppose you are going to the nightclub to have a beer and the person in charge ask for your identity to confirm your age which is required to enter. Now if you have this and the age is above that of what the government has decided, you are "&lt;em&gt;allowed&lt;/em&gt;" to enter the club and you will be stamped on your hand. Whenever you are supposed to "&lt;em&gt;prove your identity&lt;/em&gt;" to another party, the process is known as &lt;strong&gt;authentication&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does authentication differ from authorisation?
&lt;/h3&gt;

&lt;p&gt;I see people often confused with authentication and authorisation. Well at some point I also got confused but later it seemed a thin line between these two. Let's take the same club example and explore this.&lt;/p&gt;

&lt;p&gt;Suppose you are willing to go inside the club and explore its resources, but you forgot to carry the identity card. In that case, you have no right or access to its infrastructure. One can also say that "Hey you are not authorised to enter the club". Or even though you have passed the first process of authentication, but still there are some areas where only the staff of the club are allowed to enter (like the kitchen or behind the bartender table). So whenever "&lt;strong&gt;&lt;em&gt;right to access&lt;/em&gt;&lt;/strong&gt;" comes into the frame, you are talking about &lt;strong&gt;authorisation&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we see from the top, the authentication is also a form of authorisation which will give you right to access the resources that you are initially not allowed to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Understanding Cookies in Detail
&lt;/h2&gt;

&lt;p&gt;The session is stored on the server-side and the public identity of the session is saved on the client side every time there is a successful call to the login route. The server instructs the client by sending the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie" rel="noopener noreferrer"&gt;&lt;code&gt;Set-Cookie&lt;/code&gt;&lt;/a&gt; header in the response with some extra data that instruct the browser on how to store this information.&lt;/p&gt;

&lt;p&gt;Let's try sending an authentication request to a live server and see how it responds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="s1"&gt;'https://authenticationtest.com//login/?mode=simpleFormAuth'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--data-raw&lt;/span&gt; &lt;span class="s1"&gt;'email=simpleForm%40authenticationtest.com&amp;amp;password=pa%24%24w0rd'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--compressed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well as you can see in the response I have the cookie name &lt;code&gt;PHPSESSID&lt;/code&gt; and its value &lt;code&gt;2ifo....&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vrhsmw9vtqvemqgssmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vrhsmw9vtqvemqgssmo.png" alt="Cookie headers from the authenticated response" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this cookie is set successfully on the browser storage, it is automatically mapped with the current domain name (here &lt;em&gt;authenticationtest.com&lt;/em&gt;). So whenever the request will be made to the resources on this domain, the browser will automatically include the cookie name and its value in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie" rel="noopener noreferrer"&gt;&lt;code&gt;Cookie&lt;/code&gt;&lt;/a&gt; header which will be then used by these resources to identify the user.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Cookies are also used by tracking services like google ads, Facebook pixel and etc. So when you enable anti-trackers on the browser, they simply disallow the browser setting these cookies.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Setting the Expiration Configuration
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Max-Age&lt;/code&gt; option in the Set-Cookie header tells the browser after how many seconds automatically expire this cookie and delete it from its storage. This is optional because there is also another option by setting the &lt;code&gt;Expires&lt;/code&gt; option which accepts the UTC format of date and time exactly when to expire the cookies on the client-side.&lt;/p&gt;

&lt;p&gt;When the Max-Age is zero or a negative number, it will expire the cookie immediately after storing and when the Expires option is present along with this, the Max-Age has more precedence.&lt;/p&gt;

&lt;p&gt;Some of the examples of the Set-Cookie header with this configuration are shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Cookie: MYCOOKIE=Hello World; Max-Age=60  // This will be expired after 1 minute
Set-Cookie: MYCOOKIE=Hello World; Max-Age=-1  // This will be expired immediately
Set-Cookie: MYCOOKIE=Hello World; Max-Age=0
Set-Cookie: MYCOOKIE=Hello World; Expires=Wed, 1 Mar 2023 00:00:00 UTC // This will be expired on 1st march 2023 UTC time
Set-Cookie: MYCOOKIE=Hello World; Max-Age=60; Expires=Mon, 26 Mar 2018 17:04:05 UTC   // The Max-Age configuration will use and the cookie will be expired in 1 minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Sending the Cookie on a Specific Document Path
&lt;/h2&gt;

&lt;p&gt;Suppose you have two cookies, one is supposed to send on all the requests and another is to be sent on a specific document. How would you control this since they are automatically sent by the browsers? This feature is implemented in the &lt;code&gt;Path&lt;/code&gt; option of the Set-Cookie header.&lt;/p&gt;

&lt;p&gt;When the path is only &lt;code&gt;/&lt;/code&gt;, the cookie will be sent for all the requests, but when the valid path exists in the option, it will be only sent if the document location matches with the path in the form of "starts with" regex. Let me explain to you with an example – Suppose the path of the cookie is set to the &lt;em&gt;/secret&lt;/em&gt;, then this cookie will be sent for all the resources starting with "/secret", including &lt;em&gt;/secret/, /secret/doc, /secret/doc/&lt;/em&gt; and so on.&lt;/p&gt;

&lt;p&gt;Some of the examples are shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Cookie: MYCOOKIE=Hello World; Path=/          // This will be sent on all the resource requests
Set-Cookie: MYCOOKIE=Hello World; Path=/secret    // This will be sent on all the resource requests whose document location starts with /secret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This option can be used to provide the special permissions (authorization) on the specific path to the specific users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Sending the Cookies on Secure Connection
&lt;/h2&gt;

&lt;p&gt;When the request is made over a secure connection such as an HTTPS connection whether the network packets are signed by a secure hashing function a certificate, it becomes hard for the attacker to perform a man-in-the-middle attack to read or tamper with the request.&lt;/p&gt;

&lt;p&gt;Therefore to provide security from MITM attacks in the network, the server can instruct the browser to send the cookie only if the request is made in a secure environment under HTTPS. This can be done by providing &lt;code&gt;Secure&lt;/code&gt; option in the Set-Cookie header as demonstrated below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Cookie: MYCOOKIE=Hello World // This will be sent on requests made in both Http and Https connection
Set-Cookie: MYCOOKIE=Hello World; Secure // This will be sent only on requests made in Https connection
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Allowing Only HTTP Requests to Read Cookies
&lt;/h2&gt;

&lt;p&gt;If somehow the attackers get control to execute arbitrary javascript commands on the website, they can get the cookies using &lt;a href="https://developer.mozilla.org/en-US/docs/web/api/document/cookie" rel="noopener noreferrer"&gt;&lt;code&gt;document.cookie&lt;/code&gt;&lt;/a&gt; and which contains your proof of identity of the website and use it to perform actions on your behalf.&lt;/p&gt;

&lt;p&gt;Need not worry, the browser got you covered. You can set a &lt;code&gt;HttpOnly&lt;/code&gt; flag in the Set-Cookie header of the authentication response which will indicate the browser to only use this cookie when performing HTTP requests. You can still see the cookies via the developer console window, but not via javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Cookie: MYCOOKIE=Hello World; // This can be accessed by document.cookie API
Set-Cookie: MYCOOKIE=Hello World; HttpOnly // This can only be accessed while making http connections
Set-Cookie: MYCOOKIE=Hello World; HttpOnly; Secure // This can only be accessed while making secure http (https) connections
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best practices for the developers
&lt;/h2&gt;

&lt;p&gt;There are a few best practices that I use and recommend to you if you are new to the javascript world&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The users do not log out of the application and the sessions are kept on the login state. Make sure that there should be an expiration for the cookies or tokens. This will prevent the infinite login time for the user reminding them to log in to the applications. I would recommend you having 7 - 14 days of expiration time&lt;/li&gt;
&lt;li&gt;The users often rely on the external password manager like 1Password or browser built-in features. This helps them to use strong passwords without remembering all of them. The forms with proper names and inputs should be used so that password managers recognise them and automatically save and fill them. This will remove time to enter the password which can be seen by someone (kind of eavesdropping)&lt;/li&gt;
&lt;li&gt;When the user clicks on the logout button, the backend request should be made to invalidate cookies, tokens. This is a step ahead of security measures for the first point. Just in the case through XSS the cookies are stolen, it should be invalidated in the backend (associated session) so that it now becomes useless for the attackers.&lt;/li&gt;
&lt;li&gt;Even if you think the information is not as sensitive, always encrypt the cookies to a specific algorithm and same as decryption logic in the server. I would rather recommend you use a hashing algorithm and associate the user id with it on the sessions table. When the cookie is present in the protected resources, try to get the user from user id associated with that cookie hash.&lt;/li&gt;
&lt;li&gt;Apply rate-limiting to the requests brute-forcing cookie header to avoid accidentally guessing the correct cookie for a user on your application.&lt;/li&gt;
&lt;li&gt;Use of &lt;code&gt;HttpOnly&lt;/code&gt; + &lt;code&gt;Secure&lt;/code&gt; cookies are 10 times more secure than other cookies as it allows the browser to send the cookies only when the HTTP connection is made to that domain and document and secure will ensure that the protocol is secured by TLS so that there is no chance of MITM attack&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://null-byte.wonderhowto.com/how-to/write-xss-cookie-stealer-javascript-steal-passwords-0180833/" rel="noopener noreferrer"&gt;https://null-byte.wonderhowto.com/how-to/write-xss-cookie-stealer-javascript-steal-passwords-0180833/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://book.hacktricks.xyz/pentesting-web/hacking-with-cookies" rel="noopener noreferrer"&gt;https://book.hacktricks.xyz/pentesting-web/hacking-with-cookies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://salferrarello.com/httponly-cookies" rel="noopener noreferrer"&gt;https://salferrarello.com/httponly-cookies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.k3170makan.com/2013/10/aboutme-cookie-based-xss.html" rel="noopener noreferrer"&gt;http://blog.k3170makan.com/2013/10/aboutme-cookie-based-xss.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.miladbr.ir/2013/04/exploiting-unexploitable-dom-based-xss.html" rel="noopener noreferrer"&gt;http://blog.miladbr.ir/2013/04/exploiting-unexploitable-dom-based-xss.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Making Your Google Colab Notebook More Interactive</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Thu, 15 Apr 2021 07:32:23 +0000</pubDate>
      <link>https://dev.to/tbhaxor/making-your-google-colab-notebook-more-interactive-58i2</link>
      <guid>https://dev.to/tbhaxor/making-your-google-colab-notebook-more-interactive-58i2</guid>
      <description>&lt;p&gt;Hey, devs! Long time no post. I have been learning deep learning for so long time and using google colab for my projects. You know, free GPU 😅 and demos.&lt;/p&gt;

&lt;p&gt;I also used colab to send the proof of concepts or MVP to the clients and every time the clients faced problems in interacting with the notebook. &lt;/p&gt;

&lt;p&gt;This made me think every time can I make my notebook more interactive? Are there any pre-validations? What is the difference between Jupiter and google colab if it doesn't offer anything new but free GPU or TPU instances. Well, I kept on digging in and found that it does support interactive inputs of different types.&lt;/p&gt;

&lt;p&gt;In colab, it's known as "&lt;em&gt;Forms&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;I won't make this boring by adding too much text, google team has already created an example notebook explaining everything which is possible with forms. You can find the link of it below ⇓&lt;/p&gt;

&lt;p&gt;&lt;a href="https://colab.research.google.com/notebooks/forms.ipynb" rel="noopener noreferrer"&gt;https://colab.research.google.com/notebooks/forms.ipynb&lt;/a&gt;&lt;/p&gt;

</description>
      <category>colab</category>
      <category>jupyter</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PyTorch :: Performing Derivatives on Tensors</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Wed, 06 Jan 2021 17:28:57 +0000</pubDate>
      <link>https://dev.to/tbhaxor/pytorch-performing-derivatives-on-tensors-3hi5</link>
      <guid>https://dev.to/tbhaxor/pytorch-performing-derivatives-on-tensors-3hi5</guid>
      <description>&lt;p&gt;Hello everyone, &lt;/p&gt;

&lt;p&gt;Especially to those who fear from maths :P&lt;/p&gt;

&lt;p&gt;In this you will learn how to perform derivatives on tensors using pytorch. Let me tell you, once you will learn, it will be fun for you.&lt;/p&gt;

&lt;p&gt;Basically the derivatives are used for generating the parameters in the neural network. For now you will learn the basics of derivatives.&lt;/p&gt;

&lt;p&gt;Suppose you have a function 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(x)=x2f(x) = x^2 &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, so according to calculus, the derivative of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(x)f(x) &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 would be 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;df(x)x=2x1\frac{d f(x)}{x} = 2x^1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;df&lt;/span&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;x&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Let's see how can we do this and find the value of where $x=9$&lt;/p&gt;

&lt;p&gt;Read more about it's usage: &lt;a href="https://medium.com/@alfonsollanes/why-do-we-use-the-derivatives-of-activation-functions-in-a-neural-network-563f2886f2ab" rel="noopener noreferrer"&gt;https://medium.com/@alfonsollanes/why-do-we-use-the-derivatives-of-activation-functions-in-a-neural-network-563f2886f2ab&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;9.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requires_grad&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(9., requires_grad=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This time I am setting &lt;code&gt;requires_grad=True&lt;/code&gt;, this means we are using pytorch autograd engine to compute the derivatives. Derivatives are calculated by tracing the graph (enabled when &lt;code&gt;requires_grad=True&lt;/code&gt;) from root to leaf by using chain rule (back propagation).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; Only Tensors of floating point dtype can require gradients&lt;/p&gt;

&lt;p&gt;For more information you can read this wonderful post: &lt;a href="https://towardsdatascience.com/pytorch-autograd-understanding-the-heart-of-pytorchs-magic-2686cd94ec95" rel="noopener noreferrer"&gt;https://towardsdatascience.com/pytorch-autograd-understanding-the-heart-of-pytorchs-magic-2686cd94ec95&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also you can read the official documentation regarding autograd on pytorch's website: &lt;a href="https://pytorch.org/docs/stable/notes/autograd.html" rel="noopener noreferrer"&gt;https://pytorch.org/docs/stable/notes/autograd.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Moving on, let's calculate 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;x2x^2&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(81., grad_fn=&amp;lt;PowBackward0&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you would see, this time the pytorch has attached a gradient function with the tensor. &lt;/p&gt;

&lt;p&gt;To calculate the derivative, you need to call &lt;a href="https://pytorch.org/docs/stable/autograd.html#torch.Tensor.backward" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.backward()&lt;/code&gt;&lt;/a&gt; method. &lt;/p&gt;

&lt;p&gt;And use &lt;a href="https://pytorch.org/docs/stable/autograd.html#torch.Tensor.grad" rel="noopener noreferrer"&gt;&lt;code&gt;.grad&lt;/code&gt;&lt;/a&gt; property on the leaf tensor (here it's $x$)&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;backward&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(18.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Isn't it look like a magic to you? Let's calculate the derivative of another complex equation. &lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(x)=sin⁡(x22)f(x) = \sin ( \frac{x}{2 \sqrt 2} )

&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;sin&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;Where 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;x=100x = 100 &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;100&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;If you would simplify the denominator 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;222 \sqrt 2&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, it would be 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2322^{\frac{3}{2}}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mopen nulldelimiter sizing reset-size3 size6"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line mtight"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter sizing reset-size3 size6"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requires_grad&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;backward&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(100., requires_grad=True)
tensor(-0.7158, grad_fn=&amp;lt;SinBackward&amp;gt;)
tensor(-0.2469)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to test it, you can go to &lt;a href="https://www.derivative-calculator.net/" rel="noopener noreferrer"&gt;https://www.derivative-calculator.net/&lt;/a&gt; and type in the above expression.&lt;/p&gt;

&lt;p&gt;You can also calculate the partial derivatives in pytorch. For example you have 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(u,v)=uv+u2+v3f(u,v) = uv + u^2 + v^3&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;uv&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 , the derivative 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;df(u,v)du=2u+v\frac{d f(u,v)}{du} = 2u + v&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;d&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;u&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;df&lt;/span&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;u&lt;/span&gt;&lt;span class="mpunct mtight"&gt;,&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;v&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;df(u,v)dv=3v2+u\frac{d f(u,v)}{dv} = 3v^2 + u&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;d&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;v&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;df&lt;/span&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;u&lt;/span&gt;&lt;span class="mpunct mtight"&gt;,&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;v&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Let's calculate the partial derivative using pytorch where 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;u=2,v=4u = 2, v = 4&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;v&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requires_grad&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;4.&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requires_grad&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;backward&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(2., requires_grad=True)
tensor(4., requires_grad=True)
tensor(76., grad_fn=&amp;lt;AddBackward0&amp;gt;)
tensor(8.)
tensor(50.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have doubts or any idea reach me out via following sources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Email: &lt;a href="mailto:tbhaxor@gmail.com"&gt;tbhaxor@gmail.com&lt;/a&gt; (Recommended)&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/gurkirat--singh" rel="noopener noreferrer"&gt;@gurkirat--singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instagram: &lt;a href="https://instagram.com/_tbhaxor_" rel="noopener noreferrer"&gt;@_tbhaxor_&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>pytorch</category>
      <category>python</category>
      <category>neuralnetworks</category>
    </item>
    <item>
      <title>GCP Essentials :: Compute Engine</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Sun, 03 Jan 2021 20:10:11 +0000</pubDate>
      <link>https://dev.to/tbhaxor/gcp-essentials-compute-engine-3ala</link>
      <guid>https://dev.to/tbhaxor/gcp-essentials-compute-engine-3ala</guid>
      <description>&lt;p&gt;Hello everyone,&lt;/p&gt;

&lt;p&gt;Among all of other services that I would be discussing in further posts, Virtual Machine may be the most familiar and common.&lt;/p&gt;

&lt;p&gt;In this I will discuss about the Compute Engine service of Google Cloud Platform which is the service for spinning up a vps on GCP.&lt;/p&gt;

&lt;p&gt;One of the nice things about virtual machines is that they have the power and generality of a full-fledged operating system. You can flexibly configure a VPS pretty much like you configure your PC, deciding CPU, memory (type and size) and storage (types and size).&lt;/p&gt;

&lt;p&gt;You will also learn configuring the vpn and firewall to make your vps even more secure.&lt;/p&gt;

&lt;p&gt;Ohk so if you coming from some other cloud service provider, let me amaze you with this: The VPC networks that you define have &lt;strong&gt;global scope&lt;/strong&gt;. They can have &lt;strong&gt;subnets in any GCP region&lt;/strong&gt; worldwide and subnets can span the zones that make up a region.&lt;/p&gt;

&lt;p&gt;This feature let you create your own network configuration and use it as you wish.&lt;/p&gt;

&lt;p&gt;Not only this but you can &lt;strong&gt;dynamically increase the size of a subnet&lt;/strong&gt; in a custom network by expanding the range of IP addresses allocated to it. Doing that doesn't affect already configured VMs.&lt;/p&gt;

&lt;p&gt;You can find the created instances here: &lt;a href="https://console.cloud.google.com/compute/instances" rel="noopener noreferrer"&gt;https://console.cloud.google.com/compute/instances&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fofuqhe9zd253er7ddmyt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fofuqhe9zd253er7ddmyt.png" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in &lt;code&gt;gcloud&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud compute instances list --project=&amp;lt;PROJECT-ID&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you not created any instances, you will see the dashboard as above. &lt;/p&gt;

&lt;p&gt;So while creating the instances, you will see the 4 types of instances.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F16xsijoxi0mei4vu8xp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F16xsijoxi0mei4vu8xp7.png" width="489" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;General Purpose : Hosting static website or simple webapps&lt;/li&gt;
&lt;li&gt;Compute Optimized : High quality dedicated CPUs&lt;/li&gt;
&lt;li&gt;Memory Optimized : High quality dedicated RAM with more space&lt;/li&gt;
&lt;li&gt;GPU : Add the Graphical Processing Units for deploying / training machine learning applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will be choosing &lt;em&gt;General Purpose E2 Series and e2-medium&lt;/em&gt; machine for deployment.&lt;/p&gt;

&lt;p&gt;Then in base image and disk space, I have chosen the following&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtl5zqjnmkdk0sljy0g6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtl5zqjnmkdk0sljy0g6.png" width="466" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you want you instance to access the google apis, select second or third option based on your architecture&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0fec8mfuaiogcm2oc6qn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0fec8mfuaiogcm2oc6qn.png" width="465" height="190"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Currently I will be using the default access.&lt;/p&gt;

&lt;p&gt;Most of the time you will be deploying a web application on the server. If that is the case, check the firewall rules based on type of traffic you will be receiving&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4x1iyonscn8eqgbnui7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4x1iyonscn8eqgbnui7.png" width="471" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will be selecting the HTTP rule for now.&lt;/p&gt;

&lt;p&gt;Recheck everything and click "Create" button. Give some time to created and boot the instance.&lt;/p&gt;

&lt;p&gt;To create the same instance via gcloud sdk execute the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud beta compute --project=&amp;lt;PROJECT-ID&amp;gt; instances create myinstace --zone=europe-west2-c --machine-type=e2-medium --subnet=default --network-tier=PREMIUM --maintenance-policy=MIGRATE --service-account=&amp;lt;YOUR-SERVICE-ACCOUNT&amp;gt; --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --tags=http-server --image=ubuntu-2004-focal-v20201211 --image-project=ubuntu-os-cloud --boot-disk-size=30GB --boot-disk-type=pd-standard --boot-disk-device-name=myinstace --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and execute this command to add the HTTP rule for the instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud compute --project=&amp;lt;PROJECT-ID&amp;gt; firewall-rules create default-allow-http --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=http-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once it's created, you need the private key to access the VPC using ssh. The easiest way to connect to the server is &lt;code&gt;gcloud&lt;/code&gt;. It will created the SSH keys automatically (if doesn't exists) and then create&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud beta compute ssh --zone &amp;lt;ZONE-ID&amp;gt; &amp;lt;INSTANCE-NAME&amp;gt; --project &amp;lt;PROJECT-ID&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also connect to the instance from browser console. Click on the VPC you want to connect then click "Open in Browser"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uz4okij8h6z5npsl2c4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uz4okij8h6z5npsl2c4.png" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; The default username that is created while instance creation is &lt;strong&gt;&lt;code&gt;ubuntu&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since you might be a windows user or if you are linux user, gcloud will create the current logged in user in your VPC. So I personally recommend you to use the in-browser console for accessing the VPC. It will create the same username from every system you will access it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/compute/docs/gcloud-compute#top_of_page" rel="noopener noreferrer"&gt;Deploying Compute Engine via &lt;code&gt;gcloud&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/vpc/docs/firewalls" rel="noopener noreferrer"&gt;Managing Firewall Rules for VPC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/compute/docs/machine-types" rel="noopener noreferrer"&gt;Types of Machines and Their Configurations&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am waiting to see what have you deployed on your compute engine and how you are using it. Hit me up at the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Email: &lt;a href="mailto:tbhaxor@gmail.com"&gt;tbhaxor@gmail.com&lt;/a&gt; (Recommended)&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/gurkirat--singh" rel="noopener noreferrer"&gt;@gurkirat--singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instagram: &lt;a href="https://instagram.com/_tbhaxor_" rel="noopener noreferrer"&gt;@_tbhaxor_&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>gcp</category>
      <category>googlecloud</category>
      <category>cloud</category>
      <category>cloudcomputing</category>
    </item>
    <item>
      <title>PyTorch :: Understanding Tensors (Part 2)</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Fri, 01 Jan 2021 15:46:19 +0000</pubDate>
      <link>https://dev.to/tbhaxor/pytorch-understanding-tensors-part-2-4bfc</link>
      <guid>https://dev.to/tbhaxor/pytorch-understanding-tensors-part-2-4bfc</guid>
      <description>&lt;p&gt;Hello friends, Happy new year&lt;/p&gt;

&lt;p&gt;So far you have understood the types and shapes of the tensors. In this I will go more beyond the basics and teach you some more concepts about the tensors.&lt;/p&gt;

&lt;p&gt;I have created this as Part-2 as all the information in one post was lengthy :P&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Indexing and Slicing of Tensors&lt;/li&gt;
&lt;li&gt;Arithmetic Operations on Tensors&lt;/li&gt;
&lt;li&gt;Basic Functions on the Tensors&lt;/li&gt;
&lt;li&gt;Operations on 2D Tensors&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Indexing and Slicing of Tensors
&lt;/h2&gt;

&lt;p&gt;The individual values of the tensors are also a tensor, for instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the original tensor was of 1-D, the value would be of course a 0-D tensor. So to if you have 2-D tensor, and  are doing using only one index, you will get 1-D tensor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([1, 2, 3, 4])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you want to get the raw python list of 1+ D tensors, you can use &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.tolist" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.tolist()&lt;/code&gt;&lt;/a&gt; method&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like normal list, you can also update the tensor value by simply accessing the index. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([1, 2, 100, 4])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; For your sake, pytorch returns a new tensor object whenever you perform slicing on the original tensor. But it will overwrite the original tensor when you will change the value of tensor at particular index.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Arithmetic Operations on Tensors
&lt;/h2&gt;

&lt;p&gt;The very basic operations on tensors are &lt;a href="https://www.varsitytutors.com/hotmath/hotmath_help/topics/adding-and-subtracting-vector" rel="noopener noreferrer"&gt;vector additions and subtractions&lt;/a&gt;. Visit the link in case you want to study the maths behind these vector operations&lt;/p&gt;

&lt;p&gt;Suppose you have two tensors 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;uu &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;uu &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;u&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 defined as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([1., 2.])
tensor([3., 4.])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the vector operations &lt;em&gt;addition&lt;/em&gt; and &lt;em&gt;subtraction&lt;/em&gt; respectively&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([4., 6.])
tensor([-2., -2.])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scalar operations are also supported. For example, taking 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;55 &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;5&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 as our scalar quantity&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The operations &lt;em&gt;addition&lt;/em&gt;, &lt;em&gt;subtraction&lt;/em&gt;, &lt;em&gt;multiplication&lt;/em&gt; and &lt;em&gt;division&lt;/em&gt; are shown respectively&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([ 9., 11.])
tensor([-1.,  1.])
tensor([16., 22.])
tensor([1.6000, 2.8000])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In pytorch, there is no need of creating a 0D tensor to perform scalar operations you can simply use the scalar value and perform the action. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([15., 20.])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cross-product is fairly short and easy, by using &lt;strong&gt;&lt;code&gt;*&lt;/code&gt;&lt;/strong&gt; symbol. But to perform dot product, you should use &lt;a href="https://pytorch.org/docs/stable/generated/torch.dot.html#torch.dot" rel="noopener noreferrer"&gt;&lt;code&gt;torch.dot(vector1, vector2)&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.dot" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.dot(vector2)&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([3., 8.])
tensor(11.)
tensor(11.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; While performing cross-product or dot-product, &lt;em&gt;dimensions of both the tensors should be equal&lt;/em&gt;, otherwise you will get &lt;code&gt;RuntimeError&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Basic Functions on the Tensors
&lt;/h2&gt;

&lt;p&gt;Torch tensors provide a plethora of functions that you can apply on the tensors for the desired results. The first one of all is &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.mean" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.mean()&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([1., 2., 3., 4., 5.])
x.mean()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; Since mean of a tensor will be a floating tensor, you can find the mean of floating tensor only otherwise you will get &lt;a href="https://stackoverflow.com/questions/64358283/runtimeerror-can-only-calculate-the-mean-of-floating-types-got-byte-instead-f" rel="noopener noreferrer"&gt;&lt;code&gt;RuntimeError&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finding maximum or minimum value in the tensor can be done by using &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.max" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.max()&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.min" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.min()&lt;/code&gt;&lt;/a&gt; respectively&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(5.)
tensor(1.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also apply a function to a tensor element-wise. Suppose you have tensor containing various values of pi and you want to apply the sin and cos function on it. You can use &lt;a href="https://pytorch.org/docs/stable/generated/torch.sin.html#torch.sin" rel="noopener noreferrer"&gt;&lt;code&gt;torch.sin(tensor)&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://pytorch.org/docs/stable/generated/torch.cos.html#torch.cos" rel="noopener noreferrer"&gt;&lt;code&gt;torch.cos(tensor)&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

x = torch.tensor([0, np.pi / 2, np.pi])
print(x)
print(torch.sin(x))
print(torch.cos(x))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([0.0000, 1.5708, 3.1416])
tensor([ 0.0000e+00,  1.0000e+00, -8.7423e-08])
tensor([ 1.0000e+00, -4.3711e-08, -1.0000e+00])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes you will have to get an evenly spaced list of numbers between a range, you can use &lt;a href="https://pytorch.org/docs/stable/generated/torch.linspace.html" rel="noopener noreferrer"&gt;&lt;code&gt;torch.linspace(start, end, [step])&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's make it more interactive by plotting the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;sin⁡(−π)\sin (- \pi )  &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;sin&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;sin⁡(π)\sin ( \pi )  &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;sin&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;cos⁡(−π)\cos (- \pi )  &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;cos⁡(π)\cos ( \pi )  &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;cos&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;π&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Making the dataset first,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;linspace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# lower bound
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:])&lt;/span&gt; &lt;span class="c1"&gt;# upper bound
&lt;/span&gt;
&lt;span class="n"&gt;sined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cosed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sined&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cosed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([-1.5708, -1.5677, -1.5645, -1.5614, -1.5582])
tensor([1.5582, 1.5614, 1.5645, 1.5677, 1.5708])
tensor([-1.0000, -1.0000, -1.0000, -1.0000, -0.9999])
tensor([-4.3711e-08,  3.1447e-03,  6.2894e-03,  9.4340e-03,  1.2579e-02])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now using matplotlib to create the graph&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sined&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cosed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cosed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsy1ar4slq0ar9gaiagtu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsy1ar4slq0ar9gaiagtu.png" width="386" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Operations on 2D Tensors
&lt;/h2&gt;

&lt;p&gt;The greyscale images are the best example of 2D tensors. Each pixel which you see as 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;width∗heightwidth * height &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;span class="mord mathnormal"&gt;i&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;span class="mord mathnormal"&gt;h&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∗&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;h&lt;/span&gt;&lt;span class="mord mathnormal"&gt;e&lt;/span&gt;&lt;span class="mord mathnormal"&gt;i&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mord mathnormal"&gt;h&lt;/span&gt;&lt;span class="mord mathnormal"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 are the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;cols∗rowscols * rows&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;co&lt;/span&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="mord mathnormal"&gt;s&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∗&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;ro&lt;/span&gt;&lt;span class="mord mathnormal"&gt;w&lt;/span&gt;&lt;span class="mord mathnormal"&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 for the matrix.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.researchgate.net%2Fpublication%2F50211012%2Ffigure%2Ffig6%2FAS%3A669089471160327%401536534769114%2Fa-Grayscale-image-of-character-A-b-Binary-representation-of-character-A-c.ppm" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.researchgate.net%2Fpublication%2F50211012%2Ffigure%2Ffig6%2FAS%3A669089471160327%401536534769114%2Fa-Grayscale-image-of-character-A-b-Binary-representation-of-character-A-c.ppm" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above images, it is demonstrated how a binary image can be represented in a matrix. In case of grayscale image, the value of each element of the matrix would be in range of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;0−2550 - 255&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;255&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Just in case you are wondering how a RGB coloured image is represented, it is basically a 3D tensor where there are 3 matrix for each R, G and B channel. For example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.researchgate.net%2Fprofile%2FBhupendra_Pratap_Singh%2Fpublication%2F282798184%2Ffigure%2Ffig15%2FAS%3A283989639221249%401444719814399%2FColor-image-representation-and-RGB-matrix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.researchgate.net%2Fprofile%2FBhupendra_Pratap_Singh%2Fpublication%2F282798184%2Ffigure%2Ffig15%2FAS%3A283989639221249%401444719814399%2FColor-image-representation-and-RGB-matrix.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating a random 2D tensor using &lt;a href="https://pytorch.org/docs/stable/generated/torch.rand.html" rel="noopener noreferrer"&gt;&lt;code&gt;torch.rand(*size)&lt;/code&gt;&lt;/a&gt; method. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; This is a general method, you can create a random tensor of any dimension using this method&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# dimension of matrix is also called rank of matrix
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;numel&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([[0.0376, 0.4297, 0.2987],
        [0.8009, 0.1815, 0.5538],
        [0.2482, 0.7099, 0.3132]])
2
torch.Size([3, 3])
9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.numel" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.numel()&lt;/code&gt;&lt;/a&gt; method used above returns the total number of the elements in the tensor&lt;/p&gt;

&lt;p&gt;The indexing and slicing works same as 1D tensor, but there is a minor change. In 1D you were used to use &lt;code&gt;[number_start:number_end]&lt;/code&gt; for slicing and &lt;code&gt;[index]&lt;/code&gt; for getting element at particular index. &lt;/p&gt;

&lt;p&gt;Here now you have rows and columns, so the syntax would be &lt;code&gt;[index_row, index_col]&lt;/code&gt; to get the particular element at row and column in the matrix and &lt;code&gt;[number_start_row:number_end_row, number_start_col:number_end_row]&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(0.0376)
tensor(0.1815)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will give you the very first and middle element from the matrix. As shown in the following matrix &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwmla86vtm6tzufqgle7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwmla86vtm6tzufqgle7f.png" width="264" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Getting an element with indexing will always give you a new tensor of 0D&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([[0.1815, 0.5538]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To perform the matrix multiplication you can use either of them: &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.matmul" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.matmul(tensor2)&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://pytorch.org/docs/stable/generated/torch.matmul.html#torch.matmul" rel="noopener noreferrer"&gt;&lt;code&gt;torch.matmul(tensor1, tensor2)&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.mm" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.mm(tensor2)&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://pytorch.org/docs/stable/generated/torch.mm.html#torch.mm" rel="noopener noreferrer"&gt;&lt;code&gt;torch.mm(tensor1, tensor2)&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matmul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([[0.6413, 0.1338, 0.5066, 0.1618],
        [0.3807, 0.8555, 0.2187, 0.5024],
        [0.2771, 0.6381, 0.5671, 0.6934]])
tensor([[0.0263, 0.9461, 0.6314],
        [0.9180, 0.1586, 0.2589],
        [0.3363, 0.4529, 0.9433],
        [0.6760, 0.8877, 0.1171]])
tensor([[0.4195, 1.0009, 0.9363],
        [1.2086, 1.0409, 0.7270],
        [1.2525, 1.2358, 0.9563]])
tensor([[0.4195, 1.0009, 0.9363],
        [1.2086, 1.0409, 0.7270],
        [1.2525, 1.2358, 0.9563]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you have liked this post. Please share it with your friends and colleagues and help them learn the concepts of PyTorch. If you have doubts or any idea reach me out via following sources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Email: &lt;a href="mailto:tbhaxor@gmail.com"&gt;tbhaxor@gmail.com&lt;/a&gt; (Recommended)&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/gurkirat--singh" rel="noopener noreferrer"&gt;@gurkirat--singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instagram: &lt;a href="https://instagram.com/_tbhaxor_" rel="noopener noreferrer"&gt;@_tbhaxor_&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>deeplearning</category>
      <category>python</category>
      <category>pytorch</category>
      <category>neuralnetworks</category>
    </item>
    <item>
      <title>GCP Essentials :: Cloud Marketplace</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Wed, 23 Dec 2020 21:52:39 +0000</pubDate>
      <link>https://dev.to/tbhaxor/gcp-essentials-cloud-marketplace-5dkl</link>
      <guid>https://dev.to/tbhaxor/gcp-essentials-cloud-marketplace-5dkl</guid>
      <description>&lt;p&gt;Suppose you want to spin up an application without worrying about configuration of deployment settings like virtual machine instances, storage or network settings, google cloud marketplace is what you are looking for. However, you can modify many of them before the launch.&lt;/p&gt;

&lt;p&gt;You can visit this website and search for your application. &lt;a href="https://cloud.google.com/marketplace" rel="noopener noreferrer"&gt;https://cloud.google.com/marketplace&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The marketplace also contains third-party enterprise applications that might be billable, for example MongoDB cluster. &lt;/p&gt;

&lt;p&gt;The billing model would go like &lt;strong&gt;&lt;code&gt;GCP RESOURCES CHARGES + APPLICATION CHARGES + TAXES = AMOUNT TO BE PAID&lt;/code&gt;&lt;/strong&gt;, you will have to at least pay for the GCP resources you are using and some applications can be free of cost. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; For paid applications, you will see the estimates (excluding networking costs since those will vary based on how you use the application) of their monthly charges before you launch them.&lt;/p&gt;

&lt;p&gt;No more talks, let's &lt;strong&gt;"host a ghost blog using cloud marketplace"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select your project from GCP console and goto, &lt;a href="https://console.cloud.google.com/marketplace/browse" rel="noopener noreferrer"&gt;https://console.cloud.google.com/marketplace/browse&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Browse wordpress and click on "Ghost"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmv8iu97v1u4qje3vazcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmv8iu97v1u4qje3vazcy.png" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if you want to check the pricing, you can scroll down and see the detailed pricing schemes. Then click on Launch button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbbfusvce1te5z4bis2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbbfusvce1te5z4bis2l.png" width="800" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have selected free tag, so here I would have to only pay for the GCP resources only&lt;/p&gt;

&lt;p&gt;See how effectively you can configure your application. Select the options as per your needs, scroll down and hit "Deploy"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdfc19aa6yghf8d40xh6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdfc19aa6yghf8d40xh6t.png" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait for google to setup the application for you. Use the &lt;a href="https://console.cloud.google.com/dm/deployments" rel="noopener noreferrer"&gt;Deployment Manager&lt;/a&gt; to see the status of the deployment&lt;/p&gt;

&lt;p&gt;After the deployment, you can configure the application as per your need&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8k6t9ba9v3ivnkeahkn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8k6t9ba9v3ivnkeahkn4.png" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the at least you need to do to run the application :P&lt;/p&gt;

&lt;p&gt;The best thing is, if you want to login the server or want more information related to deployment, google always backs your support&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fun08reeeudm3z2hy6b04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fun08reeeudm3z2hy6b04.png" width="725" height="830"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I am waiting to see what have you deployed using the marketplace and how you are using it. Hit me up at the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Email: &lt;a href="mailto:tbhaxor@gmail.com"&gt;tbhaxor@gmail.com&lt;/a&gt; (Recommended)&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/gurkirat--singh" rel="noopener noreferrer"&gt;@gurkirat--singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instagram: &lt;a href="https://instagram.com/_tbhaxor_" rel="noopener noreferrer"&gt;@_tbhaxor_&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>gcp</category>
      <category>googlecloud</category>
      <category>cloud</category>
      <category>cloudcomputing</category>
    </item>
    <item>
      <title>PyTorch :: Understanding Tensors (Part 1)</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Wed, 23 Dec 2020 01:46:39 +0000</pubDate>
      <link>https://dev.to/tbhaxor/pytorch-understanding-tensors-part-1-od8</link>
      <guid>https://dev.to/tbhaxor/pytorch-understanding-tensors-part-1-od8</guid>
      <description>&lt;p&gt;In this post, you will start from the very basics of deep learning frameworks. &lt;strong&gt;TENSORS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So basically a neural network is the bunch of &lt;strong&gt;nodes that take one or more input, processes them and returns one or more output&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table of Content&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What are Tensors&lt;/li&gt;
&lt;li&gt;Creating Tensors&lt;/li&gt;
&lt;li&gt;Tensor Types and Their Conversions&lt;/li&gt;
&lt;li&gt;
Shapes and Dimensions

&lt;ul&gt;
&lt;li&gt;Reshaping Tensors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What are Tensors
&lt;/h2&gt;

&lt;p&gt;Tensors are the building blocks of a neural network. A PyTorch tensor is the &lt;strong&gt;generalized form of arrays in  

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;nn &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 dimensions&lt;/strong&gt; to run arbitrary computations on GPU. &lt;small&gt;&lt;a href="https://pytorch.org/tutorials/beginner/examples_tensor/two_layer_net_tensor.html#pytorch-tensors" rel="noopener noreferrer"&gt;[READ MORE]&lt;/a&gt;&lt;/small&gt; &lt;/p&gt;

&lt;p&gt;A tensor can be of any dimension. In this course you will learn about the following dimensions only&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0D -&amp;gt; Scalar numbers like &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;20&lt;/code&gt; etc. are 0-Dimensional tensors&lt;/li&gt;
&lt;li&gt;1D -&amp;gt; Array of numbers like &lt;code&gt;[1, 2, 3]&lt;/code&gt; or &lt;code&gt;[20, 30, 10]&lt;/code&gt; etc.&lt;/li&gt;
&lt;li&gt;2D -&amp;gt; Matrix of numbers like &lt;code&gt;[[1, 2, 3], [2, 3, 4]]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; During the process of learning, the network applies some tensor methods on these tensor values in order to gain the desired output.&lt;/p&gt;

&lt;p&gt;Before creating and using tensors in PyTorch, install it by following the instructions from here: &lt;a href="https://pytorch.org/get-started/locally/" rel="noopener noreferrer"&gt;https://pytorch.org/get-started/locally/&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating Tensors
&lt;/h2&gt;

&lt;p&gt;You can create the tensors by importing &lt;code&gt;torch&lt;/code&gt; and using &lt;a href="https://pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor" rel="noopener noreferrer"&gt;&lt;code&gt;torch.tensor(data)&lt;/code&gt;&lt;/a&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;

&lt;span class="n"&gt;t0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                     &lt;span class="c1"&gt;# 0-D tensor
&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;           &lt;span class="c1"&gt;# 1-D tensor
&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="c1"&gt;# 2-D tensor
&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor(10)
tensor([1, 2, 3, 4])
tensor([[1, 2, 3],
        [2, 3, 4]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To convert it into numpy array, simply use &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.numpy" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.numpy()&lt;/code&gt;&lt;/a&gt; method and use it as you wish&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;numpy&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array([[1, 2, 3],
       [2, 3, 4]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tensor Types and Their Conversions
&lt;/h2&gt;

&lt;p&gt;PyTorch supports the following data-types of the tensors&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FX83WyQp%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FX83WyQp%2Fimage.png" width="640" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get the data type  use &lt;a href="https://pytorch.org/docs/stable/tensor_attributes.html#torch-dtype" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.dtype&lt;/code&gt;&lt;/a&gt; property and &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.type" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.type()&lt;/code&gt;&lt;/a&gt; to get the PyTorch tensor type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;torch.int64
torch.LongTensor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; Difference between &lt;code&gt;.dtype&lt;/code&gt; and &lt;code&gt;.type()&lt;/code&gt; is better explained &lt;a href="https://discuss.pytorch.org/t/difference-between-torch-dtype-and-torch-tensortype/58948/2" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To explicitly create a tensor of specific type, either pass &lt;code&gt;dtype&lt;/code&gt; while defining the tensor in &lt;a href="https://pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor" rel="noopener noreferrer"&gt;&lt;code&gt;torch.tensor&lt;/code&gt;&lt;/a&gt; method or use tensor constructor. &lt;/p&gt;

&lt;p&gt;For example, here I am creating a float tensor by providing &lt;em&gt;int32&lt;/em&gt; data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;tf0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;float32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tf0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tf0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;tf1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FloatTensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tf1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tf1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([1., 2., 3., 4.])
torch.float32
tensor([5., 6., 7., 8.])
torch.float32
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can pass-in the the datatype in the &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.type" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.type([type])&lt;/code&gt;&lt;/a&gt; method, in order to change the data-type of the existing tensor.&lt;/p&gt;

&lt;p&gt;For example, changing float tensor to int32&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;tf0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tf0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# after changing, it returns a new tensor object of that type
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tf0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;torch.int32
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Shapes and Dimensions
&lt;/h2&gt;

&lt;p&gt;While training the model, you will deal with higher dimensions and neural network only accept the dimension which is defined at input layer while architecting the model.&lt;/p&gt;

&lt;p&gt;The dimension basically tells whether the tensor is 0-D or 1-D or 2-D or even higher than that. &lt;strong&gt;Dimension of tensor is also called the rank of the tensor&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To get the size you can use &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.size" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.size()&lt;/code&gt;&lt;/a&gt; method or &lt;code&gt;Tensor.shape&lt;/code&gt; property and to get the dimension of the tensor, use &lt;a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.size" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.ndimension()&lt;/code&gt;&lt;/a&gt; method or &lt;code&gt;Tensor.ndim&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ndimension&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;torch.Size([4])
1
torch.Size([2, 3])
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reshaping Tensors
&lt;/h3&gt;

&lt;p&gt;Sometimes working with neural network, you will face a situation when you are supposed to change the dimension of the tensor. This can be done by using &lt;a href="https://pytorch.org/docs/stable/tensor_view.html" rel="noopener noreferrer"&gt;&lt;code&gt;Tensor.view(nrows, ncols)&lt;/code&gt;&lt;/a&gt; method of the tensor&lt;/p&gt;

&lt;p&gt;For example, I have a tensor &lt;code&gt;[[1, 2, 3], [2, 3, 4]]&lt;/code&gt; and I want to convert it to 2D with 1 row and 6 columns&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;tx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([[1, 2, 3, 2, 3, 4]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; The number of parameters in &lt;code&gt;.view&lt;/code&gt; method is the total number of dimensions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Converting 2D to 1D tensor
&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([1, 2, 3, 2, 3, 4])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are special values for the view method. Suppose you don't know how many rows would be good while resizing your matrix, you can set it to -1 it will automatically adjust the rows for you. Same goes with columns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Telling PyTorch to keep the max 2 elements in one column and make as many rows as you want
&lt;/span&gt;&lt;span class="n"&gt;tr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Telling PyTorch to keep max 2 rows and adjust columns automatically
&lt;/span&gt;&lt;span class="n"&gt;tc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tensor([[1, 2],
        [3, 2],
        [3, 4]])
tensor([[1, 2, 3],
        [2, 3, 4]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;  Both the rows and columns can't be set to &lt;code&gt;-1&lt;/code&gt; at same time.&lt;/p&gt;

&lt;p&gt;I hope you have liked this post. Please share it with your friends and colleagues and help them learn the concepts of PyTorch. If you have doubts or any idea reach me out via following sources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Email: &lt;a href="mailto:tbhaxor@gmail.com"&gt;tbhaxor@gmail.com&lt;/a&gt; (Recommended)&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/gurkirat--singh" rel="noopener noreferrer"&gt;@gurkirat--singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instagram: &lt;a href="https://instagram.com/_tbhaxor_" rel="noopener noreferrer"&gt;@_tbhaxor_&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>deeplearning</category>
      <category>python</category>
      <category>pytorch</category>
      <category>neuralnetworks</category>
    </item>
    <item>
      <title>PyTorch :: Course Introduction</title>
      <dc:creator>Gurkirat Singh</dc:creator>
      <pubDate>Wed, 23 Dec 2020 00:15:06 +0000</pubDate>
      <link>https://dev.to/tbhaxor/pytorch-course-introduction-dfa</link>
      <guid>https://dev.to/tbhaxor/pytorch-course-introduction-dfa</guid>
      <description>&lt;p&gt;Hello friends,&lt;/p&gt;

&lt;p&gt;This is my brand new series on Deep Learning with PyTorch. In this series you will learn the basics of deep learning and neural networks. Since this course is cantered towards the PyTorch library, I will not be discussing much deeper into what are neural networks and how they work or learn.&lt;/p&gt;

&lt;p&gt;This course content is taken from &lt;a href="https://www.coursera.org/learn/deep-neural-networks-with-pytorch/" rel="noopener noreferrer"&gt;coursera&lt;/a&gt; and I have included some more information based on my research and latest releases of PyTorch library.&lt;/p&gt;

&lt;p&gt;You can still pursue the course on coursera, but here you will find more references and links to the original documentation. Also you can get the exclusive access to all the projects that I have created while learning PyTorch&lt;/p&gt;

&lt;p&gt;If you have doubts or any idea reach me out via following sources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Email: &lt;a href="mailto:tbhaxor@gmail.com"&gt;tbhaxor@gmail.com&lt;/a&gt; (Recommended)&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/tbhaxor" rel="noopener noreferrer"&gt;@tbhaxor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/gurkirat--singh" rel="noopener noreferrer"&gt;@gurkirat--singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instagram: &lt;a href="https://instagram.com/_tbhaxor_" rel="noopener noreferrer"&gt;@_tbhaxor_&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's dive into the ocean of deep learning using PyTorch.&lt;/p&gt;

</description>
      <category>pytorch</category>
      <category>python</category>
      <category>deeplearning</category>
      <category>neuralnetworks</category>
    </item>
  </channel>
</rss>
