def test_pursuit_preposition_in_front_learner(language_mode, learner):
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)
    language_generator = phase1_language_generator(language_mode)
    speaker = standard_object("speaker", MOM, added_properties=[IS_SPEAKER])
    in_front_train_curriculum = phase1_instances(
        "Preposition In Front Unit Train",
        situations=sampled(
            _in_front_template(ball,
                               table, [speaker],
                               is_training=True,
                               is_near=True),
            chooser=PHASE1_CHOOSER_FACTORY(),
            ontology=GAILA_PHASE_1_ONTOLOGY,
            max_to_sample=10,
            block_multiple_of_the_same_type=True,
        ),
        language_generator=language_generator,
    )
    in_front_test_curriculum = phase1_instances(
        "Preposition In Front Unit Test",
        situations=sampled(
            _in_front_template(ball,
                               table, [speaker],
                               is_training=False,
                               is_near=True),
            chooser=PHASE1_CHOOSER_FACTORY(),
            ontology=GAILA_PHASE_1_ONTOLOGY,
            max_to_sample=1,
            block_multiple_of_the_same_type=True,
        ),
        language_generator=language_generator,
    )

    processing_learner = learner(language_mode)

    for (
            _,
            linguistic_description,
            perceptual_representation,
    ) in in_front_train_curriculum.instances():
        processing_learner.observe(
            LearningExample(perceptual_representation, linguistic_description))

    for (
            _,
            test_linguistic_description,
            test_perceptual_representation,
    ) in in_front_test_curriculum.instances():
        descriptions_from_learner = processing_learner.describe(
            test_perceptual_representation)
        gold = test_linguistic_description.as_token_sequence()
        assert descriptions_from_learner
        assert gold in [
            desc.as_token_sequence() for desc in descriptions_from_learner
        ]
def test_subset_preposition_in_front(language_mode, learner):
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)
    speaker = standard_object("speaker", MOM, added_properties=[IS_SPEAKER])

    run_preposition_test(
        learner(language_mode),
        _in_front_template(ball,
                           table, [speaker],
                           is_training=True,
                           is_near=True),
        language_generator=phase1_language_generator(language_mode),
    )
def test_subset_preposition_in_front(language_mode, learner):
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)

    run_preposition_test(
        learner(language_mode),
        _in_front_template(
            ball,
            table,
            immutableset(),
            is_training=True,
            is_near=True,
            speaker_root_node=MOM,
        ),
        language_generator=phase1_language_generator(language_mode),
    )
예제 #4
0
def _prepositional_relation_described_curriculum(
    max_to_sample: int,
    noise_objects_sets: Iterable[Iterable[TemplateObjectVariable]],
    *,
    min_noise_relations: int = 0,
    max_noise_relations: int = 0,
    add_noise: bool,
    chooser: RandomChooser,
    samples_to_template_den: int = 1,
    block_multiple_of_same_type: bool,
    language_generator: LanguageGenerator[
        HighLevelSemanticsSituation, LinearizedDependencyTree
    ],
    include_targets_in_noise: bool = False,
    min_samples: int = 6,
) -> Phase1InstanceGroup:
    target_1 = standard_object(
        "target_1", THING, required_properties=[INTEGRATED_EXPERIMENT_PROP]
    )
    target_2 = standard_object(
        "target_2", THING, required_properties=[INTEGRATED_EXPERIMENT_PROP]
    )
    target_with_object_on = standard_object(
        "target with object on",
        INANIMATE_OBJECT,
        required_properties=[INTEGRATED_EXPERIMENT_PROP, CAN_HAVE_THINGS_RESTING_ON_THEM],
    )
    templates = (
        [
            _on_template(
                target_1,
                target_with_object_on,
                background_objects,
                is_training=True,
                background_relations=background_relations_builder(
                    background_objects,
                    num_relations,
                    target=target_1,
                    target_2=target_with_object_on,
                    include_targets_in_noise=include_targets_in_noise,
                ),
            )
            for background_objects in noise_objects_sets
            for num_relations in range(min_noise_relations, max_noise_relations)
        ]
        if add_noise
        else [
            _on_template(
                target_1, target_with_object_on, immutableset(), is_training=True
            )
        ]
    )
    templates.extend(
        [
            _beside_template(
                target_1,
                target_2,
                background_objects,
                is_right=is_right,
                is_training=True,
                background_relations=background_relations_builder(
                    background_objects,
                    num_relations,
                    target=target_1,
                    target_2=target_2,
                    include_targets_in_noise=include_targets_in_noise,
                ),
            )
            for is_right in BOOL_SET
            for background_objects in noise_objects_sets
            for num_relations in range(min_noise_relations, max_noise_relations)
        ]
        if add_noise
        else [
            _beside_template(
                target_1, target_2, immutableset(), is_right=is_right, is_training=True
            )
            for is_right in BOOL_SET
        ]
    )
    templates.extend(
        [
            _behind_template(
                target_1,
                target_2,
                background_objects,
                is_near=is_near,
                is_training=True,
                background_relations=background_relations_builder(
                    background_objects,
                    num_relations,
                    target=target_1,
                    target_2=target_2,
                    include_targets_in_noise=include_targets_in_noise,
                ),
            )
            for is_near in BOOL_SET
            for background_objects in noise_objects_sets
            for num_relations in range(min_noise_relations, max_noise_relations)
        ]
        if add_noise
        else [
            _behind_template(
                target_1, target_2, immutableset(), is_near=is_near, is_training=True
            )
            for is_near in BOOL_SET
        ]
    )
    templates.extend(
        [
            _in_front_template(
                target_1,
                target_2,
                background_objects,
                is_near=is_near,
                is_training=True,
                background_relations=background_relations_builder(
                    background_objects,
                    num_relations,
                    target=target_1,
                    target_2=target_2,
                    include_targets_in_noise=include_targets_in_noise,
                ),
            )
            for is_near in BOOL_SET
            for background_objects in noise_objects_sets
            for num_relations in range(min_noise_relations, max_noise_relations)
        ]
        if add_noise
        else [
            _in_front_template(
                target_1, target_2, immutableset(), is_near=is_near, is_training=True
            )
            for is_near in BOOL_SET
        ]
    )

    return phase2_instances(
        "Prepositional Relation",
        flatten(
            [
                sampled(
                    template,
                    ontology=INTEGRATED_EXPERIMENT_ONTOLOGY,
                    chooser=chooser,
                    max_to_sample=max(
                        math.ceil(max_to_sample / samples_to_template_den), min_samples
                    ),
                    block_multiple_of_the_same_type=block_multiple_of_same_type,
                )
                for template in templates
            ]
        ),
        language_generator=language_generator,
        perception_generator=INTEGRATED_EXPERIMENT_PERCEPTION_GENERATOR,
    )
def test_pursuit_preposition_in_front_learner(language_mode):
    rng = random.Random()
    rng.seed(0)
    learner = PrepositionPursuitLearner(
        learning_factor=0.5,
        graph_match_confirmation_threshold=0.7,
        lexicon_entry_threshold=0.7,
        rng=rng,
        smoothing_parameter=0.001,
        ontology=GAILA_PHASE_1_ONTOLOGY,
        object_recognizer=LANGUAGE_MODE_TO_OBJECT_RECOGNIZER[language_mode],
        language_mode=language_mode,
    )  # type: ignore
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)
    language_generator = phase1_language_generator(language_mode)
    in_front_train_curriculum = phase1_instances(
        "Preposition In Front Unit Train",
        situations=sampled(
            _in_front_template(
                ball,
                table,
                immutableset(),
                is_training=True,
                is_near=True,
                speaker_root_node=MOM,
            ),
            chooser=PHASE1_CHOOSER_FACTORY(),
            ontology=GAILA_PHASE_1_ONTOLOGY,
            max_to_sample=10,
        ),
        language_generator=language_generator,
    )
    in_front_test_curriculum = phase1_instances(
        "Preposition In Front Unit Test",
        situations=sampled(
            _in_front_template(
                ball,
                table,
                immutableset(),
                is_training=False,
                is_near=True,
                speaker_root_node=MOM,
            ),
            chooser=PHASE1_CHOOSER_FACTORY(),
            ontology=GAILA_PHASE_1_ONTOLOGY,
            max_to_sample=1,
        ),
        language_generator=language_generator,
    )

    for (
            _,
            linguistic_description,
            perceptual_representation,
    ) in in_front_train_curriculum.instances():
        learner.observe(
            LearningExample(perceptual_representation, linguistic_description))

    for (
            _,
            test_linguistic_description,
            test_perceptual_representation,
    ) in in_front_test_curriculum.instances():
        descriptions_from_learner = learner.describe(
            test_perceptual_representation)
        gold = test_linguistic_description.as_token_sequence()
        assert descriptions_from_learner
        assert [
            desc.as_token_sequence() for desc in descriptions_from_learner
        ][0] == gold