Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kaggle Phytoplankton
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
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
Messaoudi Rayan
Kaggle Phytoplankton
Commits
ac79a9d9
Commit
ac79a9d9
authored
2 years ago
by
Yandi
Browse files
Options
Downloads
Patches
Plain Diff
pushin my_train script
parent
ac929639
Branches
master
Branches containing commit
No related tags found
1 merge request
!1
Master into main
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
my_train.py
+66
-0
66 additions, 0 deletions
my_train.py
with
66 additions
and
0 deletions
my_train.py
0 → 100644
+
66
−
0
View file @
ac79a9d9
from
tqdm
import
tqdm
import
matplotlib.pyplot
as
plt
import
numpy
as
np
import
torch
import
wandb
def
train
(
args
,
model
,
loader
,
f_loss
,
optimizer
,
device
,
log_interval
=
100
):
"""
Train a model for one epoch, iterating over the loader
using the f_loss to compute the loss and the optimizer
to update the parameters of the model.
Arguments :
model -- A torch.nn.Module object
loader -- A torch.utils.data.DataLoader
f_loss -- The loss function, i.e. a loss Module
optimizer -- A torch.optim.Optimzer object
device -- a torch.device class specifying the device
used for computation
Returns :
"""
model
.
train
()
gradients
=
[]
out
=
[]
tar
=
[]
for
batch_idx
,
(
inputs
,
targets
)
in
tqdm
(
enumerate
(
loader
),
total
=
len
(
loader
)):
inputs
,
targets
=
inputs
.
to
(
device
),
targets
.
to
(
device
)
# Compute the forward pass through the network up to the loss
# target's shape is (B, Num_days, 1)
outputs
=
model
(
inputs
)
loss
=
f_loss
(
outputs
,
targets
)
# Backward and optimize
optimizer
.
zero_grad
()
loss
.
backward
()
#torch.nn.utils.clip_grad_norm(model.parameters(), 50)
Y
=
list
(
model
.
parameters
())[
0
].
grad
.
cpu
().
tolist
()
if
not
args
.
no_wandb
:
if
batch_idx
%
log_interval
==
0
:
wandb
.
log
({
"
train_loss
"
:
loss
})
optimizer
.
step
()
def
visualize_gradients
(
gradients
):
print
(
gradients
)
import
numpy
as
np
X
=
np
.
linspace
(
0
,
len
(
gradients
),
len
(
gradients
))
plt
.
scatter
(
X
,
gradients
)
plt
.
show
()
if
__name__
==
"
__main__
"
:
import
numpy
as
np
Y
=
[[
1
,
2
,
3
],[
2
,
4
,
8
],[
2
,
5
,
6
],
[
8
,
9
,
10
]]
X
=
np
.
linspace
(
0
,
len
(
Y
),
len
(
Y
))
for
i
,
curve
in
enumerate
(
Y
):
for
point
in
curve
:
plt
.
scatter
(
X
[
i
],
point
)
plt
.
show
()
\ No newline at end of file
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