Introduction

Hello and welcome! Today, we will delve deeper into the world of tensor processing in PyTorch by discussing and implementing the crucial concepts of Linear Layers and Activation Functions.

When working with tensors in neural networks, it is essential to understand that they are processed through various layers. A layer in a neural network refers to a collection of neurons (nodes) operating together at the same depth level within the network. PyTorch provides us with the torch.nn module, an easy and powerful tool for creating and organizing these layers.

Understanding the Linear Layer
Implementing a Linear Layer

In PyTorch, we commonly create these linear layers using the nn.Linear() function. Let's create such a layer, which will have 2 input features and 3 output features.

import torch
import torch.nn as nn

# Define an input tensor with specific values
input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32)

# Create a linear layer with 2 input features and 3 output features
layer = nn.Linear(in_features=2, out_features=3)

# Process the input through the linear layer to get initial output
output_tensor = layer(input_tensor)

# Display the original input tensor
print(f"Input Tensor:\n{input_tensor}\n")

# Display the output before activation to see the linear transformation effect
print(f"Output Tensor Before Activation:\n{output_tensor}\n")

The output of the above code will depend on the initial weights and biases, but an example might look like:

Input Tensor:
tensor([[1., 2.]])

Output Tensor Before Activation:
tensor([[ 0.2392, -1.2186, -0.9254]], grad_fn=<AddmmBackward0>)

The output tensor displayed above results from passing the input tensor through the linear layer. This layer applies a weighted sum of the input tensor values and adds a bias term to each output. The weights and biases are initialized randomly, so the exact output can vary. The grad_fn=<AddmmBackward0> in the output means that PyTorch is keeping track of this operation, which will help compute the gradients automatically during model training.

Understanding Activation Functions
Implementing the ReLU Activation Function

We can define a ReLU activation function in PyTorch using the nn.ReLU() function from the torch.nn module. Then, it can be applied to the output tensor from our linear layer.

# Define a ReLU activation function to introduce non-linearity
relu = nn.ReLU()

# Apply the ReLU function to the output of the linear layer
activated_output_relu = relu(output_tensor)

# Display the output after activation to observe the effect of ReLU
print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}")

The output tensor after activation will look as follows:

Output Tensor After ReLU Activation:
tensor([[0.2392, 0.0000, 0.0000]], grad_fn=<ReluBackward0>)

The output tensor after activation demonstrates the effect of the ReLU function. It zeroes out any negative values, converting them to zero, while keeping positive values unchanged. This introduces non-linearity into the model, which is crucial for handling more complex patterns in the data. The grad_fn=<ReluBackward0> shows that the ReLU operation is also being tracked for automatic differentiation during training.

Implementing the Sigmoid Activation Function

Similarly, we can define and apply a Sigmoid activation function in PyTorch using the nn.Sigmoid() function from the torch.nn module.

# Define a Sigmoid activation function
sigmoid = nn.Sigmoid()

# Apply the Sigmoid function to the output of the linear layer
activated_output_sigmoid = sigmoid(output_tensor)

# Display the output after applying the Sigmoid function
print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}")

The output tensor after applying the Sigmoid activation will look as follows:

Output Tensor After Sigmoid Activation:
tensor([[0.5595, 0.2282, 0.2839]], grad_fn=<SigmoidBackward0>)

The output tensor after activation shows the effect of the Sigmoid function. It squashes the input values to lie between 0 and 1. This can be particularly useful in scenarios where you want to interpret the output as probabilities. The grad_fn=<SigmoidBackward0> indicates that the Sigmoid operation is tracked for automatic differentiation during training.

Lesson Summary

Great job! Today, we explored the concept of tensor processing through Linear Layers and Activation Functions in PyTorch. Through a practical code exercise, we learned how to use these two functions in combination to transform and process an input tensor.

In the next set of exercises, you will have the opportunity to apply these concepts yourself actively. This practice will be crucial to solidifying your understanding of these concepts and your ability to process tensors effectively as you move forward in building more complex neural network architectures in PyTorch. Keep it up!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal