Operacje elementowe i redukcje
Obliczać sumę, średnią i maksimum wzdłuż wybranych osi
Operacje elementowe i redukcje to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 2 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.
Two Kinds of Operations
Tensor math splits into two families: elementwise ops that keep the shape, and reductions that collapse it down to fewer numbers.
Elementwise Keeps the Shape
An elementwise op applies the same action to every entry independently. Input shape in, same shape out, no mixing between positions.
b = a * 2 + 1
# same shape as a, every element transformedPairwise Elementwise Math
With two tensors of equal shape, elementwise ops act position by position. Add matches index to index, multiply does the same.
c = a + b
d = a * bMath Functions Are Elementwise Too
Functions like torch.relu, exp, and sqrt run elementwise. Each number is transformed on its own, and the tensor keeps its original shape.
r = torch.relu(x)
e = torch.exp(x)Reductions Collapse Numbers
A reduction combines many values into fewer. Sum, mean, and max fold a whole tensor down, by default to a single scalar.
total = x.sum()
avg = x.mean()The dim Argument Picks an Axis
Pass dim to reduce along one axis only. The chosen axis disappears while the others stay, so a 2D tensor becomes 1D.
col_sums = x.sum(dim=0)
row_means = x.mean(dim=1)keepdim Saves the Shape
Set keepdim=True to keep the reduced axis as size 1. That preserved shape is what makes later broadcasting line up cleanly.
m = x.max(dim=1, keepdim=True).valuesMean Needs Floats
mean divides, so it expects floating-point input. Call it on an integer tensor and PyTorch will complain until you cast to float first.
avg = x.float().mean()argmax Finds the Winner
Sometimes you want the position, not the value. argmax returns the index of the largest entry, which is how a classifier picks its predicted class.
pred = logits.argmax(dim=1)Chain Them Together
Real code stacks both kinds: an elementwise transform feeds a reduction. Square the errors, then take the mean, and you have mean squared error.
mse = ((pred - target) ** 2).mean()Pick Shape-Keeping or Shape-Shrinking
The rule of thumb: reach for elementwise when every value should change in place, and reach for a reduction when you need a summary like a total or average.
Quick Check
Can you tell which operation changes a tensor shape?
Recap: Transform vs Summarize
Elementwise ops keep the shape and act per value; reductions like sum and mean collapse it, with dim and keepdim controlling exactly how. 🎯
Często zadawane pytania
Czy lekcja „Operacje elementowe i redukcje” jest bezpłatna?
Tak — pełny tekst „Operacje elementowe i redukcje” 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 „Operacje elementowe i redukcje”?
Obliczać sumę, średnią i maksimum wzdłuż wybranych osi Ć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 2 z 4.
Ile czasu zajmuje lekcja „Operacje elementowe i redukcje”?
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
- Dlaczego pętle są wolne w obliczeniach matematycznych
- Operacje elementowe i redukcje
- Mnożenie macierzy za pomocą matmul i @
- Iloczyny skalarne napędzają każdą warstwę