First time initialization for Ananconda¶

First open Terminal to get a command line, then type the following:

eval "$(/opt/anaconda/bin/conda shell.bash hook)"

this should setup and activate the anaconda base environment.

Then to initialize:

conda init

This should print some information and say that the .bashrc file in your home directory has been changed.

At this point you need to exit and restart Terminal for the initialization to take effect.

Once you log back in it should automatically put you in the (base) mode for anaconda.

To turn off and on the default Ananconda base mode:

# Turns off automatically activating base
conda config --set auto_activate_base false

# Turns on automatically activating base
conda config --set auto_activate_base true

If you need to switch between Anaconda's python version (version 3.9) and the default Ubuntu python (version 3.10), use the above commands to turn on or off the Ananconda base mode. Still need to exit Terminal and restart to activate the change.

Personally, I don't want the Anaconda system to be permanently enabled, so I do this in Terminal:

$ conda config --set auto_activate_base false
$ conda activate
(base) $ # now have access to my anaconda tools
(base) $ conda deactivate
$ # no longer have access to anaconda tools

(you can do this remotely using ssh, though it's a bit annoying) (in particular, you have log out of ssh after conda init and then log back in again for the initialization to take effect.)

--Original instructions due to Raymond Val--

Installing Keras¶

Install Tensorflow¶

Keras is part of the Tensorflow distribution, so to get Keras we just have to install Tensorflow. Make sure your Anaconda base environment is activated, then execute the following:

(base) $ conda create -n tf tensorflow

This may take a little time (there were a few hiccups when I tried it, but it eventually worked). Normally, conda will tell you what packages it will download and install, and ask for confirmation.

Rather than just install the tensorflow framework, the create command created a new, special virtual environment called tf to isolate tensorflow from other packages and dependencies that might interfere with it. In any case, once installed we have to activate this new environment:

(base) $ conda activate tf

Environments are stacked which means we still have access to base. When you deactivate this environment, you'll be back at base, as if you took the tf environment off a stack of environments.

Check the Installation¶

Once in, check that it all worked by starting python and doing the following commands. Note that >>> is the Python prompt:

(tf) $ python
Python 3.9.13 (main, Aug 25 2022, 23:26:10) 
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> from tensorflow import keras
>>> # if you get no errors, it worked! However you might get some noise about CPU extensions used
>>> x = tf.constant([[5, 2], [1, 3]])
2023-01-10 22:30:45.977051: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
>>> print(x)
tf.Tensor(
[[5 2]
 [1 3]], shape=(2, 2), dtype=int32)
>>> x.numpy()
array([[5, 2],
       [1, 3]], dtype=int32)
>>> quit()
(tf) $ conda deactivate
(base) $

Reinstalling Jupyter¶

By default, virtual environments contain only the minimum set of tools and frameworks to do the job at hand. This means that the tf environment does not have Jupyter available anymore! Annoying, but we can this easily:

  1. Activate the tf environment again:

    (base) $ conda activate tf

  2. Start the Anaconda Navigator app:

    (tf) $ anaconda-navigator

  3. This may take a minute to initialize (and you may see some warning messages in the Terminal...ignore these). When the Navigator is up, it will show panels for a set of applications. Find the Jupyter Notebook app and click the Install button in the panel.

  4. When it's finished, you should be good to go! Quit the Navigator and try starting jupyter notebook as before.

NOTE: if at any point you run into issues, email me right away. Include a screenshot of the commands you were trying (and a history if available; just type history at the prompt in Terminal) and describe in as much detail as you can what went wrong.¶

Installing PyTorch¶

FOR NOW, THIS IS OPTIONAL!

PyTorch depends on several Python packages. To install them, run the following command from the base environment:

(base) $ conda install pytorch torchvision torchaudio cpuonly -c pytorch

When installation completes, test that you have PyTorch available by starting Python and entering the commands shown:

(base) $ python
Python 3.9.13 (main, Aug 25 2022, 23:26:10) 
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.3380, 0.3845, 0.3217],
        [0.8337, 0.9050, 0.2650],
        [0.2979, 0.7141, 0.9069],
        [0.1449, 0.1132, 0.1375],
        [0.4675, 0.3947, 0.1426]])
>>> quit()

As you might imagine, the rand method generates random data, so the output here might be a bit different than what you get.

Starting out with matplotlib¶

Working with deep learning models tends to generate a large amount of data, and to visualize it we'll need a great many graphs. The standard (and mostly painless) way to plot graphs in the Python ecosystem is to use matplotlib. Luckily this comes standard in the Anaconda distribution and does not need to be installed.

The basics of matplotlib can be learned quickly by running the examples in this tutorial. This is our main class exercise today.

One important point to keep in mind: the Anaconda distribution curates stable releases of the tools and frameworks it installs for us, //not// the most recent versions, in order to ensure that the tools are compatible and work together properly. If you just Google a package like matplotlib and start looking at docs or running through a tutorial on the first website you find, it might not work. We're using version 3.5.3 of matplotlib, so be sure the information you're looking at is for that specific version.

Note: I made the above document entirely in Jupyter using Markdown cells. Markdown syntax is pretty simple, simpler even than the Dokuwiki syntax (and just by the way, you can configure Dokuwiki to use standard Markdown). References:

  • A general markdown cheatsheet with explanations
  • Reference with Jupyter Markdown extensions
  • One page cheatsheet
In [ ]: