Scriva un ciclo di addestramento minimo
Scorra i dati e aggiorni i pesi
Scriva un ciclo di addestramento minimo è una lezione Deep Learning Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Deep Learning Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Deep Learning Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎉
Domande Frequenti
La lezione «Scriva un ciclo di addestramento minimo» è gratuita?
Sì — il testo completo di «Scriva un ciclo di addestramento minimo» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Deep Learning Academy, passa a CoddyKit PRO. Il corso Deep Learning Academy include 4 lezioni in totale.
Cosa imparerò in «Scriva un ciclo di addestramento minimo»?
Scorra i dati e aggiorni i pesi Eserciti Deep Learning Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Deep Learning Academy?
Non è richiesta alcuna esperienza precedente. Deep Learning Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Scriva un ciclo di addestramento minimo»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Deep Learning Academy?
Sì. Ogni lezione Deep Learning Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Forward pass, loss, backward e step
- Scriva un ciclo di addestramento minimo
- Tenga traccia dell'accuratezza durante l'addestramento
- Modalità train ed eval