Get Started
Here, in this example, we’ll create a simple linear regression model. The model accepts X (independent variable) and predicts the y(dependent variable). The model basically learns the relation between X and y.
Dataset for the model
Here we’ll create some synthetic data for our model, we’ll use numpy to generate random numbers.
Importing dependencies from NeuralPy
Let’s import the dependencies from NeuralPy.
Here we’ll use the Sequential model. Sequential is basically a linear stack of layers. Dense as a layer, Adam as an optimizer, and MSELoss for calculating the loss.
Making the model
Sequential provides an easy .add() method to add layers in the sequential model. We’ll use it to build the model.
Building the model
Once the model architecture is ready, we can build the model. This build method a PyTorch model internally.
Compiling the model
Once the model architecture is ready, we can compile with the optimizer and loss function. NeuralPy models provide an easy compile method for that. We just need to pass the optimizer and loss function in the compile method. It also accepts a metrics parameter, for this case, we don't need it.
Training the model
To train, we have the fit method. We need to pass the training and validation data, along with some other parameters to the fit method.
Predicting Results
The main purpose of the model is to predict results. In NeuralPy models, there are two methods for prediction, .predict()
and .predict_classes()
. Here for this linear regression problem, we'll use the .predict()
method.
Evaluating the models
After training, one important step is to evaluate the model on the test dataset. To do that, we have, in NeuralPy, we have a .evaluate()
method.