Exercise: Introduction to Jupyter Notebooks¶

  1. Introduction to Jupyter Notebooks
    In this course, we will program Neural networks, using Python, in Jupyter Notebooks, which some of you might not have extensive experience in. Therefore, the first part of this first exercise will be an introduction to Jupyter Notebooks.

  2. Back-propagation
    In the second part of the you will practice further working with Jupyter Notebooks by performing a single forward and backward step for a very simple ffANN.

1. Introduction to Jupyter Notebooks¶

The Jupyter Notebook is an programming environment that allows you to mix code with formatted text, equations and visualizations. It shares som similarities with Rmarkdown and Rstudio, which some of you mnight be familiar with, but while Rmarkdown was developed focusing on the R language, Jupyter Notebooks was developed focusing on the Pyhton language. (however, both these tools has since further developed to include also other programming languages.) Jupyter notebooks is web-application; you open documents, edit and compile them in a web-browser on your laptop. It is an open-source application that is continuopusly developed. by a large and dynamic community. It can be used to document your daily bioinformatics work, create nice reports or slides for a presentation.

Task 1.1. Create the nndl conda environment¶

  1. Create a dedicated folder (henceforth referred to as the nndl folder) on your laptop for NN&DL course exercises (if you have not already done this). cd into the nndl folder.
  2. Download the conda environment file nndl_python-yaml to the nndl folder, e.g.,
    curl -o nn_dl_python.yaml https://raw.githubusercontent.com/NBISweden/workshop-neural-nets-and-deep-learning/master/common_assets/conda_envs/nn_dl_python.yaml
    or alternatively
    wget https://raw.githubusercontent.com/NBISweden/workshop-neural-nets-and-deep-learning/master/common_assets/conda_envs/nn_dl_python.yaml
  3. Create the nndl conda environment using the command
    conda env create -f nndl_python-yaml
    (Note! you should already have conda installed on your laptop, else look at the prerequisite notesXXX)
  4. Activate the nndl environment using the command
    conda activate

Task 1.2. Start the Jupyter Notebook web application¶

The Jupyter Notebook web application is the user interface. It opens local html pages in your browser and allows you to edit and run the code and markdown in your notebook. Your "home page" is the Notebook dashboard window. Let's open this window.

  1. (Ensure that you're still in the nndl folder)
  2. Start a Jupyter Notebook by typing jupyter notebook
  3. You should now see the Notebook dashboard window:
    nb dashboard Files screenshot

The Notebook dashboard contains

  • Several tabs at top; we will here focus only on the two left-most ones, Files and Running and the right-most one, Nbextensions.
  • A listing of the files and folders in the current directory; since you started the notebook in the nndl folder, you should see the content of that and it should be empty. You will be able to use this to navigate among these folders and files.
  • Above to the right, you will have three buttons, an upload button, a New button and an update button. We will go throught their use below.

Task 1.3. Create a new Notebook¶

The notebook is a file (with suffix .ipynb) that will store the code and markdown you write to fulfil the purpose of the notebook. Notebook files are accessed using the notebook editor. To open a new file in the editor:

  1. (Ensure that you are in the Files tab of the jupyter Notebook main window)
  2. Click new and select _Python [conda env: nn_dlpython] in the scroll-down menu
    • this means that you will use the Python in the _nn_dlpython conda environment as your kernel for the notebook
  3. The editor will open in a new browser window with an empty and unnamed notebook:
    Empty Notebook

The editor window comprise:

  • The menu row at the top
  • To the right of this is some information whether telling
    • Whether the notebook is trusted
      • this is a security measure, newly opened existing notebooks (with unknown code) are not trusted until you explicitly/manually execute code in the notebook, at which point it becomes trusted
    • The kernel's name,
    • The kernel indicator showing whether the kernel is working (filled circle) or idle (empty circle)
  • Below this its a row of icons, the tool bar
  • At the bottom is an empty cell

We will below just go through a selection of menu or toolbar items. For more details, refer to the Jupyter Notebook documentation. A interactive tour of the user interface can also be started from the Help menu item User Interface Tour.

Note 1! Your terminal will show log messages from thew Jupyter Notebook session and will not be accessible for normal commands -- this is the expectd behaviour and not an error.

Note2! It is convenient to use keyboard shortcuts for many commands. We will below mention some of these, but many of the keyboard shortcuts can be found in the different menus after the relevant command. This teremial is usually kleft as is and new terminals are opened for any required bash commands.

Task 1.4. Naming and saving the notebook¶

  1. Save the notebook as an .ipynb file with an appropriate name, e.g., FantabulousCounter.ipynb by
    • in the File menu, choose Rename
  2. It is good practice to save your notebook while working on it ("checkpoint saved"). This can be done by either:_
    • Click the ’diskette’ icon in the toolbar,
    • In the File menu, choose Save and checkpoint
    • Type Cmd-s (Mac) ctrl-s (PC)

Task 1.5. Work with notebooks and cells -- a. markdown cells¶

A Jupyter notebook consists of a sequence of cells. Code or markdown text is written in the cells, which then can be executed. There are two main type of cell:

  • Code cells, typically with standard Python code
  • Markdown cells, with text in Markdown format. Moreover, cells can have two different modes:
  • Edit mode, when you can edit code to text in the cell; this is indicated by XXX -- press enter to enter edit mode from command mode, or click inside the cell
  • Command mode, when you can edit the notebook as a whole (e.g., add or delete cells, etc.); this is indicated by XXX -- press esc to enter edit mode from edit mode, or click outside a cell.
  1. Click in the empty cell of your notebook to make it the present cell . Try to change between Edit and Command modes
  2. At startup this cell will be Code cell; the cell type for the present will show in the 6th icon of the toolbar. Change the cell to a Markdown cell by, either:
    • click the 6th item in the toolbar and select Markdown from the scroll-down menu,
    • In the cell menu, choose item Cell type and then Markdown, or
    • Type M (this only works in Command mode) (You can try switching back-and-forth between modes, but make sure it is a Markdown cell in Editor mode before continuing.)
  3. Add some text in the cell, include some markdown formatting if you like (see Markdown cheatsheet, e.g.:

    # The phenomenal counting machine
    
    This amazing counting machine will count from 1 to 9 
    and print the consecutive sum of these numbers... and 
    that is not all; it will then do it backwards and blindfolded.
  4. Execute the cell by, either

    • click the Run icon in the toolbar,
    • In the Cell menu choose Run cells, or
    • Type Cmd-enter (Mac) ctrl-enter (PC) This converts the cell to a Compiled cell:
      Markdown cell Compiled

    To get back to the Edit mode, double-click in the cell:
    Markdown cell Edit mode

    Try going back and forth between Compiled and Edit mode

Task 1.5. Work with notebooks and cells -- b. Code cells¶

  1. Add a new cell below the present one by either:
    • click the + icon in the toolbar,
    • In the Insert menu, choose Insert cell above, or
    • Type B (this only works in Command mode)
  2. Ensure that the new cell is a Code cell and in Edit mode, and typesome python code in the cell, e.g.:

    print("Look -- no hands!")
    sum = 0
    for i in range(1,10):
      sum += i
      print(i,sum)
    
    print("And now, without seeing!")
    sum = 0
    for i in range(10,1,-1):
      sum += i
      print("X",sum)

    You should see something like:
    Code cell Edit mode

  3. Execute the cell:
    Code cell Compiled
  4. Add another cell below and then remove the new cell (ensure that the cell to be deleted is the selected one) by either
    • In the Edit menu, choose Delete cells
    • Type dd (this only works in Command mode)

Task 1.5. Close a notebook¶

  1. Close the browser tab with your new (and saved) notebook.
  2. Go to the Notebook dashboard window in your browser ad click on the Running tab. It should look something like this: nb dashboard Running screenshot There is a list of running Terminals (which usually is empty) and a list of running Notebooks.
  3. Notice that the notebook that you just closed (e.g., FantabulousCounter.ipynb) is still listed in the running Notebooks list. Clearly, just closing the browser window does not actually terminate the notebook!
  4. You can reopen a browser window for a running notebook, by clicking on the notebok name in either:
    • the Running tab or
    • the File tab of the Notebook dashboard window. Try it!
  5. To terminate a notebook, go to the Running tab of the Notebook dashboard browser window and click Shutdown in the row of the notebook, you want to shut down (e.g., FantabulousCounter.ipynb):
    nb dashboard Shutdown screenshot
  6. Ensure that the is no longer running:
    nb dashboard no Running screenshot
  7. Go to the browser window for that notebook (e.g., FantabulousCounter.ipynb). You will see a message about Dead kernel... (this is simply because you killed the kernel in item 5.):
    Dead notebook screenshot Click Don't restart and then just close the window.

(Note! You can also close the browser window first and then perform item 5.)

1.7 Quit Jupyter Notebooks¶

  1. (Save and terminate all notebooks, see above)
  2. In the Notebook dashboard browser window, click Quit in the upper righ-hand corner:
    nb dashboard quit screen shot
  3. Alternatively, do ctrl-c in the terminal where you started the Jupyter Notebook session.
  4. Close the Notebook dashboard browser window.