Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

API reference

PyTorch API reference

PyTorch deep learning

Identifiers
126
With full guides
107
Types
3

Topics covered

  • Tensors
  • Autograd
  • nn.Module
  • Optimizers
  • DataLoader
  • Training loop
  • GPU / CUDA
  • Saving models

Class

NameWhat it does
torch.Tensortorch.Tensor is PyTorch’s multidimensional container for values sharing one data type. Create tensors from existing data with torch.tensor() or use factory operations to control shape, dtype, device, and related properties.
nn.Moduletorch.nn.Module is the base class for PyTorch neural-network modules. Subclass it to define models or reusable components, compose child modules, and manage registered parameters and module-wide conversions.
nn.Lineartorch.nn.Linear is a trainable affine layer that maps each input vector from in_features to out_features. It preserves all leading dimensions and can optionally include a learnable bias.
nn.Conv2dtorch.nn.Conv2d applies a learnable 2D cross-correlation to channel-first image-like tensors. Stride, padding, dilation, and groups control spatial sampling and channel connectivity.
DataLoaderDataLoader wraps a dataset in an iterable that can customize sample order, batch samples, use single or multiple processes, and optionally pin fetched tensors in memory.
Datasettorch.utils.data.Dataset is the abstract base for map-style datasets. Subclasses provide keyed sample retrieval with __getitem__ and may provide their size through __len__.
nn.Conv1dnn.Conv1d applies learnable one-dimensional filters to multi-channel signals. It converts C_in input channels into C_out output channels while stride, padding, dilation, and groups control how the filters scan and connect.
nn.MaxPool2dClass
nn.AvgPool2dApplies 2D average pooling to spatial dimensions while preserving the number of channels. Its window, stride, padding, boundary rounding, padding inclusion, and divisor are configurable.
nn.BatchNorm2dtorch.nn.BatchNorm2d normalizes four-dimensional N×C×H×W inputs independently per channel. It normally tracks running statistics for evaluation and can apply learnable channel-wise scale and bias.
nn.LayerNormtorch.nn.LayerNorm normalizes the final dimensions of each input using its mean and variance. It can apply optional learnable per-element scale and bias while preserving the input shape.
nn.Dropouttorch.nn.Dropout randomly zeros input elements during training with probability p. It samples independently on each forward call and preserves the input shape.
nn.LSTMApplies a multi-layer long short-term memory recurrent network to an input sequence. It supports stacked, bidirectional, dropout, and projected configurations.
nn.GRUtorch.nn.GRU applies one or more gated recurrent-unit layers to an input sequence. It supports configurable hidden size, stacking, dropout between layers, batch-first layouts, and bidirectional processing.
nn.RNNnn.RNN is a multi-layer Elman recurrent module for sequence data. It supports tanh or ReLU activation, optional inter-layer dropout, and bidirectional processing.
nn.Embeddingnn.Embedding is a trainable lookup table for a fixed dictionary of vectors. Integer indices retrieve the corresponding rows, commonly converting token IDs into dense representations.
nn.MultiheadAttentiontorch.nn.MultiheadAttention implements Transformer multi-head attention, combining attention from multiple representation subspaces. It supports configurable head count, dropout, projection biases, key/value dimensions, layouts, devices, and dtypes.
nn.Transformernn.Transformer is a reference encoder-decoder implementation of the original Transformer architecture. It processes source and target feature sequences and returns decoder outputs.
nn.TransformerEncoderTransformerEncoder is a stack of TransformerEncoderLayer modules that processes a source sequence in order. It supports optional normalization, attention masks, causal masking, and nested-tensor conversion for highly padded inputs.
nn.Sequentialtorch.nn.Sequential is a registered container that applies modules in order, passing each output to the next. It is useful for compact feed-forward chains and can accept positional modules or an OrderedDict of named modules.
nn.ModuleListModuleList stores child modules in a list-like container while registering them with the parent module. It supports indexed access and iteration, but the enclosing module controls how the children are executed.
nn.Parametertorch.nn.Parameter is a Tensor subclass that marks tensor data as a module parameter. When assigned to a Module attribute, it is automatically registered and exposed through the module's parameter iterator.
nn.ReLUnn.ReLU applies the rectified linear unit element-wise, replacing negative values with zero and retaining nonnegative values. It supports inputs of any dimensionality and returns the same shape.
nn.GELUnn.GELU is a Gaussian Error Linear Units activation module. It supports the default formulation or a tanh-based approximation and preserves the input shape.
nn.Softmaxnn.Softmax normalizes tensor slices along a selected dimension into values from 0 to 1 whose sum is 1. It preserves the input shape.
nn.Sigmoidtorch.nn.Sigmoid applies the sigmoid function independently to every input element. It accepts tensors of any dimensionality and returns a tensor with the same shape.
nn.TanhClass
nn.Flattentorch.nn.Flatten is a module that combines a contiguous range of dimensions into one. By default, it preserves dimension 0 and flattens dimensions 1 through the last dimension.
nn.CrossEntropyLossCrossEntropyLoss computes classification cross entropy from unnormalized logits and either class-index or class-probability targets. It supports class weighting, ignored indices, configurable reduction, and label smoothing.
nn.MSELosstorch.nn.MSELoss measures squared differences between corresponding input and target elements. By default, it returns their mean; it can also return unreduced values or their sum.
nn.BCELossBCELoss measures binary cross-entropy between target values and input probabilities. It supports optional weighting and configurable none, mean, or sum reduction.
nn.BCEWithLogitsLossBCEWithLogitsLoss combines sigmoid and binary cross-entropy in one numerically stable module. It accepts logits and targets, supports optional weighting, and can return unreduced, averaged, or summed losses.
nn.NLLLossClass
nn.L1Losstorch.nn.L1Loss measures mean absolute error between corresponding input and target elements. Its reduction can be disabled, averaged, or summed; mean is the default.
nn.SmoothL1LossSmoothL1Loss computes a robust regression loss that is quadratic for small errors and L1 for larger errors. Its reduction defaults to the mean, and beta controls the transition threshold.
optim.Adamtorch.optim.Adam implements the Adam optimization algorithm for neural-network parameters. It supports configurable learning rate, moment coefficients, numerical stability, weight decay, AMSGrad, and multiple execution modes.
optim.SGDClass
optim.AdamWClass
optim.RMSpropRMSprop is a PyTorch optimizer that scales parameter updates using a moving average of squared gradients. It also supports momentum, weight decay, centered normalization, graph capture, maximization, and differentiable optimization steps.
optim.AdagradClass
lr_scheduler.StepLRClass
lr_scheduler.CosineAnnealingLRCosineAnnealingLR applies cosine annealing to every optimizer parameter group. It gradually moves learning rates toward eta_min over T_max scheduler iterations.
TensorDatasetClass

Function

NameWhat it does
torch.tensorCreates a new tensor by copying array-like data. It can configure dtype, device, autograd tracking, and pinned CPU memory.
torch.no_gradtorch.no_grad disables gradient calculation for a scope or decorated function. It is useful for inference because ordinary results stop requiring gradients and autograd can use less memory.
backwardFunction
forwardtorch.nn.Module.forward defines the computation performed whenever a module is called. Subclasses override it to transform inputs into outputs, while calling the module instance preserves registered hook behavior.
torch.saveSerializes a PyTorch object to a file path or file-like destination. It uses Python pickling and defaults to a newer ZIP-based file format.
F.reluFunction
torch.zerosCreates a tensor filled with zeros, using dimensions supplied as separate arguments or as a collection. Optional keywords select its dtype, layout, device, output buffer, and autograd behavior.
torch.onesCreates a tensor filled with 1, using the supplied dimensions as its shape. Optional keywords control its output tensor, dtype, layout, device, and autograd behavior.
torch.emptyAllocates a tensor with a requested shape and optional dtype, layout, device, gradient, memory, and format settings. Its elements are uninitialized, so write valid values before reading them.
torch.fullCreates a tensor with a specified shape and fills every element with one scalar value. Its dtype is inferred from the fill value unless dtype is provided explicitly.
torch.arangeCreates a 1-D tensor containing values from start up to, but excluding, end, separated by step. start defaults to 0 and step to 1; dtype is inferred unless specified.
torch.linspaceCreates a one-dimensional tensor with a chosen number of evenly spaced values from start to end, including both endpoints. Optional arguments configure its dtype, layout, device, storage, and autograd behavior.
torch.eyeCreates a two-dimensional tensor with ones on its diagonal and zeros elsewhere. It can produce square or rectangular shapes and accepts options for dtype, layout, device, output reuse, and gradient tracking.
torch.randtorch.rand creates a tensor of a requested shape with uniformly distributed random values in the half-open interval [0, 1). Optional arguments control the generator, output properties, device, gradient tracking, and memory allocation.
torch.randnCreates a tensor of a requested shape with values sampled from the standard normal distribution. Shape dimensions may be separate arguments or a list/tuple, while keyword arguments control dtype, device, autograd, storage, and randomness.
torch.randintCreates a tensor of uniformly distributed random integers in the half-open interval [low, high). The requested size determines its shape, and low defaults to zero.
torch.from_numpyCreates a PyTorch Tensor from a NumPy ndarray while sharing the same memory. Changes made through either object are visible through the other, and the resulting tensor is not resizable.
torch.as_tensorConverts array-like data to a tensor, sharing storage and preserving autograd history when possible. Matching tensors can be returned directly; dtype or device differences cause conversion.
torch.zeros_likeCreates a zero-filled tensor with the same size as an input tensor. By default, it also follows the input's dtype, layout, device, and preserved memory format.
torch.catConcatenates a sequence of tensors along a specified existing dimension. Inputs must match in every other dimension, with a documented exception for a 1-D empty tensor.
torch.stacktorch.stack combines equally sized tensors by inserting a new dimension. The dim argument chooses where that dimension goes and defaults to 0.
torch.reshapeFunction
torch.permuteReorders a tensor’s dimensions and returns a view of the original tensor. The dims argument specifies the desired axis order.
torch.transposeSwaps two selected dimensions of a tensor and returns the transposed result. For strided tensors, the result shares underlying storage with the input.
torch.squeezetorch.squeeze removes dimensions of size 1 from a tensor. Supplying dim limits removal to the selected dimensions.
torch.unsqueezeAdds a size-one dimension to a tensor at a chosen position. The returned tensor shares the input's underlying data.
torch.flattenFlattens a tensor by combining a selected inclusive range of dimensions into one. By default, all dimensions are flattened while element order is preserved.
torch.splitSplits a tensor into chunks along a selected dimension. Use an integer for repeated chunk sizes or a list for explicit sizes; returned chunks are views of the input.
torch.chunkSplits a tensor along a chosen dimension into a requested number of chunks. Outputs are views and may be fewer than requested when the division is not possible.
torch.gathertorch.gather selects values from input along a chosen dimension using an index tensor. The result has the same shape as index.
torch.wheretorch.where selects elements from input or other according to a condition, supporting broadcasting across the operands. With only a condition, it returns the indices of true elements.
torch.clampClamps every element of a tensor to a chosen lower and/or upper bound. Either bound can be omitted, and the bounds may be scalar values or tensors.
torch.addAdds other, scaled by alpha, to input elementwise. Tensor operands support broadcasting and the operation returns a tensor.
torch.multorch.mul performs elementwise multiplication between a tensor and another tensor or number. It supports broadcasting and returns a tensor containing the products.
torch.matmultorch.matmul computes tensor products whose behavior depends on operand dimensions, covering dot, matrix, vector, and batched matrix multiplication. Higher-rank batch dimensions broadcast when compatible.
torch.mmtorch.mm multiplies two 2-D tensors as matrices. Compatible shapes (n, m) and (m, p) produce a (n, p) tensor, without broadcasting.
torch.bmmPerforms matrix multiplication independently on each corresponding pair of matrices in two 3-D tensor batches. The batches must have equal sizes, and the result has shape (b, n, p) for inputs shaped (b, n, m) and (b, m, p).
torch.einsumComputes tensor contractions using Einstein-summation notation. It aligns dimensions by labels, multiplies corresponding elements, and sums labels omitted from the output.
torch.sumReturns the sum of all elements in a tensor, or reduces selected dimension(s). Use keepdim to retain reduced axes and dtype to control the accumulation and result type.
torch.meanComputes the mean of all tensor elements or reduces selected dimensions. It supports optional dtype conversion and dimension retention.
torch.stdCalculates standard deviation over selected tensor dimensions. By default it uses Bessel’s correction; choose correction=0 for population standard deviation and keepdim=True to retain reduced axes.
torch.maxReturns the maximum value of a tensor, either globally or along a chosen dimension. Dimension-wise reduction also returns the index of each maximum.
torch.minReturns the minimum value of a tensor, globally or along a selected dimension. Dimension-wise reduction also returns the index of each minimum.
torch.argmaxReturns the indices of maximum values in a tensor, either globally or across a selected dimension. If maxima are tied, it returns the first maximal index.
torch.softmaxtorch.softmax converts tensor values into normalized, nonnegative scores along a chosen dimension. Each slice along that dimension sums to 1, and an optional dtype controls the computation type.
torch.sigmoidFunction
torch.reluFunction
torch.expComputes the elementwise exponential of a tensor. It can optionally write the result to a supplied output tensor.
torch.logComputes the element-wise natural logarithm of a tensor. It returns a new tensor and optionally accepts an output tensor.
torch.sqrttorch.sqrt returns a new tensor containing the element-wise square roots of an input tensor. An optional out tensor can receive the result.
torch.absComputes the absolute value of every element in a tensor and returns the resulting tensor. An optional output tensor can receive the result.
torch.normtorch.norm computes a matrix or vector norm of a tensor, with configurable order, reduction dimensions, shape retention, output destination, and computation dtype. It is deprecated; new code should use the corresponding torch.linalg norm function.
F.softmaxApplies softmax along a selected tensor dimension, converting each slice into values from 0 to 1 that sum to 1. An optional dtype controls casting before computation.
F.cross_entropyComputes cross-entropy loss from unnormalized logits and class-index or probability targets. Supports class weighting, ignored indices, configurable reduction, and label smoothing.
F.mse_lossFunction
F.dropoutF.dropout randomly zeroes input elements during training with probability p. Use training=False to disable masking, such as during evaluation.
F.linearFunction
F.conv2dF.conv2d applies a 2D convolution to image-like input using explicit filters and optional bias. Parameters control kernel movement, padding, dilation, and channel grouping.
F.normalizeNormalizes vectors in a tensor along a chosen dimension using an Lp norm. It divides by the norm or eps, whichever is larger, with defaults of p=2 and dim=1.
F.padF.pad pads selected trailing dimensions of an N-dimensional tensor. It supports constant, reflection, replication, and circular modes, with zero-filled constant padding by default.
F.interpolateResizes 3-D, 4-D, or 5-D tensors by upsampling or downsampling their spatial dimensions. You choose the output size or scale factor and an interpolation mode such as nearest, bilinear, bicubic, trilinear, area, or nearest-exact.
torch.loadLoads an object saved by torch.save, restoring tensor storages to their saved devices or a location selected with map_location. Optional mmap enables lazy storage loading, while weights_only provides restricted deserialization.
torch.devicetorch.device identifies where a tensor is or will be allocated. It supports device strings, type-plus-index construction, accelerator-relative integer indices, and temporary default-device contexts.
autograd.gradComputes and returns gradients of selected outputs with respect to selected inputs, without accumulating them in the inputs' .grad fields. It supports vector-Jacobian products, graph retention, higher-order derivatives, and batched gradient vectors.

Module

NameWhat it does
torch.optimtorch.optim provides optimization algorithms for updating model parameters from computed gradients. Create an optimizer with parameters and algorithm-specific options, then use it in the training loop to apply updates.
autogradtorch.autograd provides PyTorch’s automatic differentiation machinery for scalar-valued functions. Mark tensors with requires_grad=True, execute the computation, and use backward or related APIs to obtain gradients.
cudatorch.cuda provides PyTorch support for CUDA tensor types and GPU computation. Import it safely, then use torch.cuda.is_available() to determine whether CUDA can be used.
tensor.backwardComputes gradients of a tensor through its autograd graph and accumulates them in leaf tensors. Non-scalar outputs require a matching gradient tensor.
tensor.itemConverts a one-element tensor into its value as a standard Python number. The operation is not differentiable and is intended for scalar extraction.
tensor.detachReturns a tensor disconnected from the current autograd graph. The result shares storage with the source, does not require gradients, and has no forward-mode AD gradients.
tensor.toConverts a tensor to a requested dtype, device, or both. It can also match another tensor's dtype and device, and may avoid copying when no conversion is needed.
tensor.cudaModule
tensor.cpuMoves a tensor to CPU memory, returning a copy when needed. If it is already on CPU, no copy occurs and the original object is returned.
tensor.numpyConverts a tensor to a NumPy ndarray. By default, conversion requires a CPU tensor with no gradients or unresolved conjugate bit and a NumPy-compatible dtype and layout; force=True permits preparation steps and possible copying.
tensor.requires_grad_Changes a tensor’s requires_grad setting in place. By default, it enables autograd recording for subsequent operations involving that tensor.
model.parametersModule.parameters() returns an iterator over a module's registered learnable parameters, including parameters in child modules by default. It is commonly passed to an optimizer for training.
model.state_dictReturns a dictionary-like mapping of a module’s learnable parameters and registered buffers. Use it to save, inspect, modify, or restore model state.
model.load_state_dictModule
model.evalModule
model.trainSets a module and its descendants to training mode. This affects layers with training-specific behavior, but it does not control gradient tracking.
model.zero_gradModule
optimizer.stepoptimizer.step() applies one optimizer update after gradients are computed. Most optimizers use it without arguments; closure-based optimizers can reevaluate the model and loss.
optimizer.zero_gradResets gradients for all tensors optimized by an optimizer. By default, it sets gradients to None instead of zero, usually reducing memory use and sometimes improving performance.

Search across every package and filter by type in your dashboard.

Start learning