--- title: Run a Jupyter Notebook inside Tensorflow --- # How to run a Jupyter Notebook inside Tensorflow Container for Accelerated Machine Learning E2E GPU Wizard? ## Launching an Instance based on Tensorflow container with Jupyter-notebook Running ### Using myaccount portal 1. First sign into the myaccount portal. 2. Navigate to GPU Wizard: - Under the compute menu, click on **GPU**. - Then click on GPU Wizard. - For NGC Container Tensorflow, click on Next under actions column. - Choose the card according to requirement. A100 is recommended. ![Image](images/jupytern1.png) 3. Choose the plan as per the requirement: - Optionally you can [`add SSH key`](/docs/tir/GettingStarted/Security/SSH_Key/#manage-ssh-keys) (recommended) or subscribe to CDP backup. - Click on **“Create my node”**. - Wait for a few minutes and confirm that node is in running state. ![Image](images/jupyter2.png) 4. Open terminal on your local PC and type the following command: ```bash ssh -NL localhost:1234:localhost:8888 root@ ``` ![Image](images/jupyter3.png) - The command usually will not show any output, which indicates that the command has run without any error. - Go to a web browser on your local PC and hit the URL: [`http://localhost:1234`](http://localhost:1234). ![Image](images/jupyter4.png) - Congratulations! Now you can run your Python code inside this Jupyter notebook, which has TensorFlow and all the libraries frequently used in machine learning pre configured. - To get the most out of GPU acceleration, use [`RAPIDS`](https://rapids.ai/start.html) and [`DALI`](https://docs.nvidia.com/deeplearning/dali/user-guide/docs/), which are already installed inside this container. - RAPIDS and DALI accelerate tasks in machine learning, including data loading and preprocessing, beyond just the learning process. ## How to Run - MNIST dataset is a kind of **“Hello world”** implementation in machine learning. - Here we are about to use Fashion MNIST (clothing classification) as given in the NGC link [`here`](https://catalog.ngc.nvidia.com/orgs/nvidia/resources/fashion_mnist_tf_example/files). - Copy the `wget` command as given below. ![Image](images/jupyter5.png) - In your Jupyter Notebook UI, create a new notebook. ![Image](images/jupyter6.png) - Enter **‘!’** in the cell and paste the `wget` command. ![Image](images/jupyter7.png) - Once you run the command, you can find the downloaded file on the Jupyter home page. ![Image](images/jupyter8.png) - Now open the notebook [`FashionMNIST+Notebook.ipynb`](http://localhost:1234/notebooks/FashionMNIST%2BNotebook.ipynb). - You can skip up to step 4 (which was already done) and directly go to Step 5. - You can run cell by cell from Step 5 and observe each output, or you can go to the menu bar and select **“Cell -> Run All”** to run all of them. ![Image](images/jupyter9.png) ![Image](images/jupyter10.png) - You can see that after 15 epochs, the accuracy will be around 92% on training data and 88% on testing data. ## How to troubleshoot if the notebook isn’t accessible 1. SSH into your node with the public IP: `ssh username@`. 2. Check the output of the `docker ps` command. ![Image](images/jupyter11.png) 3. If it shows a container is running using the TensorFlow image, you can use the following command in the terminal: ```bash cnid=$(docker ps |grep tensorflow|head -1|awk '{print $1}');/usr/bin/nohup /usr/bin/docker exec $cnid /usr/local/bin/jupyter-notebook --no-browser --port=8888 --allow-root --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.quit_button=False --NotebookApp.notebook_dir=/home/tensorflow & ``` ![Image](images/jupyter3.png) - The command usually will not show any output, which indicates that the command has run without any error. - Go to a web browser on your local PC and hit the URL: [`http://localhost:1234`](http://localhost:1234). ![Image](images/jupyter4.png) - Congratulations! Now you can run your Python code inside this Jupyter notebook, which has TensorFlow and all the libraries frequently used in machine learning pre configured. - To get the most out of GPU acceleration, use [`RAPIDS`](https://rapids.ai/start.html) and [`DALI`](https://docs.nvidia.com/deeplearning/dali/user-guide/docs/), which are already installed inside this container. - RAPIDS and DALI accelerate tasks in machine learning, including data loading and preprocessing, beyond just the learning process. ## How to Run - MNIST dataset is a kind of **“Hello world”** implementation in machine learning. - Here we are about to use Fashion MNIST (clothing classification) as given in the NGC link [`here`](https://catalog.ngc.nvidia.com/orgs/nvidia/resources/fashion_mnist_tf_example/files). - Copy the `wget` command as given below. ![Image](images/jupyter5.png) - In your Jupyter Notebook UI, create a new notebook. ![Image](images/jupyter6.png) - Enter **‘!’** in the cell and paste the `wget` command. ![Image](images/jupyter7.png) - Once you run the command, you can find the downloaded file on the Jupyter home page. ![Image](images/jupyter8.png) - Now open the notebook [`FashionMNIST+Notebook.ipynb`](http://localhost:1234/notebooks/FashionMNIST%2BNotebook.ipynb). - You can skip up to step 4 (which was already done) and directly go to Step 5. - You can run cell by cell from Step 5 and observe each output, or you can go to the menu bar and select **“Cell -> Run All”** to run all of them. ![Image](images/jupyter9.png) ![Image](images/jupyter10.png) - You can see that after 15 epochs, the accuracy will be around 92% on training data and 88% on testing data. ## How to troubleshoot if the notebook isn’t accessible 1. SSH into your node with the public IP: `ssh username@`. 2. Check the output of the `docker ps` command. ![Image](images/jupyter11.png) 3. If it shows a container is running using the TensorFlow image, you can use the following command in the terminal: ```bash cnid=$(docker ps |grep tensorflow|head -1|awk '{print $1}');/usr/bin/nohup /usr/bin/docker exec $cnid /usr/local/bin/jupyter-notebook --no-browser --port=8888 --allow-root --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.quit_button=False --NotebookApp.notebook_dir=/home/tensorflow & ``` ![Image](images/jupyter13.png) - Now create an SSH tunnel again on your local PC with the following command: **`ssh -NL localhost:1234:localhost:8888 root@`** and attempt to access the Jupyter notebook. ![Image](images/jupyter3.png) ## Why doesn't my notebook have a password? The notebook is accessed via an SSH tunnel, which creates a secure encrypted connection between the node and the PC, making it accessible only for those who have SSH access. However, if you want to set a password, you can do so by following these steps: - Stop any existing Jupyter notebook processes inside the container. - Edit the field **–NotebookApp.password=’Your password’** in the above commands. - To set it on the next boot, open the file **/etc/rc.local** in your server and set the password as shown below. ![Image](images/jupyter18.png) ---