Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Matematiske Operasjoner med Tensorer | PyTorch Introduksjon
PyTorch-essensielt

bookMatematiske 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:

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

Matriseoperasjoner

PyTorch støtter også matrisemultiplikasjon og skalarprodukt, som utføres ved hjelp av funksjonen torch.matmul():

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

Du kan også bruke @-operatoren for matrisesmultiplikasjon:

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

Aggregeringsoperasjoner

Aggregeringsoperasjoner beregner sammendragsstatistikk fra tensorer, som sum, gjennomsnitt, maksimum og minimumsverdier, som kan beregnes ved hjelp av deres respektive metoder.

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

Aggregeringsmetoder har også to valgfrie parametere:

  • dim: angir dimensjonen (tilsvarende axis i NumPy) som operasjonen skal brukes på. Som standard, hvis dim ikke er angitt, brukes operasjonen på alle elementene i tensoren;
  • keepdim: et boolsk flagg (False som standard). Hvis satt til True, beholdes den reduserte dimensjonen som en dimensjon med størrelse 1 i resultatet, slik at det opprinnelige antallet dimensjoner bevares.
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 muliggjør operasjoner mellom tensorer med ulike former ved å automatisk utvide dimensjonene. Hvis du trenger en repetisjon om broadcasting, finner du mer informasjon her.

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

Nyttige matematiske funksjoner

PyTorch tilbyr også ulike matematiske funksjoner som eksponentialer, logaritmer og trigonometriske funksjoner.

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

Hvilken av følgende operasjoner utfører korrekt matrise-multiplikasjon i PyTorch?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 7

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

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

bookMatematiske 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:

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

Matriseoperasjoner

PyTorch støtter også matrisemultiplikasjon og skalarprodukt, som utføres ved hjelp av funksjonen torch.matmul():

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

Du kan også bruke @-operatoren for matrisesmultiplikasjon:

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

Aggregeringsoperasjoner

Aggregeringsoperasjoner beregner sammendragsstatistikk fra tensorer, som sum, gjennomsnitt, maksimum og minimumsverdier, som kan beregnes ved hjelp av deres respektive metoder.

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

Aggregeringsmetoder har også to valgfrie parametere:

  • dim: angir dimensjonen (tilsvarende axis i NumPy) som operasjonen skal brukes på. Som standard, hvis dim ikke er angitt, brukes operasjonen på alle elementene i tensoren;
  • keepdim: et boolsk flagg (False som standard). Hvis satt til True, beholdes den reduserte dimensjonen som en dimensjon med størrelse 1 i resultatet, slik at det opprinnelige antallet dimensjoner bevares.
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 muliggjør operasjoner mellom tensorer med ulike former ved å automatisk utvide dimensjonene. Hvis du trenger en repetisjon om broadcasting, finner du mer informasjon her.

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

Nyttige matematiske funksjoner

PyTorch tilbyr også ulike matematiske funksjoner som eksponentialer, logaritmer og trigonometriske funksjoner.

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

Hvilken av følgende operasjoner utfører korrekt matrise-multiplikasjon i PyTorch?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 7
some-alt