In this blog, we’ll learn about a new Deep Learning library, NeuralPy. It is similar to Keras in many ways but works on top of PyTorch. To understand NeuralPy, we’ll create a simple linear regression model that predicts the value of y for a given X.

First, let’s understand what NeuralPy is?

NeuralPy is a Keras like Deep Learning library that works on top of PyTorch. It is entirely written on Python, available on GitHub under an MIT license. It is a simple, easy to use Deep Learning library that is cross-compatible with PyTorch models and suitable for all kinds of machine learning experiments, learning, research, etc.

Here are some highlights of NeuralPy:

  • Provides an easy interface that is suitable for fast prototyping, learning, and research
  • Works on top of PyTorch
  • Can run on both CPU and GPU
  • Cross-Compatible with PyTorch models

To learn more about NeuralPy, please check the NeuralPy GitHub repository and official documentation.

Note: NeuralPy currently is at a very early stage of development, because of that, it supports limited types of layers, optimizers, loss functions, etc. Also, it is unstable and there could be bugs.

So now as we understand what NeuralPy is, let setup the development environment.

NeuralPy works on top of PyTorch, so before installing NeuralPy, we need to install PyTorch.

Installing PyTorch

To install PyTorch, go to their official website at the following link: https://pytorch.org/get-started/locally/ and follow their instructions.

If you have CUDA available on your system, then please install CUDA supported version of PyTorch.

NeuralPy also needs Numpy, but as it a dependency of PyTorch, there is no need to install it separately.

Installing NeuralPy

To install NeuralPy, type the following command on terminal/cmd.

pip install neuralpy-torch

If you have multiple versions of Python installed on your system, you might need to use pip3.

Once we’ve NeuralPy on our system, we’re ready to start working on the Linear Regression Model

For making any Machine Learning model, we first need some dataset. Here in this blog, for simplicity, we’ll not use any dataset, instead, we’ll create a synthetic dataset using numpy.

So first import numpy and then type the following code. Here we are using the np.random.rand method to create some random data points for our model. We’re creating three versions of the data, one for training, one for validation, and finally one for testing/evaluation.

# Importing dependencies
import numpy as np

# Random seed for numpy
np.random.seed(1969)

# Generating the data
# Training data
X_train = np.random.rand(100, 1) * 10
y_train = X_train + 5 * np.random.rand(100, 1)

# Validation data
X_validation = np.random.rand(100, 1) * 10
y_validation = X_validation + 5 * np.random.rand(100, 1)

# Test data
X_test = np.random.rand(100, 1) * 10
y_test = X_test + 5 * np.random.rand(100, 1)

Once we have the data ready, we can start making the model.

In this case, we’ll use a simple Sequential model. NeuralPy provides a Sequential class, that is similar to Keras Sequential class in many ways. Internally it uses PyTorch’s Sequential class to build the model.

In NeuralPy currently, at the time of writing this blog, there are only 2 ways to make a Model, on through the Sequential class and one using the Model subclass. We’ll discuss the Model subclass in some future blogs, for now, check the documentation for more information.

To create a Sequential model, write the below code.

from neuralpy.models import Sequential

# Making the model
model = Sequential()

Here first, we’re importing the Sequential model class from the model module, and then initializing the class.

Sequential supports 3 parameters, all of them are optional and have default values. Here in this blog, we’ll use the default values. For more information about those parameters, please check the documentation.

Once the model is ready, we can start adding layers to the model.

Currently, NeuralPy supports only one type of Layer, and that is the Dense Layer. A Dense Layer is a normal densely connected Neural Network layer. It performs a linear transformation of the input. Luckily, for this Linear Regression model, we just need this Dense layer :)

To use this Dense Layer, first we need to import it. The Sequential model class provides an easy .add() method to add layers. We’ll use the add method and pass the Dense layer with proper parameters.

#neural-networks #deep-learning #keras #pytorch #deep learning

 A Keras like deep learning library works on top of PyTorch
1.90 GEEK