> For the complete documentation index, see [llms.txt](https://paperspace.gitbook.io/gradient/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://paperspace.gitbook.io/gradient/master/tutorials/tutorials-list/getting-started-with-gradient-notebooks.md).

# Getting Started with Notebooks

{% embed url="<https://youtu.be/i4pvLzvw2ME>" %}
Jupyter notebooks are quick and easy to launch. 1m48s.&#x20;
{% endembed %}

## **Objectives**

* Launch a Jupyter Notebook with a single click
* Access existing datasets available within Gradient
* Train a machine learning model
* Save the model for inferencing

{% hint style="success" %}
This example is available in our ML Showcase!  You can clone the notebook [here](https://ml-showcase.paperspace.com/projects/logistic-regression-with-scikit-learn).&#x20;
{% endhint %}

## **Introduction**

Gradient provides one-click access to Jupyter Notebooks. You can choose pre-configured environments to launch Notebook instances or create a container with custom environments.

In this walkthrough, we will launch a Jupyter Notebook to train a logistic regression model based on the MNIST dataset. Gradient comes with a set of datasets that are readily available at the `/datasets` location. Instead of downloading the MNIST dataset to local storage, we will access the existing dataset.

We will also learn how to save the model for inferencing by persisting the final joblib file to the `/storage` location.

You can [download ](https://drive.google.com/file/d/14vO3W3l7Ap2nLcerchqm8PTfuSrQXW0e/view?usp=sharing)the completed Jupyter Notebook and upload it to the VM.&#x20;

## Launching a Notebook Instance

Select the Gradient product and then click on the **Notebooks** tab to create a new notebook.

After naming your notebook, the next step is to choose a pre-configured environment or container.

Within the container picker, select **Filter > All** and then locate the container called **Jupyter Notebook Data Science Stack.** This container will come with the core modules needed for our model.

![Select Filter > All and then select the container named Jupyter Notebook Data Science Stack](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-MRNtneRWHxDiD6GgLEN%2F-MRO0CL4KyVKUAgwSkLc%2FScreen%20Shot%202021-01-18%20at%209.01.07%20PM.png?alt=media\&token=334a1bdc-dd53-43f5-a153-7797a9f3f6b0)

In the next step, choose the machine type. Since we don’t need high-end machines with GPUs, we can choose a low-cost instance. Select the **Free GPU** machine to access free GPU instances.

![Enable low-cost instances and select the Free GPU cluster](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-MRNtneRWHxDiD6GgLEN%2F-MRO0ii33Mxb6xRNel9B%2FScreen%20Shot%202021-01-18%20at%209.03.26%20PM.png?alt=media\&token=41a0e24c-cb86-4664-a9d1-d69e412d9de5)

The notebook will now provision. The notebook status will be **Provisioning** or **Pending** for a minute or two.

![The notebook needs a couple minutes to provision](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-MROMQuM2JyBhkgGB5LP%2F-MRONg8LxuQEOdlzdF7F%2FScreen%20Shot%202021-01-18%20at%2010.43.44%20PM.png?alt=media\&token=bf126fe5-b35c-44b4-be92-517ab2fd702a)

![Newly created notebooks take a minute or two to initalize](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-MRNtneRWHxDiD6GgLEN%2F-MRO11_Z5o8dUT9YyAja%2FScreen%20Shot%202021-01-18%20at%209.04.51%20PM.png?alt=media\&token=a52ea3fc-ae2c-438d-a34b-0f42aa6730d0)

We are now ready to launch the Notebook. Choose Python 3 option under the  Notebooks section.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpclc8YFir4TuWvtz%2Fstep-4-4.jpg?alt=media\&token=e4ca7528-539d-43e4-91b0-cd4629d7a03d)

Rename the Notebook to give it a meaningful name. We are now ready to train the model.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcloBy9H2ucLIL0V%2Fstep-4-5.jpg?alt=media\&token=d1f13ed6-5803-40ed-bffa-360f0881922c)

## Training the Model&#x20;

Start by importing the modules. We are using Scikit-learn and relevant modules for this model. Since the environment doesn’t have *joblib* module, we will install it before using it. This is a one-time task that needs to run at the beginning of the training job.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpclrmgoh-GPWSDwt%2Fstep-5-5.jpg?alt=media\&token=64085f08-8a44-403f-a3d1-3b980f796b6d)

Next, we will create a couple of helper functions that load the dataset and changes the shape as expected by Scikit-learn.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpclt4d1Hmg_NpgAd%2Fstep-5-6.jpg?alt=media\&token=c570f131-9b9d-4525-97f0-115d7579a14e)

We will now load the MNIST dataset from `/datasets` location. You can browse the files within the Jupyter environment.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpclhm5LXEFzVqFxQ%2Fstep-4-4a.jpg?alt=media\&token=b67ab30b-e74b-47a1-aae5-399828ecf6a8)

The *loadMNIST* helper function loads the dataset and converts into a NumPy array.

![](https://lh4.googleusercontent.com/Y3pndStlAy97SeauOMDIsflYY8u5bFIVmRY9s89ZUtrwrxfkHa6Ww7Cct1zHHuVTTMR98gNR5LuYbgGHRP0rfgCoq0m8RZvhr5dgoKj-JhV79TzOUN5YkHuOwAbnL0wCrhHVpFlL)

Let us verify if the dataset is loaded correctly by randomly visualizing a few data points.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpclyD-8SVeEzWGQg%2Fstep-5-8.jpg?alt=media\&token=fa603511-445c-4bb4-bd39-af38ef346771)

Before we pass the training and test data to Scikit-learn Logistic Regression object, we need to reshape it.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcm-i2YEc1hcuHYh%2Fstep-5-9.jpg?alt=media\&token=e40fa199-5dd5-4742-9046-4384f5436e31)

We are now ready to fit the data into a logistic regression model.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcm8QwDwY85Zy8QZ%2Fstep-5-10.jpg?alt=media\&token=5a44b9ce-7c3b-46d5-868f-6c7238d6f454)

Let’s call the predict method to see how accurate our model is. We will use the output of this to generate a confusion matrix.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcm9V0owkQHKWCby%2Fstep-5-11.jpg?alt=media\&token=0f6e158e-f5f4-4892-b579-1c27ed3152ea)

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcmCto3JmJrDwxcQ%2Fstep-5-12.jpg?alt=media\&token=12510dad-55ca-4fb2-b96b-219c91107f23)

This prints a confusion matrix shown below:

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcmEjAqP0WVhzztO%2Fstep-5-13.jpg?alt=media\&token=e73750b0-b48c-49c9-8ec0-0790588d0bee)

Finally, we will persist the trained model at `/storage/mnist` for accessing it later. The model that is saved to `model.pkl` is available to other Notebooks and Jobs launched within your account.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcmHhVpPRYLOl0Aa%2Fstep-5-14.jpg?alt=media\&token=57c5fb51-5594-450c-94e4-518b3dfd1cb6)

You can use the Jupyter environment to navigate to the `/storage/mnist` directory to find the saved model.

![](https://1320806315-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHZRFUkajubOAmgu6Rd%2F-LgLpXyaONXCTpaQEVXF%2F-LgLpcmI9fOxTJkO1ECH%2Fstep-5-15.jpg?alt=media\&token=8085cb3b-f12c-46b2-a935-a4865f14427e)
