Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pseudo_image
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
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
Léo Schneider
pseudo_image
Commits
28d81131
Commit
28d81131
authored
2 months ago
by
Schneider Leo
Browse files
Options
Downloads
Patches
Plain Diff
add : lars optimizer and weight scheduling according to barlow twin
parent
2886a781
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
barlow_twin_like/main.py
+66
-2
66 additions, 2 deletions
barlow_twin_like/main.py
with
66 additions
and
2 deletions
barlow_twin_like/main.py
+
66
−
2
View file @
28d81131
import
math
import
os
import
os
import
seaborn
as
sn
import
seaborn
as
sn
import
numpy
as
np
import
numpy
as
np
...
@@ -12,6 +13,65 @@ from model import BarlowTwins, BaseClassifier
...
@@ -12,6 +13,65 @@ from model import BarlowTwins, BaseClassifier
from
dataset_barlow
import
load_data_duo
from
dataset_barlow
import
load_data_duo
from
config
import
load_args_barlow
from
config
import
load_args_barlow
class
LARS
(
optim
.
Optimizer
):
def
__init__
(
self
,
params
,
lr
,
weight_decay
=
0
,
momentum
=
0.9
,
eta
=
0.001
,
weight_decay_filter
=
False
,
lars_adaptation_filter
=
False
):
defaults
=
dict
(
lr
=
lr
,
weight_decay
=
weight_decay
,
momentum
=
momentum
,
eta
=
eta
,
weight_decay_filter
=
weight_decay_filter
,
lars_adaptation_filter
=
lars_adaptation_filter
)
super
().
__init__
(
params
,
defaults
)
def
exclude_bias_and_norm
(
self
,
p
):
return
p
.
ndim
==
1
@torch.no_grad
()
def
step
(
self
):
for
g
in
self
.
param_groups
:
for
p
in
g
[
'
params
'
]:
dp
=
p
.
grad
if
dp
is
None
:
continue
if
not
g
[
'
weight_decay_filter
'
]
or
not
self
.
exclude_bias_and_norm
(
p
):
dp
=
dp
.
add
(
p
,
alpha
=
g
[
'
weight_decay
'
])
if
not
g
[
'
lars_adaptation_filter
'
]
or
not
self
.
exclude_bias_and_norm
(
p
):
param_norm
=
torch
.
norm
(
p
)
update_norm
=
torch
.
norm
(
dp
)
one
=
torch
.
ones_like
(
param_norm
)
q
=
torch
.
where
(
param_norm
>
0.
,
torch
.
where
(
update_norm
>
0
,
(
g
[
'
eta
'
]
*
param_norm
/
update_norm
),
one
),
one
)
dp
=
dp
.
mul
(
q
)
param_state
=
self
.
state
[
p
]
if
'
mu
'
not
in
param_state
:
param_state
[
'
mu
'
]
=
torch
.
zeros_like
(
p
)
mu
=
param_state
[
'
mu
'
]
mu
.
mul_
(
g
[
'
momentum
'
]).
add_
(
dp
)
p
.
add_
(
mu
,
alpha
=-
g
[
'
lr
'
])
def
adjust_learning_rate
(
args
,
optimizer
,
loader
,
step
):
max_steps
=
args
.
epochs
*
len
(
loader
)
warmup_steps
=
10
*
len
(
loader
)
base_lr
=
args
.
batch_size
/
256
if
step
<
warmup_steps
:
lr
=
base_lr
*
step
/
warmup_steps
else
:
step
-=
warmup_steps
max_steps
-=
warmup_steps
q
=
0.5
*
(
1
+
math
.
cos
(
math
.
pi
*
step
/
max_steps
))
end_lr
=
base_lr
*
0.001
lr
=
base_lr
*
q
+
end_lr
*
(
1
-
q
)
optimizer
.
param_groups
[
0
][
'
lr
'
]
=
lr
*
args
.
learning_rate_weights
optimizer
.
param_groups
[
1
][
'
lr
'
]
=
lr
*
args
.
learning_rate_biases
def
save_model
(
model
,
path
):
def
save_model
(
model
,
path
):
print
(
'
Model saved
'
)
print
(
'
Model saved
'
)
torch
.
save
(
model
.
state_dict
(),
path
)
torch
.
save
(
model
.
state_dict
(),
path
)
...
@@ -212,11 +272,15 @@ def run():
...
@@ -212,11 +272,15 @@ def run():
if
args
.
opti
==
'
adam
'
:
if
args
.
opti
==
'
adam
'
:
optimizer
=
optim
.
Adam
(
model
.
parameters
(),
lr
=
args
.
lr
)
optimizer
=
optim
.
Adam
(
model
.
parameters
(),
lr
=
args
.
lr
)
else
:
elif
args
.
opti
==
'
LARS
'
:
optimizer
=
optim
.
SGD
(
model
.
parameters
(),
lr
=
args
.
lr
,
momentum
=
0.9
)
optimizer
=
LARS
(
model
.
parameters
(),
lr
=
0
)
else
:
optimizer
=
optim
.
SGD
(
model
.
parameters
(),
lr
=
args
.
lr
)
best_loss
=
np
.
inf
best_loss
=
np
.
inf
for
e
in
range
(
args
.
epoches
):
for
e
in
range
(
args
.
epoches
):
if
args
.
opti
==
'
LARS
'
:
adjust_learning_rate
(
args
,
optimizer
,
data_train
,
e
)
_
=
train_representation
(
model
,
data_train
,
optimizer
,
e
,
args
.
wandb
)
_
=
train_representation
(
model
,
data_train
,
optimizer
,
e
,
args
.
wandb
)
if
e
%
args
.
eval_inter
==
0
:
if
e
%
args
.
eval_inter
==
0
:
loss
=
test_representation
(
model
,
data_val
,
e
,
args
.
wandb
)
loss
=
test_representation
(
model
,
data_val
,
e
,
args
.
wandb
)
...
...
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