<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="http://arthurcgusmao.com/feed.xml" rel="self" type="application/atom+xml" /><link href="http://arthurcgusmao.com/" rel="alternate" type="text/html" /><updated>2023-02-02T22:27:41+00:00</updated><id>http://arthurcgusmao.com/feed.xml</id><title type="html">arthurcgusmao.com</title><subtitle>My personal blog</subtitle><author><name>Arthur Colombini Gusmão</name></author><entry><title type="html">Configure SSH Keys Authentication</title><link href="http://arthurcgusmao.com/programming/2020/03/04/ssh-authentication.html" rel="alternate" type="text/html" title="Configure SSH Keys Authentication" /><published>2020-03-04T00:00:00+00:00</published><updated>2020-03-04T00:00:00+00:00</updated><id>http://arthurcgusmao.com/programming/2020/03/04/ssh-authentication</id><content type="html" xml:base="http://arthurcgusmao.com/programming/2020/03/04/ssh-authentication.html">&lt;p&gt;Secure Shell (SSH) is a cryptographic network protocol, i.e., a set of network rules that use cryptography for operating network services securely. The typical application is using SSH to access remote machines and perform command-line, login, and remote command execution, but any network service can be secured with SSH. [1]&lt;/p&gt;

&lt;p&gt;The freely available version of SSH present in most Linux environments is called &lt;strong&gt;OpenSSH&lt;/strong&gt;. [2]&lt;/p&gt;

&lt;h2 id=&quot;the-ssh-directory&quot;&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh&lt;/code&gt; directory&lt;/h2&gt;

&lt;p&gt;OpenSSH stores its configurations on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ssh&lt;/code&gt; directory, present under the user’s root directory. The .ssh directory is automatically created when the user runs the ssh command for the first time. If it doesn’t exist in your computer yet, you may created it using the command below (notice the 700 permission for security). [3]&lt;/p&gt;

&lt;div class=&quot;language-console highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;go&quot;&gt;mkdir -p ~/.ssh &amp;amp;&amp;amp; chmod 700 ~/.ssh
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;ssh-keys-configuration&quot;&gt;SSH Keys configuration&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;SSH Keys&lt;/em&gt; allow authentication without the need of a password [2]. It uses two keys, one &lt;em&gt;private&lt;/em&gt; and one &lt;em&gt;public&lt;/em&gt;. The public key has the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pub&lt;/code&gt; extension as its filetype. The name of these keys is usually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa.pub&lt;/code&gt;, and they are usually placed in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;To generate the keys using the RSA algorithm, run:&lt;/p&gt;
&lt;div class=&quot;language-console highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;go&quot;&gt;ssh-keygen -t rsa
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;At this step you will be prompted for a password; you may choose to leave it empty, but be aware that whoever gains access to your private key file will be able to login to your remote machines. [5]&lt;/p&gt;

&lt;p&gt;In order to SSH into another machine without typing a password, the remote machine must have a copy of your client’s public key under its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/authorized_keys&lt;/code&gt; file. You can either manually &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys#copying-your-public-ssh-key-to-a-server-without-ssh-copy-id&quot;&gt;append to that file&lt;/a&gt; (if you need to manually create the file, make sure it has permissions “600” for security) or use the utility command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-copy-id&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-console highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;go&quot;&gt;ssh-copy-id username@remotehost
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;ssh-agent-and-related-utilities&quot;&gt;SSH Agent and related utilities&lt;/h2&gt;

&lt;h3 id=&quot;the-built-in-ssh-agent-utility&quot;&gt;The built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-agent&lt;/code&gt; utility&lt;/h3&gt;

&lt;p&gt;By default, you will have to enter your private key passphrase every time you use it. However, you can avoid to repeatedly do this by running an SSH agent (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-agent -s&lt;/code&gt;), a small utility that stores your private key after you have entered the passphrase for the first time [5]. After your SSH-agent is running, add your key via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-add&lt;/code&gt;, and type your passphrase. Now, every time you need to use your private key, no passphrase will be prompted.&lt;/p&gt;

&lt;h3 id=&quot;the-smarter-ssh-ident-utility&quot;&gt;The smarter &lt;a href=&quot;https://github.com/ccontavalli/ssh-ident&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-ident&lt;/code&gt;&lt;/a&gt; utility&lt;/h3&gt;

&lt;p&gt;Ideally, your SSH-agent would automatically be run at startup, so that you don’t have to manually start it and add the keys yourself. However, this approach has the inconvinience that you will need to type your passphrase for all keys even if not using them throughout your session. To solve this issue, a &lt;a href=&quot;https://unix.stackexchange.com/a/90869/173702&quot;&gt;number of solutions&lt;/a&gt; have been proposed, one of them being &lt;a href=&quot;https://github.com/ccontavalli/ssh-ident&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-ident&lt;/code&gt;&lt;/a&gt;, which creates SSH agents on demand as your SSH keys are first needed. Read the repository’s documentation for more information.&lt;/p&gt;

&lt;h2 id=&quot;openssh-client-side-configuration&quot;&gt;OpenSSH client-side configuration&lt;/h2&gt;

&lt;p&gt;Configuration on the client side is done via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/config&lt;/code&gt; file, which should be readable and writable only by the user (permission 600).&lt;/p&gt;

&lt;div class=&quot;language-console highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;go&quot;&gt;touch ~/.ssh/config &amp;amp;&amp;amp; chmod 600 ~/.ssh/config
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The structure of the file defines, for each block, an alias for host you want to access, followed by the actual hostname and additional configuration. Below you find an example configuration, adapted from [3].&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Host myalias
    HostName 192.168.1.10
    User myuser
    Port 3022
    IdentityFile ~/.ssh/targaryen.key
    LocalForward 31086 14.120.240.50:31086
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The example above can be edited to keep only the lines useful for your configuration. It is possible to use wildcards like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Host *&lt;/code&gt; in your config file also. [3,4]&lt;/p&gt;

&lt;h3 id=&quot;prevent-ssh-connection-time-outs&quot;&gt;Prevent SSH connection time outs&lt;/h3&gt;

&lt;p&gt;An example of wildcards use in your client’s SSH configuration is when you want to make sure the SSH connections are not timed out by firewalls or related. To do so, you can add to your client’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/config&lt;/code&gt; file (create if it doesn’t exist) the snippet below:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Host *
    ServerAliveInterval 60
    ServerAliveCountMax 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;For more details see references [6] and [7].&lt;/p&gt;

&lt;h2 id=&quot;further-notes&quot;&gt;Further notes&lt;/h2&gt;

&lt;p&gt;If you are using a web services provider, be aware that it may employ meta-mechanisms to control SSH authentication in the remote machine. For instance, Google Compute actively modifies/deletes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/authorized_keys&lt;/code&gt; file of virtual machines for that purpose; changes you make to that file may be lost over time.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;https://en.wikipedia.org/wiki/Secure_Shell&lt;/li&gt;
  &lt;li&gt;https://help.ubuntu.com/lts/serverguide/openssh-server.html&lt;/li&gt;
  &lt;li&gt;https://linuxize.com/post/using-the-ssh-config-file&lt;/li&gt;
  &lt;li&gt;https://superuser.com/a/1462597/629663&lt;/li&gt;
  &lt;li&gt;https://www.digitalocean.com/community/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys&lt;/li&gt;
  &lt;li&gt;https://patrickmn.com/aside/how-to-keep-alive-ssh-sessions/&lt;/li&gt;
  &lt;li&gt;https://unix.stackexchange.com/questions/3026/what-options-serveraliveinterval-and-clientaliveinterval-in-sshd-config-exac&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Programming" /><summary type="html">Secure Shell (SSH) is a cryptographic network protocol, i.e., a set of network rules that use cryptography for operating network services securely. The typical application is using SSH to access remote machines and perform command-line, login, and remote command execution, but any network service can be secured with SSH. [1]</summary></entry><entry><title type="html">Matplotlib intro (pyplot)</title><link href="http://arthurcgusmao.com/programming/2018/04/06/matplotlib-intro.html" rel="alternate" type="text/html" title="Matplotlib intro (pyplot)" /><published>2018-04-06T00:00:00+00:00</published><updated>2018-04-06T00:00:00+00:00</updated><id>http://arthurcgusmao.com/programming/2018/04/06/matplotlib-intro</id><content type="html" xml:base="http://arthurcgusmao.com/programming/2018/04/06/matplotlib-intro.html">&lt;p&gt;A very simple introduction to Matplotlib in Python. It tries to present fundamental concepts without clinging to many details. If you have time I highly recommend you another post which is much more conceptual, detailed and illustrated than mine: &lt;a href=&quot;https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo&quot;&gt;“Artist” in matplotlib&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;basics-figures-and-axes&quot;&gt;Basics: Figures and Axes&lt;/h2&gt;

&lt;p&gt;The most important thing that you have to know is that Matplotlib has two main classes for plotting curves and setting up configurations: &lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Figure&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://matplotlib.org/api/axes_api.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Axes&lt;/code&gt;&lt;/a&gt;. In this section we present these objects and its main methods.&lt;/p&gt;

&lt;h3 id=&quot;figure&quot;&gt;Figure&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Figure&lt;/code&gt;&lt;/a&gt; object is a major object that contains all of the plot elements (including the &lt;a href=&quot;https://matplotlib.org/api/axes_api.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Axes&lt;/code&gt;&lt;/a&gt; object, which we will see next). It can be accessed in the following manners:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# returns a Figure instance
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last line is a shortcut for getting the current figure and its axes; see &lt;a href=&quot;https://stackoverflow.com/questions/34162443/why-do-many-examples-use-fig-ax-plt-subplots-in-matplotlib-pyplot-python&quot;&gt;these answers&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;Configurations such as saving and displaying the figure are set at the figure-level:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;savefig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'filepath.png'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# saves figure
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# displays figure
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;axes&quot;&gt;Axes&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://matplotlib.org/api/axes_api.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Axes&lt;/code&gt;&lt;/a&gt; are the objects that have plotting methods. Each figure can contain multiple axes, thus allowing you to make multiple plots on the same figure. They are created and accessed from the figure object:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# create new axes
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# add a set of subplots to this figure (default 1)
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# get existing axes
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# list of axes in `fig`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once you have the axis object that you want to plot a curve into, the only thing you need to do is to call the plotting method:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are many different kinds of plotting methods (which we will not cover), here are a few:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ax.plot&lt;/code&gt;&lt;/a&gt; Lines and/or Markers&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ax.bar&lt;/code&gt;&lt;/a&gt; Bars chart&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ax.hist&lt;/code&gt;&lt;/a&gt; Histogram&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plt.scatter&lt;/code&gt;&lt;/a&gt; Scatter (has capability to render a different size and/or color for each point, in contrast to plot)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;shortcuts-the-pyplot-interface&quot;&gt;Shortcuts: the Pyplot interface&lt;/h3&gt;

&lt;p&gt;To make things easier, Matplotlib has shortcuts for the methods mentioned above directly through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyplot&lt;/code&gt;. That is, lots of methods can be called from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyplot&lt;/code&gt;, regardless of whether they would normally be called from a Figure or an Axis object:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Example 1
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;savefig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'filepath.png'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Example 2
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# get figure and 1 axes (already added to fig)
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;axis-labels-legends-and-title&quot;&gt;Axis labels, Legends, and Title&lt;/h2&gt;

&lt;p&gt;Legend, title and axis labels should be set in the axis object:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# TITLE AND AXIS LABELS
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'xlabel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ylabel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# or
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'xlabel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ylabel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# LEGEND
# add labels to curves
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ya&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Algorithm A'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Algorithm B'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# call legend function
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, Matplotlib has shortcuts for these methods directly through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyplot&lt;/code&gt; as well, in the case you are dealing with a single plot:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# shortcuts
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'xlabel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ylabel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;subplots&quot;&gt;Subplots&lt;/h2&gt;

&lt;p&gt;In Matplotlib, the subplot functionality allows you to specify where the axes should be situated (in the figure) according to a subplot grid.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;221&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# creates an axis in the position specified (row-col-num)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;222&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# creates an axis in the position specified (row-col-num)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;223&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# creates an axis in the position specified (row-col-num)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;224&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# creates an axis in the position specified (row-col-num)
# or
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nrows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ncols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# add a set of subplots to this figure
# or
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nrows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ncols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# shortcut for getting both figure and (all) axes
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After getting the axes, you can call the plotting method of your choice for each one.&lt;/p&gt;

&lt;h4 id=&quot;breaking-out-of-the-grid&quot;&gt;Breaking out of the grid&lt;/h4&gt;

&lt;p&gt;In addition, these functions do not impose a fixed grid, they simply create new axes that occupy specific positions. Because of that, you can create subplots with varying widths and heights in the same figure.
&lt;!-- Some examples are covered in [this tutorial](https://plot.ly/matplotlib/subplots/). --&gt;&lt;/p&gt;

&lt;p&gt;An option for a very flexible plotting scheme is the following: specify a starting position &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(x0,y0)&lt;/code&gt; and the size of your new axis &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(width,height)&lt;/code&gt; by passing the quadruple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(x0,y0,width,height)&lt;/code&gt; to &lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.add_subplot&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_subplot&lt;/code&gt;&lt;/a&gt; via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;position&lt;/code&gt; keyword argument:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ax1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Alternatively, you can use the &lt;a href=&quot;https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.add_axes&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_axes&lt;/code&gt;&lt;/a&gt; method, whose calling signature is the same quadruple. Its differences to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_subplot&lt;/code&gt; method are well discussed &lt;a href=&quot;https://stackoverflow.com/a/43330553/5103881&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;decoration&quot;&gt;Decoration&lt;/h2&gt;

&lt;p&gt;Usually configuration of the curves, bars, histograms, etc. are done in the respective plotting function:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'green'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'o'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linestyle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'dashed'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;linewidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;markersize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Check out the docs for more details:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/2.0.2/api/colors_api.html&quot;&gt;Colors&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/2.0.1/api/lines_api.html&quot;&gt;Lines&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://matplotlib.org/api/markers_api.html&quot;&gt;Markers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;specific-tips&quot;&gt;Specific tips&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;The parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alpha&lt;/code&gt; is usually the transparency of the curve.&lt;/li&gt;
  &lt;li&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plt.tight_layout()&lt;/code&gt; when your subplots have titles&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;varying-colors-w-custom-colormap&quot;&gt;Varying colors w/ custom colormap&lt;/h4&gt;
&lt;p&gt;Say you want to plot a set of lines with their colors varying according to some criteria.
Matplotlib define a set of &lt;a href=&quot;https://matplotlib.org/3.3.0/tutorials/colors/colormaps.html&quot;&gt;colormaps&lt;/a&gt; via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;matplotlib.cm&lt;/code&gt;, that returns a color from the colormap given your input (which can be in the range [0,1] or [0, 255]):&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cm&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/images/posts/matplotlib/varying_colors_with_colormap.png&quot; alt=&quot;Varying colors with colormap&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;other-resources&quot;&gt;Other resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Matplotlib_Cheat_Sheet.pdf&quot;&gt;DataCamp Matplotlib Cheatsheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;

&lt;h4 id=&quot;highlighting-specific-data-points-in-a-scatter-plot&quot;&gt;Highlighting specific data points in a scatter plot&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/matplotlib/specific_data_points_vs_dataset.png&quot; alt=&quot;Highlighting specific data points in a scatter plot&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;color-warmth-in-scatter-plot&quot;&gt;Color warmth in scatter plot&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/matplotlib/linear_regression.png&quot; alt=&quot;Color warmth in scatter plot&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;true-vs-predicted-data-points&quot;&gt;True vs predicted data points&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/matplotlib/true_vs_predicted _data_points.png&quot; alt=&quot;True vs predicted data points&quot; /&gt;&lt;/p&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Programming" /><summary type="html">A very simple introduction to Matplotlib in Python. It tries to present fundamental concepts without clinging to many details. If you have time I highly recommend you another post which is much more conceptual, detailed and illustrated than mine: “Artist” in matplotlib.</summary></entry><entry><title type="html">Statistics basic concepts</title><link href="http://arthurcgusmao.com/machine%20learning/2018/03/10/statistics-basic-concepts.html" rel="alternate" type="text/html" title="Statistics basic concepts" /><published>2018-03-10T00:00:00+00:00</published><updated>2018-03-10T00:00:00+00:00</updated><id>http://arthurcgusmao.com/machine%20learning/2018/03/10/statistics-basic-concepts</id><content type="html" xml:base="http://arthurcgusmao.com/machine%20learning/2018/03/10/statistics-basic-concepts.html">&lt;p&gt;This is my personal cheatsheet and tutorial for statistics. It is far from being a complete introduction, the main goal is to have a centralized resource where one can quickly remember or access more detailed resources. Many paragraphs are excerpts from the linked Wikipedia articles in the associated section.
\(\def\mean#1{\bar #1}
    \DeclareMathOperator{\EV}{\mathbb{E}}
    \DeclareMathOperator{\Prob}{\mathbb{P}}\)&lt;/p&gt;

&lt;h2 id=&quot;concepts-of-research-methods&quot;&gt;Concepts of Research Methods&lt;/h2&gt;

&lt;h3 id=&quot;construcs&quot;&gt;Construcs&lt;/h3&gt;

&lt;p&gt;Something hard to measure, that you’ll have to define how to measure.&lt;/p&gt;

&lt;h3 id=&quot;operational-definition&quot;&gt;Operational Definition&lt;/h3&gt;

&lt;p&gt;It’s how you will measure a given construct.&lt;/p&gt;

&lt;h3 id=&quot;sample&quot;&gt;Sample&lt;/h3&gt;
&lt;p&gt;A &lt;a href=&quot;https://en.wikipedia.org/wiki/Sample_(statistics)&quot;&gt;&lt;strong&gt;sample&lt;/strong&gt;&lt;/a&gt; is a (sub)set of elements from a population of interest.&lt;/p&gt;

&lt;p&gt;When it is not feasible to directly measure the value of a population parameter (e.g., unmanageable population size), we can infer its likely value using a sample (a subset of manageable size).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; make sure to distinguish between the terms &lt;em&gt;sample size&lt;/em&gt; and &lt;em&gt;number of samples&lt;/em&gt;. Use the former to describe the number of elements in a sample, and the latter to refer to the number of different samples (each constituted of many elements), if that is the case.&lt;/p&gt;

&lt;h3 id=&quot;parameter-vs-statistic&quot;&gt;Parameter vs Statistic&lt;/h3&gt;

&lt;p&gt;A &lt;a href=&quot;https://en.wikipedia.org/wiki/Statistical_parameter&quot;&gt;&lt;strong&gt;parameter&lt;/strong&gt;&lt;/a&gt; refers to a quantity the defines the actual, entire population (the original distribution) of interest. In contrast, a &lt;a href=&quot;https://en.wikipedia.org/wiki/Statistic&quot;&gt;&lt;strong&gt;statistic&lt;/strong&gt;&lt;/a&gt; refers to a quantity computed using values in a sample (part of the population).&lt;/p&gt;

&lt;p&gt;A statistic can be used to estimate a population parameter, to describe a sample, or to evaluate a hypothesis. When used to estimate a population parameter, it is called an &lt;em&gt;estimator&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;relationships-vs-causation&quot;&gt;Relationships vs Causation&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Relationships&lt;/strong&gt; can be shown by observational studies (eg. by seeing a point distribution). &lt;strong&gt;Causation&lt;/strong&gt;, however, has to be found through &lt;em&gt;controlled&lt;/em&gt; experiments.&lt;/p&gt;

&lt;h2 id=&quot;measures-of-central-tendency&quot;&gt;Measures of central tendency&lt;/h2&gt;

&lt;h3 id=&quot;mode&quot;&gt;Mode&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The value at which frequency is highest.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The mode doesn’t really represent the data well because:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s not affected by all scores in the dataset;&lt;/li&gt;
  &lt;li&gt;It changes radically depending on the sample;&lt;/li&gt;
  &lt;li&gt;We can’t really describe the mode by an equation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;median&quot;&gt;Median&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The value in the middle of the distribution.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;(If there are an even number of elements, the median will be the average of the two in the middle.)&lt;/p&gt;

&lt;p&gt;The median is more robust to outliers than the mean. It is therefore the best measure of central tendency when dealing with highly skewed distributions.&lt;/p&gt;

&lt;h3 id=&quot;mean-expected-value&quot;&gt;Mean (expected value)&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;The weighted average, or expected value.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Population mean: $\mu$&lt;/li&gt;
  &lt;li&gt;Sample mean: $\mean{x}$ (can be used to estimate $\mu$)&lt;/li&gt;
  &lt;li&gt;The mean is &lt;strong&gt;sensitive to outliers&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Law_of_large_numbers&quot;&gt;law of large numbers&lt;/a&gt; states that as a sample size grows, its mean gets closer to the average of the whole population.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/mode-median-mean-skewed.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The mean is always pulled towards the longest tail of the distribution, in relation to the median.&lt;/p&gt;

&lt;h2 id=&quot;variability-of-data&quot;&gt;Variability of data&lt;/h2&gt;

&lt;h3 id=&quot;range&quot;&gt;Range&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Range = MaxValue - MinValue&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;interquartile-range-iqr&quot;&gt;Interquartile Range (IQR)&lt;/h3&gt;

&lt;p&gt;IQR is the distance between the 25% and the 75% higher measure of the data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;IQR = Q3 - Q1&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Problem with Range and IQR: neither takes all data into account.&lt;/p&gt;

&lt;h3 id=&quot;outlier&quot;&gt;Outlier&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;An observation point that is distant from other observations.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Values below (Q1 - 1.5*IQR) or above (Q3 + 1.5*IQR) are usually considered outliers.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/outlier-boxplot.png&quot; alt=&quot;&quot; /&gt;
Outliers are represented as dots in a boxplot.&lt;/p&gt;

&lt;h2 id=&quot;estimators-bias-and-variance&quot;&gt;Estimators, Bias and Variance&lt;/h2&gt;

&lt;p&gt;(from a machine learning point of view)&lt;/p&gt;

&lt;h3 id=&quot;point-estimation&quot;&gt;Point Estimation&lt;/h3&gt;

&lt;p&gt;Point estimation is the attempt to provide the single “best” prediction of some quantity of interest. A &lt;strong&gt;point estimator&lt;/strong&gt; or &lt;strong&gt;statistic&lt;/strong&gt; is any function of the data:&lt;/p&gt;

\[\boldsymbol{\hat \theta}_m = g(\boldsymbol{x}^{(1)},...,\boldsymbol{x}^{(m)}).\]

&lt;p&gt;The definition does not require that $g$ return a value that is close to the true $\boldsymbol{\theta}$ or even that the range of $g$ is the same as the set of allowable values of $\boldsymbol{\theta}$.&lt;/p&gt;

&lt;h3 id=&quot;bias&quot;&gt;Bias&lt;/h3&gt;

&lt;p&gt;Bias measures the expected deviation from the true value of the function or parameter. The bias of an estimator is defined as:&lt;/p&gt;

\[\text{bias}(\boldsymbol{\hat\theta}_m) = \EV(\boldsymbol{\hat\theta}_m) - \boldsymbol{\theta}\]

&lt;p&gt;where the expectation is over the data (seen as samples from a random variable).&lt;/p&gt;

&lt;h3 id=&quot;variance-and-standard-deviation&quot;&gt;Variance and Standard Deviation&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Variance&lt;/strong&gt; provides a measure of the deviation from the expected estimator value that any particular sampling of the data is likely to cause.&lt;/p&gt;

&lt;p&gt;We call &lt;em&gt;deviation&lt;/em&gt; a measure of difference between the observed value of a variable and some other value, often that variable’s mean. We calculate the &lt;strong&gt;variance&lt;/strong&gt; by taking the &lt;em&gt;mean of squared deviations&lt;/em&gt;:&lt;/p&gt;

\[\sigma^2 = \sum_{i=1}^{n} \frac{(x_i - \mean{x})^2}{n}\]

&lt;p&gt;The &lt;strong&gt;standard deviation&lt;/strong&gt; is simply the root square of the variance. One interesting characteristic of the standard deviation is the &lt;a href=&quot;https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule&quot;&gt;68–95–99.7 rule&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;bessels-correction-unbiased-estimator-of-the-population-variance&quot;&gt;Bessel’s Correction (unbiased estimator of the population variance)&lt;/h4&gt;

&lt;p&gt;You use $(n - 1)$ instead of $(n)$ to calculate the standard deviation when estimating the standard deviation for the population from a sample.&lt;/p&gt;

\[\sigma \approx s = \sqrt{\sum_{i=1}^{n} \frac{(x_i - \mean{x})^2}{n - 1}}\]

&lt;p&gt;Using the &lt;em&gt;sample standard deviation&lt;/em&gt; (s) corrects the bias in the estimation of the population variance. It also partially corrects the bias in the estimation of the population standard deviation. However, the correction often increases the mean squared error in these estimations.&lt;/p&gt;

&lt;p&gt;These [&lt;a href=&quot;https://www.youtube.com/watch?v=1HmiArFN-EI&quot;&gt;1&lt;/a&gt;, &lt;a href=&quot;https://www.youtube.com/watch?v=9ONRMymR2Eg&quot;&gt;2&lt;/a&gt;] short videos try to bring some intuition on why to use $(n - 1)$ instead of $(n)$.&lt;/p&gt;

&lt;p&gt;The problem is that, by using the estimate $(\mean{x})$ instead of the actual population mean $(\mu)$, we create bias in the estimated variance by underestimating it, because $(\mean{x})$ is the value that minimizes $\sum_{i=1}(x_i - \mean{x})^2$, whereas $\mu$ could be any other value that makes it larger.&lt;/p&gt;

&lt;p&gt;Using $n-1$ in $s^2$ to estimate $\sigma^2$ fixes this bias, making $s^2$, on average, equals $\sigma^2$ (unbiased estimator).&lt;/p&gt;

&lt;h4 id=&quot;z-score&quot;&gt;Z-Score&lt;/h4&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Standard_score&quot;&gt;Z-Score&lt;/a&gt; (aka standard score) maps a sample $x_i$ to how many standard deviations $\sigma$ it is from the mean $\mu$:&lt;/p&gt;

\[z_i = \frac{x_i - \mu}{\sigma} \ \ \ \implies \ \ \ x_i = \mu + z_i \cdot \sigma\]

&lt;p&gt;By its definition, the z-score is a &lt;em&gt;dimensionless&lt;/em&gt; quantity. It can mapped directly to a percentile when you know the shape of the distribution (or, in case you do not, to weaker bounds using &lt;a href=&quot;https://en.wikipedia.org/wiki/Chebyshev's_inequality&quot;&gt;Chebyshev’s inequality&lt;/a&gt;).&lt;/p&gt;

&lt;h3 id=&quot;the-bias-vs-variance-tradeoff&quot;&gt;The Bias vs Variance Tradeoff&lt;/h3&gt;

&lt;p&gt;How to choose between two estimators: one with more bias and another with more variance? The most common way to negotiate this trade-off is to use cross-validation. Alternatively, we can also compare the &lt;strong&gt;mean squared error&lt;/strong&gt; (MSE) of the estimates:&lt;/p&gt;

\[\begin{align}
\text{MSE} &amp;amp; = \EV \left[ \left( \hat\theta_m - \theta \right)^2 \right] \\
&amp;amp; = \text{Bias} \left( \hat\theta_m \right)^2 + \text{Var} \left( \hat\theta_m \right)^2
\end{align}\]

&lt;p&gt;The relationship between bias and variance is tightly linked to the machine learning concepts of capacity, underfitting and overfitting.&lt;/p&gt;

&lt;h2 id=&quot;sampling-distributions&quot;&gt;Sampling distributions&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Sampling_distribution&quot;&gt;sampling distribution&lt;/a&gt; is the probability distribution of the values a statistic (e.g., sample mean or sample variance) can take on.&lt;/p&gt;

&lt;h3 id=&quot;standard-error&quot;&gt;Standard Error&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Standard_error&quot;&gt;&lt;strong&gt;standard error&lt;/strong&gt;&lt;/a&gt; of a statistic corresponds to the standard deviation of its sampling distribution. If the parameter or the statistic is the mean, it is called the standard error of the mean (SEM).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/sampling-distribution.png&quot; style=&quot;display: block; margin: auto; width: 80%;&quot; /&gt;
&lt;em&gt;Depiction of distribution sampling, taken from [2].&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;standard-error-of-the-mean-sem-sigma_barx&quot;&gt;Standard Error of the Mean (SEM) $\sigma_{\bar{x}}$&lt;/h3&gt;

&lt;p&gt;The SEM quantifies the precision of the mean, i.e., it measures how far the sample mean is likely to be from the true population mean. In other words, the SEM is the standard deviation of the distribution that defines the values of the sample mean we will observe, for each sample of size $n$ we take from the population.&lt;/p&gt;

&lt;p&gt;Since $\sigma$ is seldom known (to calculate it we’d have to make lots of experiments, finding many different values for the mean, and calculating its standard deviation), the SEM is usually estimated by using the sample standard deviation:&lt;/p&gt;

\[\text{SEM} = \sigma_{\bar{x}} = \frac{\sigma}{\sqrt{n}} \approx \frac{s}{\sqrt{n}}\]

&lt;p&gt;The proof of the relationship between the SEM, the standard deviation of the population $\sigma$, and the sample size $n$ is shown below (&lt;a href=&quot;https://stats.stackexchange.com/questions/89154/general-method-for-deriving-the-standard-error&quot;&gt;reference&lt;/a&gt;). The first step is allowed because the $X_i$ are independently sampled, so the variance of the sum is just the sum of the variances.&lt;/p&gt;

\[\text{Var}\bigg(\frac{\sum_{i=1}^{n}{X_i}}{n}\bigg) = \frac{1}{n^2}\text{Var}\bigg(\sum_{i=1}^{n}{X_i}\bigg) = \frac{1}{n^2}\sum_{i=1}^{n}\text{Var}\bigg({X_i}\bigg) = \frac{1}{n^2}\sum_{i=1}^{n}{\sigma^2} = \frac{\sigma^2}{n}\]

&lt;p&gt;We can use the SEM to interpret different populations and see if there is evidence that there are differences or similarities between them:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/interpreting_SEM.png&quot; alt=&quot;&quot; /&gt;
&lt;em&gt;How the standard error of the mean helps us interpret populations, from &lt;a href=&quot;https://www.youtube.com/watch?v=3UPYpOLeRJg&quot;&gt;this YouTube video&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can also use the relationship between the SEM and the sample size $n$ to define how many elements we need in our sample to achieve a desired level of statistical significance.&lt;/p&gt;

&lt;p&gt;Link: &lt;a href=&quot;http://onlinestatbook.com/stat_sim/sampling_dist/index.html&quot;&gt;simulation applet&lt;/a&gt; to explore aspects of sampling distributions.&lt;/p&gt;

&lt;h3 id=&quot;margin-of-error-and-confidence-interval&quot;&gt;Margin of error and confidence interval&lt;/h3&gt;

&lt;p&gt;Given a &lt;em&gt;confidence level&lt;/em&gt; $\gamma$ chosen by the investigator, a &lt;a href=&quot;https://en.wikipedia.org/wiki/Confidence_interval&quot;&gt;&lt;em&gt;confidence interval&lt;/em&gt;&lt;/a&gt; has a probability $\gamma$ of containing the true underlying parameter. In turn, the &lt;a href=&quot;https://en.wikipedia.org/wiki/Margin_of_error&quot;&gt;&lt;em&gt;margin of error&lt;/em&gt;&lt;/a&gt; (MOE) corresponds to how distant from the interval mean its margins are (i.e., half the width of the confidence interval).&lt;/p&gt;

&lt;p&gt;A line of inferential reasoning one could use to define a confidence interval is:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We know the properties of the sampling distribution, e.g., we know that the probability ofh yoaving the sample mean $\bar{x}$ fall between 1.96 standard errors $\sigma_{\bar{x}}$ from the population mean $\mu$ is 95%.&lt;/li&gt;
  &lt;li&gt;Thus, we know that $\mu$ is at most $1.96\sigma_{\bar{x}}$ away from $\bar{x}$ with 95% confidence.&lt;/li&gt;
  &lt;li&gt;We calculate $\sigma_{\bar{x}}$ and arrive at our confidence interval.
    &lt;ul&gt;
      &lt;li&gt;If we know $\sigma$, then it is straightforward;&lt;/li&gt;
      &lt;li&gt;If we don’t know $\sigma$, we estimate it using the sample standard deviation $s$.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Which gives us the general formula:&lt;/p&gt;

\[\text{CI} = \left( \bar{x} - z_{\gamma}\frac{\sigma}{\sqrt{n}} \ , \  \bar{x} + z_{\gamma}\frac{\sigma}{\sqrt{n}} \right)\]

&lt;h2 id=&quot;statistical-hypothesis-testing&quot;&gt;Statistical hypothesis testing&lt;/h2&gt;

&lt;h3 id=&quot;null-and-alternative-hypothesis&quot;&gt;Null and alternative hypothesis&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Null_hypothesis&quot;&gt;null hypothesis&lt;/a&gt; $H_0$ is a default position that there is no relationship between two measured phenomena or no association among groups. It is usually contrasted with an &lt;a href=&quot;https://en.wikipedia.org/wiki/Alternative_hypothesis&quot;&gt;alternative hypothesis&lt;/a&gt; $H_1$, a new theory.&lt;/p&gt;

&lt;p&gt;Statistical hypotheses are usually formulated in terms of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Test_statistic&quot;&gt;test statistic&lt;/a&gt;, a one-value numerical summary of a dataset, defined in a way that distinctions between the two hypotheses can be made. The test statistic must be such that its sampling distribution and, consequently, p-values can be calculated.&lt;/p&gt;

&lt;p&gt;It is common to define $H_0$ as the test statistic being the same (precisely, &lt;em&gt;not significantly different&lt;/em&gt;) for the different groups/conditions we are examining, and the alternative hypothesis as the group/conditions having &lt;em&gt;significantly&lt;/em&gt; different values of the test statistic:&lt;/p&gt;

\[\begin{align}
  H_0 : \mu_1 = \mu_2 \quad , \quad H_1 &amp;amp; : \mu_1 \neq \mu_2 \quad \text{(two-tailed test)} \\[0.8ex]
  H_0 : \mu_1 \le \mu_2 \quad , \quad H_1 &amp;amp; : \mu_1 \gt \mu_2 \quad \text{(one-tailed test)} \\
  H_0 : \mu_1 \ge \mu_2 \quad , \quad H_1 &amp;amp; : \mu_1 \lt \mu_2 \quad \text{(one-tailed test)} \\
\end{align}\]

&lt;p&gt;Notice that the boundaries between $H_0$ and $H_1$ are defined in terms of &lt;em&gt;statistical significance&lt;/em&gt;, not in the strict mathematical sense. For instance, if we found out that the (true) test parameter of the experimental group would be slightly but not significantly different than that of the control group, we would still consider $H_0$ as the correct hypothesis.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/One-_and_two-tailed_tests&quot;&gt;Two-tailed&lt;/a&gt; (non-directional) tests are more general (more conservative);  &lt;a href=&quot;https://en.wikipedia.org/wiki/One-_and_two-tailed_tests&quot;&gt;one-tailed&lt;/a&gt; (directional) tests are used when we expect a direction of the treatment effect (e.g., we want to compare a new webpage interface with an established one and we care only about assessing if the change will improve what is currently deployed).&lt;/p&gt;

&lt;!-- Z-test is any statistical  --&gt;

&lt;h3 id=&quot;p-value&quot;&gt;p-value&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/P-value&quot;&gt;&lt;em&gt;p-value&lt;/em&gt;&lt;/a&gt; refers to the probability of obtaining test results equals to or more extreme than the actual observations, under the assumptions that the null hypothesis $H_0$ is correct.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;$\text{p-value} = \Prob(T \ge t \mid H_0)$ for one-sided, right tail test.&lt;/li&gt;
  &lt;li&gt;$\text{p-value} = \Prob(T \le t \mid H_0)$ for one-sided, left tail test.
&lt;!-- $\text{p-value} = \Prob{T \le t \mid H_0}$ for one-sided, right tail test. --&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The smaller the p-value, the less likely $H_0$ holds, and the higher the statistical significance is said to be.&lt;/p&gt;

&lt;h3 id=&quot;statistical-significance&quot;&gt;Statistical significance&lt;/h3&gt;

&lt;p&gt;In simple words, &lt;a href=&quot;https://en.wikipedia.org/wiki/Statistical_significance&quot;&gt;statistical significance&lt;/a&gt; is a determination by an analyst that the results in the data are not explainable by chance alone [3].&lt;/p&gt;

&lt;p&gt;More precisely, given a significance level $\alpha$ (chosen before the study) and the p-value $p$ of a result, the result is &lt;em&gt;statistically significant&lt;/em&gt; when $p &amp;lt; \alpha$.&lt;/p&gt;

&lt;p&gt;Values for $\alpha$ are usually set to 5% or lower, depending on the field of the study and what probability is considered “unlikely” to occur.&lt;/p&gt;

&lt;h2 id=&quot;other-concepts&quot;&gt;Other concepts&lt;/h2&gt;

&lt;h3 id=&quot;iid&quot;&gt;i.i.d.&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables&quot;&gt;Independent and identically distributed&lt;/a&gt; random variables refers to a collection of random variables that are mutually independent from each other and sampled from the same probability distribution.&lt;/p&gt;

&lt;h3 id=&quot;central-limit-theorem&quot;&gt;Central limit theorem&lt;/h3&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Central_limit_theorem&quot;&gt;central limit theorem&lt;/a&gt; states that, when i.i.d. random variables are summed, the resulting distribution tends toward a normal distribution, as the sample size grow, even if the original variables themselves are not normally distributed.&lt;/p&gt;

&lt;h3 id=&quot;continuity-correction&quot;&gt;Continuity correction&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Continuity_correction&quot;&gt;Continuity correction&lt;/a&gt; are used when approximating a discrete random variable with a continuous one [&lt;a href=&quot;https://www.youtube.com/watch?v=kOcrXXSluTc&quot;&gt;ref&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/binomial-distribution.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;type-i-and-type-ii-errors&quot;&gt;Type I and Type II errors&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Type I error&lt;/em&gt; is rejecting a true null hypothesis; &lt;em&gt;Type II error&lt;/em&gt; is not rejecting a false null hypothesis. (&lt;a href=&quot;https://en.wikipedia.org/wiki/Type_I_and_type_II_errors&quot;&gt;Type I and type II errors - Wikipedia&lt;/a&gt;)&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Wikipedia&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.udacity.com/course/statistics--st095&quot;&gt;Udacity course on Statistics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;https://www.investopedia.com/terms/s/statistically_significant.asp&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Machine Learning" /><summary type="html">This is my personal cheatsheet and tutorial for statistics. It is far from being a complete introduction, the main goal is to have a centralized resource where one can quickly remember or access more detailed resources. Many paragraphs are excerpts from the linked Wikipedia articles in the associated section. \(\def\mean#1{\bar #1} \DeclareMathOperator{\EV}{\mathbb{E}} \DeclareMathOperator{\Prob}{\mathbb{P}}\)</summary></entry><entry><title type="html">Markdown and Pandoc for academic writing</title><link href="http://arthurcgusmao.com/academia/2018/01/27/markdown-pandoc.html" rel="alternate" type="text/html" title="Markdown and Pandoc for academic writing" /><published>2018-01-27T00:00:00+00:00</published><updated>2018-01-27T00:00:00+00:00</updated><id>http://arthurcgusmao.com/academia/2018/01/27/markdown-pandoc</id><content type="html" xml:base="http://arthurcgusmao.com/academia/2018/01/27/markdown-pandoc.html">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;In this post we are interested in using Markdown to produce great quality, scientific PDF files. This includes being able to use most of the capabilities that LaTeX has directly in Markdown. In order to do that we resort to a library named Pandoc, which is able to convert documents from one format to another seamlessly.&lt;/p&gt;

&lt;p&gt;Before jumping into the technical details though, we first present what are these tools, for the unfamiliar reader, and our motivation.&lt;/p&gt;

&lt;h3 id=&quot;markdown&quot;&gt;Markdown&lt;/h3&gt;

&lt;p&gt;Wikipedia defines Markdown as a lightweight markup language with plain text formatting syntax.&lt;/p&gt;

&lt;p&gt;It lets you easily define headings, use basic text formatting tools such as bold, italics and underlines, place links and images, etc. A quick demonstration can be found &lt;a href=&quot;http://www.unexpected-vortices.com/sw/rippledoc/quick-markdown-example.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;pandoc&quot;&gt;Pandoc&lt;/h3&gt;

&lt;p&gt;From &lt;a href=&quot;https://pandoc.org/MANUAL.html&quot;&gt;its website&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Pandoc is a Haskell library for converting from one markup format to another, and a command-line tool that uses this library.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Examples of the markups that Pandoc handles are HTML (.html), Microsoft Word (.docx), LaTeX and LaTeX Beamer (.tex), Markdown (.md), Org mode (.org), PDF (.pdf), among many, many others.&lt;/p&gt;

&lt;p&gt;Looking at the list above, one can see the power of Pandoc. It has everything we need to achieve our proposed writing framework.&lt;/p&gt;

&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;Since LaTeX already works the way we want (and does it pretty well), why would we want to mess with that and change an entire framework that has been working great for years?&lt;/p&gt;

&lt;p&gt;Well, we are not really giving up LaTeX. Pandoc uses LaTeX to produce the final PDF file, so what we are actually doing is incorporating everything that is good in LaTeX into a simpler markup language, where editing the source code is more visual appealing and easier to understand:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Commands and markup are simpler than in LaTeX.&lt;/li&gt;
  &lt;li&gt;The raw file is more readable.&lt;/li&gt;
  &lt;li&gt;You can use LaTeX directly in Markdown.&lt;/li&gt;
  &lt;li&gt;It’s possible to convert the Markdown input file to LaTeX (or other formats) if necessary, in case it’s really not possible to do what you want directly in Markdown.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s see how to configure our desired environment.&lt;/p&gt;

&lt;h2 id=&quot;configuration&quot;&gt;Configuration&lt;/h2&gt;

&lt;h3 id=&quot;installing-pandoc&quot;&gt;Installing Pandoc&lt;/h3&gt;

&lt;p&gt;Installing Pandoc is very easy, since they provide package installers for many different platforms. See &lt;a href=&quot;https://pandoc.org/installing.html&quot;&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;installing-pandoc-filters&quot;&gt;Installing Pandoc filters&lt;/h3&gt;

&lt;p&gt;The Pandoc command (command line) has an option to pass filters, which are like extensions to the internal representation that Pandoc creates when reading/writing documents. For a more in depth explanation see &lt;a href=&quot;https://pandoc.org/filters.html&quot;&gt;the docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What we need to know here is that there are some functionalities that LaTeX has are not built-in in Pandoc, and we will need filters for that.&lt;/p&gt;

&lt;p&gt;Filters are easy to install and they are usually well documented. &lt;a href=&quot;https://github.com/jgm/pandoc/wiki/Pandoc-Filters&quot;&gt;A list&lt;/a&gt; of third party filters can be found on the wiki.&lt;/p&gt;

&lt;p&gt;After you have installed a new filter, remember that it should be in your executable path in order to be easily accessible. If you are using an environment manager for Python such as &lt;a href=&quot;https://conda.io/&quot;&gt;conda&lt;/a&gt; and installed a filter through pip, consider copying the executable to a more general path. For instance:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; ~/.anaconda2/bin/pandoc-fignos /usr/local/bin/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Below are some of the filters that I personally use:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tomduck/pandoc-fignos&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pandoc-fignos&lt;/code&gt;&lt;/a&gt; for figure numbering and referencing.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tomduck/pandoc-eqnos&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pandoc-eqnos&lt;/code&gt;&lt;/a&gt; for equation numbers.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/jgm/pandoc-citeproc&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pandoc-citeproc&lt;/code&gt;&lt;/a&gt; for citations and bibliography.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;/h2&gt;

&lt;h3 id=&quot;executing-pandoc-via-the-command-line&quot;&gt;Executing Pandoc via the command line&lt;/h3&gt;

&lt;p&gt;Pandoc can be easily executed via the command line. The simplest form would be:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pandoc input.md &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; output.pdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Look at how it works smoothly: Pandoc automatically see the format of the output you are trying to produce and generates the correct one.&lt;/p&gt;

&lt;p&gt;If you are creating a more complex document, however, you will need to provide more information, such as filters. Keep in mind that the order of the filters matter, since some filters may. An example of a more complex call to the command is:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pandoc input.md &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; output.pdf &lt;span class=&quot;nt&quot;&gt;--filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;pandoc-fignos &lt;span class=&quot;nt&quot;&gt;--filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;pandoc-eqnos &lt;span class=&quot;nt&quot;&gt;--filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;pandoc-citeproc &lt;span class=&quot;nt&quot;&gt;--number-sections&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;executing-pandoc-in-atom-text-editor&quot;&gt;Executing Pandoc in Atom (text editor)&lt;/h3&gt;

&lt;p&gt;If you write lots of Markdown files, &lt;a href=&quot;http://atom.io&quot;&gt;Atom&lt;/a&gt; is the number one editor you should try. It has great built-in functionality, such as a Markdown previewer which lets you see how the final document is rendered on the fly.&lt;/p&gt;

&lt;p&gt;However, there an extension of the default previewer, named &lt;a href=&quot;https://github.com/shd101wyy/markdown-preview-enhanced&quot;&gt;Markdown Preview Enhanced&lt;/a&gt; which really makes the experience of writing in Markdown incredible. It has features like auto-syncing the preview with the source code, automatically managing images, applying pandoc filters to the preview, among others.&lt;/p&gt;

&lt;p&gt;Once you setup all the Pandoc filters you are using in this package, you will have an almost instantly preview of your document (that sinchronizes even when scrolled), in the same way that it will be produced by Pandoc. This makes the experience of writing really delightful. Since the package is well documented, I am only going to outline some features of it.&lt;/p&gt;

&lt;p&gt;What is more relevant for our purposes is that the package is capable of calling Pandoc and producing a pdf file. This is useful because otherwise we would have to open a terminal and execute the command, remembering to pass in all the filters and whatever would be necessary. To create the pdf document, you simply right-click at the preview and select Pandoc. Please look at the package’s configurations for more details. It is possible to configure options for how each document will be produced separately, such as export path, table of contents, and even LaTeX options. More details for that in &lt;a href=&quot;https://shd101wyy.github.io/markdown-preview-enhanced/#/pandoc-pdf&quot;&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another very interesting thing that Markdown Preview Enhanced helps is in making beautiful presentations. Check out &lt;a href=&quot;https://rawgit.com/shd101wyy/markdown-preview-enhanced/master/docs/presentation-intro.html&quot;&gt;this introduction&lt;/a&gt; for a glimpse.&lt;/p&gt;

&lt;p&gt;There is also a good topic on many other packages that you may find useful in &lt;a href=&quot;https://discuss.atom.io/t/using-atom-for-academic-writing/19222&quot;&gt;Atom’s discussion forum&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;a-yaml-front-matter-template&quot;&gt;A YAML front matter template&lt;/h2&gt;

&lt;p&gt;Here is a template of a YAML front matter that I personally use in my documents:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
title: &quot;Your title here&quot;
abstract: &quot;Your abstract here&quot;
author: Arthur


###
### Bibliography settings
###
bibliography:
    - /home/arthurcgusmao/Documents/zotero.bib
#csl: /home/arthurcgusmao/.csl/apa.csl
link-citations: true


###
### Formatting settings
###
documentclass: article
fontsize: 12pt
#geometry: margin=1.0in
geometry: &quot;left=3cm,right=3cm,top=2cm,bottom=2cm&quot;
header-includes:
    - \usepackage{times}
urlcolor: blue


###
### Markdown Preview Enhanced package settings
###
output:
    pdf_document:
        toc: false
        number_sections: true
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Academia" /><summary type="html">Introduction</summary></entry><entry><title type="html">How to read a scientific article</title><link href="http://arthurcgusmao.com/academia/2017/10/23/how-to-read-a-scientific-article.html" rel="alternate" type="text/html" title="How to read a scientific article" /><published>2017-10-23T00:00:00+00:00</published><updated>2017-10-23T00:00:00+00:00</updated><id>http://arthurcgusmao.com/academia/2017/10/23/how-to-read-a-scientific-article</id><content type="html" xml:base="http://arthurcgusmao.com/academia/2017/10/23/how-to-read-a-scientific-article.html">&lt;p&gt;This post is a summary of the article &lt;a href=&quot;http://www.owlnet.rice.edu/~cainproj/courses/HowToReadSciArticle.pdf&quot;&gt;“How to Read a Scientific Article”&lt;/a&gt; written by Purugganan and Hewitt. At the end, I present a simple template that I decided to use myself for taking notes when reading papers.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Reading a scientific article is not a simple task and you should not read it like a textbook. Below are presented practical steps that you should have in mind.&lt;/p&gt;

&lt;h4 id=&quot;1-skim-the-article-and-identify-its-structure&quot;&gt;1. Skim the article and identify its structure.&lt;/h4&gt;

&lt;p&gt;Before you begin reading, the first thing you should do is identify the document’s structure. This way you are able to extract information faster and easier, and decide which parts will be first read. If the paper follows the conventional IMRD (Introduction, Methods, Results and Discussion) structure, for each section described below you will find the mentioned features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract.&lt;/strong&gt; Usually contains four points: (a) purpose of study (why it was done), (b) methodology (how it was done), (c) results, (d) conclusion (what it means). This is where you should start reading.
&lt;!-- You should probably start reading by the abstract. --&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction.&lt;/strong&gt; Serves to create interest and provide enough information to the reader. This is usually done by leading the reader from broad information to the focal point or question of the paper, also describing related work and situating the proposed work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Methods.&lt;/strong&gt; Describes with technical language and detail what experiments were done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Results and Discussion.&lt;/strong&gt; The Results section states what was found, comparing the results. The Discussion section situates the work in the broader context of the field. It also states a clear answer to the question addressed by the paper and explains how the results support it.&lt;/p&gt;

&lt;h4 id=&quot;2-distinguish-main-points&quot;&gt;2. Distinguish main points.&lt;/h4&gt;

&lt;p&gt;Articles contain a lot of information and you should be able to find the main points. Some indicators of where they are located can be useful: title, abstract, keywords, figures, the first and couple last sentences of the introduction and specific words or phrases like &lt;em&gt;surprising, unexpected, in contrast to previous work, seldom been addressed, we propose, we introduce, we develop&lt;/em&gt;, etc.&lt;/p&gt;

&lt;h4 id=&quot;3-generate-questions-and-be-aware-of-your-understanding&quot;&gt;3. Generate questions and be aware of your understanding.&lt;/h4&gt;

&lt;p&gt;Before reading, you should question the credibility of the authors, work, journal, how your current knowledge can impact on your understanding of the paper, if you are reading the important parts and if there is someone with whom you can discuss confusing parts. After reading, question what is the problem addressed and its importance, the method used, what are the findings and their quality, how it relates to other work, what are the specific applications and future experiments.&lt;/p&gt;

&lt;h4 id=&quot;4-draw-inferences&quot;&gt;4. Draw inferences.&lt;/h4&gt;

&lt;p&gt;Experiments have demonstrated that readers who draw inferences understand and recall information better. Each part of information contained in an article can be related to what you already know or to some other part of the article. By linking the dots you learn more.&lt;/p&gt;

&lt;h4 id=&quot;5-take-notes-as-you-read&quot;&gt;5. Take notes as you read.&lt;/h4&gt;

&lt;p&gt;Notes improve recall and comprehension. It is useful to develop a template which you’ll use when reading an article. Time spent filling it out will pay off in the future. Important fields that your template should have are: citation, keywords, subject, hypothesis, methodology, results, summary of key points, context, significance and cited references, among others.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;my-notetaking-template&quot;&gt;My notetaking template&lt;/h2&gt;

&lt;div id=&quot;note-taking-template&quot;&gt;
    &lt;h3&gt;My notes&lt;/h3&gt;

    &lt;h4&gt;# General subject&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Specific subject&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Hypothesis&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Methodology&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Results&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Summary of key points&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Context (how this article relates to other work in the field)&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Significance (to the field and to your own work)&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Important cited references&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

    &lt;h4&gt;# Other comments&lt;/h4&gt;
    &lt;p&gt;...&lt;/p&gt;
    &lt;br /&gt;

&lt;/div&gt;
&lt;style&gt;
    #note-taking-template {
        border-radius: 10px;
        border: 3px groove #ddd;
        padding: 10px;
    }
    #note-taking-template h3:first-of-type {
        margin-top: 0;
    }
    #note-taking-template h4 {
        font-weight: bold;
    }
&lt;/style&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Academia" /><summary type="html">This post is a summary of the article “How to Read a Scientific Article” written by Purugganan and Hewitt. At the end, I present a simple template that I decided to use myself for taking notes when reading papers.</summary></entry><entry><title type="html">Managing your Bibliography with Zotero and Google Drive</title><link href="http://arthurcgusmao.com/academia/2017/09/27/managing-your-bibliography.html" rel="alternate" type="text/html" title="Managing your Bibliography with Zotero and Google Drive" /><published>2017-09-27T00:00:00+00:00</published><updated>2017-09-27T00:00:00+00:00</updated><id>http://arthurcgusmao.com/academia/2017/09/27/managing-your-bibliography</id><content type="html" xml:base="http://arthurcgusmao.com/academia/2017/09/27/managing-your-bibliography.html">&lt;p&gt;This post is a guide for setting up a bunch of softwares that will allow you to have a great free tool for managing bibliography in your computer (Zotero) together with a complete backup of all your data, including metadata like tags and notes, and access to all PDFs in another mobile device like Android. To accomplish this, we are going to use the following tools:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Google Drive&lt;/li&gt;
  &lt;li&gt;Zotero&lt;/li&gt;
  &lt;li&gt;ZotFile plugin for Zotero&lt;/li&gt;
  &lt;li&gt;Zotero Connector for Google Chrome&lt;/li&gt;
  &lt;li&gt;PDF-XChange Editor&lt;/li&gt;
  &lt;li&gt;Xodo PDF Reader &amp;amp; Editor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The role of Google Drive here is to sync your attachments (usually the PDF of the papers you saved) across all your devices and also assure you have a free backup in the cloud. The great win of this setup is that you probably will have a lot more space free in your Google Drive that the free storage limit that Zotero allows for. Then Zotero will be used only for managing the bibliography itself. ZotFile is a plugin that will help us with saving the attachments to the right location, so they can be in a place synced with Google Drive. The last three tools really depend on your operating systems and browser of choice, they were put here thinking of someone that uses Linux with Google Chrome and Android.&lt;/p&gt;

&lt;h2 id=&quot;zotero-configuration&quot;&gt;Zotero configuration&lt;/h2&gt;

&lt;h3 id=&quot;saving-the-pdfs-outside-zotero-data-directory&quot;&gt;Saving the PDFs outside Zotero Data Directory&lt;/h3&gt;

&lt;p&gt;We are going to use Google Drive to keep a copy of each PDF in the cloud. Therefore, we must configure Zotero to use relative paths for linked file attachments. It can be done by, inside Zotero, going to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Edit &amp;gt; Preferences &amp;gt; Advanced &amp;gt; Linked Attachment Base Directory&lt;/code&gt; and choosing the Base Directory where you’ll want to save your files.&lt;/p&gt;

&lt;p&gt;However, this only guarantees that Zotero will be able to find attached files that are located in the selected directory above, but will &lt;em&gt;not&lt;/em&gt; automatically save new downloaded files there (e.g., files downloaded via the &lt;a href=&quot;https://chrome.google.com/webstore/detail/zotero-connector/ekhagklcjbdpajgpjgmbionohlpdbjgc?hl=en&quot;&gt;Zotero Connector for Google Chrome&lt;/a&gt; will still be saved into the Data Directory). To change this behavior, we’ll use a Zotero plugin named ZotFile.&lt;/p&gt;

&lt;h3 id=&quot;setting-up-zotfile&quot;&gt;Setting up ZotFile&lt;/h3&gt;

&lt;p&gt;Download and install &lt;a href=&quot;http://zotfile.com/&quot;&gt;ZotFile&lt;/a&gt; like any other Zotero plugin. Then inside Zotero go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tools &amp;gt; ZotFile Preferences &amp;gt; General Settings &amp;gt; Location of Files&lt;/code&gt;. Select &lt;em&gt;Custom Location&lt;/em&gt; and select the same base directory you used in the step above. Be sure to uncheck the option &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Automatically rename attachments files using parent metadata&lt;/code&gt; in Zotero (same location as in &lt;em&gt;automatic saving of snapshots&lt;/em&gt; — see &lt;em&gt;Other configurations&lt;/em&gt; section below), I found that when this was checked ZotFile would not be able to move the file to the desired location.&lt;/p&gt;

&lt;p&gt;You can also setup ZotFile to store the attachments into subfolders according to rules that depend on the metadata of the files by changing &lt;em&gt;“Use subfolder defined by”&lt;/em&gt;. For myself, I set it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/%T&lt;/code&gt; which corresponds to the item type.&lt;/p&gt;

&lt;h3 id=&quot;auto-backup-of-zotero-data-directory&quot;&gt;Auto Backup of Zotero Data Directory&lt;/h3&gt;

&lt;p&gt;Since Zotero metadata are saved at the &lt;a href=&quot;https://www.zotero.org/support/zotero_data&quot;&gt;Zotero Data Directory&lt;/a&gt;, it is important to keep an updated backup of it. Otherwise you are at risk of losing all your tags, web links, etc. To make sure a backup would be run everyday I used anacron, following the steps proposed by &lt;a href=&quot;https://askubuntu.com/a/235090&quot;&gt;this answer&lt;/a&gt;. The anacron command I used was:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1   5   zotero.backup   zip /path/to/zoterobackup.zip /path/to/Zotero/ &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;viewing-and-annotating-your-pdfs&quot;&gt;Viewing and annotating your PDFs&lt;/h2&gt;

&lt;p&gt;Recently I found out that &lt;a href=&quot;https://www.tracker-software.com/product/pdf-xchange-editor&quot;&gt;PDF-XChange Editor&lt;/a&gt; works gracefully with &lt;a href=&quot;https://en.wikipedia.org/wiki/Wine_(software)&quot;&gt;Wine&lt;/a&gt;. I have tried a lot of PDF editors and viewers for Linux and this one is the best by far. Highly recommended. From my personal experience, I found that the newest version (7.0.324.3) was somewhat buggy in the 64 bit version, so I installed the 32 bit one and the problems were gone.&lt;/p&gt;

&lt;p&gt;Edit 2020-11-15: Recently I came across &lt;a href=&quot;https://code-industry.net/free-pdf-editor/&quot;&gt;Master PDF&lt;/a&gt;, an alternative PDF editor that runs natively on Linux, without the need for Wine. I have been using it for the last weeks successfully instead of PDF-XChange.&lt;/p&gt;

&lt;!-- An interesting thing for you to do if you want to default the opening of PDF files to evince but make an exception to open them with PDF-XChange when in a specific directory is [this script](https://gist.github.com/slowkow/8834315):
```bash
#!/bin/bash

# Check if the pdf is in the Zotero folder.
if [[ &quot;$1&quot; == */Bibliography/* ]]
then
    wine '/home/arthurcgusmao/.wine/drive_c/Program Files (x86)/PDF Editor/PDFXEdit.exe' &quot;$1&quot;
else
    evince &quot;$1&quot;
fi
```
Place the script above into `~/.local/bin/` and set it as the default PDF program:

1. Open `~/.local/share/applications/defaults.list`
2. Add `application/pdf=scriptabove` --&gt;

&lt;p&gt;For your Android device, you will want a PDF viewer that allows you to annotate the document and that is also capable of syncing with Google Drive, since all the attachments will be there. Unfortunately, the default PDF viewer for Google Drive app is not capable of annotating the document, so we have to resort to an external tool. I tested few of them and the one that worked best for me was &lt;a href=&quot;https://www.xodo.com/&quot;&gt;XODO PDF Reader &amp;amp; Annotator&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;other-configurations&quot;&gt;Other configurations&lt;/h2&gt;

&lt;h3 id=&quot;recursive-subcollections-in-zotero&quot;&gt;Recursive subcollections in Zotero&lt;/h3&gt;

&lt;p&gt;By default Zotero does not list items of sub-collections in the item-list. You can modify that through the &lt;a href=&quot;https://forums.zotero.org/discussion/3317/recursive-display-of-all-items-of-a-collection-and-its-subcollections/#Comment_14225&quot;&gt;recursive collections&lt;/a&gt; advanced option:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/zotero_recursivecollections.png&quot; alt=&quot;Set the custom variable `extensions.zotero.recursiveCollections` to true by double clicking over the Value column in the respective line.&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;disable-automatic-saving-of-snapshots&quot;&gt;Disable automatic saving of snapshots&lt;/h3&gt;

&lt;p&gt;One feature that can be annoying is that the Zotero connector for chrome will automatically save a snapshot from sites like arxiv. This can be disabled in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Preferences &amp;gt; General &amp;gt; File Handling&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/zotero_disable_snapshots.png&quot; alt=&quot;Uncheck the checkbox to disable automatic saving of snapshots.&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;automate-bib-file-generation&quot;&gt;Automate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bib&lt;/code&gt; file generation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/retorquere/zotero-better-bibtex&quot;&gt;zotero-better-bibtex&lt;/a&gt; is a plugin that can automate bibtex exporting, among other utilities. This is crucial for removing the distraction of having to regenerate your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bib&lt;/code&gt; file every time you add another item to your library.&lt;/p&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Academia" /><summary type="html">This post is a guide for setting up a bunch of softwares that will allow you to have a great free tool for managing bibliography in your computer (Zotero) together with a complete backup of all your data, including metadata like tags and notes, and access to all PDFs in another mobile device like Android. To accomplish this, we are going to use the following tools:</summary></entry><entry><title type="html">Downloading MP3 audio from YouTube</title><link href="http://arthurcgusmao.com/programming/2017/09/16/downloading-mp3-from-youtube.html" rel="alternate" type="text/html" title="Downloading MP3 audio from YouTube" /><published>2017-09-16T00:00:00+00:00</published><updated>2017-09-16T00:00:00+00:00</updated><id>http://arthurcgusmao.com/programming/2017/09/16/downloading-mp3-from-youtube</id><content type="html" xml:base="http://arthurcgusmao.com/programming/2017/09/16/downloading-mp3-from-youtube.html">&lt;p&gt;This is a simple tutorial to easily download lots of mp3 audios from YouTube videos. We are going to create a python script that uses &lt;a href=&quot;https://rg3.github.io/youtube-dl/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;youtube-dl&lt;/code&gt;&lt;/a&gt; to download and convert to audios the videos we are interested in, from an initial list of videos.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Make sure you have &lt;a href=&quot;https://rg3.github.io/youtube-dl/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;youtube-dl&lt;/code&gt;&lt;/a&gt; installed;&lt;/li&gt;
  &lt;li&gt;Make a .txt file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list.txt&lt;/code&gt; where each line corresponds to a link from a YouTube video;&lt;/li&gt;
  &lt;li&gt;Save and run the python script below.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;subprocess&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'list.txt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;list_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;youtube-dl --extract-audio --audio-format mp3 -l &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Example of .txt file:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;https://www.youtube.com/watch?v=hYZmK46--Mc&amp;amp;index=2&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=0j7dwG1cXc4&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=3
https://www.youtube.com/watch?v=JhV9f1l3H1A&amp;amp;index=4&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=GcNGtZPPSS8&amp;amp;index=5&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=ulam38XpQG4&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=6
https://www.youtube.com/watch?v=rBq-EPieRwU&amp;amp;index=7&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=Pv9Cs3FB2vQ&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=8
https://www.youtube.com/watch?v=WWs6GBwXnmw&amp;amp;index=9&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=GPPdWfoeGbs&amp;amp;index=10&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=ijSng_X9soc&amp;amp;index=11&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=ayEoiU5MOg4&amp;amp;index=12&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=XulpS3cAkOM&amp;amp;index=13&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=2gFLwkeJYB4&amp;amp;index=14&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=sem-8FpR10U&amp;amp;index=15&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=4Eo6zIcEYhk&amp;amp;index=16&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=W7bRGzFt2oE&amp;amp;index=17&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=GfF7EpHXyDY&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=18
https://www.youtube.com/watch?v=5WSlkzz2_Bk&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=19
https://www.youtube.com/watch?v=2_18oWkVq08&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=20
https://www.youtube.com/watch?v=6p6pKehb7Rk&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=21
https://www.youtube.com/watch?v=dSfZgygLjiw&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=22
https://www.youtube.com/watch?v=MJJj0BBrWOE&amp;amp;index=23&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
https://www.youtube.com/watch?v=Fyq8pT8IpCQ&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY&amp;amp;index=24
https://www.youtube.com/watch?v=mggupbkTmWc&amp;amp;index=25&amp;amp;list=PLFfM65xLnO-GvqhGu5vyM1vdm_CGkdQOY
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Programming" /><summary type="html">This is a simple tutorial to easily download lots of mp3 audios from YouTube videos. We are going to create a python script that uses youtube-dl to download and convert to audios the videos we are interested in, from an initial list of videos.</summary></entry><entry><title type="html">TensorFlow basics</title><link href="http://arthurcgusmao.com/machine%20learning/2017/08/15/tensorflow-basics.html" rel="alternate" type="text/html" title="TensorFlow basics" /><published>2017-08-15T00:00:00+00:00</published><updated>2017-08-15T00:00:00+00:00</updated><id>http://arthurcgusmao.com/machine%20learning/2017/08/15/tensorflow-basics</id><content type="html" xml:base="http://arthurcgusmao.com/machine%20learning/2017/08/15/tensorflow-basics.html">&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;

&lt;p&gt;This is what worked best for me, in Ubuntu 16.04.&lt;/p&gt;

&lt;h3 id=&quot;install-cuda&quot;&gt;Install cuda&lt;/h3&gt;

&lt;p&gt;Install cuda using apt-get (&lt;a href=&quot;http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#axzz4pToHShYz&quot;&gt;instructions here&lt;/a&gt;). You will need to download a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.deb&lt;/code&gt; package containing repository metadata which can be found in &lt;a href=&quot;https://developer.nvidia.com/cuda-downloads&quot;&gt;nvidia’s developer platform&lt;/a&gt;, and then proceed following the steps in the documentation.&lt;/p&gt;

&lt;p&gt;Don’t forget to perform the mandatory post-installation steps. The step for configuring POWER9 is only necessary if you are using a POWER9 system (Power 9 is a high-end server CPU made by IBM).&lt;/p&gt;

&lt;p&gt;Obs: if you installed cuda via apt-get, it may be interesting to put it on hold so that it doesn’t get updated automatically and end up breaking down your workflow unexpectedly, since tensorflow binaries are specific to a cuda version:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-mark hold cuda
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;install-cudnn&quot;&gt;Install cudnn&lt;/h3&gt;

&lt;p&gt;Install cudnn &lt;em&gt;without&lt;/em&gt; using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.deb&lt;/code&gt; package (&lt;a href=&quot;https://developer.nvidia.com/rdp/cudnn-download&quot;&gt;link here&lt;/a&gt;). For me, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.deb&lt;/code&gt; package provided by nvidia never seemed to get the right paths.&lt;/p&gt;

&lt;p&gt;The installation is very simple: after downloaded, uncompress the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tgz&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/Downloads/
&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-zxvf&lt;/span&gt; cudnn-9.1-linux-x64-v7.1.tgz

&lt;span class=&quot;c&quot;&gt;# __Output should be:__&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# cuda/include/cudnn.h&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# cuda/NVIDIA_SLA_cuDNN_Support.txt&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# cuda/lib64/libcudnn.so&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# cuda/lib64/libcudnn.so.7&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# cuda/lib64/libcudnn.so.7.1.3&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# cuda/lib64/libcudnn_static.a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, there will be 2 directories inside the extracted folder: include and lib64. They correspond to the directories with the same name inside /usr/local/cuda where you should move the respective files. That is all that is needed:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo mv&lt;/span&gt; ~/Downloads/cuda/include/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /usr/local/cuda/include/
&lt;span class=&quot;nb&quot;&gt;sudo mv&lt;/span&gt; ~/Downloads/cuda/lib64/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /usr/local/cuda/lib64/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;install-tensorflow&quot;&gt;Install TensorFlow&lt;/h3&gt;

&lt;p&gt;It’s possible to install tensorflow using Anaconda (&lt;a href=&quot;https://www.tensorflow.org/install/install_linux#InstallingAnaconda&quot;&gt;instructions here&lt;/a&gt;) using &lt;a href=&quot;https://github.com/tensorflow/tensorflow/blob/master/README.md&quot;&gt;individual whl files&lt;/a&gt;. Make sure that the wheel (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.whl&lt;/code&gt;) file you are installing was built for the python, cuda and cudnn versions you have installed.&lt;/p&gt;

&lt;p&gt;However, if you want maximum performance, you will want to &lt;a href=&quot;https://www.tensorflow.org/install/install_sources&quot;&gt;build it from source&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;testing-tensorflow&quot;&gt;Testing TensorFlow&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Create TensorFlow object called tensor
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hello_constant&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Hello World!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Run the tf.constant operation in the session
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hello_constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;variables-and-placeholders&quot;&gt;Variables and Placeholders&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/36693740/whats-the-difference-between-tf-placeholder-and-tf-variable&quot;&gt;This question&lt;/a&gt; describes pretty well the difference between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Variable()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.placeholder()&lt;/code&gt; and when each should be used.&lt;/p&gt;

&lt;h3 id=&quot;variables&quot;&gt;Variables&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/programmers_guide/variables#the_problem&quot;&gt;Doc here&lt;/a&gt;. An important point to remember about variables is that they exist outside the context of a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session.run&lt;/code&gt; call. Be sure to initialize their value by creating an initializer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.global_variables_initializer()&lt;/code&gt; and running it through the session.&lt;/p&gt;

&lt;p&gt;Variables are usually defined like so:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([...,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;biases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([...]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;placeholders&quot;&gt;Placeholders&lt;/h3&gt;

&lt;p&gt;With placeholders, their values should be passed via a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feed_dict&lt;/code&gt; when running the session.&lt;/p&gt;

&lt;p&gt;Since you’ll want to feed more than one single example in the input (your batch_size is usually greater than 1), it is common to create the input placeholder like this:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;which corresponds to a tensor with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt; (i.e., a variable number of) lines and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n_features&lt;/code&gt; columns.&lt;/p&gt;

&lt;p&gt;To define a scalar, create a tensor with 0 dimensions:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;relus&quot;&gt;ReLUs&lt;/h2&gt;

&lt;p&gt;The ReLU function is provided via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.relu()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Hidden Layer with ReLU activation function
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_biases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_biases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;convolutions&quot;&gt;Convolutions&lt;/h2&gt;

&lt;p&gt;The functions &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/conv2d&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.conv2d()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/bias_add&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.bias_add()&lt;/code&gt;&lt;/a&gt; help you create a convolutional layer:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strides&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[...],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'SAME|VALID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias_add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# activation function
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.conv2d&lt;/code&gt; requires the input be 4D (batch_size, height, width, depth). The difference between the two types of padding is &lt;a href=&quot;https://stackoverflow.com/a/39371113/5103881&quot;&gt;nicely explained here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;pooling&quot;&gt;Pooling&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/max_pool&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.max_pool()&lt;/code&gt;&lt;/a&gt; helps you apply max pooling to the convolutional layer:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_pool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ksize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[...],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strides&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[...],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'SAME|VALID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ksize&lt;/code&gt; is the filter size. 2x2 filters with a stride of 2x2 are common in practice.&lt;/p&gt;

&lt;h2 id=&quot;dropout&quot;&gt;Dropout&lt;/h2&gt;

&lt;p&gt;Dropout forces the network to learn &lt;strong&gt;redundant&lt;/strong&gt; representations, making things more robust and preventing overfitting. Also, it makes the network act as if taking the consensus of an ensemble of networks, improving performance. See &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/dropout&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.dropout()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# probability to keep units
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;biases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;biases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to compensate for dropped units, the function automatically multiplies kept units by $\frac{1}{keep_prob}$.&lt;/p&gt;

&lt;h2 id=&quot;embedding-layers&quot;&gt;Embedding Layers&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/embedding_lookup&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.embedding_lookup()&lt;/code&gt;&lt;/a&gt; does the job of retrieving rows automatically from an embedding layer. In the example below we create an embedding layer for the case of word representation, considering that the number of possible words (or, in general, the number of possible indexes we are going to feed into the network) is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vocab_size&lt;/code&gt; and the number of latent factors in the embedding is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;embed_dim&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ids&lt;/code&gt; should be a tensor with type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int32&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int64&lt;/code&gt; containing the ids of the rows to be retrieved.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/a/41922877/5103881&quot;&gt;This answer&lt;/a&gt; in stackoverflow has a more detailed explanation.&lt;/p&gt;

&lt;h2 id=&quot;lstms&quot;&gt;LSTMs&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build_cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lstm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BasicLSTMCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;drop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DropoutWrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lstm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_keep_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;drop&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MultiRNNCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;build_cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dynamic_rnn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source_sequence_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;batch-normalization&quot;&gt;Batch normalization&lt;/h2&gt;

&lt;h4 id=&quot;using-tflayersbatch_normalization-higher-level&quot;&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.layers.batch_normalization()&lt;/code&gt; (higher level)&lt;/h4&gt;

&lt;p&gt;First, add batch normalization to the layer (usually before the activation function). A new parameter to the layer will be necessary, indicating if it is training or not. Keep in mind that you will need to create a new placeholder for this boolean value later.&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fully_connected&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prev_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_training&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prev_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_normalization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;training&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_training&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, you will need to tell tensorflow to update the population statistics while training:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;control_dependencies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GraphKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UPDATE_OPS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_opt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;using-tfnnbatch_normalization-lower-level&quot;&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.batch_normalization()&lt;/code&gt; (lower level)&lt;/h4&gt;

&lt;p&gt;If you want to implement a lower level batch_normalization function, you will use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.batch_normalization()&lt;/code&gt;. See the documentation for more details. &lt;a href=&quot;https://github.com/arthurcgusmao/dlnd_batch_normalization/blob/master/Batch_Normalization_Solutions.ipynb&quot;&gt;This jupyter notebook&lt;/a&gt; can be of help as well.&lt;/p&gt;

&lt;h2 id=&quot;training-the-network&quot;&gt;Training the network&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# define loss and optimizer
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax_cross_entropy_with_logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GradientDescentOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_examples&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{...})&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{...})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Remember to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keep_prob = 1&lt;/code&gt; when calculating the loss.&lt;/p&gt;

&lt;h2 id=&quot;save-and-load-progress&quot;&gt;Save and load progress&lt;/h2&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.train.Saver&lt;/code&gt; lets you save any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Variable&lt;/code&gt; to your file system.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# do your stuff
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'./model.ckpt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ckpt&lt;/code&gt; stands for checkpoint.&lt;/p&gt;

&lt;p&gt;To restore the session, do:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'./model.ckpt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# no need to call the initializer
&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# do your stuff
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When loading the data tensorflow uses the names it assigns to variables, so be sure to check out the next section.&lt;/p&gt;

&lt;h2 id=&quot;names-and-scopes&quot;&gt;Names and Scopes&lt;/h2&gt;

&lt;p&gt;Tensorflow automatically assings names to each variable. These names are used for reusing the same variables in different contexts (or scopes) and when reloading data. You are able to define the naming structure of the variables (like if it was a namespace) with the functions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.name_scope()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.variable_scope&lt;/code&gt; (their difference is well explained in these answers: &lt;a href=&quot;https://stackoverflow.com/a/43580096/5103881&quot;&gt;[a1]&lt;/a&gt;, &lt;a href=&quot;https://stackoverflow.com/a/37534656/5103881&quot;&gt;[a2]&lt;/a&gt;), which cause all groups of related objects to have the same naming structure. A good way to do it when defining functions to create the layers is:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fc_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channels_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channels_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'fc'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channels_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channels_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'W'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channels_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s possible to set names after the variable was created also, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.identity()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'weights'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;shapes-and-dimensions&quot;&gt;Shapes and dimensions&lt;/h2&gt;

&lt;p&gt;To get the &lt;a href=&quot;https://stackoverflow.com/a/40666375/5103881&quot;&gt;shape as a list of ints&lt;/a&gt;, do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensor.get_shape().as_list()&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;tensorboard&quot;&gt;TensorBoard&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/get_started/summaries_and_tensorboard&quot;&gt;TensorBoard&lt;/a&gt; is a suite of visualization tools to make debugging, optimization and understading of TF graphs easier.&lt;/p&gt;

&lt;p&gt;To use TB we need first write data from TF to disk, using the class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.summary.FileWriter()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/tmp/example_name/1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# this can also be passed in the above statement
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And then use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorboard&lt;/code&gt; specifying the logging directory:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;tensorboard &lt;span class=&quot;nt&quot;&gt;--logdir&lt;/span&gt; /tmp/example_name/1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;To export condensed information about the model, you use &lt;a href=&quot;https://www.tensorflow.org/api_guides/python/summary&quot;&gt;summaries&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'accuracy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accuracy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'input'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fc_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...):&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;histogram&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;weights&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;histogram&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;biases&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;histogram&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;activations&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# collect summaries
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;merged_summary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;merge_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;merged_summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]})&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Histograms are useful when you have a bunch of numbers (like in a matrix) and you want to look at the distribution of it.&lt;/p&gt;

&lt;p&gt;It may be an interesting idea to save different sets of summaries to disk for the varying hyperparameters you may want to experiment with:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# hyperparameter search
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_fc_layers&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# save a different summary for each configuration
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;hparam_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_hparam_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_fc_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/tmp/example_name/&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hparam_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# actually run with the new settings
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A very interesting feature that TB has is the &lt;strong&gt;Embedding Visualizer&lt;/strong&gt;, which lets you project high dimensional data into 3 dimensions. Code shown above was taken from &lt;a href=&quot;https://github.com/dandelionmane/tf-dev-summit-tensorboard-tutorial/blob/master/mnist.py&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;

&lt;p&gt;Lots of Tensorflow examples &lt;a href=&quot;https://github.com/aymericdamien/TensorFlow-Examples&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Machine Learning" /><summary type="html">Installation</summary></entry><entry><title type="html">Linux Commands Cheatsheet</title><link href="http://arthurcgusmao.com/programming/2017/07/28/linux-basic-commands.html" rel="alternate" type="text/html" title="Linux Commands Cheatsheet" /><published>2017-07-28T00:00:00+00:00</published><updated>2017-07-28T00:00:00+00:00</updated><id>http://arthurcgusmao.com/programming/2017/07/28/linux-basic-commands</id><content type="html" xml:base="http://arthurcgusmao.com/programming/2017/07/28/linux-basic-commands.html">&lt;!-- # Useful linux commands --&gt;

&lt;h3 id=&quot;read-the-docs&quot;&gt;Read the docs!&lt;/h3&gt;

&lt;p&gt;Maybe one of the most underused tools in the command-line is the documentation of the commands themselves. You can easily open the docs for the vast majority of the commands you will need simply by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man &amp;lt;command&amp;gt;&lt;/code&gt;. Then search for what you are interested in by typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;alternatives-to-man&quot;&gt;Alternatives to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;While &lt;strong&gt;man&lt;/strong&gt; is the default command to show docs, the community has developed alternatives with simpler output:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;tldr&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm install -g tldr&lt;/code&gt;) (personally I find it the best option);&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bropages&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gem install bropages&lt;/code&gt;);&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;cheat&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip install cheat&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;compress-files-eg-making-a-backup&quot;&gt;Compress files (e.g. making a backup)&lt;/h3&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-zcvf&lt;/span&gt; archive-name.tar.gz directory-name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;z: Compress archive using gzip program&lt;/li&gt;
  &lt;li&gt;c: Create archive&lt;/li&gt;
  &lt;li&gt;v: Verbose i.e display progress while creating archive&lt;/li&gt;
  &lt;li&gt;f: Archive File name&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;uncompress-files-eg-restoring-a-backup&quot;&gt;Uncompress files (e.g. restoring a backup)&lt;/h3&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-zxvf&lt;/span&gt; archive-name.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;x: Extract files&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;accessing-a-remote-machine&quot;&gt;Accessing a remote machine&lt;/h3&gt;
&lt;p&gt;Having a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pem&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; key.pem ubuntu@0.0.0.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;copy-files-between-machines&quot;&gt;Copy files between machines&lt;/h3&gt;
&lt;p&gt;Having a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pem&lt;/code&gt; file and using scp:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;scp &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; key.pem file1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;...&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; fileN ubuntu@0.0.0.0://home/ubuntu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using rsync via ssh:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;rsync &lt;span class=&quot;nt&quot;&gt;-avz&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ssh -p 22000&quot;&lt;/span&gt; remote_user@remote_host:/remote/path/ ~/local/path/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;rsync options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a: Archive mode (see &lt;a href=&quot;https://serverfault.com/a/141778/360330&quot;&gt;this answer&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;v: Verbose&lt;/li&gt;
  &lt;li&gt;z: Compress data during transfer&lt;/li&gt;
  &lt;li&gt;e: Specify remote shell to use (ssh)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See &lt;a href=&quot;https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/&quot;&gt;this blog post&lt;/a&gt; for a great depiction of many other different ways to use rsync.&lt;/p&gt;

&lt;h3 id=&quot;find-search-for-files&quot;&gt;Find (Search for files)&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;find &lt;span class=&quot;nv&quot;&gt;$directory&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*.json'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The command above will search for all files that have the extesion ‘json’ in the directory specified (including subdirectories).&lt;/p&gt;

&lt;h3 id=&quot;grep-search-for-strings-in-many-files&quot;&gt;Grep (Search for strings in many files)&lt;/h3&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rn&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;string&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;r: Recursive&lt;/li&gt;
  &lt;li&gt;n: Show line Number&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;symbolic-links&quot;&gt;Symbolic links&lt;/h3&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &amp;lt;original-file-path&amp;gt; &amp;lt;simlink-path&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;s: Creates symbolic link instead of hard link.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;disk-usage-du&quot;&gt;Disk Usage (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;du&lt;/code&gt;)&lt;/h3&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;du&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-sh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;file_path]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Options:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;s (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--summarize&lt;/code&gt;): display only a total of each argument&lt;/li&gt;
  &lt;li&gt;h (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--human-readable&lt;/code&gt;): print sizes in human readable format (e.g., 1K 234M 2G)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;screen-terminal-sessions-in-the-background&quot;&gt;Screen (terminal sessions in the background)&lt;/h3&gt;

&lt;p&gt;List current screen sessions:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;screen &lt;span class=&quot;nt&quot;&gt;-ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Start a new screen session with name [name]:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;screen &lt;span class=&quot;nt&quot;&gt;-S&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;name]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Resume (reconnect) to the screen named [name]&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;screen &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;name]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;commands-when-you-are-inside-a-screen&quot;&gt;Commands when you are inside a screen&lt;/h4&gt;

&lt;p&gt;Detach from current screen:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-a&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Rename current screen to [name]:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-a&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sessionname [name]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;system-statistics&quot;&gt;System statistics&lt;/h3&gt;

&lt;p&gt;Performance statistics for all logical processors:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mpstat &lt;span class=&quot;nt&quot;&gt;-P&lt;/span&gt; ALL 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Memory usage statistics:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vmstat &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Memory and CPU usage per process:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;top
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;git&quot;&gt;Git&lt;/h3&gt;

&lt;p&gt;Pretty logging&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git log &lt;span class=&quot;nt&quot;&gt;--pretty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;oneline &lt;span class=&quot;nt&quot;&gt;--all&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--decorate&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--graph&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pull only a specific directory:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; relative/path/to/dir
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Download only a specific directory (without having to clone the whole repo):
check &lt;a href=&quot;https://stackoverflow.com/a/18194523/5103881&quot;&gt;this answer&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;packages-on-ubuntu&quot;&gt;Packages on Ubuntu&lt;/h3&gt;

&lt;p&gt;See where a package is installed:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dpkg &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; &amp;lt;packagename&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;sorting-mp3-in-a-usb-stick&quot;&gt;Sorting mp3 in a USB stick&lt;/h3&gt;

&lt;p&gt;Check path to USB stick (search for device name):&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lsblk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Unmount:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;umount /dev/sdb1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sort using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fatsort&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;fatsort /dev/sdb1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Programming" /><summary type="html"></summary></entry><entry><title type="html">Jupyter Notebook Tips</title><link href="http://arthurcgusmao.com/programming/2017/06/18/jupyter-notebook-tips.html" rel="alternate" type="text/html" title="Jupyter Notebook Tips" /><published>2017-06-18T00:00:00+00:00</published><updated>2017-06-18T00:00:00+00:00</updated><id>http://arthurcgusmao.com/programming/2017/06/18/jupyter-notebook-tips</id><content type="html" xml:base="http://arthurcgusmao.com/programming/2017/06/18/jupyter-notebook-tips.html">&lt;h2 id=&quot;seeing-pythons-logging-messages-in-a-jupyter-notebook&quot;&gt;Seeing python’s logging messages in a Jupyter Notebook&lt;/h2&gt;

&lt;p&gt;Originally answered &lt;a href=&quot;https://stackoverflow.com/a/41060201/5103881&quot;&gt;in stackoverflow&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; is the default stream for the logging module, so in IPython and Jupyter notebooks you
might not see anything unless you configure the stream to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'%(asctime)s | %(levelname)s : %(message)s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Hello world!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;truncating-pandas-float-display&quot;&gt;Truncating pandas float display&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# changes default formatting of float to 3 decimal places
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float_format&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{:.3f}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;!--
## Matplotlib

### Two plots side by side

https://stackoverflow.com/a/42818547/5103881
--&gt;

&lt;h2 id=&quot;changing-pythons-path&quot;&gt;Changing Python’s path&lt;/h2&gt;

&lt;p&gt;It is possible to change the path Python uses to search for files so that you can import modules that otherwise would only be possible by having your notebook file in the same directory. This is useful when you want to have a specific folder for your notebooks in order to be more organized.&lt;/p&gt;

&lt;p&gt;To do that, run the following code:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/path/to/application/app/folder'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For more details see &lt;a href=&quot;https://stackoverflow.com/a/4383597/5103881&quot;&gt;this answer&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;fix-installed-packages-not-being-imported&quot;&gt;Fix installed packages not being imported&lt;/h2&gt;

&lt;p&gt;Sometimes, when using conda, you may find that you’re not able to import installed packages into your notebook. One of the reasons may be because Jupyter is not using the correct kernel.&lt;/p&gt;

&lt;p&gt;To find out what kernel is being used, run (within the notebook):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you find that this is the problem, a simple solution is simply installing jupyter within the conda environment you are using:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source &lt;/span&gt;activate &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;yourenvname]
conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;jupyter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Other alternative solutions to this problem can be found &lt;a href=&quot;https://github.com/jupyter/notebook/issues/2563&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;using-git-to-version-control-jupyter-notebooks&quot;&gt;Using git to version control jupyter notebooks&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://timstaley.co.uk/posts/making-git-and-jupyter-notebooks-play-nice/&quot;&gt;This post&lt;/a&gt; explains in a very nice way different ways we can combine jupyter notebook with git tracking only changes in code.&lt;/p&gt;

&lt;h2 id=&quot;tracking-time&quot;&gt;Tracking time&lt;/h2&gt;

&lt;p&gt;Jupyter and IPython make it really easy to track time in the cells you execute:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'%time will output the time of running only this line'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Jupyter can also track'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'the time that running an entire cell took'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'when you use %%time'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For more accurate results you may also want to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;timeit&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time&lt;/code&gt;, which Jupyter also allows for. See &lt;a href=&quot;https://stackoverflow.com/questions/17579357/time-time-vs-timeit-timeit&quot;&gt;this answer&lt;/a&gt; for more details on the difference between the two.&lt;/p&gt;

&lt;h2 id=&quot;saving-the-current-notebook-from-code&quot;&gt;Saving the current notebook from code&lt;/h2&gt;

&lt;p&gt;It’s a bit of a hack, but you can save the current notebook you are running the code from using the following lines:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;IPython.display&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Javascript&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Javascript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'IPython.notebook.save_checkpoint();'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;converting-a-notebook-from-the-command-line&quot;&gt;Converting a notebook from the command line&lt;/h2&gt;

&lt;p&gt;The command below will convert the notebook to html. It’s possible to define other formats as well (check &lt;a href=&quot;https://nbconvert.readthedocs.io/en/latest/customizing.html&quot;&gt;the documentation&lt;/a&gt;).&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bash&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;jupyter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nbconvert&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notebook_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipynb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;exporting-history&quot;&gt;Exporting history&lt;/h2&gt;

&lt;p&gt;It’s possible to export the history (each command that was issued) of your current notebook by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%notebook&lt;/code&gt; magic:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;notebook&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;history&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipynb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;running-bash-commands&quot;&gt;Running bash commands&lt;/h2&gt;

&lt;p&gt;To run a bash command, the magic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%%bash&lt;/code&gt; at the first line of a cell will do it for you:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bash&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;history&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipynb&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you want to access a python variable within the script cell, use (see &lt;a href=&quot;https://stackoverflow.com/questions/19579546/can-i-access-python-variables-within-a-bash-or-script-ipython-notebook-c&quot;&gt;this answer&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;$myPythonVar&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;$myOtherVar&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;This bash script knows about $1 and $2&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is also possible to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subprocess&lt;/code&gt; module to run bash commands from python:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;bash_command&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mv history.ipynb {}/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;export_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;subprocess&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;process&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Popen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bash_command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;communicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;remote-access-to-another-machine&quot;&gt;Remote access to another machine&lt;/h2&gt;

&lt;p&gt;Start the server &lt;u&gt;on the remote machine&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;jupyter notebook &lt;span class=&quot;nt&quot;&gt;--no-browser&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8889
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Open an SSH tunnel (port forwarding) &lt;u&gt;from your local machine&lt;/u&gt; to the remote one:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ssh &lt;span class=&quot;nt&quot;&gt;-N&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; localhost:8888:localhost:8889 remote_user@remote_host
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You’ll then be able to access the remote kernel in your local browser by going to &lt;a href=&quot;http://localhost:8888&quot;&gt;http://localhost:8888&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;setting-up-a-password&quot;&gt;Setting up a password&lt;/h2&gt;

&lt;p&gt;Instead of a token, it’s possible to set up a default password that jupyter will use. Follow &lt;a href=&quot;http://testnb.readthedocs.io/en/stable/examples/Notebook/Configuring%20the%20Notebook%20and%20Server.html#setting-a-password&quot;&gt;these instructions&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;module-autoreload&quot;&gt;Module autoreload&lt;/h2&gt;

&lt;p&gt;These configurations will auto-reload modules (changes in the source code will not require manual reloading—see &lt;a href=&quot;http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython&quot;&gt;stackoverflow&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_ext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;autoreload&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;autoreload&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;other-references&quot;&gt;Other references&lt;/h2&gt;

&lt;p&gt;Other posts I found helpful that cover jupyter notebook hints:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/&quot;&gt;dataquest.io&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Arthur Colombini Gusmão</name></author><category term="Programming" /><summary type="html">Seeing python’s logging messages in a Jupyter Notebook</summary></entry></feed>