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
fa669936
There was an error fetching the commit references. Please try again later.
Commit
fa669936
authored
2 years ago
by
Maja Jabłońska
Browse files
Options
Downloads
Patches
Plain Diff
Add MetadataField
parent
f4eea1c8
1 merge request
!46
Merge COMBO 3.0 into master
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
combo/data/fields/metadata_field.py
+69
-0
69 additions, 0 deletions
combo/data/fields/metadata_field.py
with
69 additions
and
0 deletions
combo/data/fields/metadata_field.py
0 → 100644
+
69
−
0
View file @
fa669936
"""
Adapted from AllenNLP
https://github.com/allenai/allennlp/blob/main/allennlp/data/fields/metadata_field.py
"""
from
typing
import
Any
,
Dict
,
List
,
Mapping
from
combo.data.fields.field
import
DataArray
,
Field
class
MetadataField
(
Field
[
DataArray
],
Mapping
[
str
,
Any
]):
"""
A `MetadataField` is a `Field` that does not get converted into tensors. It just carries
side information that might be needed later on, for computing some third-party metric, or
outputting debugging information, or whatever else you need. We use this in the BiDAF model,
for instance, to keep track of question IDs and passage token offsets, so we can more easily
use the official evaluation script to compute metrics.
We don
'
t try to do any kind of smart combination of this field for batched input - when you use
this `Field` in a model, you
'
ll get a list of metadata objects, one for each instance in the
batch.
# Parameters
metadata : `Any`
Some object containing the metadata that you want to store. It
'
s likely that you
'
ll want
this to be a dictionary, but it could be anything you want.
"""
__slots__
=
[
"
metadata
"
]
def
__init__
(
self
,
metadata
:
Any
)
->
None
:
self
.
metadata
=
metadata
def
__getitem__
(
self
,
key
:
str
)
->
Any
:
try
:
return
self
.
metadata
[
key
]
# type: ignore
except
TypeError
:
raise
TypeError
(
"
your metadata is not a dict
"
)
def
__iter__
(
self
):
try
:
return
iter
(
self
.
metadata
)
except
TypeError
:
raise
TypeError
(
"
your metadata is not iterable
"
)
def
__len__
(
self
):
try
:
return
len
(
self
.
metadata
)
except
TypeError
:
raise
TypeError
(
"
your metadata has no length
"
)
def
get_padding_lengths
(
self
)
->
Dict
[
str
,
int
]:
return
{}
def
as_tensor
(
self
,
padding_lengths
:
Dict
[
str
,
int
])
->
DataArray
:
return
self
.
metadata
# type: ignore
def
empty_field
(
self
)
->
"
MetadataField
"
:
return
MetadataField
(
None
)
def
batch_tensors
(
self
,
tensor_list
:
List
[
DataArray
])
->
List
[
DataArray
]:
# type: ignore
return
tensor_list
def
__str__
(
self
)
->
str
:
return
"
MetadataField (print field.metadata to see specific information).
"
def
human_readable_repr
(
self
):
if
hasattr
(
self
.
metadata
,
"
human_readable_repr
"
):
return
self
.
metadata
.
human_readable_repr
()
return
self
.
metadata
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