PyTorch Model

The PyTorch Model interface in the Data Science module provides a flexible and developer-friendly environment for building, training, and deploying deep learning models within the platform. It supports seamless integration with PyTorch’s dynamic computation graph, allowing data scientists to iterate quickly, debug intuitively, and experiment with complex neural network architectures. Users can import datasets directly from connected data sources, preprocess them using built-in DataPrep pipelines, and train models using GPU-accelerated compute environments. The interface also simplifies model tracking, parameter tuning, and version management, ensuring reproducible workflows from experimentation to production. Once trained, models can be exported, visualized, or deployed as APIs for downstream applications and AI Agents, enabling powerful end-to-end machine learning lifecycle management within the platform.

Installing Required Libraries

Installing the required libraries is the prerequisite for this model to execute successfully. Use the following code to install the Torch library.

!!pip install torch

The following output will be displayed after executing the cell.

Step-1: Import Libraries

We import PyTorch for building neural networks, sklearn for splitting datasets, pandas and numpy for data handling, and matplotlib for plotting.

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Step-2: Load Dataset

Load the Iris dataset from a CSV file. Assign column names and map species names to numeric labels for easier training.

names = ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',
       'petal width (cm)', 'species']
dataset = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv', names=names)
mappings = {
   "Iris-setosa": 0,
   "Iris-versicolor": 1,
   "Iris-virginica": 2
}
dataset["species"] = dataset["species"].apply(lambda x: mappings[x])

Step-3: Split Features and Target

Separate input features (X) and target labels (y). Split the dataset into training (80%) and testing (20%) sets. Convert arrays to PyTorch tensors for model training.

X = dataset.drop("species",axis=1).values
y = dataset["species"].values
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.20)
X_train = torch.FloatTensor(X_train)
X_test = torch.FloatTensor(X_test)
y_train = torch.LongTensor(y_train)
y_test = torch.LongTensor(y_test)

Step-4: Inspect Features

Display the feature columns for verification.

X = dataset.drop("species",axis=1)
X.head()

After executing the code, the data set preview will be displayed below.

Step-5: Define Neural Network

Create a fully connected neural network with two hidden layers. ReLU activation is applied to hidden layers. Output layer returns raw logits suitable for CrossEntropyLoss.

class Model(nn.Module):
    def __init__(self, input_features=4, hidden_layer1=25, hidden_layer2=30, output_features=3):
        super().__init__()
        self.fc1 = nn.Linear(input_features,hidden_layer1)                  
        self.fc2 = nn.Linear(hidden_layer1, hidden_layer2)                  
        self.out = nn.Linear(hidden_layer2, output_features)      
        
    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.out(x)
        return x

Step-6: Initialize Model, Loss, and Optimizer

Initialize the neural network model. Use CrossEntropyLoss for multi-classification. Adam optimizer is used for training.

model = Model()
model
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

Step-7: Train the Model

Train the model for 100 epochs. For each epoch: Perform a forward pass. Compute the loss. Backpropagate and update the model weights. Store the loss values to visualize progress later.

The model was trained for 100 epochs using an iterative optimization process. For each epoch, the following steps were executed:

  1. Forward Pass: Input data was passed through the model to generate predictions.

  2. Loss Computation: A loss function was applied to quantify the discrepancy between the model's predictions and the true target values.

  3. Backpropagation and Weight Update: The calculated loss was backpropagated through the model, and an optimization algorithm (e.g., Stochastic Gradient Descent, Adam) was used to compute and apply updates to the model's weights and biases.

  4. Loss Tracking: The loss value from each epoch was recorded to monitor the training progress and enable subsequent visualization of the learning curve.

epochs = 100
losses = []
for i in range(epochs):
    y_pred = model.forward(X_train)
    loss = criterion(y_pred, y_train)
    losses.append(loss)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

Step-8: Save the Trained Model

Use NotebookExecutor to save the model for future predictions.

from Notebook.DSNotebook.NotebookExecutor import NotebookExecutor
nb = NotebookExecutor()
saved_model = nb.save_model(
    model=model, 
    modelName='Pytoch_950', 
    modelType='ml', 
    X=pd.DataFrame(X_train.numpy(), columns=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']),
    y=pd.Series(y_train.numpy()),
    estimator_type='classification', 
    column_headers=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
)

Step-9: Load the Saved Model

Load the previously saved model using its ID.

from Notebook.DSNotebook.NotebookExecutor import NotebookExecutor
nb = NotebookExecutor()
loaded_model = nb.load_saved_model('84831763532253719')

Step-10: Convert Test Set to DataFrame

The PyTorch tensor must be converted into a Pandas DataFrame format. This transformation is necessary to meet the required input specification for the NotebookExecutor.predict function, enabling the batch execution of inference tasks.

The conversion typically involves the following steps:

  1. Convert to NumPy: The PyTorch tensor is converted into a NumPy array (e.g., using .numpy()).

  2. Create DataFrame: The resulting NumPy array is then used to instantiate a Pandas DataFrame, ensuring that the column names and index align with the expected structure of the NotebookExecutor.predict function input.

# Convert tensor to numpy array
numpy_array = X_test.numpy()
column_headers = ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
# df = pd.DataFrame(numpy_array, columns=column_headers)
X_test_df = pd.DataFrame(numpy_array, columns=column_headers)

Step-11: Predict on Test Set

The modeltype parameter is explicitly set to 'ml' (Machine Learning) to ensure the prediction mechanism is configured for traditional machine learning models (as opposed to deep learning or other model types).

nb.predict(model = loaded_model, dataframe = X_test_df, modeltype='ml',column_headers=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']) 
 #Choose modeltype 'ml' for machine learning models and 'cv' for computer vision model 
 #ex: For machine learning model nb.predict(model = model, modeltype = 'ml', dataframe = df) 
 #ex: For computer vision keras model nb.predict(model = model, modeltype = 'cv', imgs = imgs, imgsize = (28, 28), dim = 1, class_names = class_names) 
 #and for pytorch model(model = model, modeltype = 'cv', imgs = imgs, class_names = class_names) 
 #Note: in case of any prediction error, the user squeezed the image data in keras.

The function will return a resulting set of predictions which represents the model's output for each instance in the test set.

Last updated