Matematiske Operationer med Tensorer
Elementvise operationer
Elementvise operationer anvendes på hvert element i tensoren individuelt. Disse operationer, såsom addition, subtraktion og division, fungerer på samme måde som i NumPy:
123456789101112131415import 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}")
Matrixoperationer
PyTorch understøtter også matrixmultiplikation og dotprodukt, som udføres ved hjælp af funktionen torch.matmul()
:
123456import 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}")
Du kan også bruge @
-operatoren til matrixmultiplikation:
12345import torch x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) z = x @ y print(f"Matrix Multiplication with @: {z}")
Aggregeringsoperationer
Aggregeringsoperationer beregner sammenfattende statistikker fra tensorer, såsom sum, gennemsnit, maksimum og minimumsværdier, som kan beregnes ved hjælp af deres respektive metoder.
12345678910import 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()}")
Aggregeringsmetoder har også to valgfrie parametre:
dim
: angiver dimensionen (svarende tilaxis
i NumPy) langs hvilken operationen anvendes. Som standard, hvisdim
ikke angives, anvendes operationen på alle elementer i tensoren;keepdim
: et boolesk flag (False
som standard). Hvis det sættes tilTrue
, bevares den reducerede dimension som en dimension med størrelse1
i outputtet, hvilket bevarer det oprindelige antal dimensioner.
12345678import 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)}")
Broadcasting
Broadcasting muliggør operationer mellem tensores af forskellige former ved automatisk at udvide dimensionerne. Hvis du har brug for en genopfriskning af broadcasting, kan du finde flere detaljer her.
123456import 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}")
Nyttige matematiske funktioner
PyTorch tilbyder også forskellige matematiske funktioner såsom eksponentialer, logaritmer og trigonometriske funktioner.
1234567tensor = 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()}")
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain more about element-wise operations in PyTorch?
What is the difference between element-wise and matrix operations?
How does broadcasting work in PyTorch?
Awesome!
Completion rate improved to 5
Matematiske Operationer med Tensorer
Stryg for at vise menuen
Elementvise operationer
Elementvise operationer anvendes på hvert element i tensoren individuelt. Disse operationer, såsom addition, subtraktion og division, fungerer på samme måde som i NumPy:
123456789101112131415import 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}")
Matrixoperationer
PyTorch understøtter også matrixmultiplikation og dotprodukt, som udføres ved hjælp af funktionen torch.matmul()
:
123456import 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}")
Du kan også bruge @
-operatoren til matrixmultiplikation:
12345import torch x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) z = x @ y print(f"Matrix Multiplication with @: {z}")
Aggregeringsoperationer
Aggregeringsoperationer beregner sammenfattende statistikker fra tensorer, såsom sum, gennemsnit, maksimum og minimumsværdier, som kan beregnes ved hjælp af deres respektive metoder.
12345678910import 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()}")
Aggregeringsmetoder har også to valgfrie parametre:
dim
: angiver dimensionen (svarende tilaxis
i NumPy) langs hvilken operationen anvendes. Som standard, hvisdim
ikke angives, anvendes operationen på alle elementer i tensoren;keepdim
: et boolesk flag (False
som standard). Hvis det sættes tilTrue
, bevares den reducerede dimension som en dimension med størrelse1
i outputtet, hvilket bevarer det oprindelige antal dimensioner.
12345678import 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)}")
Broadcasting
Broadcasting muliggør operationer mellem tensores af forskellige former ved automatisk at udvide dimensionerne. Hvis du har brug for en genopfriskning af broadcasting, kan du finde flere detaljer her.
123456import 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}")
Nyttige matematiske funktioner
PyTorch tilbyder også forskellige matematiske funktioner såsom eksponentialer, logaritmer og trigonometriske funktioner.
1234567tensor = 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()}")
Tak for dine kommentarer!