0Pricing
Deep Learning Academy · Lección

Minimice una función a mano en Python

Programe el descenso del gradiente en una curva sencilla

Minimice una función a mano en Python es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 4 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.

A Tiny Practice Problem

Let's run gradient descent ourselves on one simple curve. We will minimize a basic function in plain Python before trusting any framework to do it.

Pick a Function

We use a single-dip parabola. Its lowest point sits at x equal to 3, so that is the minimum our code should discover on its own.

def f(x):
    return (x - 3) ** 2

Know the Gradient

For this curve the slope, or gradient, is two times x minus three. In real models autograd computes this for you, but here we write it directly.

def grad(x):
    return 2 * (x - 3)

Start Somewhere

Descent needs a starting point. We pick an arbitrary initial value far from the answer so we can watch the steps march toward it.

x = 0.0

Choose a Learning Rate

We set a modest learning rate so steps are big enough to make progress but small enough to avoid overshooting the dip.

lr = 0.1

The Update Step

Each iteration applies the same rule from before: subtract the scaled gradient from x. One line does all the downhill work.

x = x - lr * grad(x)

Loop It

One step is not enough, so we wrap the update in a loop. Twenty or so iterations let x slide steadily toward the minimum.

for i in range(20):
    x = x - lr * grad(x)

Watch It Converge

Print x and f(x) each pass and you will see them converge: x creeps toward 3 and the function value shrinks toward zero.

    print(round(x, 4), round(f(x), 6))

Steps Get Smaller

Notice the moves shrink as you approach the bottom. Near the minimum the gradient is tiny, so each step naturally slows down without any extra code.

Try a Bad Rate

Set the learning rate to something like 1.1 and rerun. x bounces away instead of settling, showing live how a too-big step diverges.

lr = 1.1

Same Idea, Bigger Scale

This handful of lines is exactly what deep learning does, just with millions of weights and an automatic gradient. The core loop never changes.

Quick Check

What signals that descent is converging?

Recap

You coded gradient descent by hand: define a function and its gradient, start somewhere, then loop the update rule until x converges on the minimum. ✅

Preguntas frecuentes

¿La lección «Minimice una función a mano en Python» es gratis?

Sí — el texto completo de «Minimice una función a mano en Python» 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 «Minimice una función a mano en Python»?

Programe el descenso del gradiente en una curva sencilla 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 4 de 4.

¿Cuánto tiempo toma la lección «Minimice una función a mano en Python»?

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

  1. La pérdida como un paisaje que recorrer hacia abajo
  2. Los gradientes apuntan cuesta arriba: avance en sentido contrario
  3. Tasa de aprendizaje: demasiado grande, demasiado pequeña o justa
  4. Minimice una función a mano en Python
← Volver a Deep Learning Academy