Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gradient_descent
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
maths
gradient_descent
Commits
f1f1bde2
Verified
Commit
f1f1bde2
authored
1 year ago
by
iliya.saroukha
Browse files
Options
Downloads
Patches
Plain Diff
fix: momentum and nesterov working
parent
185c8f14
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gd.py
+27
-28
27 additions, 28 deletions
gd.py
with
27 additions
and
28 deletions
gd.py
+
27
−
28
View file @
f1f1bde2
...
...
@@ -32,8 +32,9 @@ def base_gd(f: Function, init_pt: list[float], lr: float) -> \
f_call
=
callable_func
(
f
)
# while np.linalg.norm(grad) > 1e-6:
while
iter
<
1e4
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-5
:
break
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
df
.
loc
[
iter
]
=
[
x
,
y
,
f_call
(
x
,
y
),
np
.
linalg
.
norm
(
grad
)]
...
...
@@ -59,20 +60,20 @@ def momentum_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
f_call
=
callable_func
(
f
)
prev_step
=
np
.
array
([
0
,
0
])
curr_step
=
np
.
array
([
0
,
0
])
step
=
np
.
array
([
0
,
0
])
# while np.linalg.norm(grad) > 1e-6:
while
iter
<
1e4
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-5
:
break
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
df
.
loc
[
iter
]
=
[
x
,
y
,
f_call
(
x
,
y
),
np
.
linalg
.
norm
(
grad
)]
curr_
step
=
-
momentum
*
prev_
step
-
lr
*
grad
step
=
momentum
*
step
+
lr
*
grad
x
+
=
curr_
step
[
0
]
y
+
=
curr_
step
[
1
]
x
-
=
step
[
0
]
y
-
=
step
[
1
]
prev_step
=
curr_step
iter
+=
1
return
df
...
...
@@ -90,24 +91,22 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
f_call
=
callable_func
(
f
)
prev_step
=
np
.
array
([
0
,
0
])
curr_step
=
np
.
array
([
0
,
0
])
step
=
np
.
array
([
0
,
0
])
# while np.linalg.norm(grad) > 1e-6:
while
iter
<
1e4
:
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
df
.
loc
[
iter
]
=
[
x
,
y
,
f_call
(
x
,
y
),
np
.
linalg
.
norm
(
grad
)]
if
np
.
linalg
.
norm
(
grad
)
<
1e-5
:
break
momentum_prev
=
-
momentum
*
prev_step
offset_grad
=
np
.
array
([
partial
x
(
x
-
momentum_prev
[
0
],
y
-
momentum_prev
[
1
])
,
parti
al
y
(
x
-
momentum_prev
[
0
],
y
-
momentum_prev
[
1
]
)]
)
grad_with_prev_step
=
np
.
array
([
partialx
(
x
-
step
[
0
],
y
-
step
[
1
]),
partial
y
(
x
-
step
[
0
],
y
-
step
[
1
])
])
df
.
loc
[
iter
]
=
[
x
,
y
,
f_c
al
l
(
x
,
y
),
np
.
linalg
.
norm
(
grad
)]
curr_
step
=
momentum
_prev
-
lr
*
offset_grad
step
=
momentum
*
step
+
lr
*
grad_with_prev_step
x
+
=
curr_
step
[
0
]
y
+
=
curr_
step
[
1
]
x
-
=
step
[
0
]
y
-
=
step
[
1
]
prev_step
=
curr_step
iter
+=
1
return
df
...
...
@@ -116,7 +115,7 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
if
__name__
==
"
__main__
"
:
x
,
y
=
symbols
(
'
x y
'
)
#
f: Function = x**2 +
5
* y**2
f
:
Function
=
x
**
2
+
6
*
y
**
2
# f: Function = 1 - exp(-10 * x**2 - y**2)
# f: Function = x**2 * y - 2 * x * y**3 + 3 * x * y + 4
...
...
@@ -131,21 +130,21 @@ if __name__ == "__main__":
# f: Function = (x + 2 * y - 7)**2 + (2 * x + y - 5)**2
# Ackley(x, y)
f
:
Function
=
-
20.0
*
exp
(
-
0.2
*
sqrt
(
0.5
*
(
x
**
2
+
y
**
2
)))
-
\
exp
(
0.5
*
(
cos
(
2
*
pi
*
x
)
+
cos
(
2
*
pi
*
y
)))
+
exp
(
1
)
+
20
#
f: Function = -20.0 * exp(-0.2 * sqrt(0.5 * (x**2 + y**2))) - \
#
exp(0.5 * (cos(2 * pi * x) + cos(2 * pi * y))) + exp(1) + 20
f_call
=
callable_func
(
f
)
LR
=
1e-
1
MOMENTUM
=
1e-1
LR
=
1e-
2
MOMENTUM
=
0.9
plot_range
=
(
1
0
,
1
0
)
plot_range
=
(
3
0
,
3
0
)
# init_pt = [9, -8]
#
init_pt = [
1, 1
]
init_pt
=
[
20
,
30
]
init_pt
=
np
.
array
([
np
.
random
.
randint
(
-
plot_range
[
0
],
plot_range
[
0
]
+
1
),
np
.
random
.
randint
(
-
plot_range
[
1
],
plot_range
[
1
]
+
1
)])
#
init_pt = np.array([np.random.randint(-plot_range[0], plot_range[0] + 1),
#
np.random.randint(-plot_range[1], plot_range[1] + 1)])
base
=
base_gd
(
f
,
init_pt
,
LR
)
momentum
=
momentum_gd
(
f
,
init_pt
,
LR
,
MOMENTUM
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment