Layers
Anonymous contributor
Published Nov 15, 2024
Contribute to Docs
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
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Skill path
Build a Machine Learning Model
Learn to build machine learning models with Python.Includes 10 CoursesWith CertificateBeginner Friendly23 hours - Course
PyTorch for Classification
Build AI classification models with PyTorch using binary and multi-label techniques.With CertificateBeginner Friendly3 hours