This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import torch
|
||||
|
||||
from data.schema import FACT_TYPE
|
||||
from eval.benchmark_extractor import hard_negative_recall_at_one, recall_at_one
|
||||
|
||||
|
||||
def test_retrieval_metrics_distinguish_hard_negative():
|
||||
labels = torch.tensor(
|
||||
[
|
||||
[FACT_TYPE, 0, 0, 4, -1, -1],
|
||||
[FACT_TYPE, 0, 0, 5, -1, -1],
|
||||
]
|
||||
)
|
||||
candidates = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
|
||||
queries = torch.tensor([[0.9, 0.1], [0.1, 0.9]])
|
||||
assert recall_at_one(queries, labels, candidates, labels) == 1.0
|
||||
assert hard_negative_recall_at_one(queries, labels, candidates, labels) == 1.0
|
||||
+8
-1
@@ -1,5 +1,5 @@
|
||||
from data.generate_rules import add_rule
|
||||
from data.generate_scenes import generate_scene
|
||||
from data.generate_scenes import HELD_OUT_SHAPES, generate_scene
|
||||
|
||||
|
||||
def test_scene_generation_is_deterministic():
|
||||
@@ -15,4 +15,11 @@ def test_held_out_rule_changes_combination():
|
||||
held_out = add_rule(scene, seed=17, held_out=True)
|
||||
assert train["combination"] != held_out["combination"]
|
||||
assert train["answer"] == held_out["answer"]
|
||||
assert len(train["text_facts"]) == 1
|
||||
assert len(train["text_rules"]) == 1
|
||||
|
||||
|
||||
def test_ood_scene_uses_held_out_visual_composition():
|
||||
scene = generate_scene(5, seed=17, held_out_composition=True)
|
||||
first = scene["objects"][0]
|
||||
assert first["shape"] == HELD_OUT_SHAPES[first["color"]]
|
||||
|
||||
+29
-1
@@ -1,6 +1,10 @@
|
||||
import torch
|
||||
|
||||
from data.schema import FACT_TYPE, RULE_TYPE
|
||||
from losses.alignment_loss import cross_modal_alignment_loss
|
||||
from losses.matching_loss import hungarian_matching_loss
|
||||
from losses.prototype_loss import canonical_prototype_loss
|
||||
from models.fact_prototype import CanonicalFactPrototype
|
||||
from models.slot_extractor import SlotExtractor
|
||||
|
||||
|
||||
@@ -15,13 +19,37 @@ def test_slot_extractor_and_matching_loss_backward():
|
||||
num_subjects=12,
|
||||
num_predicates=7,
|
||||
num_objects=12,
|
||||
num_state_types=3,
|
||||
)
|
||||
tokens = torch.randn(2, 10, 32)
|
||||
mask = torch.ones(2, 10, dtype=torch.bool)
|
||||
predictions = extractor(tokens, mask)
|
||||
targets = [torch.tensor([[0, 0, 4], [1, 1, 5]]), torch.tensor([[1, 0, 6]])]
|
||||
targets = [
|
||||
torch.tensor(
|
||||
[
|
||||
[FACT_TYPE, 0, 0, 4, -1, -1],
|
||||
[RULE_TYPE, 1, 1, 5, 6, 7],
|
||||
]
|
||||
),
|
||||
torch.tensor([[FACT_TYPE, 1, 0, 6, -1, -1]]),
|
||||
]
|
||||
result = hungarian_matching_loss(predictions, targets)
|
||||
assert torch.isfinite(result.loss)
|
||||
result.loss.backward()
|
||||
assert extractor.slot_queries.grad is not None
|
||||
|
||||
|
||||
def test_cross_modal_alignment_and_prototype_losses():
|
||||
assignments = [(torch.tensor([0]), torch.tensor([0]))]
|
||||
targets = [torch.tensor([[FACT_TYPE, 0, 0, 4, -1, -1]])]
|
||||
visual = torch.randn(1, 2, 16, requires_grad=True)
|
||||
text = torch.randn(1, 2, 16, requires_grad=True)
|
||||
alignment = cross_modal_alignment_loss(
|
||||
visual, targets, assignments, text, targets, assignments
|
||||
)
|
||||
prototype = CanonicalFactPrototype(16, 3, 12, 7, 12)
|
||||
prototype_value = canonical_prototype_loss(visual, targets, assignments, prototype)
|
||||
loss = alignment + prototype_value
|
||||
assert torch.isfinite(loss)
|
||||
loss.backward()
|
||||
assert visual.grad is not None
|
||||
|
||||
Reference in New Issue
Block a user