Title: Transforms — PyTorch main documentation
Description: Data transforms in PyTorch C++ — Stack, Normalize, Lambda, and Collate transforms for data pipelines.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Transforms",
"headline": "Transforms",
"description": "Data transforms in PyTorch C++ \u2014 Stack, Normalize, Lambda, and Collate transforms for data pipelines.",
"url": "/api/data/transforms.html",
"articleBody": "Transforms# Transforms apply preprocessing to data samples, such as normalization or augmentation. They can be chained using the .map() method on datasets. Transform (Base Class)# The base class for all transforms. Subclass this to create custom transforms. template\u003ctypename Input, typename Output\u003eclass Transform : public torch::data::transforms::BatchTransform\u003cstd::vector\u003cInput\u003e, std::vector\u003cOutput\u003e\u003e# A transformation of individual input examples to individual output examples. Just like a Dataset is a BatchDataset, a Transform is a BatchTransform that can operate on the level of individual examples rather than entire batches. The batch-level transform is implemented (by default) in terms of the example-level transform, though this can be customized. Public Types using InputType = Input# using OutputType = Output# Public Functions virtual OutputType apply(InputType input) = 0# Applies the transformation to the given input. inline virtual std::vector\u003cOutput\u003e apply_batch(std::vector\u003cInput\u003e input_batch) override# Applies the transformation over the entire input_batch. BatchTransform (Base Class)# Base class for transforms that operate on entire batches. template\u003ctypename InputBatch, typename OutputBatch\u003eclass BatchTransform# A transformation of a batch to a new batch. Subclassed by torch::data::transforms::Transform\u003c Example\u003c Tensor, Tensor \u003e, Example\u003c Tensor, Tensor \u003e \u003e, torch::data::transforms::Transform\u003c Input, Input \u003e, torch::data::transforms::Stack\u003c Example\u003c\u003e \u003e, torch::data::transforms::Stack\u003c TensorExample \u003e Public Types using InputBatchType = InputBatch# using OutputBatchType = OutputBatch# Public Functions virtual ~BatchTransform() = default# virtual OutputBatch apply_batch(InputBatch input_batch) = 0# Applies the transformation to the given input_batch. TensorTransform# Base class for transforms that operate on tensors specifically. template\u003ctypename Target = Tensor\u003eclass TensorTransform : public torch::data::transforms::Transform\u003cExample\u003cTensor, Tensor\u003e, Example\u003cTensor, Tensor\u003e\u003e# A Transform that is specialized for the typical Example\u003cTensor, Tensor\u003e combination. It exposes a single operator() interface hook (for subclasses), and calls this function on input Example objects. Public Types using E = Example\u003cTensor, Target\u003e# Public Functions virtual Tensor operator()(Tensor input) = 0# Transforms a single input tensor to an output tensor. inline virtual OutputType apply(InputType input) override# Implementation of Transform::apply that calls operator(). Normalize# Normalizes tensors with a given mean and standard deviation. template\u003ctypename Target = Tensor\u003estruct Normalize : public torch::data::transforms::TensorTransform\u003cTensor\u003e# Normalizes input tensors by subtracting the supplied mean and dividing by the given standard deviation. Public Functions inline Normalize(ArrayRef\u003cdouble\u003e mean, ArrayRef\u003cdouble\u003e stddev)# Constructs a Normalize transform. The mean and standard deviation can be anything that is broadcastable over the input tensors (like single scalars). inline virtual torch::Tensor operator()(Tensor input) override# Transforms a single input tensor to an output tensor. Public Members torch::Tensor mean# torch::Tensor stddev# Stack# Stacks a batch of tensors into a single tensor. template\u003ctypename T = Example\u003c\u003e\u003estruct Stack# Example: auto dataset = torch::data::datasets::MNIST(\"./data\") .map(torch::data::transforms::Normalize\u003c\u003e(0.5, 0.5)) .map(torch::data::transforms::Stack\u003c\u003e()); Lambda# template\u003ctypename Input, typename Output = Input\u003eclass Lambda : public torch::data::transforms::Transform\u003cInput, Input\u003e# Public Types using FunctionType = std::function\u003cOutput(Input)\u003e# Public Functions inline explicit Lambda(FunctionType function)# Constructs the Lambda from the given function object. inline virtual OutputType apply(InputType input) override# Applies the user-provided function object to the input. TensorLambda# template\u003ctypename Target = Tensor\u003eclass TensorLambda : public torch::data::transforms::TensorTransform\u003cTensor\u003e# A Lambda specialized for the typical Example\u003cTensor, Tensor\u003e input type. Public Types using FunctionType = std::function\u003cTensor(Tensor)\u003e# Public Functions inline explicit TensorLambda(FunctionType function)# Creates a TensorLambda from the given function. inline virtual Tensor operator()(Tensor input) override# Applies the user-provided functor to the input tensor. BatchLambda# template\u003ctypename Input, typename Output = Input\u003eclass BatchLambda : public torch::data::transforms::BatchTransform\u003cInput, Input\u003e# A BatchTransform that applies a user-provided functor to a batch. Public Types using FunctionType = std::function\u003cOutputBatchType(InputBatchType)\u003e# Public Functions inline explicit BatchLambda(FunctionType function)# Constructs the BatchLambda from the given function object. inline virtual OutputBatchType apply_batch(InputBatchType input_batch) override# Applies the user-provided function object to the input_batch. Chaining Transforms# Transforms can be chained together using .map(): auto dataset = torch::data::datasets::MNIST(\"./data\") .map(torch::data::transforms::Normalize\u003c\u003e(0.1307, 0.3081)) .map(torch::data::transforms::Stack\u003c\u003e());",
"author": {
"@type": "Organization",
"name": "PyTorch Contributors",
"url": "https://pytorch.org"
},
"image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "/api/data/transforms.html"
},
"datePublished": "2023-01-01T00:00:00Z",
"dateModified": "2023-01-01T00:00:00Z"
}
| docsearch:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | Data transforms in PyTorch C++ — Stack, Normalize, Lambda, and Collate transforms for data pipelines. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 3 |
Links:
Viewport: width=device-width, initial-scale=1