Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.learn.tensorflow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
bob
bob.learn.tensorflow
Commits
01ac7f66
Commit
01ac7f66
authored
6 years ago
by
Tiago de Freitas Pereira
Browse files
Options
Downloads
Patches
Plain Diff
Added tests for the new losses
parent
89ea404b
No related branches found
No related tags found
1 merge request
!75
A lot of new features
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bob/learn/tensorflow/loss/utils.py
+6
-2
6 additions, 2 deletions
bob/learn/tensorflow/loss/utils.py
bob/learn/tensorflow/test/test_loss.py
+76
-0
76 additions, 0 deletions
bob/learn/tensorflow/test/test_loss.py
with
82 additions
and
2 deletions
bob/learn/tensorflow/loss/utils.py
+
6
−
2
View file @
01ac7f66
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Amir Mohammadi <amir.mohammadi@idiap.ch>
import
tensorflow
as
tf
def
balanced_softmax_cross_entropy_loss_weights
(
labels
,
dtype
):
def
balanced_softmax_cross_entropy_loss_weights
(
labels
,
dtype
=
"
float32
"
):
"""
Computes weights that normalizes your loss per class.
Labels must be a batch of one-hot encoded labels. The function takes labels and
...
...
@@ -82,7 +86,7 @@ def balanced_softmax_cross_entropy_loss_weights(labels, dtype):
return
weights
def
balanced_sigmoid_cross_entropy_loss_weights
(
labels
,
dtype
):
def
balanced_sigmoid_cross_entropy_loss_weights
(
labels
,
dtype
=
"
float32
"
):
"""
Computes weights that normalizes your loss per class.
Labels must be a batch of binary labels. The function takes labels and
...
...
This diff is collapsed.
Click to expand it.
bob/learn/tensorflow/test/test_loss.py
0 → 100644
+
76
−
0
View file @
01ac7f66
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
import
tensorflow
as
tf
import
numpy
from
bob.learn.tensorflow.loss
import
balanced_softmax_cross_entropy_loss_weights
,
\
balanced_sigmoid_cross_entropy_loss_weights
def
test_balanced_softmax_cross_entropy_loss_weights
():
labels
=
numpy
.
array
([[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
0
,
0
,
1
],
[
0
,
1
,
0
],
[
0
,
0
,
1
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
0
,
0
,
1
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
0
,
1
,
0
],
[
1
,
0
,
0
],
[
0
,
1
,
0
],
[
1
,
0
,
0
],
[
0
,
0
,
1
],
[
0
,
0
,
1
],
[
1
,
0
,
0
],
[
0
,
0
,
1
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
0
,
1
,
0
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
0
,
1
,
0
],
[
1
,
0
,
0
],
[
0
,
0
,
1
],
[
1
,
0
,
0
]],
dtype
=
"
int32
"
)
with
tf
.
Session
()
as
session
:
weights
=
session
.
run
(
balanced_softmax_cross_entropy_loss_weights
(
labels
))
expected_weights
=
numpy
.
array
([
0.53333336
,
0.53333336
,
1.5238096
,
2.1333334
,
\
1.5238096
,
0.53333336
,
0.53333336
,
1.5238096
,
\
0.53333336
,
0.53333336
,
0.53333336
,
0.53333336
,
\
0.53333336
,
0.53333336
,
2.1333334
,
0.53333336
,
\
2.1333334
,
0.53333336
,
1.5238096
,
1.5238096
,
\
0.53333336
,
1.5238096
,
0.53333336
,
0.53333336
,
\
2.1333334
,
0.53333336
,
0.53333336
,
0.53333336
,
\
2.1333334
,
0.53333336
,
1.5238096
,
0.53333336
],
\
dtype
=
"
float32
"
)
assert
numpy
.
allclose
(
weights
,
expected_weights
)
def
test_balanced_sigmoid_cross_entropy_loss_weights
():
labels
=
numpy
.
array
([
1
,
1
,
0
,
0
,
0
,
1
,
1
,
0
,
1
,
1
,
1
,
1
,
1
,
1
,
0
,
1
,
0
,
1
,
0
,
0
,
1
,
0
,
1
,
1
,
0
,
1
,
1
,
1
,
0
,
1
,
0
,
1
],
dtype
=
"
int32
"
)
with
tf
.
Session
()
as
session
:
weights
=
session
.
run
(
balanced_sigmoid_cross_entropy_loss_weights
(
labels
,
dtype
=
'
float32
'
))
expected_weights
=
numpy
.
array
([
0.8
,
0.8
,
1.3333334
,
1.3333334
,
1.3333334
,
0.8
,
0.8
,
1.3333334
,
0.8
,
0.8
,
0.8
,
0.8
,
0.8
,
0.8
,
1.3333334
,
0.8
,
1.3333334
,
0.8
,
1.3333334
,
1.3333334
,
0.8
,
1.3333334
,
0.8
,
0.8
,
1.3333334
,
0.8
,
0.8
,
0.8
,
1.3333334
,
0.8
,
1.3333334
,
0.8
],
dtype
=
"
float32
"
)
assert
numpy
.
allclose
(
weights
,
expected_weights
)
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