Rate this Page

Running Tutorials in Google Colab#

When you run a tutorial in Google Colab, there might be additionalrequirements and dependencies that you need to meet in orderfor the tutorial to work properly. This section contains notes on how toconfigure various settings in order to successfullyrun PyTorch tutorials in Google Colab.

PyTorch Version in Google Colab#

Wen you are running a tutorial that requires a version of PyTorch that hasjust been released, that version might not be yet available in Google Colab.To check that you have the requiredtorch and compatible domain librariesinstalled, run!piplist.

If the installed version of PyTorch is lower than required,uninstall it and reinstall again by running the following commands:

!pip3uninstall--yestorchtorchaudiotorchvisiontorchtexttorchdata!pip3installtorchtorchaudiotorchvisiontorchtexttorchdata

Using Tutorial Data from Google Drive in Colab#

We’ve added a new feature to tutorials that allows users to open thenotebook associated with a tutorial in Google Colab. You may need tocopy data to your Google drive account to get the more complex tutorialsto work.

In this example, we’ll demonstrate how to change the notebook in Colabto work with the Chatbot Tutorial. To do this, you’ll first need to belogged into Google Drive. (For a full description of how to access datain Colab, you can view their example notebookhere.)

To get started open theChatbotTutorialin your browser.

At the top of the page clickRun in Google Colab.

The file will open in Colab.

If you selectRuntime, and thenRun All, you’ll get an error as thefile can’t be found.

To fix this, we’ll copy the required file into our Google Drive account.

  1. Log into Google Drive.

  2. In Google Drive, make a folder nameddata, with a subfolder namedcornell.

  3. Visit the Cornell Movie Dialogs Corpus and download the movie-corpus ZIP file.

  4. Unzip the file on your local machine.

  5. Copy the fileutterances.jsonl to thedata/cornell folder that youcreated in Google Drive.

Now we’ll need to edit the file in_ _Colab to point to the file onGoogle Drive.

In Colab, add the following to top of the code section over the linethat beginscorpus\_name:

fromgoogle.colabimportdrivedrive.mount('/content/gdrive')

Change the two lines that follow:

  1. Change thecorpus\_name value to"cornell".

  2. Change the line that begins withcorpus to this:

corpus=os.path.join("/content/gdrive/My Drive/data",corpus_name)

We’re now pointing to the file we uploaded to Drive.

Now when you click theRun cell button for the code section,you’ll be prompted to authorize Google Drive and you’ll get anauthorization code. Paste the code into the prompt in Colab and youshould be set.

Rerun the notebook from theRuntime /Run All menu command andyou’ll see it process. (Note that this tutorial takes a long time torun.)

Hopefully this example will give you a good starting point for runningsome of the more complex tutorials in Colab. As we evolve our use ofColab on the PyTorch tutorials site, we’ll look at ways to make thiseasier for users.

Enabling CUDA#

Some tutorials require a CUDA-enabled device (NVIDIA GPU), which involveschanging the Runtime type prior to executing the tutorial.To change the Runtime in Google Colab, on the top drop-down menu selectRuntime,then selectChange runtime type. UnderHardware accelerator, selectT4GPU,then clickSave.