Forme, dtype e indicizzazione
Legga la forma di un tensore e ne selezioni una parte
Forme, dtype e indicizzazione è una lezione Deep Learning Academy gratuita su CoddyKit. Questa è la lezione 1 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.
Tensors Are Just Numbers in a Grid
A tensor is a box of numbers arranged in rows, columns, or deeper. Everything in deep learning flows through them.
Shape Describes the Layout
The shape tells you how many values sit along each dimension. Read it like dimensions of a box: rows by columns. 📦
Check Shape with .shape
Ask any tensor for its .shape and PyTorch hands you the size of every dimension in order.
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x.shape) # torch.Size([2, 3])Dimensions Count with .ndim
The number of dimensions is the tensor's rank. A scalar is 0, a vector is 1, a matrix is 2, and images go higher.
x = torch.tensor([[1, 2], [3, 4]])
print(x.ndim) # 2Every Tensor Has a Dtype
The dtype is the kind of number stored, like float32 or int64. It controls precision and which math is allowed.
x = torch.tensor([1.0, 2.0, 3.0])
print(x.dtype) # torch.float32Float32 Is the Default for Learning
Most networks train in float32 because gradients need decimals. Integers can't hold the tiny fractional updates.
Cast Between Dtypes
Change a tensor's type with .to() or .float(). This is how you match a model's expected precision.
x = torch.tensor([1, 2, 3])
y = x.to(torch.float32)
print(y.dtype) # torch.float32Indexing Grabs One Value
Indexing uses square brackets to pull out a single element by its position, counting from zero.
x = torch.tensor([10, 20, 30])
print(x[0]) # tensor(10)Two Indices for a Matrix
For a 2D tensor, give a row then a column. The order always follows the shape you saw earlier.
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x[1, 2]) # tensor(6)Slicing Takes a Range
Use a colon to grab a whole slice. start:stop keeps everything up to but not including stop.
x = torch.tensor([10, 20, 30, 40])
print(x[1:3]) # tensor([20, 30])Slice Rows and Columns Together
A lone colon means take all of that dimension. This is how you select a whole column across every row.
x = torch.tensor([[1, 2], [3, 4]])
print(x[:, 0]) # tensor([1, 3])Quick Check
Time to test what shape and indexing really mean.
Recap: Shapes, Dtypes & Indexing
You can now read a tensor's shape, check its dtype, and slice into it. These three skills unlock every tensor operation ahead. 🎉
Domande Frequenti
La lezione «Forme, dtype e indicizzazione» è gratuita?
Sì — il testo completo di «Forme, dtype e indicizzazione» è 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 «Forme, dtype e indicizzazione»?
Legga la forma di un tensore e ne selezioni una parte 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 1 di 4.
Quanto tempo richiede la lezione «Forme, dtype e indicizzazione»?
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
- Forme, dtype e indicizzazione
- Reshape, view, squeeze e unsqueeze
- Regole di broadcasting che evitano i cicli
- I tensori comunicano con NumPy