Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Use new Instance.duplicate() method instead of deepcopy #64

Merged
merged 4 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Nothing yet
- Replaced `deepcopy` of `Instance`s with new `Instance.duplicate()` method.

### Added

Expand Down
5 changes: 2 additions & 3 deletions allennlp_models/coref/predictors/coref.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from typing import List, Dict

from overrides import overrides
Expand Down Expand Up @@ -96,7 +95,7 @@ def predictions_to_labeled_instances(
span_field: ListField = instance["spans"] # type: ignore
instances = []
for cluster in predicted_clusters:
new_instance = deepcopy(instance)
new_instance = instance.duplicate()
span_labels = [
0 if (span.span_start, span.span_end) in cluster else -1 # type: ignore
for span in span_field
Expand All @@ -108,7 +107,7 @@ def predictions_to_labeled_instances(
instances.append(new_instance)
if not instances:
# No predicted clusters; we just give an empty coref prediction.
new_instance = deepcopy(instance)
new_instance = instance.duplicate()
span_labels = [-1] * len(span_field) # type: ignore
new_instance.add_field(
"span_labels", SequenceLabelField(span_labels, span_field), self._model.vocab
Expand Down
3 changes: 1 addition & 2 deletions allennlp_models/lm/predictors/masked_language_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from typing import Dict

from overrides import overrides
Expand All @@ -19,7 +18,7 @@ def predict(self, sentence_with_masks: str) -> JsonDict:
def predictions_to_labeled_instances(
self, instance: Instance, outputs: Dict[str, numpy.ndarray]
):
new_instance = deepcopy(instance)
new_instance = instance.duplicate()
token_field: TextField = instance["tokens"] # type: ignore
mask_targets = [Token(target_top_k[0]) for target_top_k in outputs["words"]]

Expand Down
3 changes: 1 addition & 2 deletions allennlp_models/lm/predictors/next_token_lm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from typing import Dict

from overrides import overrides
Expand All @@ -19,7 +18,7 @@ def predict(self, sentence: str) -> JsonDict:
def predictions_to_labeled_instances(
self, instance: Instance, outputs: Dict[str, numpy.ndarray]
):
new_instance = deepcopy(instance)
new_instance = instance.duplicate()
token_field: TextField = instance["tokens"] # type: ignore
mask_targets = [Token(target_top_k[0]) for target_top_k in outputs["words"]]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List, Dict
from copy import deepcopy

import numpy
from overrides import overrides
Expand Down Expand Up @@ -51,7 +50,7 @@ def _json_to_instance(self, json_dict: JsonDict) -> Instance:
def predictions_to_labeled_instances(
self, instance: Instance, outputs: Dict[str, numpy.ndarray]
) -> List[Instance]:
new_instance = deepcopy(instance)
new_instance = instance.duplicate()
label = numpy.argmax(outputs["label_logits"])
# Skip indexing, we have integer representations of the strings "entailment", etc.
new_instance.add_field("label", LabelField(int(label), skip_indexing=True))
Expand Down
3 changes: 1 addition & 2 deletions allennlp_models/rc/predictors/bidaf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from typing import Dict, List

from overrides import overrides
Expand Down Expand Up @@ -57,7 +56,7 @@ def _json_to_instance(self, json_dict: JsonDict) -> Instance:
def predictions_to_labeled_instances(
self, instance: Instance, outputs: Dict[str, numpy.ndarray]
) -> List[Instance]:
new_instance = deepcopy(instance)
new_instance = instance.duplicate()
# For BiDAF
if "best_span" in outputs:
span_start_label = outputs["best_span"][0]
Expand Down