WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Intermediate · question 25 of 100

How do you implement custom loss functions in PyTorch? Provide an example.?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

In PyTorch, custom loss functions can be implemented by defining a function that takes two arguments, ‘y_pred‘ and ‘y_true‘, where ‘y_pred‘ contains the predicted values and ‘y_true‘ contains the ground truth values. The loss function should then compute the error between ‘y_pred‘ and ‘y_true‘ and return the result.

Here is an example of a custom loss function implementation in PyTorch:

import torch

def custom_loss(y_pred, y_true):
    # calculate the mean square error between y_pred and y_true
    mse = torch.mean((y_pred - y_true) ** 2)
    # apply some custom scaling
    custom_factor = 0.5
    scaled_mse = custom_factor * mse
    return scaled_mse

In this example, the custom loss function takes two arguments: ‘y_pred‘ and ‘y_true‘. It calculates the mean square error (MSE) between ‘y_pred‘ and ‘y_true‘ and then applies some custom scaling using the ‘custom_factor‘ scalar. Finally, the scaled MSE is returned as the output of the function.

To use this custom loss function in a PyTorch model, you can simply call it in the training loop, as follows:

model = MyModel()

optimizer = torch.optim.SGD(params=model.parameters(), lr=0.01)

criterion = custom_loss # pass the custom loss function to the criterion argument

for inputs, labels in data_loader:
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

In this example, the ‘custom_loss‘ function is passed to the ‘criterion‘ argument of the training loop, which is used to evaluate the loss between the predicted outputs and the ground truth labels. The ‘backward()‘ method computes the gradients of the loss with respect to the model parameters, and the ‘step()‘ method updates the model parameters using the optimizer.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics