PyTorch Tensor Operations Cheat Sheet

PyTorch Tensor Operations Cheat Sheet

PyTorch Tensor Operations Cheat Sheet

Creating Tensors

  1. Create a tensor:

    tensor = torch.tensor([1, 2, 3])
  2. Create a zero tensor:

    zeros = torch.zeros(3, 3)
  3. Create an identity matrix:

    identity = torch.eye(3)
  4. Create a tensor with random values:

    rand_tensor = torch.rand(3, 3)
  5. Create a tensor with specific range:

    range_tensor = torch.arange(0, 10, 2)

Indexing and Slicing

  1. Index a tensor:

    element = tensor[0]
  2. Slice a tensor:

    slice = tensor[1:3]
  3. Access rows/columns:

    row = tensor[0, :]
    col = tensor[:, 0]

Shape Manipulation

  1. Get tensor shape:

    shape = tensor.shape
  2. Reshape tensor:

    reshaped = tensor.view(2, 2)
  3. Flatten tensor:

    flattened = tensor.flatten()

Arithmetic Operations

  1. Addition:

    result = tensor1 + tensor2
  2. Subtraction:

    result = tensor1 - tensor2
  3. Multiplication:

    result = tensor1 * tensor2
  4. Division:

    result = tensor1 / tensor2

Element-wise Operations

  1. Element-wise square root:

    sqrt = torch.sqrt(tensor)
  2. Element-wise exponentiation:

    exp = torch.exp(tensor)
  3. Element-wise logarithm:

    log = torch.log(tensor)
  4. Element-wise absolute value:

    abs = torch.abs(tensor)

Reduction Operations

  1. Sum of all elements:

    sum = tensor.sum()
  2. Mean of all elements:

    mean = tensor.mean()
  3. Maximum element:

    max_val = tensor.max()
  4. Minimum element:

    min_val = tensor.min()

Matrix Operations

  1. Matrix multiplication:

    matmul = torch.matmul(matrix1, matrix2)
  2. Element-wise matrix multiplication:

    element_wise = matrix1 * matrix2
  3. Matrix transposition:

    transposed = matrix.T
  4. Matrix inverse:

    inverse = torch.inverse(matrix)

Broadcasting

  1. Expand tensor dimensions:

    expanded = tensor.unsqueeze(0)
  2. Broadcast tensors for element-wise operations:

    result = tensor1 + tensor2

GPU Acceleration

  1. Move tensor to GPU:

    tensor.to('cuda')
  2. Perform operations on GPU:

    result = tensor1 + tensor2

Conversion

  1. Convert to NumPy array:

    numpy_array = tensor.numpy()
  2. Convert to Python scalar:

    scalar = tensor.item()

Concatenation and Stacking

  1. Concatenate tensors along a dimension:

    concatenated = torch.cat([tensor1, tensor2], dim=0)
  2. Stack tensors along a new dimension:

    stacked = torch.stack([tensor1, tensor2], dim=0)

Element-wise Comparison

  1. Element-wise equality:

    equal = torch.eq(tensor1, tensor2)
  2. Element-wise greater than:

    gt = torch.gt(tensor1, tensor2)
  3. Element-wise less than or equal:

    le = torch.le(tensor1, tensor2)

Element-wise Logic Operations

  1. Element-wise logical AND:

    logical_and = tensor1 & tensor2
  2. Element-wise logical OR:

    logical_or = tensor1 | tensor2
  3. Element-wise logical NOT:

    logical_not = ~tensor

Random Number Generation

  1. Generate random tensor from uniform distribution:

    uniform = torch.rand(3, 3)
  2. Generate random tensor from normal distribution:

    normal = torch.randn(3, 3)

Element-wise Round Operations

  1. Round elements to nearest integer:

    rounded = torch.round(tensor)
  2. Floor elements to nearest integer:

    floored = torch.floor(tensor)
  3. Ceiling elements to nearest integer:

    ceiled = torch.ceil(tensor)

This comprehensive cheat sheet covers a wide range of tensor operations in PyTorch. Experiment with these operations to enhance your understanding and proficiency in working with tensors. For more advanced operations and details, refer to the official PyTorch documentation.

Feel free to use this cheat sheet as a reference guide for tensor operations in PyTorch!