Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Mathematische Operationen mit Tensors | Einführung in PyTorch
Pytorch Grundlagen

book
Mathematische Operationen mit Tensors

Elementweise Operationen

Elementweise Operationen werden auf jedes Element im Tensor einzeln angewendet. Diese Operationen, wie Addition, Subtraktion und Division, funktionieren ähnlich wie in NumPy:

import torch
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
# Element-wise addition
addition_result = a + b
print(f"Addition: {addition_result}")
# Element-wise subtraction
subtraction_result = a - b
print(f"Subtraction: {subtraction_result}")
# Element-wise multiplication
multiplication_result = a * b
print(f"Multiplication: {multiplication_result}")
# Element-wise division
division_result = a / b
print(f"Division: {division_result}")
123456789101112131415
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) # Element-wise addition addition_result = a + b print(f"Addition: {addition_result}") # Element-wise subtraction subtraction_result = a - b print(f"Subtraction: {subtraction_result}") # Element-wise multiplication multiplication_result = a * b print(f"Multiplication: {multiplication_result}") # Element-wise division division_result = a / b print(f"Division: {division_result}")
copy

Matrixoperationen

PyTorch unterstützt auch Matrixmultiplikation und Skalarprodukt, die mit der Funktion torch.matmul() durchgeführt werden:

import torch
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[5, 6], [7, 8]])
# Matrix multiplication
z = torch.matmul(x, y)
print(f"Matrix multiplication: {z}")
123456
import torch x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) # Matrix multiplication z = torch.matmul(x, y) print(f"Matrix multiplication: {z}")
copy

Sie können auch den @ Operator für die Matrixmultiplikation verwenden:

import torch
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[5, 6], [7, 8]])
z = x @ y
print(f"Matrix Multiplication with @: {z}")
12345
import torch x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) z = x @ y print(f"Matrix Multiplication with @: {z}")
copy

Aggregationsoperationen

Aggregationsoperationen berechnen Zusammenfassungsstatistiken aus Tensors, wie Summe, Mittelwert, Maximum und Minimum, die mit ihren jeweiligen Methoden berechnet werden können.

import torch
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]]).float()
# Sum of all elements
print(f"Sum: {tensor.sum()}")
# Mean of all elements
print(f"Mean: {tensor.mean()}")
# Maximum value
print(f"Max: {tensor.max()}")
# Minimum value
print(f"Min: {tensor.min()}")
12345678910
import torch tensor = torch.tensor([[1, 2, 3], [4, 5, 6]]).float() # Sum of all elements print(f"Sum: {tensor.sum()}") # Mean of all elements print(f"Mean: {tensor.mean()}") # Maximum value print(f"Max: {tensor.max()}") # Minimum value print(f"Min: {tensor.min()}")
copy

Aggregationsmethoden haben auch zwei optionale Parameter:

  • dim: gibt die Dimension an (ähnlich wie axis in NumPy), entlang der die Operation angewendet wird. Standardmäßig, wenn dim nicht angegeben ist, wird die Operation auf alle Elemente des Tensors angewendet;

  • keepdim: ein boolesches Flag (False standardmäßig). Wenn auf True gesetzt, wird die reduzierte Dimension als Größe 1 Dimension im Ergebnis beibehalten, wodurch die ursprüngliche Anzahl der Dimensionen erhalten bleibt.

import torch
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
# Aggregation operations along specific dimensions
print(f"Sum along rows (dim=1): {tensor.sum(dim=1)}")
print(f"Sum along columns (dim=0): {tensor.sum(dim=0)}")
# Aggregation with keepdim=True
print(f"Sum along rows with keepdim (dim=1): {tensor.sum(dim=1, keepdim=True)}")
print(f"Sum along columns with keepdim (dim=0): {tensor.sum(dim=0, keepdim=True)}")
12345678
import torch tensor = torch.tensor([[1, 2, 3], [4, 5, 6]]) # Aggregation operations along specific dimensions print(f"Sum along rows (dim=1): {tensor.sum(dim=1)}") print(f"Sum along columns (dim=0): {tensor.sum(dim=0)}") # Aggregation with keepdim=True print(f"Sum along rows with keepdim (dim=1): {tensor.sum(dim=1, keepdim=True)}") print(f"Sum along columns with keepdim (dim=0): {tensor.sum(dim=0, keepdim=True)}")
copy

Broadcasting

Broadcasting ermöglicht Operationen zwischen Tensors unterschiedlicher Formen, indem automatisch Dimensionen erweitert werden. Wenn Sie eine Auffrischung zu Broadcasting benötigen, finden Sie weitere Details hier.

import torch
a = torch.tensor([[1, 2, 3]]) # Shape (1, 3)
b = torch.tensor([[4], [5]]) # Shape (2, 1)
# Broadcasting addition
c = a + b
print(f"Broadcasted addition: {c}")
123456
import torch a = torch.tensor([[1, 2, 3]]) # Shape (1, 3) b = torch.tensor([[4], [5]]) # Shape (2, 1) # Broadcasting addition c = a + b print(f"Broadcasted addition: {c}")
copy

Nützliche mathematische Funktionen

PyTorch bietet auch verschiedene mathematische Funktionen wie Exponentialfunktionen, Logarithmen und trigonometrische Funktionen.

tensor = torch.tensor([1.0, 2.0, 3.0])
# Exponentiation
print(f"Exponent: {tensor.exp()}")
# Logarithm
print(f"Logarithm: {tensor.log()}")
# Sine
print(f"Sine: {tensor.sin()}")
1234567
tensor = torch.tensor([1.0, 2.0, 3.0]) # Exponentiation print(f"Exponent: {tensor.exp()}") # Logarithm print(f"Logarithm: {tensor.log()}") # Sine print(f"Sine: {tensor.sin()}")
copy
question mark

Welche der folgenden Operationen führt die Matrixmultiplikation in PyTorch korrekt aus?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt