PyTorch TanH

pytorch tanh

In my decade-plus journey as a Python developer, I’ve witnessed the evolution of deep learning frameworks, with PyTorch emerging as one of the most useful tools in the field. When building neural networks, activation functions play a crucial role in introducing non-linearity to our models. The hyperbolic tangent function, commonly known as TanH, is one … Read more >>

PyTorch Softmax: dim, log_softmax and CrossEntropyLoss

PyTorch softmax: the Command Prompt showing the wrong loss produced by applying softmax before CrossEntropyLoss

torch.softmax turns raw model scores into probabilities that add up to 1: Two things go wrong with it constantly. Picking the wrong dim normalises across the batch instead of across the classes, and applying softmax before CrossEntropyLoss applies it twice. Both are demonstrated below, on Python 3.12.5, PyTorch 2.14.0+cpu. Using softmax in PyTorch Every value … Read more >>

How to Resize Images in PyTorch

pytorch resize

As a Python developer with over a decade of experience, I’ve worked extensively with image processing libraries. PyTorch’s image manipulation capabilities have become indispensable in my deep learning projects. A client recently asked me to create an image classification model to identify various American landmarks. The dataset included images of different sizes, ranging from small … Read more >>

torch.cat in PyTorch: Joining Tensors Along a Dimension

torch.cat in PyTorch: the Command Prompt showing two tensors joined along dim 0 and dim 1 with the resulting shapes

torch.cat joins a sequence of tensors along a dimension that already exists. The shapes have to match everywhere except that one dimension, and the result never gains an axis. That last part is the entire difference between torch.cat and torch.stack, which does add an axis. The runs below are on PyTorch 2.14.0 on Python 3.12.5, … Read more >>

How to Use PyTorch Stack?

torch stack

As a Python developer with over a decade of experience in deep learning frameworks, I’ve found PyTorch’s tensor manipulation functions to be incredibly useful yet sometimes overlooked. Among these functions, torch.stack() is one that deserves special attention. When I first started building neural networks, I often struggled with combining multiple tensors efficiently. That’s when I … Read more >>

How to Create PyTorch Empty Tensor?

pytorch empty tensor

Have you ever been in the middle of coding a neural network and needed to create a placeholder tensor to store results? I know I have, countless times. After working with PyTorch for over a decade, creating empty tensors has become second nature to me. When I first started, this seemingly simple task caused me … Read more >>

How to Use PyTorch Flatten for Neural Network Models

pytorch flatten

In my decade-plus journey as a Python developer, I’ve found that reshaping tensors is a crucial operation when building neural networks. One of the most common reshaping operations I perform is flattening multi-dimensional data into a 1D or 2D tensor. PyTorch’s Flatten layer is a simple yet useful tool that I use regularly in my … Read more >>

Understand PyTorch Conv3d

conv3d

Over my decade-plus journey as a Python developer, I’ve witnessed the evolution of deep learning frameworks firsthand. When it comes to processing 3D data, such as medical scans, video sequences, or volumetric imagery, PyTorch’s Conv3d has been my go-to tool. I recall my first project, analyzing brain MRI scans, where I struggled with traditional 2D … Read more >>

PyTorch View

torch view

When I first began working with PyTorch over a decade ago, one of the most frequent operations I performed was reshaping tensors. Whether I was preparing data for a convolutional neural network or reorganizing outputs for further processing, this method became my go-to tool. In this article, I will share everything I’ve learned about using … Read more >>

PyTorch nn.Conv1d: Shapes, Weights and Examples

PyTorch nn.Conv1d: the Command Prompt showing the input shape, output shape, weight shape and parameter count of a 1D convolution

nn.Conv1d is the 1D convolution layer in PyTorch, for signals, time series and text. It expects a 3D tensor of (batch, channels, length): Nearly every problem with it is a shape problem, and the two shapes to keep straight are that input and the weight, which is (out_channels, in_channels, kernel_size). Runs below are on PyTorch … Read more >>