Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
adversarial-hatespeech
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
mai
adversarial-hatespeech
Commits
c995a9ac
Commit
c995a9ac
authored
2 years ago
by
mai
Browse files
Options
Downloads
Patches
Plain Diff
Move evaluation to utils/eval.py
parent
19a42a54
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
test.py
+14
-9
14 additions, 9 deletions
test.py
utils/attack.py
+2
-1
2 additions, 1 deletion
utils/attack.py
utils/eval.py
+58
-0
58 additions, 0 deletions
utils/eval.py
with
74 additions
and
10 deletions
test.py
+
14
−
9
View file @
c995a9ac
import
torch
from
transformers
import
AutoTokenizer
,
AutoModelForSequenceClassification
### from models.py
from
pretrained.models
import
*
import
json
from
nltk.tokenize.treebank
import
TreebankWordDetokenizer
from
utils.eval
import
eval
from
utils.attack
import
attack
device
=
'
cuda
'
if
torch
.
cuda
.
is_available
()
else
'
cpu
'
...
...
@@ -43,17 +44,21 @@ for post in dataset(test_ids):
# counter += 1
detokenized
=
TreebankWordDetokenizer
().
detokenize
(
post
[
"
post_tokens
"
])
probabilities
=
eval
(
detokenized
,
model
,
tokenizer
)
print
(
f
"
Normal:
{
probabilities
[
0
][
0
]
}
\n
Hatespeech:
{
probabilities
[
0
][
1
]
}
\n\n
"
)
# print(f"Normal: {probabilities[1][0]}\nHatespeech: {probabilities[1][1]}\n\n")
# ATTACK HERE
batch
=
attack
(
detokenized
)
inputs
=
tokenizer
(
batch
,
return_tensors
=
"
pt
"
,
padding
=
True
).
to
(
device
)
prediction_logits
,
_
=
model
(
input_ids
=
inputs
[
'
input_ids
'
],
attention_mask
=
inputs
[
'
attention_mask
'
])
softmax
=
torch
.
nn
.
Softmax
(
dim
=
1
)
probs
=
softmax
(
prediction_logits
)
print
(
f
"
Normal:
{
probs
[
0
][
0
]
}
\n
Hatespeech:
{
probs
[
0
][
1
]
}
\n\n
"
)
print
(
f
"
Normal:
{
probs
[
1
][
0
]
}
\n
Hatespeech:
{
probs
[
1
][
1
]
}
\n\n
"
)
# batch = attack(detokenized)
# inputs = tokenizer(detokenized, return_tensors="pt", padding=True).to(device)
# prediction_logits, _ = model(input_ids=inputs['input_ids'],attention_mask=inputs['attention_mask'])
# softmax = torch.nn.Softmax(dim=1)
# probs = softmax(prediction_logits)
# print(f"Normal: {probs[0][0]}\nHatespeech: {probs[0][1]}\n\n")
# print(f"Normal: {probs[1][0]}\nHatespeech: {probs[1][1]}\n\n")
#
break
# print("------------------")
...
...
This diff is collapsed.
Click to expand it.
utils/attack.py
+
2
−
1
View file @
c995a9ac
import
transformers
def
attack
(
sentence
,
model
):
def
attack
(
sentence
,
model
,
tokenizer
):
model
=
model
.
to
(
device
)
This diff is collapsed.
Click to expand it.
utils/eval.py
0 → 100644
+
58
−
0
View file @
c995a9ac
from
typing
import
Union
import
torch
# from transformers import AutoTokenizer, AutoModelForSequenceClassification
# from pretrained.models import *
#
# device = "cuda" if torch.cuda.is_available() else "cpu"
#
# tokenizer = AutoTokenizer.from_pretrained(
# "Hate-speech-CNERG/bert-base-uncased-hatexplain-rationale-two"
# )
# model = Model_Rational_Label.from_pretrained(
# "Hate-speech-CNERG/bert-base-uncased-hatexplain-rationale-two"
# )
# model = model.to(device)
def
eval
(
text
,
model
,
tokenizer
):
"""
Get model
'
s prediction on a text.
Parameters
----------
text : Union[str, list]
Text to be classified. Either a single string or a list of strings
model : transformers.AutoModelForSequenceClassification
Trained HateXplain model
tokenizer : transformers.AutoTokenizer
Tokenizer from trained HateXplain model
Returns
-------
probabilities : torch.Tensor
If text is only one string, then get probabilities with
`probabilities[0][0]` for `normal` and
`probabilities[0][1]` for `hatespeech`.
If text is multiple strings in a list, then get probabilities with
`probabilities[i][0]` and `probabilities[i][1]`, respectively, where
`i` is the sample in the batch.
"""
device
=
'
cuda
'
if
torch
.
cuda
.
is_available
()
else
'
cpu
'
model
=
model
.
to
(
device
)
inputs
=
tokenizer
(
detokenized
,
return_tensors
=
"
pt
"
,
padding
=
True
).
to
(
device
)
prediction_logits
,
_
=
model
(
input_ids
=
inputs
[
'
input_ids
'
],
attention_mask
=
inputs
[
'
attention_mask
'
]
)
softmax
=
torch
.
nn
.
Softmax
(
dim
=
1
)
probabilities
=
softmax
(
prediction_logits
)
# print(f"Normal: {probabilities[0][0]}\nHatespeech: {probabilities[0][1]}\n\n")
return
probabilities
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