Pythonで関数を手作業で最小化する
単純な曲線に対する勾配降下法を実装します
「Pythonで関数を手作業で最小化する」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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) ** 2Know 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.0Choose 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.1The 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.1Same 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. ✅
AI チューターと学ぶ Python — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「Pythonで関数を手作業で最小化する」レッスンは無料ですか?
はい。「Pythonで関数を手作業で最小化する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「Pythonで関数を手作業で最小化する」で何を学びますか?
単純な曲線に対する勾配降下法を実装します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Pythonで関数を手作業で最小化する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 損失を下るべき地形として捉える
- 勾配は上り坂を指す——だから逆方向に進む
- 学習率:大きすぎず小さすぎず、ちょうどよく
- Pythonで関数を手作業で最小化する