Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pytorch Without Pytorch
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
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Falguni Ghosh
Pytorch Without Pytorch
Commits
8068810f
Commit
8068810f
authored
1 year ago
by
Falguni Ghosh
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
4ff7f3c2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
2_CNN/Optimizers.py
+67
-0
67 additions, 0 deletions
2_CNN/Optimizers.py
with
67 additions
and
0 deletions
2_CNN/Optimizers.py
0 → 100644
+
67
−
0
View file @
8068810f
import
numpy
as
np
class
Sgd
:
learning_rate
=
None
output
=
None
def
__init__
(
self
,
learning_rate
):
self
.
learning_rate
=
learning_rate
def
calculate_update
(
self
,
weight_tensor
,
gradient_tensor
):
self
.
output
=
weight_tensor
-
(
self
.
learning_rate
*
gradient_tensor
)
return
np
.
copy
(
self
.
output
)
class
SgdWithMomentum
:
learning_rate
=
None
momentum_rate
=
None
def
__init__
(
self
,
learning_rate
,
momentum_rate
):
self
.
learning_rate
=
learning_rate
self
.
momentum_rate
=
momentum_rate
self
.
prev_momentum
=
0
def
calculate_update
(
self
,
weight_tensor
,
gradient_tensor
):
momentum
=
(
self
.
momentum_rate
*
self
.
prev_momentum
)
-
(
self
.
learning_rate
*
gradient_tensor
)
output
=
weight_tensor
+
momentum
self
.
prev_momentum
=
np
.
copy
(
momentum
)
return
np
.
copy
(
output
)
class
Adam
:
k
=
1
def
__init__
(
self
,
learning_rate
,
mu
,
rho
):
self
.
learning_rate
=
learning_rate
self
.
momentum_rate
=
mu
self
.
prev_momentum
=
0
self
.
prev_rho
=
0
self
.
rho
=
rho
def
calculate_update
(
self
,
weight_tensor
,
gradient_tensor
):
g_curr
=
self
.
learning_rate
*
gradient_tensor
momentum
=
(
self
.
momentum_rate
*
self
.
prev_momentum
)
+
(
1
-
self
.
momentum_rate
)
*
(
self
.
learning_rate
*
gradient_tensor
)
r_curr
=
self
.
rho
*
self
.
prev_rho
+
(
1
-
self
.
rho
)
*
(
g_curr
**
2
)
bias_curr_mom
=
momentum
/
(
1
-
self
.
momentum_rate
**
self
.
k
)
bias_curr_rho
=
r_curr
/
(
1
-
self
.
rho
**
self
.
k
)
self
.
prev_rho
=
np
.
copy
(
r_curr
)
self
.
prev_momentum
=
np
.
copy
(
momentum
)
output
=
weight_tensor
-
self
.
learning_rate
*
(
bias_curr_mom
/
(
np
.
sqrt
(
bias_curr_rho
)
+
np
.
finfo
(
float
).
eps
))
self
.
k
+=
1
return
np
.
copy
(
output
)
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