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
16c5edfe
Verified
Commit
16c5edfe
authored
1 year ago
by
iliya.saroukha
Browse files
Options
Downloads
Patches
Plain Diff
refined epsilon value
parent
f1f1bde2
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gd.py
+14
-15
14 additions, 15 deletions
gd.py
with
14 additions
and
15 deletions
gd.py
+
14
−
15
View file @
16c5edfe
...
@@ -33,7 +33,7 @@ def base_gd(f: Function, init_pt: list[float], lr: float) -> \
...
@@ -33,7 +33,7 @@ def base_gd(f: Function, init_pt: list[float], lr: float) -> \
f_call
=
callable_func
(
f
)
f_call
=
callable_func
(
f
)
while
iter
<
1e4
:
while
iter
<
1e4
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-
5
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-
6
:
break
break
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
df
.
loc
[
iter
]
=
[
x
,
y
,
f_call
(
x
,
y
),
np
.
linalg
.
norm
(
grad
)]
df
.
loc
[
iter
]
=
[
x
,
y
,
f_call
(
x
,
y
),
np
.
linalg
.
norm
(
grad
)]
...
@@ -63,7 +63,7 @@ def momentum_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
...
@@ -63,7 +63,7 @@ def momentum_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
step
=
np
.
array
([
0
,
0
])
step
=
np
.
array
([
0
,
0
])
while
iter
<
1e4
:
while
iter
<
1e4
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-
5
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-
6
:
break
break
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
...
@@ -95,7 +95,7 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
...
@@ -95,7 +95,7 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
while
iter
<
1e4
:
while
iter
<
1e4
:
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
grad
=
np
.
array
([
partialx
(
x
,
y
),
partialy
(
x
,
y
)])
if
np
.
linalg
.
norm
(
grad
)
<
1e-
5
:
if
np
.
linalg
.
norm
(
grad
)
<
1e-
6
:
break
break
grad_with_prev_step
=
np
.
array
([
partialx
(
x
-
step
[
0
],
y
-
step
[
1
]),
grad_with_prev_step
=
np
.
array
([
partialx
(
x
-
step
[
0
],
y
-
step
[
1
]),
...
@@ -115,8 +115,8 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
...
@@ -115,8 +115,8 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
x
,
y
=
symbols
(
'
x y
'
)
x
,
y
=
symbols
(
'
x y
'
)
f
:
Function
=
x
**
2
+
6
*
y
**
2
#
f: Function = x**2 + 6 * y**2
#
f: Function = 1 - exp(-10 * x**2 - y**2)
f
:
Function
=
1
-
exp
(
-
10
*
x
**
2
-
y
**
2
)
# f: Function = x**2 * y - 2 * x * y**3 + 3 * x * y + 4
# f: Function = x**2 * y - 2 * x * y**3 + 3 * x * y + 4
# Rosenbrock(x, y)
# Rosenbrock(x, y)
...
@@ -131,17 +131,16 @@ if __name__ == "__main__":
...
@@ -131,17 +131,16 @@ if __name__ == "__main__":
# Ackley(x, y)
# Ackley(x, y)
# f: Function = -20.0 * exp(-0.2 * sqrt(0.5 * (x**2 + y**2))) - \
# 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
#
exp(0.5 * (cos(2 * pi * x) + cos(2 * pi * y))) + exp(1) + 20
f_call
=
callable_func
(
f
)
f_call
=
callable_func
(
f
)
LR
=
1e-2
LR
=
1e-2
MOMENTUM
=
0.9
MOMENTUM
=
0.9
plot_range
=
(
30
,
30
)
plot_range
=
(
2
,
2
)
# 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),
# init_pt = np.array([np.random.randint(-plot_range[0], plot_range[0] + 1),
# np.random.randint(-plot_range[1], plot_range[1] + 1)])
# np.random.randint(-plot_range[1], plot_range[1] + 1)])
...
@@ -170,20 +169,20 @@ if __name__ == "__main__":
...
@@ -170,20 +169,20 @@ if __name__ == "__main__":
ax
.
contour
(
X
,
Y
,
Z
,
20
,
cmap
=
'
RdGy
'
,
offset
=
0
)
ax
.
contour
(
X
,
Y
,
Z
,
20
,
cmap
=
'
RdGy
'
,
offset
=
0
)
ax
.
plot
(
base
[
'
x
'
],
base
[
'
y
'
],
'
-o
'
,
color
=
'
red
'
,
label
=
'
b
ase
'
)
ax
.
plot
(
base
[
'
x
'
],
base
[
'
y
'
],
'
-o
'
,
color
=
'
red
'
,
label
=
'
B
ase
'
)
ax
.
plot
(
momentum
[
'
x
'
],
momentum
[
'
y
'
],
'
-o
'
,
ax
.
plot
(
momentum
[
'
x
'
],
momentum
[
'
y
'
],
'
-o
'
,
color
=
'
violet
'
,
label
=
'
m
omentum
'
)
color
=
'
cyan
'
,
label
=
'
M
omentum
'
)
ax
.
plot
(
nesterov
[
'
x
'
],
nesterov
[
'
y
'
],
'
-o
'
,
ax
.
plot
(
nesterov
[
'
x
'
],
nesterov
[
'
y
'
],
'
-o
'
,
color
=
'
green
'
,
label
=
'
n
esterov
'
)
color
=
'
orange
'
,
label
=
'
N
esterov
'
)
ax
.
legend
()
ax
.
legend
()
plt
.
figure
(
2
)
plt
.
figure
(
2
)
plt
.
contour
(
X
,
Y
,
Z
,
20
,
cmap
=
'
RdGy
'
)
plt
.
contour
(
X
,
Y
,
Z
,
20
,
cmap
=
'
RdGy
'
)
plt
.
plot
(
base
[
'
x
'
],
base
[
'
y
'
],
'
-o
'
,
color
=
'
red
'
,
label
=
'
b
ase
'
)
plt
.
plot
(
base
[
'
x
'
],
base
[
'
y
'
],
'
-o
'
,
color
=
'
red
'
,
label
=
'
B
ase
'
)
plt
.
plot
(
momentum
[
'
x
'
],
momentum
[
'
y
'
],
'
-o
'
,
plt
.
plot
(
momentum
[
'
x
'
],
momentum
[
'
y
'
],
'
-o
'
,
color
=
'
violet
'
,
label
=
'
m
omentum
'
)
color
=
'
cyan
'
,
label
=
'
M
omentum
'
)
plt
.
plot
(
nesterov
[
'
x
'
],
nesterov
[
'
y
'
],
'
-o
'
,
plt
.
plot
(
nesterov
[
'
x
'
],
nesterov
[
'
y
'
],
'
-o
'
,
color
=
'
green
'
,
label
=
'
n
esterov
'
)
color
=
'
orange
'
,
label
=
'
N
esterov
'
)
plt
.
legend
()
plt
.
legend
()
plt
.
xlabel
(
'
x
'
)
plt
.
xlabel
(
'
x
'
)
plt
.
ylabel
(
'
y
'
)
plt
.
ylabel
(
'
y
'
)
...
...
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