Sling Academy
Home/PyTorch/What are PyTorch tensors?

What are PyTorch tensors?

Last updated: April 14, 2023

A tensor in PyTorch is a multi-dimensional matrix containing elements of a single data type. Tensors are similar to NumPy arrays but can also be operated on a CUDA-capable NVIDIA GPU.

A tensor can have any number of dimensions and any shape as long as all the numbers have the same type. For example, you can have a tensor of integers or a tensor of floats (decimal numbers) but not a mix of both.

Below are some examples of PyTorch tensors showcasing various dimensions and data types.

Scalar (0-dimensional tensor):

import torch

scalar_tensor = torch.tensor(1.11223344)
print(scalar_tensor)
# tensor(1.1122)

Vector (1-dimensional tensor):

import torch

vector_tensor = torch.tensor([1.0, 2.0, 3.0, 4.0])
print(vector_tensor)
# tensor([1., 2., 3., 4.])

Matrix (2-dimensional tensor):

import torch

matrix_tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
print(matrix_tensor)

Output:

tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])

4-dimensional tensor:

tensor_4d = torch.tensor([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[[9, 10], [11, 12]], [[13, 14], [15, 16]]]])
print(tensor_4d)

Output:

tensor([[[[ 1,  2],
          [ 3,  4]],

         [[ 5,  6],
          [ 7,  8]]],


        [[[ 9, 10],
          [11, 12]],

         [[13, 14],
          [15, 16]]]])

Tensor with specified data type:

import torch

int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64)
print(int_tensor.dtype)

Output:

torch.int64

Next Article: PyTorch: How to create a tensor from a Python list

Series: Working with Tensors in PyTorch

PyTorch

You May Also Like

  • Addressing "UserWarning: floor_divide is deprecated, and will be removed in a future version" in PyTorch Tensor Arithmetic
  • In-Depth: Convolutional Neural Networks (CNNs) for PyTorch Image Classification
  • Implementing Ensemble Classification Methods with PyTorch
  • Using Quantization-Aware Training in PyTorch to Achieve Efficient Deployment
  • Accelerating Cloud Deployments by Exporting PyTorch Models to ONNX
  • Automated Model Compression in PyTorch with Distiller Framework
  • Transforming PyTorch Models into Edge-Optimized Formats using TVM
  • Deploying PyTorch Models to AWS Lambda for Serverless Inference
  • Scaling Up Production Systems with PyTorch Distributed Model Serving
  • Applying Structured Pruning Techniques in PyTorch to Shrink Overparameterized Models
  • Integrating PyTorch with TensorRT for High-Performance Model Serving
  • Leveraging Neural Architecture Search and PyTorch for Compact Model Design
  • Building End-to-End Model Deployment Pipelines with PyTorch and Docker
  • Implementing Mixed Precision Training in PyTorch to Reduce Memory Footprint
  • Converting PyTorch Models to TorchScript for Production Environments
  • Deploying PyTorch Models to iOS and Android for Real-Time Applications
  • Combining Pruning and Quantization in PyTorch for Extreme Model Compression
  • Using PyTorch’s Dynamic Quantization to Speed Up Transformer Inference
  • Applying Post-Training Quantization in PyTorch for Edge Device Efficiency