Matematiske Operasjoner med Tensorer
Elementvise operasjoner
Elementvise operasjoner brukes på hvert enkelt element i tensoren individuelt. Disse operasjonene, som addisjon, subtraksjon og divisjon, fungerer på samme måte 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}")
Matriseoperasjoner
PyTorch støtter også matrisemultiplikasjon og skalarprodukt, som utføres ved hjelp av funksjonen 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å bruke @
-operatoren for matrisesmultiplikasjon:
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}")
Aggregeringsoperasjoner
Aggregeringsoperasjoner beregner sammendragsstatistikk fra tensorer, som sum, gjennomsnitt, maksimum og minimumsverdier, som kan beregnes ved hjelp av 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 parametere:
dim
: angir dimensjonen (tilsvarendeaxis
i NumPy) som operasjonen skal brukes på. Som standard, hvisdim
ikke er angitt, brukes operasjonen på alle elementene i tensoren;keepdim
: et boolsk flagg (False
som standard). Hvis satt tilTrue
, beholdes den reduserte dimensjonen som en dimensjon med størrelse1
i resultatet, slik at det opprinnelige antallet dimensjoner bevares.
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 muliggjør operasjoner mellom tensorer med ulike former ved å automatisk utvide dimensjonene. Hvis du trenger en repetisjon om broadcasting, finner du mer informasjon 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 funksjoner
PyTorch tilbyr også ulike matematiske funksjoner som eksponentialer, logaritmer og trigonometriske funksjoner.
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()}")
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
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 Operasjoner med Tensorer
Sveip for å vise menyen
Elementvise operasjoner
Elementvise operasjoner brukes på hvert enkelt element i tensoren individuelt. Disse operasjonene, som addisjon, subtraksjon og divisjon, fungerer på samme måte 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}")
Matriseoperasjoner
PyTorch støtter også matrisemultiplikasjon og skalarprodukt, som utføres ved hjelp av funksjonen 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å bruke @
-operatoren for matrisesmultiplikasjon:
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}")
Aggregeringsoperasjoner
Aggregeringsoperasjoner beregner sammendragsstatistikk fra tensorer, som sum, gjennomsnitt, maksimum og minimumsverdier, som kan beregnes ved hjelp av 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 parametere:
dim
: angir dimensjonen (tilsvarendeaxis
i NumPy) som operasjonen skal brukes på. Som standard, hvisdim
ikke er angitt, brukes operasjonen på alle elementene i tensoren;keepdim
: et boolsk flagg (False
som standard). Hvis satt tilTrue
, beholdes den reduserte dimensjonen som en dimensjon med størrelse1
i resultatet, slik at det opprinnelige antallet dimensjoner bevares.
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 muliggjør operasjoner mellom tensorer med ulike former ved å automatisk utvide dimensjonene. Hvis du trenger en repetisjon om broadcasting, finner du mer informasjon 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 funksjoner
PyTorch tilbyr også ulike matematiske funksjoner som eksponentialer, logaritmer og trigonometriske funksjoner.
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()}")
Takk for tilbakemeldingene dine!