Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
topic-classifier
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
Container registry
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bibliovid
topic-classifier
Commits
d275fd7d
Commit
d275fd7d
authored
5 years ago
by
Benoit Favre
Browse files
Options
Downloads
Patches
Plain Diff
fix prediction and add f-score loss
parent
7a7cbb6b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
data.py
+1
-1
1 addition, 1 deletion
data.py
model.py
+4
-3
4 additions, 3 deletions
model.py
predict.py
+1
-2
1 addition, 2 deletions
predict.py
with
6 additions
and
6 deletions
data.py
+
1
−
1
View file @
d275fd7d
...
@@ -27,7 +27,7 @@ def to_int(tokenizer, label_vocab, hparams, dataset):
...
@@ -27,7 +27,7 @@ def to_int(tokenizer, label_vocab, hparams, dataset):
for
article
in
dataset
:
for
article
in
dataset
:
text
=
'
|
'
.
join
([
''
.
join
(
article
[
feature
])
for
feature
in
hparams
.
selected_features
])
text
=
'
|
'
.
join
([
''
.
join
(
article
[
feature
])
for
feature
in
hparams
.
selected_features
])
int_texts
.
append
(
bert_text_to_ids
(
tokenizer
,
text
,
hparams
.
max_len
))
int_texts
.
append
(
bert_text_to_ids
(
tokenizer
,
text
,
hparams
.
max_len
))
int_labels
.
append
([
1
if
label
in
'
topics
'
in
article
and
article
[
'
topics
'
]
else
0
for
label
in
sorted_labels
])
int_labels
.
append
([
1
if
'
topics
'
in
article
and
label
in
article
[
'
topics
'
]
else
0
for
label
in
sorted_labels
])
return
int_texts
,
int_labels
return
int_texts
,
int_labels
...
...
This diff is collapsed.
Click to expand it.
model.py
+
4
−
3
View file @
d275fd7d
...
@@ -12,7 +12,7 @@ from transformers import AutoModel
...
@@ -12,7 +12,7 @@ from transformers import AutoModel
import
data
import
data
# based on https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric
# based on https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric
def
f1_score_
binary
(
y_pred
,
y_true
,
epsilon
=
1e-7
):
def
binary_
f1_score_
with_logits
(
y_pred
,
y_true
,
epsilon
=
1e-7
):
y_pred
=
torch
.
sigmoid
(
y_pred
)
y_pred
=
torch
.
sigmoid
(
y_pred
)
y_true
=
y_true
.
float
()
y_true
=
y_true
.
float
()
...
@@ -25,7 +25,8 @@ def f1_score_binary(y_pred, y_true, epsilon=1e-7):
...
@@ -25,7 +25,8 @@ def f1_score_binary(y_pred, y_true, epsilon=1e-7):
recall
=
tp
/
(
tp
+
fn
+
epsilon
)
recall
=
tp
/
(
tp
+
fn
+
epsilon
)
f1
=
2
*
(
precision
*
recall
)
/
(
precision
+
recall
+
epsilon
)
f1
=
2
*
(
precision
*
recall
)
/
(
precision
+
recall
+
epsilon
)
f1
=
f1
.
clamp
(
min
=
epsilon
,
max
=
1
-
epsilon
)
#f1 = f1.clamp(min=epsilon, max=1 - epsilon)
f1
=
torch
.
where
(
torch
.
isnan
(
f1
),
torch
.
zeros_like
(
f1
),
f1
)
return
1
-
f1
.
mean
()
return
1
-
f1
.
mean
()
...
@@ -46,7 +47,7 @@ class Model(LightningModule):
...
@@ -46,7 +47,7 @@ class Model(LightningModule):
if
self
.
hparams
.
loss
==
'
bce
'
:
if
self
.
hparams
.
loss
==
'
bce
'
:
self
.
loss_function
=
F
.
binary_cross_entropy_with_logits
self
.
loss_function
=
F
.
binary_cross_entropy_with_logits
elif
self
.
hparams
.
loss
==
'
f1
'
:
elif
self
.
hparams
.
loss
==
'
f1
'
:
self
.
loss_function
=
f1_score_
binary
self
.
loss_function
=
binary_
f1_score_
with_logits
else
:
else
:
raise
ValueError
(
'
invalid loss
"
%s
"'
%
self
.
hparams
.
loss
)
raise
ValueError
(
'
invalid loss
"
%s
"'
%
self
.
hparams
.
loss
)
...
...
This diff is collapsed.
Click to expand it.
predict.py
+
1
−
2
View file @
d275fd7d
...
@@ -34,14 +34,13 @@ def main(hparams):
...
@@ -34,14 +34,13 @@ def main(hparams):
#y = y.to(device)
#y = y.to(device)
with
torch
.
no_grad
():
with
torch
.
no_grad
():
y_scores
=
model
(
x
)
y_scores
=
model
(
x
)
y_pred
=
y_scores
>
0
predictions
.
extend
(
y_scores
.
cpu
().
tolist
())
predictions
.
extend
(
y_scores
.
cpu
().
tolist
())
return
predictions
return
predictions
predictions
=
generate_predictions
(
model
,
test_loader
)
predictions
=
generate_predictions
(
model
,
test_loader
)
for
i
,
article
in
enumerate
(
dataset
):
for
i
,
article
in
enumerate
(
dataset
):
article
[
'
topic-scores
'
]
=
{
label
:
score
for
label
,
score
in
zip
(
sorted_labels
,
predictions
[
i
])}
article
[
'
topic-scores
'
]
=
{
label
:
score
for
label
,
score
in
zip
(
sorted_labels
,
predictions
[
i
])}
article
[
'
topic-pred
'
]
=
[
label
for
label
,
score
in
zip
(
sorted_labels
,
predictions
[
i
])
if
score
>
0
]
article
[
'
topic-pred
'
]
=
[
label
for
label
,
score
in
zip
(
sorted_labels
,
predictions
[
i
])
if
score
>
=
0
]
print
(
json
.
dumps
(
dataset
,
indent
=
2
))
print
(
json
.
dumps
(
dataset
,
indent
=
2
))
...
...
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