Debug School

rakesh kumar
rakesh kumar

Posted on

What Is a Training Pipeline in Machine Learning using pytorch

Defination
COMPLETE pytorch TRAINING PIPELINE (FULL CODE BLOCK)
Architecture diagram
COMPLETE KERAS TRAINING PIPELINE (FULL CODE BLOCK)

Training pipeline

Defination 1
A training pipeline is the repeated cycle of prediction, error measurement, gradient computation, and parameter updates used to train a machine learning model.

Defination 2
A training pipeline is the repeated cycle of prediction, error measurement, gradient computation, and parameter updates used to train a machine learning model.

Defination 3
A training pipeline is a structured sequence of steps through which a machine learning model learns from data. It defines how the model:

Processes input data
Generates predictions
Calculates error (loss)
Computes gradients
Updates its parameters
Repeats the process until the model learns

COMPLETE pytorch TRAINING PIPELINE (FULL CODE BLOCK)

Defining the model

class MySimpleNN():

  def __init__(self, X):

    self.weights = torch.rand(X.shape[1], 1, dtype=torch.float64, requires_grad=True)
    self.bias = torch.zeros(1, dtype=torch.float64, requires_grad=True)

  def forward(self, X):
    z = torch.matmul(X, self.weights) + self.bias
    y_pred = torch.sigmoid(z)
    return y_pred

  def loss_function(self, y_pred, y):
    # Clamp predictions to avoid log(0)
    epsilon = 1e-7
    y_pred = torch.clamp(y_pred, epsilon, 1 - epsilon)

    # Calculate loss
    loss = -(y_train_tensor * torch.log(y_pred) + (1 - y_train_tensor) * torch.log(1 - y_pred)).mean()
    return loss
Enter fullscreen mode Exit fullscreen mode

Training Pipeline

learning_rate = 0.1
epochs = 25

# create model
model = MySimpleNN(X_train_tensor)

# define loop
for epoch in range(epochs):

  # forward pass
  y_pred = model.forward(X_train_tensor)

  # loss calculate
  loss = model.loss_function(y_pred, y_train_tensor)

  # backward pass
  loss.backward()

  # parameters update
  with torch.no_grad():
    model.weights -= learning_rate * model.weights.grad
    model.bias -= learning_rate * model.bias.grad

  # zero gradients
  model.weights.grad.zero_()
  model.bias.grad.zero_()

  # print loss in each epoch
  print(f'Epoch: {epoch + 1}, Loss: {loss.item()}')
Enter fullscreen mode Exit fullscreen mode


pytorch

COMPLETE KERAS TRAINING PIPELINE (FULL CODE BLOCK)

import tensorflow as tf
from tensorflow.keras import layers, Model

# custom model
class MySimpleNN(Model):
    def __init__(self, input_dim):
        super(MySimpleNN, self).__init__()
        self.dense = layers.Dense(1, activation='sigmoid')

    def call(self, inputs):
        return self.dense(inputs)


# create model
input_dim = X_train.shape[1]
model = MySimpleNN(input_dim)

# define loss and optimizer
loss_fn = tf.keras.losses.BinaryCrossentropy()
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)

epochs = 100

# training loop
for epoch in range(epochs):

    with tf.GradientTape() as tape:
        # forward pass
        y_pred = model(X_train, training=True)

        # loss calculation
        loss = loss_fn(y_train, y_pred)

    # backward pass (compute gradients)
    gradients = tape.gradient(loss, model.trainable_variables)

    # parameters update
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))

    # print loss
    print(f'Epoch: {epoch + 1}, Loss: {loss.numpy()}')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)