{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wmCbxoDrADcF" }, "source": [ "# Google Colab setup with Google Drive folder\n", "\n", "This notebook provides the code you need to set up Google Colab to run and import files from within a Google Drive folder.\n", "\n", "This will allow you to upload assignment code to your Google Drive and then run the code on Google Colab machines (with free GPUs if needed). \n", "\n", "You will need to create a folder in your Google Drive to hold your assignments and you will need to open the notebook on Colaboratory from within this folder before running the set up code." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "zWhrmhqVCyGH" }, "source": [ "# Mount Google Drive\n", "\n", "This will allow the Colab machine to access Google Drive folders by mounting the drive on the machine. You will be asked to copy and paste an authentication code." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "colab_type": "code", "executionInfo": { "elapsed": 366, "status": "ok", "timestamp": 1600213503031, "user": { "displayName": "Punit Jha", "photoUrl": "", "userId": "07885534541681120711" }, "user_tz": 300 }, "id": "Wv2oKmF9AJtI", "outputId": "c63b3bc9-d246-4a86-ed6d-fee97dd3023a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount(\"/content/gdrive\", force_remount=True).\n" ] } ], "source": [ "from google.colab import drive\n", "drive.mount('/content/gdrive')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4Qs04PPwDOFy" }, "source": [ "# Change directory to allow imports\n", "\n", "\n", "As noted above, you should create a Google Drive folder to hold all your assignment files. You will need to add this code to the top of any python notebook you run to be able to import python files from your drive assignment folder (you should change the file path below to be your own assignment folder)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 191 }, "colab_type": "code", "executionInfo": { "elapsed": 283, "status": "error", "timestamp": 1600213505398, "user": { "displayName": "Punit Jha", "photoUrl": "", "userId": "07885534541681120711" }, "user_tz": 300 }, "id": "UA2-UyfpEc9O", "outputId": "9e8713d0-9a9a-4ac9-fc85-51c2cf9cfe22" }, "outputs": [ { "ename": "FileNotFoundError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-6-262f1174020d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"gdrive/My Drive/DL_stuff/assignment1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'gdrive/My Drive/DL_stuff/assignment1'" ] } ], "source": [ "import os\n", "os.chdir(\"gdrive/My Drive/DL_stuff/assignment1\")" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DDU5aVgR9QBx" }, "source": [ "# Set up GPU and PyTorch\n", "\n", "If you would like to use a GPU, ensure that your notebook on Colaboratory is set up with this capability. After opening the notebook on Colaboratory, go to Edit>Notebook settings, select \"Python 3\" under \"Runtime type,\" select \"GPU\" under \"Hardware accelerator,\" and save. If you make these changes, you will have to run the above Google Drive mounting and directory change code again.\n", "\n", "Next, install PyTorch:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 109 }, "colab_type": "code", "executionInfo": { "elapsed": 4206, "status": "ok", "timestamp": 1600202066973, "user": { "displayName": "Punit Jha", "photoUrl": "", "userId": "07885534541681120711" }, "user_tz": 300 }, "id": "kjbQtzKT9Uc2", "outputId": "f44d75be-00e8-4d9b-ebda-579a69386957" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (1.6.0+cu101)\n", "Requirement already satisfied: torchvision in /usr/local/lib/python3.6/dist-packages (0.7.0+cu101)\n", "Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch) (0.16.0)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch) (1.18.5)\n", "Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision) (7.0.0)\n" ] } ], "source": [ "!pip3 install torch torchvision" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "u_BekZYY9Vzx" }, "source": [ "Make sure that pytorch is installed and works with GPU:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "colab_type": "code", "executionInfo": { "elapsed": 14007, "status": "ok", "timestamp": 1600202083232, "user": { "displayName": "Punit Jha", "photoUrl": "", "userId": "07885534541681120711" }, "user_tz": 300 }, "id": "8TXSJWQa9efx", "outputId": "b376d2e1-97de-4817-d377-9a67af7586c0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([1.], device='cuda:0')\n" ] } ], "source": [ "import torch\n", "a = torch.Tensor([1]).cuda()\n", "print(a)" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "co_lab_Setup.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }