Initialize the notebook

Download the code and import dependencies

In this tutorial, you will write a Jupyter notebook to train an RL agent and level a simple game. In addition to the notebook that you write, you will need code from a Python package that has already been written for this tutorial. You need to download that code to your notebook instance so that you can use it.

  1. Download the code and decompress it by pasting the following into the first notebook cell. Use Shift+Enter to run the cell:

    %%bash
    mkdir -p ~/SageMaker/frozen-lake
    curl https://s3.amazonaws.com/sagemaker-us-east-1-537534971119/frozen-lake.tar.gz > frozen-lake.tar.gz
    tar xvzf frozen-lake.tar.gz -C ~/SageMaker/frozen-lake/
    

    The %%bash magic command runs the code in the cell as a bash script in a subprocess.

    The /home/ec2-user/SageMaker folder is an attached volume for storage. Anything saved there will persist if you stop your instance and restart it later.

  2. The code you downloaded is a pip installable Python package that you will use throughout the tutorial. Install the frozen-lake package in editable mode by running the following in a new cell:

    !pip install -e ~/SageMaker/frozen-lake
    

    The exclamation point escapes a single-line bash script. This command uses the pip executable of this notebook’s kernel.

  3. Restart the kernel by clicking the restart icon. This makes the frozen-lake package you just installed available to be imported:

  4. Copy the code below into a new cell and run it. This has all the dependencies you will need for the tutorial.

    %matplotlib inline
    import matplotlib.pyplot as plt
    
    import numpy as np
    import pandas as pd
    import torch
    from gym.envs.toy_text.frozen_lake import FrozenLakeEnv
    
    from sagemaker import get_execution_role
    from sagemaker.pytorch import PyTorch
    from sagemaker.tuner import (
        HyperparameterTuner,
        ContinuousParameter,
        IntegerParameter,
    )
    
    from frozen_lake import (
        Level, LeveledFrozenLake,
        play_level, train, DeepQConfig,
        get_state, DeepQNetwork, moving_average,
        play_manually, get_test_level
    )
    

Notice the imports from frozen_lake at the bottom. These classes and functions are all defined in the frozen-lake package you downloaded and installed. If you change the code in one of them, you don’t need to re-install the package (since you installed it in editable mode) but you do need to restart the kernel for the notebook (see previous step). After you finish the tutorial, consider making changes to the source files and re-running different steps to see how the changes affect results.