0Pricing
Deep Learning Academy · Aula

Minimize uma Função Manualmente em Python

Programe a descida do gradiente em uma curva simples

Minimize uma Função Manualmente em Python é uma aula grátis de Deep Learning Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Deep Learning Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Deep Learning Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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. ✅

Perguntas Frequentes

A aula “Minimize uma Função Manualmente em Python” é grátis?

Sim — o texto completo de “Minimize uma Função Manualmente em Python” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Deep Learning Academy, atualize para CoddyKit PRO. O curso de Deep Learning Academy inclui 4 aulas no total.

O que vou aprender em “Minimize uma Função Manualmente em Python”?

Programe a descida do gradiente em uma curva simples Você pratica Deep Learning Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Deep Learning Academy?

Nenhuma experiência prévia é necessária. Deep Learning Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Minimize uma Função Manualmente em Python”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Deep Learning Academy?

Sim. Cada aula de Deep Learning Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. A Perda como uma Paisagem a Percorrer
  2. Gradientes Apontam para Cima — Então Avance na Direção Oposta
  3. Taxa de Aprendizado: Grande Demais, Pequena Demais ou Ideal
  4. Minimize uma Função Manualmente em Python
← Voltar para Deep Learning Academy