Running ML Code the Free and Easy Way
You’ve probably watched a few machine learning tutorials on the web but when it’s time to start writing some code you’re faced with the dreaded environment setup challenge and perhaps hardware limitations. Fret not, Google Colab is here to save the day! This post covers the steps of how to set up a Python Notebook on Colab. Let’s dive right in!
Running ML Code on Colab
Colaboratory (or Colab) is a free tool which can be used to set up your Machine Learning projects in a Jupyter notebook environment. Google’s goal with Colaboratory is to provide users with a research tool for machine learning education and research. It runs entirely on the cloud and even offers you a free GPU (with some limits) for resource intensive models! Colab makes it easy to set up your project and use popular Machine Learning libraries such as Tensorflow, PyTorch, Keras , Scikit-learn etc.
Example: the Korbit Healthcare Regression Model
Follow along as we go through the process of how to set up a Python Notebook on Colab. As an example, we’ll use the code in the Linear Regression post to predict the time until cancer recurs.
Step 1: Select Your Source
Go to Colab’s website and you will be prompted with a dialog box (see image below).
Here, you can choose the source (Google Drive/GitHub/Local Storage) of your notebook or start a notebook from scratch. We’ll be using the Linear Regression model Github repo for our code. Click Github and enter the URL or search by the username. Once you see the right path, select it.
Alternatively, if you have access to a Colab notebook, you can also create copy by clicking the “Copy to Drive” button. This will automatically create a copy of the notebook on your Drive. You’ll be able to edit the code of the existing notebook.
You should see the code and be able to edit it now. Step 1: complete!
Step 2: Upload Your Data
Now that the notebook is available on Colab we just need to add and link to the dataset. For this example, we created a “Data” folder under the Colab Notebooks section on the Google Drive and saved the wpbc.data file in there. You can always customize this folder structure but make sure to add these changes to the dataset_path string.
Let’s import “drive” and mount it. Again, double check and make sure the dataset_path fits with your folder structure.
from google.colab import drive drive.mount('/content/drive') dataset_path = "drive/My Drive/Colab Notebooks/data/wpbc.data"
Step 3: Permissions
Run the code by selecting Runtime -> Run all from the menu bar or just use the shortcut Ctrl + F9.
Once you run this code, a permissions URL will appear under the dataset_path variable (“Go to this URL in the browser”). Click the link and allow permissions.
Once the permissions are clear, you’ll see the credentials you need to input back in your Colab notebook. Copy the string.
Paste this string in the authorization input and click enter.
The code should run smoothly and render all the graphs accordingly.
Step 4: Run The Code
We now have the notebook running! We can explore the dataset and make changes to the model to try out different things.
Step 5: Analyze and Edit The Code
You can also checkout and copy the notebook on Colab here. Congratulations on running your code in Colab! Now you’re ready to start customizing your model and analyze the insights.
The code for this article was provided by Yash Mathur. Thanks for this great example Yash!
Note: Korbit Technologies is not affiliated, sponsored by or in no way connected nor attempts any connection to the “Google Colaboratory or Colab” and “Python” trademarks.
Learn Machine Learning Fundamentals
For a deeper comprehension about how linear regression works and clear examples of this algorithm in machine learning (with technical explanations like gradient descent and regularization), enroll in the “Learn AI with an AI Course” with Audrey Durand.
Machine Learning in Healthcare Series
In this series of articles we explore the use of machine learning in the healthcare industry. Important concepts and algorithms are covered with real applications including open datasets, open code (available on Github/Colab) used to run the analyses and the final results and insights.
Subscribe to the Korbit Newsletter
Stay up to date with news, blog posts, code examples and new courses. The newsletter goes out every month.