PyTorch Layers
Anonymous contributor
Published Nov 15, 2024
Layers are modules that perform operations on input data to build neural networks.
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 torchimport torch.nn as nnmodel = nn.Sequential(# First layer: Linear transformation from 56 input features to 128 output featuresnn.Linear(56, 128), # This layer learns to transform input datann.ReLU(), # Activation function to introduce non-linearity# Second layer: Linear transformation from 128 input features to 26 output featuresnn.Linear(128, 26), # This layer continues to transform the datann.ReLU(), # Another activation function for non-linearity# Third layer: Linear transformation from 26 input features to 1 output featurenn.Linear(26, 1) # Final output layer that produces the model's prediction)
All contributors
- Anonymous contributor
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