This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
metadata_field.py
68 lines (50 loc) · 2.21 KB
/
metadata_field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import Any, Dict, List, Mapping
from overrides import overrides
from allennlp.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")
@overrides
def get_padding_lengths(self) -> Dict[str, int]:
return {}
@overrides
def as_tensor(self, padding_lengths: Dict[str, int]) -> DataArray:
return self.metadata # type: ignore
@overrides
def empty_field(self) -> "MetadataField":
return MetadataField(None)
@overrides
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)."