0Pricing
Deep Learning Academy · Lekcja

Reguły broadcastingu, które oszczędzają pętle

Łączyć element po elemencie tensory o różnych kształtach

Reguły broadcastingu, które oszczędzają pętle to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Combine Without Matching Shapes

Broadcasting lets PyTorch stretch a smaller tensor to fit a bigger one, so you skip writing loops to repeat values.

Add a Scalar to Everything

The simplest broadcast: add one number to a whole tensor. PyTorch applies it to every element at once. ✨

x = torch.tensor([1, 2, 3])
print(x + 10)  # tensor([11, 12, 13])

Compare Shapes Right to Left

Broadcasting lines up dimensions from the right. It then checks each pair to decide if they can combine.

Rule One: Equal Sizes Match

Two dimensions are compatible when they are equal. Matching sizes line up one to one with no stretching needed.

Rule Two: A Size of 1 Stretches

If one dimension is 1, it expands to match the other. That single value is reused across the whole axis.

a = torch.tensor([[1], [2], [3]])
b = torch.tensor([10, 20])
print((a + b).shape)  # torch.Size([3, 2])

Missing Dimensions Count as 1

When one tensor has fewer dimensions, PyTorch pads it with leading ones. A vector can broadcast against a matrix this way.

m = torch.ones(2, 3)
v = torch.tensor([1, 2, 3])
print((m + v).shape)  # torch.Size([2, 3])

Add a Bias Across Rows

A classic use: add a row bias to every sample in a batch. One small vector reaches every row for free.

batch = torch.zeros(4, 3)
bias = torch.tensor([1.0, 2.0, 3.0])
print((batch + bias).shape)  # torch.Size([4, 3])

Normalize a Column at Once

Subtract a per-column mean with broadcasting and center your data in one line, no loop over rows required.

x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
m = x.mean(dim=0)
print(x - m)

When Shapes Don't Broadcast

If two dimensions differ and neither is 1, the broadcast fails and PyTorch raises a clear size error.

Add Dimensions to Steer Broadcasting

Use unsqueeze to insert a size-1 axis exactly where you need it. This guides broadcasting toward the shape you want.

col = torch.tensor([1, 2, 3]).unsqueeze(1)
print(col.shape)  # torch.Size([3, 1])

Why Broadcasting Is Fast

Broadcasting never copies the stretched values, it reuses them. That makes vectorized math far faster than Python loops. 🚀

Quick Check

Can you predict whether two shapes will broadcast?

Recap: Broadcasting Rules

You learned the two rules: dimensions match when equal or when one is 1. Master this and loops melt into clean, fast tensor math. 💪

Często zadawane pytania

Czy lekcja „Reguły broadcastingu, które oszczędzają pętle” jest bezpłatna?

Tak — pełny tekst „Reguły broadcastingu, które oszczędzają pętle” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Reguły broadcastingu, które oszczędzają pętle”?

Łączyć element po elemencie tensory o różnych kształtach Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?

Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Reguły broadcastingu, które oszczędzają pętle”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?

Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Kształty, dtype i indeksowanie
  2. Reshape, view, squeeze i unsqueeze
  3. Reguły broadcastingu, które oszczędzają pętle
  4. Tensory współpracują z NumPy
← Powrót do Deep Learning Academy