Operaciones elemento a elemento y reducciones
Calcule la suma, la media y el máximo en los ejes elegidos
Operaciones elemento a elemento y reducciones es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 2 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.
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. 🎯
Preguntas frecuentes
¿La lección «Operaciones elemento a elemento y reducciones» es gratis?
Sí — el texto completo de «Operaciones elemento a elemento y reducciones» 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 «Operaciones elemento a elemento y reducciones»?
Calcule la suma, la media y el máximo en los ejes elegidos 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 2 de 4.
¿Cuánto tiempo toma la lección «Operaciones elemento a elemento y reducciones»?
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
- Por qué los bucles son lentos para las matemáticas
- Operaciones elemento a elemento y reducciones
- Multiplicación de matrices con matmul y @
- Los productos punto impulsan cada capa