Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
individual_project
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
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Joel Oksanen
individual_project
Commits
c7ab3edc
Commit
c7ab3edc
authored
4 years ago
by
Joel Oksanen
Browse files
Options
Downloads
Patches
Plain Diff
Acquire and plot attention values for targets
parent
05c24955
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ADA/SA/bert_analyzer.py
+16
-2
16 additions, 2 deletions
ADA/SA/bert_analyzer.py
ADA/SA/bert_dataset.py
+2
-5
2 additions, 5 deletions
ADA/SA/bert_dataset.py
with
18 additions
and
7 deletions
ADA/SA/bert_analyzer.py
+
16
−
2
View file @
c7ab3edc
...
@@ -7,6 +7,7 @@ from bert_dataset import BertDataset, Instance, polarity_indices, generate_batch
...
@@ -7,6 +7,7 @@ from bert_dataset import BertDataset, Instance, polarity_indices, generate_batch
import
time
import
time
import
numpy
as
np
import
numpy
as
np
from
sklearn
import
metrics
from
sklearn
import
metrics
import
matplotlib.pyplot
as
plt
semeval_2014_train_path
=
'
data/SemEval-2014/Laptop_Train_v2.xml
'
semeval_2014_train_path
=
'
data/SemEval-2014/Laptop_Train_v2.xml
'
semeval_2014_test_path
=
'
data/SemEval-2014/Laptops_Test_Gold.xml
'
semeval_2014_test_path
=
'
data/SemEval-2014/Laptops_Test_Gold.xml
'
...
@@ -93,13 +94,25 @@ class BertAnalyzer:
...
@@ -93,13 +94,25 @@ class BertAnalyzer:
def
analyze_sentence
(
self
,
text
,
char_from
,
char_to
):
def
analyze_sentence
(
self
,
text
,
char_from
,
char_to
):
instance
=
Instance
(
text
,
char_from
,
char_to
)
instance
=
Instance
(
text
,
char_from
,
char_to
)
_
,
tg_from
,
tg_to
=
instance
.
get
()
tokens
,
tg_from
,
tg_to
=
instance
.
get
()
texts
,
target_indices
=
instance
.
to_tensor
()
texts
,
target_indices
=
instance
.
to_tensor
()
with
torch
.
no_grad
():
with
torch
.
no_grad
():
outputs
,
attentions
=
self
.
net
(
texts
,
target_indices
)
outputs
,
attentions
=
self
.
net
(
texts
,
target_indices
)
target_attentions
=
torch
.
mean
(
attentions
,
1
)[
0
][
tg_from
+
1
:
tg_to
+
2
]
target_attentions
=
torch
.
mean
(
attentions
,
1
)[
0
][
tg_from
+
1
:
tg_to
+
2
]
mean_target_att
=
torch
.
mean
(
target_attentions
,
0
)
# plot attention histogram
att_values
=
mean_target_att
.
numpy
()[
1
:
-
1
]
ax
=
plt
.
subplot
(
111
)
width
=
0.3
bins
=
[
x
-
width
/
2
for
x
in
range
(
1
,
len
(
att_values
)
+
1
)]
ax
.
bar
(
bins
,
att_values
,
width
=
width
)
ax
.
set_xticks
(
list
(
range
(
1
,
len
(
att_values
)
+
1
)))
ax
.
set_xticklabels
(
tokens
,
rotation
=
45
,
rotation_mode
=
'
anchor
'
,
ha
=
'
right
'
)
plt
.
show
()
_
,
pred
=
torch
.
max
(
outputs
.
data
,
1
)
_
,
pred
=
torch
.
max
(
outputs
.
data
,
1
)
return
pred
return
pred
...
@@ -107,4 +120,5 @@ class BertAnalyzer:
...
@@ -107,4 +120,5 @@ class BertAnalyzer:
sentiment_analyzer
=
BertAnalyzer
()
sentiment_analyzer
=
BertAnalyzer
()
sentiment_analyzer
.
load_saved
()
sentiment_analyzer
.
load_saved
()
sentiment
=
sentiment_analyzer
.
analyze_sentence
(
'
I hate this laptop
'
,
12
,
18
)
sentiment
=
sentiment_analyzer
.
analyze_sentence
(
'
I will never buy another computer from HP/Compaq or do business with Circuit City again.
'
,
39
,
48
)
print
(
'
sentiment:
'
,
sentiment
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
ADA/SA/bert_dataset.py
+
2
−
5
View file @
c7ab3edc
...
@@ -16,7 +16,6 @@ def generate_batch(batch):
...
@@ -16,7 +16,6 @@ def generate_batch(batch):
return_tensors
=
'
pt
'
)
return_tensors
=
'
pt
'
)
max_tg_len
=
max
(
entry
[
'
to
'
]
-
entry
[
'
from
'
]
for
entry
in
batch
)
max_tg_len
=
max
(
entry
[
'
to
'
]
-
entry
[
'
from
'
]
for
entry
in
batch
)
print
(
max_tg_len
)
target_indices
=
torch
.
tensor
([[[
min
(
t
,
entry
[
'
to
'
])]
*
HIDDEN_OUTPUT_FEATURES
target_indices
=
torch
.
tensor
([[[
min
(
t
,
entry
[
'
to
'
])]
*
HIDDEN_OUTPUT_FEATURES
for
t
in
range
(
entry
[
'
from
'
],
entry
[
'
from
'
]
+
max_tg_len
+
1
)]
for
t
in
range
(
entry
[
'
from
'
],
entry
[
'
from
'
]
+
max_tg_len
+
1
)]
for
entry
in
batch
])
for
entry
in
batch
])
...
@@ -27,13 +26,11 @@ def generate_batch(batch):
...
@@ -27,13 +26,11 @@ def generate_batch(batch):
def
token_for_char
(
char_idx
,
text
,
tokens
):
def
token_for_char
(
char_idx
,
text
,
tokens
):
compressed_idx
=
len
(
re
.
sub
(
r
'
\s+
'
,
''
,
text
)[:
char_idx
+
1
])
-
1
compressed_idx
=
len
(
re
.
sub
(
r
'
\s+
'
,
''
,
text
[:
char_idx
+
1
]))
-
1
token_idx
=
-
1
token_idx
=
-
1
while
compressed_idx
>=
0
:
while
compressed_idx
>=
0
:
token_idx
+=
1
token_idx
+=
1
compressed_idx
-=
len
(
tokens
[
token_idx
].
replace
(
'
##
'
,
''
))
compressed_idx
-=
len
(
tokens
[
token_idx
].
replace
(
'
##
'
,
''
))
return
token_idx
return
token_idx
...
@@ -80,7 +77,7 @@ class Instance:
...
@@ -80,7 +77,7 @@ class Instance:
def
get
(
self
):
def
get
(
self
):
tokens
=
tokenizer
.
tokenize
(
self
.
text
)
tokens
=
tokenizer
.
tokenize
(
self
.
text
)
idx_from
=
token_for_char
(
self
.
char_from
,
self
.
text
,
tokens
)
idx_from
=
token_for_char
(
self
.
char_from
,
self
.
text
,
tokens
)
idx_to
=
token_for_char
(
self
.
char_to
,
self
.
text
,
tokens
)
idx_to
=
token_for_char
(
self
.
char_to
-
1
,
self
.
text
,
tokens
)
return
tokens
,
idx_from
,
idx_to
return
tokens
,
idx_from
,
idx_to
def
to_tensor
(
self
):
def
to_tensor
(
self
):
...
...
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