Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pyPID
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
Devashish Lohani
pyPID
Commits
186b21ea
Commit
186b21ea
authored
2 years ago
by
Devashish Lohani
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
279fbb46
No related branches found
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
models/loss.py
+30
-0
30 additions, 0 deletions
models/loss.py
with
30 additions
and
0 deletions
models/loss.py
0 → 100644
+
30
−
0
View file @
186b21ea
import
torch
def
get_memory_loss
(
memory_att
):
"""
The memory attribute should be with size [batch_size, memory_dim, reduced_time_dim, f_h, f_w]
loss = \sum_{t=1}^{reduced_time_dim} (-mem) * (mem + 1e-12).log()
averaged on each pixel and each batch
2. average over batch_size * fh * fw
"""
s
=
memory_att
.
shape
memory_att
=
(
-
memory_att
)
*
(
memory_att
+
1e-12
).
log
()
# [batch_size, memory_dim, time, fh, fw]
memory_att
=
memory_att
.
sum
()
/
(
s
[
0
]
*
s
[
-
2
]
*
s
[
-
1
])
return
memory_att
def
get_unormalized_data
(
x_input
,
x_recons
,
mean
,
std
):
x_input
=
x_input
.
mul
(
std
).
add
(
mean
)
x_recons
=
x_recons
.
mul
(
std
).
add
(
mean
)
return
x_input
,
x_recons
def
get_reconstruction_loss
(
x_input
,
x_recons
,
mean
=
0.5
,
std
=
0.5
):
"""
Calculates the reconstruction loss between x_input and x_recons
x_input: [batch_size, ch, time, imh, imw]
x_recons: [batch_size, ch, time, imh, imw]
"""
batch_size
,
ch
,
time_dimension
,
imh
,
imw
=
x_input
.
shape
x_input
,
x_recons
=
get_unormalized_data
(
x_input
,
x_recons
,
mean
,
std
)
recons_loss
=
(
x_input
-
x_recons
)
**
2
recons_loss
=
recons_loss
.
sum
().
sqrt
()
/
(
batch_size
*
imh
*
imw
)
return
recons_loss
\ 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