Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
combo
Manage
Activity
Members
Labels
Plan
Issues
20
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Syntactic Tools
combo
Commits
23e0c9ce
Commit
23e0c9ce
authored
4 years ago
by
Mateusz Klimaszewski
Committed by
Mateusz Klimaszewski
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Sort deps when uncollapsing nodes, mask root label possibility when root isn't head of a token.
parent
d0dc576f
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!9
Enhanced dependency parsing develop to master
,
!8
Enhanced dependency parsing
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
combo/models/parser.py
+23
-2
23 additions, 2 deletions
combo/models/parser.py
combo/utils/graph.py
+2
-1
2 additions, 1 deletion
combo/utils/graph.py
with
25 additions
and
3 deletions
combo/models/parser.py
+
23
−
2
View file @
23e0c9ce
"""
Dependency parsing models.
"""
import
math
from
typing
import
Tuple
,
Dict
,
Optional
,
Union
,
List
import
numpy
as
np
...
...
@@ -115,11 +116,13 @@ class DependencyRelationModel(base.Predictor):
"""
Dependency relation parsing model.
"""
def
__init__
(
self
,
root_idx
:
int
,
head_predictor
:
HeadPredictionModel
,
head_projection_layer
:
base
.
Linear
,
dependency_projection_layer
:
base
.
Linear
,
relation_prediction_layer
:
base
.
Linear
):
super
().
__init__
()
self
.
root_idx
=
root_idx
self
.
head_predictor
=
head_predictor
self
.
head_projection_layer
=
head_projection_layer
self
.
dependency_projection_layer
=
dependency_projection_layer
...
...
@@ -130,6 +133,7 @@ class DependencyRelationModel(base.Predictor):
mask
:
Optional
[
torch
.
BoolTensor
]
=
None
,
labels
:
Optional
[
Union
[
torch
.
Tensor
,
List
[
torch
.
Tensor
]]]
=
None
,
sample_weights
:
Optional
[
Union
[
torch
.
Tensor
,
List
[
torch
.
Tensor
]]]
=
None
)
->
Dict
[
str
,
torch
.
Tensor
]:
device
=
x
.
device
if
mask
is
not
None
:
mask
=
mask
[:,
1
:]
relations_labels
,
head_labels
=
None
,
None
...
...
@@ -151,7 +155,23 @@ class DependencyRelationModel(base.Predictor):
relation_prediction
=
self
.
relation_prediction_layer
(
dep_rel_pred
)
output
=
head_output
output
[
"
prediction
"
]
=
(
relation_prediction
.
argmax
(
-
1
)[:,
1
:],
head_output
[
"
prediction
"
])
if
self
.
training
:
output
[
"
prediction
"
]
=
(
relation_prediction
.
argmax
(
-
1
)[:,
1
:],
head_output
[
"
prediction
"
])
else
:
# Mask root label whenever head is not 0.
relation_prediction_output
=
relation_prediction
[:,
1
:]
mask
=
(
head_output
[
"
prediction
"
]
==
0
)
vocab_size
=
relation_prediction_output
.
size
(
-
1
)
root_idx
=
torch
.
tensor
([
self
.
root_idx
],
device
=
device
)
relation_prediction_output
[
mask
]
=
(
relation_prediction_output
.
masked_select
(
mask
.
unsqueeze
(
-
1
))
.
reshape
(
-
1
,
vocab_size
)
.
index_fill
(
-
1
,
root_idx
,
10e10
))
relation_prediction_output
[
~
mask
]
=
(
relation_prediction_output
.
masked_select
(
~
(
mask
.
unsqueeze
(
-
1
)))
.
reshape
(
-
1
,
vocab_size
)
.
index_fill
(
-
1
,
root_idx
,
-
10e10
))
output
[
"
prediction
"
]
=
(
relation_prediction_output
.
argmax
(
-
1
),
head_output
[
"
prediction
"
])
if
labels
is
not
None
and
labels
[
0
]
is
not
None
:
if
sample_weights
is
None
:
...
...
@@ -195,5 +215,6 @@ class DependencyRelationModel(base.Predictor):
head_predictor
=
head_predictor
,
head_projection_layer
=
head_projection_layer
,
dependency_projection_layer
=
dependency_projection_layer
,
relation_prediction_layer
=
relation_prediction_layer
relation_prediction_layer
=
relation_prediction_layer
,
root_idx
=
vocab
.
get_token_index
(
"
root
"
,
vocab_namespace
)
)
This diff is collapsed.
Click to expand it.
combo/utils/graph.py
+
2
−
1
View file @
23e0c9ce
...
...
@@ -110,5 +110,6 @@ def restore_collapse_edges(tree_tokens):
"
deps
"
:
f
"
{
head
}
:
{
empty_node_relation
}
"
}
)
token
[
"
deps
"
]
=
"
|
"
.
join
(
deps
)
deps
=
sorted
([
d
.
split
(
"
:
"
,
1
)
for
d
in
deps
],
key
=
lambda
x
:
float
(
x
[
0
]))
token
[
"
deps
"
]
=
"
|
"
.
join
([
f
"
{
k
}
:
{
v
}
"
for
k
,
v
in
deps
])
return
empty_tokens
This diff is collapsed.
Click to expand it.
Preview
0%
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