docre¶
knowledge_graph.news.docre
Load ATLOP/DREEAM checkpoints and predict relations from DocRED inputs.
build_model(implementation: str, tokenizer, classes: int, *, checkpoint: Path | None = None)
¶
Initialize from RoBERTa for training, or restore a complete relation checkpoint on CPU.
Source code in src/knowledge_graph/news/docre.py
22 23 24 25 26 27 28 29 30 31 32 33 | |
load_checkpoint(model, checkpoint: Path, device: torch.device)
¶
Load FP32 weights; inference uses the same BF16 autocast as validation.
Source code in src/knowledge_graph/news/docre.py
36 37 38 39 40 | |
model_inputs(batch, device: torch.device) -> dict
¶
Move encoder tensors to the model device and retain entity and pair offsets.
Source code in src/knowledge_graph/news/docre.py
43 44 45 46 47 48 49 50 | |
infer(model, features, batch_size: int, *, long_input_encoder=None) -> np.ndarray
¶
Source code in src/knowledge_graph/news/docre.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
predicted_triples(predictions: np.ndarray, features: list[dict], id2rel: dict[int, str]) -> set[tuple]
¶
Source code in src/knowledge_graph/news/docre.py
70 71 72 73 74 75 76 77 78 | |
DocREPredictor
¶
Predict relations from DocRED records with a trained ATLOP or DREEAM checkpoint.
Source code in src/knowledge_graph/news/docre.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
tokenizer = AutoTokenizer.from_pretrained(ENCODER)
instance-attribute
¶
device = torch.device('cuda')
instance-attribute
¶
model = build_model(implementation, self.tokenizer, len(rel2id), checkpoint=checkpoint).to(self.device)
instance-attribute
¶
predict(inputs: list[dict]) -> set[tuple]
¶
Return (title, head, relation, tail) tuples from inputs capped at 1,024 subwords.
Source code in src/knowledge_graph/news/docre.py
91 92 93 | |
predict_with_windows(inputs: list[dict]) -> set[tuple]
¶
Predict on whole documents capped at 2,048 subwords using overlapping encoder windows.
Source code in src/knowledge_graph/news/docre.py
95 96 97 | |