Model

neuralpy.models.Model(force_cpu=False, training_device=None, random_state=None)
info

NeuralPy Model Class is mostly stable and can be used for any project. In the future, any chance of breaking changes is very low.

The Model class on NeuralPy is a wrapper class that wraps a PyTorch model and provides some simple methods to train, predict, evaluate, etc. In NeuralPy every model is based on this class and they inherits the Model class.

The Model class can be used for training any PyTorch model.

Supported Arguments

  • force_cpu=False: (Boolean) If True, then uses CPU even if CUDA is available
  • training_device=None: (NeuralPy device class) Device that will be used for training predictions. If you use training_device then it ignores the force_cpu parameter
  • random_state: (Integer) Random state for the device

Supported Methods

.compile() method:

In the NeuralPy Model, the compile method is responsible for attaching a loss function and optimizer with the model and this method needs to be called before training. This method can also be used for setting metrics for the model so that NeuralPy Model can evaluate those during training.

Supported Arguments:
  • optimizer: (NeuralPy Optimizer class) Adds an optimizer to the model
  • loss_function: (NeuralPy Loss Function class) Adds a loss function to the model
  • metrics: (String[]) Metrics that will be evaluated by the model. Currently only supports accuracy.

Example Code

from neuralpy.models import Model
from neuralpy.optimizer import Adam
from neuralpy.loss_functions import MSELoss
...
# Rest of the imports
...
model = Sequential()
...
# Model Architecture
...
# Calling .compile to build the model
# and attach a optimizer and loss function with the model
model.compile(optimizer=Adam(), loss_function=MSELoss(),metrics=["accuracy"])

.fit() Method

The .fit() method is used for training the NeuralPy model.

Supported Arguments
  • train_data: (Tuple(NumPy Array, NumPy Array | Generator)) Pass the training data as a tuple like (X, y) where X is training data and y is the labels for the training the model. The function accepts both numpy array and generator functions that return numpy array.
  • validation_data=None:(Tuple(NumPy Array, NumPy Array) | Generator) Pass the validation data as a tuple like (X, y) where X is test data and y is the labels for the validating the model. This field is optional. The function accepts both numpy array and generator functions that return numpy array.
  • epochs=10: (Integer) Number of epochs
  • batch_size=32: (Integer) Batch size for training.
  • steps_per_epoch=None: (Integer) No of steps in each epoch. Used only for generator.
  • validation_steps=None: (Integer) No of validation steps in each epoch. Used on for generators.
  • callbacks=None: (Array) Array of callbacks
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Training the model
model.fit(train_data, test_data, epochs=10, batch_size=32)

.predict() Method

The .predict() method is used for predicting outputs from the model.

Supported Arguments
  • predict_data: (NumPy Array | Generator) Data to be predicted
  • predict_steps=None: No of steps in the generator
  • batch_size=None: (Integer) Batch size for predicting. If not provided, then the entire data is predicted once.
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Predicting using the trained model
y_pred = model.predict(X, batch_size=32)

.predict_class() Method

The .predict_class() method is used for predicting classes using the trained model. This method works only if accuracy is passed in the metrics parameter on the .compile() method.

Supported Arguments
  • predict_data: (NumPy Array | Generator) Data to be predicted
  • predict_steps=None: No of steps in the generator
  • batch_size=None: (Integer) Batch size for predicting. If not provided, then the entire data is predicted once.
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Predicting the labels using the trained model
y_pred = model.predict_class(X, batch_size=32)

.evaluate() Method

The .evaluate() method is used for evaluating models using the test dataset.

Supported Arguments
  • X: (NumPy Array | Generator) Data to be predicted
  • y: (NumPy Array | Generator) Original labels of X
  • batch_size=None: (Integer) Batch size for predicting. If not provided, then the entire data is predicted once.
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Evaluating the labels using the trained model
results = model.evaluate(X, batch_size=32)

.summary() Method

The .summary() method is for getting a summary of the model

Supported Arguments
  • None
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Detailed summary of the model
print(model.summary())

.get_model() Method

The .get_model() method is used for getting the PyTorch model from the NeuralPy model. After extracting the model, the model can be treated just like a regular PyTorch model.

Supported Arguments
  • None
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Extracting the PyTorch model
pytorch_model = model.get_model()

.set_model() Method

The .set_model() method is used for converting a PyTorch model to a NeuralPy model. After this conversion, the model can be trained using NeuralPy optimizer and loss_functions.

Supported Arguments
  • model: (PyTorch model) A valid class based on the Sequential PyTorch model.
Example Code
from neuralpy.models import Model
...
# Rest of the code
...
# Converting the PyTorch model to NeuralPy model
model.set_model(pytorch_model)