Zum Inhalt

Tensor

Ein Tensor ist die grundlegende Datenstruktur in Deep Learning – eine mehrdimensionale Verallgemeinerung von Matrizen.


Dimensionen

Dimension Name Beispiel
0D Skalar 3.14
1D Vektor [1, 2, 3]
2D Matrix Bild (Graustufen)
3D 3D-Tensor Bild (RGB)
4D 4D-Tensor Batch von Bildern

In PyTorch

import torch

# Verschiedene Tensoren
skalar = torch.tensor(3.14)           # 0D
vektor = torch.tensor([1, 2, 3])      # 1D - shape: (3,)
matrix = torch.randn(3, 4)            # 2D - shape: (3, 4)
batch = torch.randn(32, 3, 224, 224)  # 4D - Batch von 32 RGB-Bildern

Wichtige Eigenschaften

  • Shape: Dimensionen des Tensors
  • dtype: Datentyp (float32, float16, int64, ...)
  • device: Wo liegt der Tensor (CPU, GPU)
t = torch.randn(2, 3, 4, device='cuda', dtype=torch.float16)
print(t.shape)   # torch.Size([2, 3, 4])
print(t.device)  # cuda:0
print(t.dtype)   # torch.float16

Tensor-Operationen

Die meisten Operationen in neuronalen Netzen sind Tensor-Operationen:

  • Matrix-Multiplikation (@ oder torch.matmul)
  • Element-weise Operationen (+, -, *, /)
  • Reduktionen (sum, mean, max)
  • Reshaping (view, reshape, transpose)

Siehe auch