0Pricing
Deep Learning Academy · Lekcja

Napisać minimalną pętlę uczenia

Iterować po danych i aktualizować wagi

Napisać minimalną pętlę uczenia 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.

From Steps to a Loop

You know the four moves of one iteration. Now you wrap them in a loop that walks over your data again and again until the model is trained. 🔁

Gather the Ingredients

Before looping you need three things: a model, a loss function, and an optimizer that holds your model's parameters. Set them up once, up front.

loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters())

Loop Over Epochs

The outer loop counts epochs, one full pass through your training data. A few epochs may be plenty for a small problem, more for a hard one.

for epoch in range(epochs):
    ...

Loop Over Batches

Inside each epoch you iterate the dataloader, which hands you one batch of inputs and labels at a time instead of the whole dataset at once.

for x, y in dataloader:
    ...

Zero, Then Forward

Start each batch by clearing old gradients, then run the forward pass to get predictions. Order matters: zero first, then predict.

optimizer.zero_grad()
pred = model(x)

Measure, Then Learn

Compute the loss, call backward for gradients, then step the optimizer. These three lines are where every weight actually improves.

loss = loss_fn(pred, y)
loss.backward()
optimizer.step()

The Full Skeleton

Stitch it together and you have a complete training loop. It is short on purpose: this same shape powers projects of every size.

for epoch in range(epochs):
    for x, y in dataloader:
        optimizer.zero_grad()
        loss = loss_fn(model(x), y)
        loss.backward()
        optimizer.step()

Watch the Loss

Print the loss each epoch so you can see learning happen. A number that trends down means your loop is working as intended.

print(epoch, loss.item())

Why item Matters

Use loss.item to pull out a plain Python float. Logging the raw tensor instead keeps the whole computation graph alive and wastes memory.

If Loss Stalls

If the loss refuses to drop, suspect the basics first: a learning rate that is too high or too low, or a forgotten zero_grad each step.

Small but Complete

This loop is tiny yet it is genuinely complete. Everything fancier you meet later just adds validation, logging, or speed on top of these few lines.

Quick Check

What does the inner loop iterate over in a minimal training loop?

Recap

You built a real training loop: loop epochs, loop batches, then zero, forward, loss, backward, step. Print the loss to watch it learn. 🎉

Często zadawane pytania

Czy lekcja „Napisać minimalną pętlę uczenia” jest bezpłatna?

Tak — pełny tekst „Napisać minimalną pętlę uczenia” 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 „Napisać minimalną pętlę uczenia”?

Iterować po danych i aktualizować wagi Ć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 „Napisać minimalną pętlę uczenia”?

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. Przejście w przód, strata, wstecz i krok
  2. Napisać minimalną pętlę uczenia
  3. Śledzić dokładność podczas uczenia
  4. Tryb train a tryb eval
← Powrót do Deep Learning Academy