Best Books on PyTorch and Machine Learning to Buy in October 2025

Machine Learning with PyTorch and Scikit-Learn: Develop machine learning and deep learning models with Python



Deep Learning for Coders with Fastai and PyTorch: AI Applications Without a PhD



Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools



Mastering PyTorch: Create and deploy deep learning models from CNNs to multimodal models, LLMs, and beyond



PyTorch Pocket Reference: Building and Deploying Deep Learning Models



Generative AI with Python and PyTorch: Navigating the AI frontier with LLMs, Stable Diffusion, and next-gen AI applications



The Hundred-Page Language Models Book: hands-on with PyTorch (The Hundred-Page Books)



Build a Large Language Model (From Scratch)


In PyTorch, you can add a mask to a loss function by simply multiplying the loss tensor with the mask tensor before computing the final loss value.
For example, if you have a loss function defined as criterion = nn.CrossEntropyLoss()
, and you have a mask tensor called mask
, you can apply the mask to the loss function like this:
output = model(inputs) loss = criterion(output, labels) masked_loss = torch.mean(loss * mask)
In this example, labels
are the ground truth labels for the inputs, model
is your neural network model, and mask
is a binary tensor indicating which elements of the loss should be ignored. The torch.mean
function averages the masked loss values across all elements to compute the final loss value.
Adding a mask to a loss function can be useful in situations where certain parts of the loss should be ignored, such as handling imbalanced data or focusing on specific regions of an image.
How to handle outliers in the loss function with a mask in PyTorch?
One way to handle outliers in the loss function with a mask in PyTorch is to apply a mask to the loss calculation. The mask can be used to selectively ignore the outliers in the input data during the loss computation.
Here is an example code snippet to handle outliers in the loss function with a mask in PyTorch:
import torch
def custom_loss_function(output, target, threshold): # Calculate the absolute difference between the output and target loss = torch.abs(output - target)
# Create a mask to identify the outliers based on the threshold value
mask = loss < threshold
# Apply the mask to the loss calculation
masked\_loss = loss\[mask\]
# Compute the mean of the masked loss
loss = torch.mean(masked\_loss)
return loss
Example usage
output = torch.tensor([1.0, 2.0, 10.0, 4.0]) target = torch.tensor([0.5, 3.0, 5.0, 4.5]) threshold = 5.0
loss = custom_loss_function(output, target, threshold) print(loss)
In this example, the custom_loss_function()
calculates the absolute difference between the output and target tensors. It then creates a mask based on a threshold value to identify outliers and applies the mask to the loss calculation. The mean of the masked loss is computed as the final loss value.
You can adjust the threshold value based on the specific characteristics of your data and the nature of the outliers you want to handle in the loss function.
What is the syntax for applying a mask to the loss function in PyTorch?
In PyTorch, you can apply a mask to the loss function by multiplying the loss tensor with the mask tensor before taking the mean. Here is an example of the syntax for applying a mask to the loss function in PyTorch:
import torch
Define loss function
criterion = torch.nn.CrossEntropyLoss()
Define target and output tensors
output = torch.randn(5, 10) # example output tensor target = torch.empty(5, dtype=torch.long).random_(10) # example target tensor
Define mask tensor
mask = torch.tensor([1, 1, 0, 1, 0]) # example mask tensor
Apply mask to loss
loss = criterion(output, target) masked_loss = torch.mean(loss * mask.float())
Print the masked loss
print(masked_loss.item())
In this example, we first define the loss function (in this case, CrossEntropyLoss) and create the target and output tensors. We then define a mask tensor with binary values indicating which elements to include in the loss calculation. Finally, we apply the mask to the loss by multiplying the loss tensor with the mask tensor and taking the mean of the masked loss.
How to incorporate a mask into the loss calculation in PyTorch?
To incorporate a mask into the loss calculation in PyTorch, you can multiply the loss by the mask. Here's an example code snippet to demonstrate how to do this:
import torch import torch.nn as nn
Define your loss function
criterion = nn.CrossEntropyLoss()
Create your input tensor and target labels
input_tensor = torch.randn(3, 5) # Batch size of 3, 5 classes target_labels = torch.tensor([0, 2, 4])
Create a mask tensor
mask = torch.tensor([1, 0, 1]) # Mask out the second sample
Calculate the loss
loss = criterion(input_tensor, target_labels)
Multiply the loss by the mask
masked_loss = loss * mask
print(masked_loss)
In this code snippet, we first define a CrossEntropyLoss as our loss function. We then generate some random input data and target labels. We also create a mask tensor where we specify which samples we want to mask out (in this case, the second sample).
We calculate the loss using the CrossEntropyLoss function, and then multiply the loss by the mask tensor to incorporate it into the loss calculation.
This way, the loss calculation will only take into account the unmasked samples, allowing you to selectively mask out certain samples in the loss calculation.