Formas, dtypes e indexación
Consulte la forma de un tensor y seleccione una parte
Formas, dtypes e indexación es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Deep Learning Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Deep Learning Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Preguntas frecuentes
¿La lección «Formas, dtypes e indexación» es gratis?
Sí — el texto completo de «Formas, dtypes e indexación» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Deep Learning Academy, actualiza a CoddyKit PRO. El curso de Deep Learning Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Formas, dtypes e indexación»?
Consulte la forma de un tensor y seleccione una parte Practicas Deep Learning Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Deep Learning Academy?
No se requiere experiencia previa. Deep Learning Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Formas, dtypes e indexación»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Deep Learning Academy?
Sí. Cada lección de Deep Learning Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Formas, dtypes e indexación
- Reshape, view, squeeze y unsqueeze
- Reglas de broadcasting que le ahorran bucles
- Los tensores se comunican con NumPy