<?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: Jan</title>
    <description>The latest articles on DEV Community by Jan (@jankrepl).</description>
    <link>https://dev.to/jankrepl</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%2F509898%2Ff51949b9-0e17-430d-8315-aea20365f51f.jpeg</url>
      <title>DEV Community: Jan</title>
      <link>https://dev.to/jankrepl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jankrepl"/>
    <language>en</language>
    <item>
      <title>Forward hooks in PyTorch</title>
      <dc:creator>Jan</dc:creator>
      <pubDate>Wed, 20 Jan 2021 12:42:51 +0000</pubDate>
      <link>https://dev.to/jankrepl/forward-hooks-in-pytorch-4eeh</link>
      <guid>https://dev.to/jankrepl/forward-hooks-in-pytorch-4eeh</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--naCwLCiu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jztgw41587pnu41oiwhm.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--naCwLCiu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jztgw41587pnu41oiwhm.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Forward hooks are custom functions that get executed right after the forward pass. Among other things, one can use them together with TensorBoard to visualize activations of any layer.&lt;/p&gt;

&lt;h1&gt;
  
  
  Motivation
&lt;/h1&gt;

&lt;p&gt;When using &lt;code&gt;torch.nn.Module&lt;/code&gt;, did you ever wonder what the difference between the &lt;code&gt;forward&lt;/code&gt; and the &lt;code&gt;__call__&lt;/code&gt; methods is?&lt;br&gt;
One can roughly say that &lt;code&gt;__call__&lt;/code&gt; = &lt;code&gt;forward&lt;/code&gt; + execution of various hooks.&lt;/p&gt;
&lt;h1&gt;
  
  
  Hooks?
&lt;/h1&gt;

&lt;p&gt;Hooks are custom functions that get executed at specific moments during the forward/backward phase. They allow us to inspect what is going on inside of the network. The specific use cases are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debugging&lt;/li&gt;
&lt;li&gt;Logging&lt;/li&gt;
&lt;li&gt;Visualizing (this post)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are interested to learn more about hooks checkout the &lt;a href="https://pytorch.org/tutorials/beginner/former_torchies/nnft_tutorial.html#forward-and-backward-function-hooks"&gt;official docs&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Tell me more about forward hooks!
&lt;/h1&gt;

&lt;p&gt;Forward hook is a function that accepts 3 arguments&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;module_instance&lt;/code&gt; : Instance of the layer your are attaching the hook to&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;input&lt;/code&gt; : tuple of tensors (or other) that we pass as the input to the forward method&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;output&lt;/code&gt; : tensor (or other) that is the output of the the forward method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you define it, you need to "register" the hook with your desired layer via the &lt;code&gt;register_forward_hook&lt;/code&gt; method.&lt;br&gt;
Once registered, the hook will be executed right after the forward method. You do not have to worry about triggering it manually!&lt;/p&gt;
&lt;h1&gt;
  
  
  Too vague,… I need to see an example!
&lt;/h1&gt;

&lt;p&gt;I created a hands-on video tutorial where I explain step by step how to use forward hooks together with TensorBoard. The goal is to visualize activations of any layer of choice (=creating a histogram of its values for a given sample / batch).&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1ZbLA7ofasY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The tutorial does not talk about several related (interesting) topics.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;backward hooks&lt;/li&gt;
&lt;li&gt;forward pre hooks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would encourage the reader to learn more about them:)&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Hooks are hidden gems of PyTorch. Specifically, the forward hooks allow you to debug and visualize what is going on inside of your network. This post provided a first look into what they are and how one can use them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Credits
&lt;/h1&gt;

&lt;p&gt;Cover photo &lt;a href="https://unsplash.com/@steve_j"&gt;https://unsplash.com/@steve_j&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deeplearning</category>
      <category>machinelearning</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>mltype — Typing practice for programmers</title>
      <dc:creator>Jan</dc:creator>
      <pubDate>Sat, 07 Nov 2020 12:43:49 +0000</pubDate>
      <link>https://dev.to/jankrepl/mltype-typing-practice-for-programmers-1d4f</link>
      <guid>https://dev.to/jankrepl/mltype-typing-practice-for-programmers-1d4f</guid>
      <description>&lt;p&gt;&lt;strong&gt;mltype&lt;/strong&gt; is a command line tool for improving typing skills. It does so with a tiny bit of deep learning.&lt;/p&gt;

&lt;p&gt;If you clicked on this post hoping you would learn something about static typing, type annotations or similar, this is NOT the right article. The typing I talk about in this post is the thing you do with you keyboard. Or to be precise&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The action or skill of writing something by means of a typewriter or computer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Motivation
&lt;/h1&gt;

&lt;p&gt;A few months ago I decided to learn touch typing! I know what you are thinking… “Are you a faster typist than before and was all the pain worth it?” I would definitely say yes and yes. However, the internet is full of similar before and after testimonials and I am not going to write yet another one.&lt;/p&gt;

&lt;p&gt;What I want to talk about is that I was really surprised how few resources there are for practising touch typing with programming languages. After a quick google search you will probably discover the following sites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.speedcoder.net/" rel="noopener noreferrer"&gt;http://www.speedcoder.net/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://typing.io/" rel="noopener noreferrer"&gt;https://typing.io/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While the above websites have multiple strong points, let me point out some of their shortcomings&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lack of variability and element of surprise&lt;/li&gt;
&lt;li&gt;Manual selection of source files and corresponding lines&lt;/li&gt;
&lt;li&gt;Not customizable&lt;/li&gt;
&lt;li&gt;Not free (typing.com)&lt;/li&gt;
&lt;li&gt;Not nerdy enough — would it not be possible to do it in the terminal?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the above mentioned reasons, I decided to give it a shot and write my own typing practice software: &lt;strong&gt;mltype&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fog3sshslod916tlf5gar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fog3sshslod916tlf5gar.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What does it do?
&lt;/h1&gt;

&lt;p&gt;In short, it is a command line tool (written in Python). It uses neural networks to generate text that looks like a programming language (or normal language). Additionally, it provides non-machine learning functionalities like reading text from a file or standard input.&lt;/p&gt;

&lt;p&gt;If you wonder what kind of “neural network” is behind it I would more than encourage you to (re)read the The Unreasonable Effectiveness of Recurrent Neural Networks by Andrej Karpathy. &lt;strong&gt;mltype&lt;/strong&gt; is doing more or less the same thing in the background. To be precise, there is a character-level language model. It spits out a probability distribution over the next character given previous characters. Most importantly, it tries to hide all the complexity and boring details of the training and inference from the user. Generating text from an existing model and training a new model can both be done in a single command.&lt;/p&gt;

&lt;h1&gt;
  
  
  Examples
&lt;/h1&gt;

&lt;p&gt;Below are some examples of different programming languages. All the models that generated them and many other pretrained models are available for download (see the README.md on github).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F25swjagltewu5tvbhoz4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F25swjagltewu5tvbhoz4.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fon5dha3mpbs4iaj56z79.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fon5dha3mpbs4iaj56z79.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Wanna try it?
&lt;/h1&gt;

&lt;p&gt;If you want to know more and try it out yourself visit the below links!&lt;/p&gt;

&lt;p&gt;github: &lt;a href="https://github.com/jankrepl/mltype" rel="noopener noreferrer"&gt;https://github.com/jankrepl/mltype&lt;/a&gt;&lt;br&gt;
docs: &lt;a href="https://mltype.readthedocs.io/en/latest/" rel="noopener noreferrer"&gt;https://mltype.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>deeplearning</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
