def test_pursuit_preposition_beside_learner(language_mode, learner):
    language_generator = phase1_language_generator(language_mode)
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)
    beside_train_curriculum = phase1_instances(
        "Preposition Beside Unit Train",
        situations=sampled(
            _beside_template(ball,
                             table,
                             immutableset(),
                             is_training=True,
                             is_right=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,
    )
    beside_test_curriculum = phase1_instances(
        "Preposition Beside Unit Test",
        situations=sampled(
            _beside_template(ball,
                             table,
                             immutableset(),
                             is_training=False,
                             is_right=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 beside_train_curriculum.instances():
        processing_learner.observe(
            LearningExample(perceptual_representation, linguistic_description))

    for (
            _,
            test_linguistic_description,
            test_perceptual_representation,
    ) in beside_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
        ]
Пример #2
0
def _make_m6_beside_curriculum(
    num_samples: Optional[int],
    noise_objects: Optional[int],
    language_generator: LanguageGenerator[HighLevelSemanticsSituation,
                                          LinearizedDependencyTree],
) -> Phase1InstanceGroup:
    return phase1_instances(
        "Preposition on",
        situations=chain(*[
            sampled(
                _beside_template(
                    object_1,
                    object_2,
                    make_noise_objects(noise_objects),
                    is_training=True,
                    is_right=True,
                ),
                chooser=PHASE1_CHOOSER_FACTORY(),
                ontology=GAILA_PHASE_1_ONTOLOGY,
                max_to_sample=num_samples if num_samples else 1,
                block_multiple_of_the_same_type=True,
            ) for object_1 in r.sample(SMALL_OBJECT_VARS, 3)
            for object_2 in r.sample(LARGE_OBJECT_VARS, 3)
        ]),
        perception_generator=GAILA_M6_PERCEPTION_GENERATOR,
        language_generator=language_generator,
    )
def test_subset_preposition_beside(language_mode, learner):
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)

    run_preposition_test(
        learner(language_mode),
        _beside_template(ball,
                         table,
                         immutableset(),
                         is_training=True,
                         is_right=True),
        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_beside_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
    language_generator = phase1_language_generator(language_mode)
    ball = standard_object("ball", BALL)
    table = standard_object("table", TABLE)
    beside_train_curriculum = phase1_instances(
        "Preposition Beside Unit Train",
        situations=sampled(
            _beside_template(ball,
                             table,
                             immutableset(),
                             is_training=True,
                             is_right=True),
            chooser=PHASE1_CHOOSER_FACTORY(),
            ontology=GAILA_PHASE_1_ONTOLOGY,
            max_to_sample=10,
        ),
        language_generator=language_generator,
    )
    beside_test_curriculum = phase1_instances(
        "Preposition Beside Unit Test",
        situations=sampled(
            _beside_template(ball,
                             table,
                             immutableset(),
                             is_training=False,
                             is_right=True),
            chooser=PHASE1_CHOOSER_FACTORY(),
            ontology=GAILA_PHASE_1_ONTOLOGY,
            max_to_sample=1,
        ),
        language_generator=language_generator,
    )

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

    for (
            _,
            test_linguistic_description,
            test_perceptual_representation,
    ) in beside_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