.empty()
Anonymous contributor
Published Oct 5, 2024
Contribute to Docs
The .empty()
method creates a tensor with uninitialized data. This means that the tensor is allocated memory without setting its values, which may contain arbitrary data (such as NaNs or other undefined values). The shape of the tensor must be specified as an argument.
Syntax
torch.empty(size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format)
The parameters are as follows:
size
: Specifies the shape of the tensor. It can be an integer or a tuple of integers representing the dimensionsout
(Optional): The output Tensor, defaults toNone
.dtype
(Optional): Specifies the desired data type of the tensor.layout
(Optional): Specifies the layout (torch.layout
) of the output tensor, defaults totorch.strided
.device
(Optional): Specifies the device (torch.device
) of the output tensor, defaults toNone
.requires_grad
(Optional): A boolean indicating whether autograd will record operations on the output tensor, defaults toFalse
.pin_memory
(Optional): A boolean indicating whether the tensor is allocated in pinned memory. This only works for CPU tensors. Defaults toFalse
.memory_format
(Optional): Specifies the memory format (torch.memory_format
) of the output tensor, defaults totorch.contiguous_format
.
Example
The example below uses the .empty()
method:
import torcht0 = torch.empty((1, 3))print(t0)
The returned tensor is as follows:
tensor([1.245e+11, NaN, -2.956e-52])
Codebyte Example
Run the following code to see how the .empty()
method works:
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours