├── VAE_path.pt ├── VAE_score.pt ├── VAE_score_vr.pt ├── VAE_score_mc100.pt ├── VAE_score_vr_mc100.pt ├── .gitignore ├── figures ├── CB-figure-1-mean.png └── CB-figure-1-log-norm-const.png ├── environment.yml ├── environment_cpuonly.yml ├── secure-notebook-server.sh └── README.md /VAE_path.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/VAE_path.pt -------------------------------------------------------------------------------- /VAE_score.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/VAE_score.pt -------------------------------------------------------------------------------- /VAE_score_vr.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/VAE_score_vr.pt -------------------------------------------------------------------------------- /VAE_score_mc100.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/VAE_score_mc100.pt -------------------------------------------------------------------------------- /VAE_score_vr_mc100.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/VAE_score_vr_mc100.pt -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Jupyter 2 | **/.ipynb_checkpoints 3 | 4 | # Data 5 | **/data 6 | 7 | # Python 8 | **/__pycache__ -------------------------------------------------------------------------------- /figures/CB-figure-1-mean.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/figures/CB-figure-1-mean.png -------------------------------------------------------------------------------- /figures/CB-figure-1-log-norm-const.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vsimkus/pmr2024-vae/HEAD/figures/CB-figure-1-log-norm-const.png -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | # Create environment with `conda env create -f environment.yml` 2 | # Activate environment with `conda activate pmr` 3 | # Update environment with `conda env update --file environment.yml` 4 | # Remove environment with `conda remove --name pmr --all` 5 | 6 | name: pmr 7 | channels: 8 | - pytorch 9 | - conda-forge 10 | - anaconda 11 | dependencies: 12 | - python=3.8 13 | - pytorch=1.11.0 14 | - torchvision=0.12.0 15 | - numpy=1.21.4 16 | - matplotlib 17 | - notebook 18 | - ipywidgets 19 | - tqdm -------------------------------------------------------------------------------- /environment_cpuonly.yml: -------------------------------------------------------------------------------- 1 | # Create environment with `conda env create -f environment_cpuonly.yml` 2 | # Activate environment with `conda activate pmr` 3 | # Update environment with `conda env update --file environment_cpuonly.yml` 4 | # Remove environment with `conda remove --name pmr --all` 5 | 6 | name: pmr 7 | channels: 8 | - pytorch 9 | - conda-forge 10 | - anaconda 11 | dependencies: 12 | - python=3.8 13 | - pytorch=1.11.0 14 | - cpuonly 15 | - torchvision=0.12.0 16 | - numpy=1.21.4 17 | - matplotlib 18 | - notebook 19 | - ipywidgets 20 | - tqdm -------------------------------------------------------------------------------- /secure-notebook-server.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Adapted from the MLP course 3 | # Configure Jupyter notebook server to use password authentication 4 | # Make sure Conda environment is active as will assume it is later 5 | [ -z "$CONDA_PREFIX" ] && echo "Need to have Conda environment activated." && exit 1 6 | if [ "$#" -gt 2 ]; then 7 | echo "Usage: bash secure-notebook-server.sh [jupyter-path] [open-ssl-config-path]" 8 | exit 1 9 | fi 10 | # If specified read Jupyter directory from passed argument 11 | JUPYTER_DIR=${1:-"$HOME/.jupyter"} 12 | # If specified read OpenSSL config file path from passed argument 13 | # This is needed due to bug in how Conda handles config path 14 | export OPENSSL_CONF=${2:-"$CONDA_PREFIX/ssl/openssl.cnf"} 15 | SEPARATOR="=================================================================\n" 16 | # Create default config file if one does not already exist 17 | if [ ! -f "$JUPYTER_DIR/jupyter_notebook_config.py" ]; then 18 | echo "No existing notebook configuration file found, creating new one ..." 19 | printf $SEPARATOR 20 | jupyter notebook --generate-config 21 | printf $SEPARATOR 22 | echo "... notebook configuration file created." 23 | fi 24 | # Get user to enter notebook server password 25 | echo "Getting notebook server password hash. Enter password when prompted ..." 26 | printf $SEPARATOR 27 | HASH=$(python -c "from notebook.auth import passwd; print(passwd());") 28 | printf $SEPARATOR 29 | echo "... got password hash." 30 | # Generate self-signed OpenSSL certificate and key file 31 | echo "Creating certificate file ..." 32 | printf $SEPARATOR 33 | CERT_INFO="/C=UK/ST=Scotland/L=Edinburgh/O=University of Edinburgh/OU=School of Informatics/CN=$USER/emailAddress=$USER@sms.ed.ac.uk" 34 | openssl req \ 35 | -x509 -nodes -days 365 \ 36 | -subj "/C=UK/ST=Scotland/L=Edinburgh/O=University of Edinburgh/OU=School of Informatics/CN=$USER/emailAddress=$USER@sms.ed.ac.uk" \ 37 | -newkey rsa:1024 -keyout "$JUPYTER_DIR/key.key" \ 38 | -out "$JUPYTER_DIR/cert.pem" 39 | printf $SEPARATOR 40 | echo "... certificate created." 41 | # Setting permissions on key file 42 | chmod 600 "$JUPYTER_DIR/key.key" 43 | # Add password hash and certificate + key file paths to config file 44 | echo "Setting up configuration file..." 45 | printf $SEPARATOR 46 | echo " adding password hash" 47 | SRC_PSW="^#\?c\.NotebookApp\.password[ ]*=[ ]*u['"'"'"]\(sha1:[a-fA-F0-9]\+\)\?['"'"'"]" 48 | DST_PSW="c.NotebookApp.password = u'$HASH'" 49 | grep -q "c.NotebookApp.password" $JUPYTER_DIR/jupyter_notebook_config.py 50 | if [ ! $? -eq 0 ]; then 51 | echo DST_PSW >> $JUPYTER_DIR/jupyter_notebook_config.py 52 | else 53 | sed -i "s/$SRC_PSW/$DST_PSW/" $JUPYTER_DIR/jupyter_notebook_config.py 54 | fi 55 | echo " adding certificate file path" 56 | SRC_CRT="^#\?c\.NotebookApp\.certfile[ ]*=[ ]*u['"'"'"]\([^'"'"'"]+\)\?['"'"'"]" 57 | DST_CRT="c.NotebookApp.certfile = u'$JUPYTER_DIR/cert.pem'" 58 | grep -q "c.NotebookApp.certfile" $JUPYTER_DIR/jupyter_notebook_config.py 59 | if [ ! $? -eq 0 ]; then 60 | echo DST_CRT >> $JUPYTER_DIR/jupyter_notebook_config.py 61 | else 62 | sed -i "s|$SRC_CRT|$DST_CRT|" $JUPYTER_DIR/jupyter_notebook_config.py 63 | fi 64 | echo " adding key file path" 65 | SRC_KEY="^#\?c\.NotebookApp\.keyfile[ ]*=[ ]*u['"'"'"]\([^'"'"'"]+\)\?['"'"'"]" 66 | DST_KEY="c.NotebookApp.keyfile = u'$JUPYTER_DIR/key.key'" 67 | grep -q "c.NotebookApp.keyfile" $JUPYTER_DIR/jupyter_notebook_config.py 68 | if [ ! $? -eq 0 ]; then 69 | echo DST_KEY >> $JUPYTER_DIR/jupyter_notebook_config.py 70 | else 71 | sed -i "s|$SRC_KEY|$DST_KEY|" $JUPYTER_DIR/jupyter_notebook_config.py 72 | fi 73 | printf $SEPARATOR 74 | echo "... finished setting up configuration file." 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PMR tutorial on VAEs 2023-2024 2 | 3 | This is a repository for a computer tutorial of Probabilistic Modelling and Reasoning (2023/2024) - a University of Edinburgh master's course. 4 | 5 | There are two notebooks provided: 6 | 7 | * [Tutorial.ipynb](./Tutorial.ipynb) which contains some coding exercises (with the solutions provided in the text). 8 | * [Tutorial-completed.ipynb](Tutorial-completed.ipynb) where the code has been filled-in for you so you can immediately run it on your machine. 9 | 10 | To preview the Jupyter notebook we recommend using [nbviewer](https://nbviewer.org/github/vsimkus/pmr2024-vae/blob/main/Tutorial.ipynb) since GitHub does not properly render it (or to preview the version with the filled-in code see [this](https://nbviewer.org/github/vsimkus/pmr2024-vae/blob/main/Tutorial-completed.ipynb)). 11 | 12 | ## Installation 13 | 14 | Before you run the experiments you will first need to setup the environment on your preferred machine. If you have previously created the `pmr` conda environment for the [HMM tutorials](https://github.com/vsimkus/pmr2024-hmm) you may now only need to update the environment (see below). 15 | 16 | ### Environments 17 | 18 | We provide two environment files: [`environment.yml`](./environment.yml) and [`environment_cpuonly.yml`](./environment_cpuonly.yml). If you are *not* going to use CUDA (i.e. GPU) for running the experiments you may want to install/update from `environment_cpuonly.yml`, which will use significantly less storage (~2.5GB) than `environment.yml` (~6.5GB) which additionally installs the necessary dependencies for using the GPU. 19 | 20 | You may also want to run `conda clean -t` after you've installed/updated your environment to remove the downloaded raw packages that are no longer necessary. 21 | 22 | ### Upgrading an existing environment 23 | 24 | If you already have a `pmr` conda environment on your machine simply open the terminal and navigate to the project directory and type: 25 | 26 | * `conda env update --file environment_cpuonly.yml` if you are not going to use the GPU 27 | * or `conda env update --file environment.yml` if you plan to use the GPU. 28 | 29 | ### Installing on your machine 30 | 31 | If you haven't already done so, you'll need to open terminal on your machine and then follow the below instructions 32 | 33 | * Install git ([linux](https://git-scm.com/download/linux), [macOS](https://git-scm.com/download/mac), [windows](https://git-scm.com/download/win)) to access the repository if you don't have it already 34 | * Clone the git repository on your machine or DICE by using `git clone` tool in the terminal (you can find a guide [here](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository)) 35 | * Once you've cloned the repository, step into the directory by entering `cd pmr2024-vae` into the terminal 36 | * If you don’t already have it also install miniconda ([linux](https://conda.io/projects/conda/en/latest/user-guide/install/linux.html), [macOS](https://conda.io/projects/conda/en/latest/user-guide/install/macos.html), [windows](https://conda.io/projects/conda/en/latest/user-guide/install/windows.html)), which will allow you to manage all python dependencies per project 37 | * You can now create the `pmr` conda environment by typing `conda env create -f environment.yml` (or `conda env create -f environment_cpuonly.yml`). This step may take a while to complete and since it has to download large binaries you should better be connected to a good internet connection. 38 | 39 | ### Installing on DICE 40 | 41 | Should you wish to use the DICE machines to run the notebook follow these alternative installation instructions 42 | 43 | * Clone the git repository on your machine or DICE by using `git clone` tool in the terminal (you can find a guide [here](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository)). Git should already be installed on your DICE machine. 44 | * Once you've cloned the repository, step into the directory by entering `cd pmr2024-vae` into the terminal 45 | * You may have installed miniconda for other courses (e.g. MLP), so you can use that too. If you have to install miniconda on your DICE follow the detailed explanation [here](https://github.com/VICO-UoE/mlpractical/blob/mlp2023-24/lab1/notes/environment-set-up.md#2-installing-miniconda) 46 | * You can now create the `pmr` conda environment by typing `conda env create -f environment.yml` (or `conda env create -f environment_cpuonly.yml`). This step may take a while to complete and since it has to download large binaries you should better be connected to a good internet connection. 47 | 48 | Make sure that you run all jobs on `student.compute`, i.e. when you start up a terminal type `ssh student.compute`. 49 | Also note, that due to limited resources on the DICE server, at certain times it may be very slow. If you're finding yourself in this situation - come back later, or run your jobs overnight. Each of the main experiments in this tutorial should not take longer than half an hour to complete. 50 | 51 | ### Google Colab 52 | 53 | You can access and run the notebook directly via this link . More details can be found at . 54 | 55 | Note that Colab is intended for interactive use, and hence it may time-out your notebook if you don't interact with it for a while (typically it prompts to confirm if you want to keep the notebook alive every couple of hours), so you will not be able to leave it unattended overnight. Also note that the Colab notebook already includes all the required dependencies, however, the versions may differ, hence the results may differ slightly but that should not be a problem for this tutorial. 56 | 57 | ## Starting the Jupyter server 58 | 59 | Once you have the environment prepared you can start your jupyter notebook 60 | 61 | * Activate the conda environment with `conda activate pmr` 62 | * Now you will be able to start your jupyter server by typing `jupyter notebook`, which will start the server and open a browser to access the tutorial notebook. Click `Tutorial.ipynb` in the browser window. You can stop the server by pressing Ctrl+c (or Cmd+c) when you are done with it. 63 | Note that if you're running on DICE you should be [nice](https://en.wikipedia.org/wiki/Nice_(Unix)) to each other and instead run `nice -n 19 jupyter notebook` to lower the priority of your job. 64 | 65 | ### Extra step for those using DICE 66 | 67 | Before starting the jupyter server on a DICE server you must secure it with a passphrase, this is to prevent other people on the Informatics network from accessing your Jupyter server. Type the following in the terminal and follow the prompts (make sure you have activated your conda environment by typing `conda activate pmr` before running the following) 68 | 69 | ```bash 70 | bash secure-notebook-server.sh 71 | ``` 72 | 73 | ## Running the Jupyter notebook **remotely** on DICE 74 | 75 | This section details how to run the tutorial on DICE remotely without physically sitting in front of an Informatics desktop. You don't need to read this if you are using one of the machines in Appleton tower. 76 | 77 | The description in this sections is a shorter version of the detailed steps described in the [MLP course](https://github.com/VICO-UoE/mlpractical/blob/mlp2023-24/lab1/notes/remote-working-guide.md), so refer to the linked document if you need more details. 78 | 79 | ### (Prerequisite, if your are not on the University network) Connecting to the Informatics VPN 80 | 81 | Majority of you will be connected to the University network, hence those can skip this. 82 | If instead you want to run the experiments on the DICE machines remotely from *outside* the University network you can follow the below steps to setup. 83 | 84 | According to the Informatics [policy](https://blog.inf.ed.ac.uk/systems/2021/11/23/changes-to-the-ssh-service/) you will need to connect through the Informatics VPN to be able to remotely connect to the DICE machines. See for VPN setup instructions on any OS. When the guide asks you to download ovpn configuration file, choose `Informatics-EdLAN-AT.ovpn`. 85 | 86 | ### SSH connection to student.compute 87 | 88 | Once you are connected to Informatics network (or VPN), you can then in a different terminal window `ssh` to the DICE machines (replace `[your-student-id]` below with your student id, e.g. s1234567) 89 | 90 | ```bash 91 | ssh -t [your-student-id]@student.ssh.inf.ed.ac.uk ssh student.compute 92 | ``` 93 | 94 | ### Installation (remote DICE) 95 | 96 | If you haven't already, follow the installation instructions for DICE above. 97 | 98 | ### Securing your notebook 99 | 100 | Now before starting the jupyter server you must secure it with a passphrase, this is to prevent other people on the Informatics network from accessing your Jupyter server. Type the following in the terminal and follow the prompts (make sure you have activated your conda environment by typing `conda activate pmr` before running the following) 101 | 102 | ```bash 103 | bash secure-notebook-server.sh 104 | ``` 105 | 106 | ### Running the notebook 107 | 108 | Having setup the passphrase we can now start up the server: 109 | 110 | ```bash 111 | nice -n 19 jupyter notebook --no-browser 112 | ``` 113 | 114 | You can stop the server by pressing Ctrl+c (or Cmd+c), when you're done with it. 115 | 116 | You should use `nice` to lower the priority of your jobs when using shared resources, such as the `student.compute` server. 117 | 118 | Running the above command will give you an address with a port, e.g. `http://localhost:8795`, you will need to note the port address e.g. `8795` for the next step. 119 | 120 | ### Forwarding the notebook page to your machine 121 | 122 | Now that you have a running jupyter server, the final step is to forward the service to your local machine. For a detailed guide see [this](https://github.com/VICO-UoE/mlpractical/blob/mlp2023-24/lab1/notes/remote-working-guide.md#forwarding-a-connection-to-the-notebook-server-over-ssh) which covers all operating systems. On Linux and MacOS you should just be able to run the below command in a new terminal window 123 | 124 | ```bash 125 | ssh -N -o ProxyCommand="ssh -q [your-student-id]@student.ssh.inf.ed.ac.uk nc student.compute 22" -L [local-port]:localhost:[remote-port] [student-id]@student.compute 126 | ``` 127 | 128 | where `[local-port]` is a local port you want to assign it, e.g. `8889` and `[remote-port]` is the port you have noted before, i.e. `8795`. 129 | 130 | You can now navigate to `http://localhost/8889` in a web browser on your local machine to access the remote Jupyter notebook. It will ask for the passphrase you have setup earlier. 131 | 132 | Once you're done with the notebook, just press Ctrl+c (or Cmd+c) in all the terminal windows you have used until the processes are stopped and then close them. 133 | 134 | ## Attributions 135 | 136 | The tutorial in this repository was authored by [Vaidotas Šimkus](https://github.com/vsimkus) and [Michael Gutmann](https://michaelgutmann.github.io/). 137 | --------------------------------------------------------------------------------