PyTorch Layers

Anonymous contributor's avatar
Anonymous contributor
Published Nov 15, 2024
Contribute to Docs

Layers are modules that perform operations on input data to build neural networks.

  • Learn to build machine learning models with Python.
    • Includes 10 Courses
    • With Certificate
    • Beginner Friendly.
      23 hours
  • Build AI classification models with PyTorch using binary and multi-label techniques.
    • With Certificate
    • Beginner Friendly.
      3 hours

Syntax

By using PyTorch’s .Sequential() method to build a neural network, we can specify layers and activation functions in sequence from input to output as shown below:

import torch
import torch.nn as nn

# Define the model for the neural network
model = nn.Sequential(
    # Define layers and activation functions here
    nn.Linear(in_features, out_features),
    nn.ReLU(),
    nn.Linear(in_features, out_features)
)

Example

Below is an example of a basic multi-layer neural network defined using PyTorch’s nn.Sequential() method, where the data is processed through a specified sequence of layers and activation functions:

import torch
import torch.nn as nn
model = nn.Sequential(
# First layer: Linear transformation from 56 input features to 128 output features
nn.Linear(56, 128), # This layer learns to transform input data
nn.ReLU(), # Activation function to introduce non-linearity
# Second layer: Linear transformation from 128 input features to 26 output features
nn.Linear(128, 26), # This layer continues to transform the data
nn.ReLU(), # Another activation function for non-linearity
# Third layer: Linear transformation from 26 input features to 1 output feature
nn.Linear(26, 1) # Final output layer that produces the model's prediction
)

All contributors

Contribute to Docs

Learn PyTorch on Codecademy

  • Learn to build machine learning models with Python.
    • Includes 10 Courses
    • With Certificate
    • Beginner Friendly.
      23 hours
  • Build AI classification models with PyTorch using binary and multi-label techniques.
    • With Certificate
    • Beginner Friendly.
      3 hours