Building Neural Networks With PyTorch in Google Colab
In this article, see how to build neural networks with PyTorch in Google Colab.
Join the DZone community and get the full member experience.Join For Free
Deep Learning With PyTorch in Google Colab
PyTorch and Google Colab have become synonymous with Deep Learning as they provide people with an easy and affordable way to quickly get started building their own neural networks and training models. GPUs aren’t cheap, which makes building your own custom workstation challenging for many. Although the cost of a deep learning workstation can be a barrier to many, these systems have become more affordable recently thanks to the lower cost of NVIDIA’s new RTX 30 series.
Even with more affordable options of having your own deep learning system on hand, many people still flock to using PyTorch and Google Colab as they get used to working with deep learning projects.
PyTorch and Google Colab Are Powerful for Developing Neural Networks
PyTorch was developed by Facebook and has become famous among the Deep Learning Research Community. It allows for parallel processing and has an easily readable syntax that caused an uptick in adoption. PyTorch is generally easier to learn and lighter to work with than TensorFlow, and is great for quick projects and building rapid prototypes. Many use PyTorch for computer vision and natural language processing (NLP) applications.
Google Colab was developed by Google to help the masses access powerful GPU resources to run deep learning experiments. It offers GPU and TPU support and integration with Google Drive for storage. These reasons make it a great choice for building simple neural networks, especially compared to something like Random Forest.
Using Google Colab
Google Colab offers a combination of environment setup options with a Jupyter-like interface, GPU support (including Free and Paid options), storage, and code documenting ability all in one application. Data Scientists can have an all-inclusive Deep Learning experience without having to spend a fortune on GPU support.
Documenting code is important for sharing code between people, and it’s important to have a single neutral place to store data science projects. The Jupyter notebook interface combined with GPU instances allow for a nice reproducible environment. You can also import notebooks from GitHub or upload your own.
An important note: since Python 2 has become outdated, it is no longer available on Colab. However, there is still legacy code running Python 2. Thankfully, Colab has a fix for this, which you can use to still run Python 2. If you give it a try, you’ll see there’s a warning that Python 2 is officially deprecated in Google Colab.
PyTorch is functionally like any other deep learning library, wherein it offers a suite of modules to build deep learning models. A difference will be the PyTorch Tensor Class which is similar to the Numpy ndarray.
A major plus for Tensors is that is has inherent GPU support. Tensors can run on either a CPU or GPU. To run on a GPUm we can just change the environment to use a GPU using the built-in CUDA module in PyTorch. This makes switching between GPU and CPU easy.
Data presented to a neural network has to be in a numerical format. Using PyTorch, we do this by representing data as a Tensor. A Tensor is a data structure that can store data in N dimensions; a Vector is a 1-dimensional Tensor, a matrix is a 2-dimensional Tensor. In layman’s terms, tensors can store in higher dimensions compared to a vector or a matrix.
Why Is a GPU Preferred?
Tensor processing libraries can be used to compute a multitude of calculations, but when using a 1-core GPU, it takes a lot of time for the calculations to compile.
This is where Google Colab comes in. It is technically free, but probably not suited towards large scale industrial deep learning. It is geared more towards beginner to mid-level practitioners. It does offer a paid service for larger-scale projects, such as being connected for up to 24 hours instead of 12 hours in the free version and can provide direct access to more powerful resources if needed.
How to Code a Basic Neural Network
In order to get started building a basic neural network, we need to install PyTorch in the Google Colab environment. This can be done by running the following pip command and by using the rest of the code below:
PyTorch and Google Colab Are Great Choices in Data Science
PyTorch and Google Colab are useful, powerful, and simple choices and have been widely adopted among the data science community despite PyTorch only being released in 2017 (3 years ago!) and Google Colab in 2018 (2 years ago!).
They have been shown to be great choices in Deep Learning, and as new developments are being released, they may become the best tools to use. Both are backed by two of the biggest names in Tech: Facebook and Google. PyTorch offers a comprehensive set of tools and modules to simplify the deep learning process as much as possible, while Google Colab offers an environment to manage your coding and reproducibility for your projects.
If you’re already using either of these, what have you found to be most valuable to your own work?
Published at DZone with permission of Kevin Vu. See the original article here.
Opinions expressed by DZone contributors are their own.