Beispiel #1
0
 def test_simple_case(self):
     entries = [Environment(
         {"ground_truth": "y = x + 1"},
         set(["ground_truth"])
     )]
     dataset = ListDataset(entries)
     d = get_samples(dataset, MockParser())
     aencoder = ActionSequenceEncoder(d, 0)
     action_sequence = GroundTruthToActionSequence(MockParser())(
         ground_truth="y = x + 1"
     )
     transform = EncodeActionSequence(aencoder)
     ground_truth = transform(
         action_sequence=action_sequence,
         reference=[Token(None, "foo", "foo"), Token(None, "bar", "bar")],
     )
     assert np.array_equal(
         [
             [3, -1, -1], [4, -1, -1], [-1, 1, -1], [1, -1, -1],
             [5, -1, -1], [-1, 2, -1], [1, -1, -1], [4, -1, -1],
             [-1, 3, -1], [1, -1, -1], [6, -1, -1], [-1, 4, -1],
             [1, -1, -1]
         ],
         ground_truth.numpy()
     )
Beispiel #2
0
 def test_eval(self):
     entries = [Environment(
         {"text_query": "ab test", "ground_truth": "y = x + 1"},
         set(["ground_truth"])
     )]
     dataset = ListDataset(entries)
     d = get_samples(dataset, MockParserWithoutVariadicArgs())
     aencoder = ActionSequenceEncoder(d, 0)
     action_sequence = GroundTruthToActionSequence(MockParserWithoutVariadicArgs())(
         "y = x + 1"
     )
     transform = AddQueryForTreeGenDecoder(aencoder, 3,)
     query = transform(
         reference=[Token(None, "ab", "ab"), Token(None, "test", "test")],
         action_sequence=action_sequence,
         train=False
     )
     assert np.array_equal(
         [
             [-1, -1, -1], [2, -1, -1], [3, 2, -1], [4, 3, 2],
             [3, 2, -1], [5, 3, 2], [5, 3, 2], [4, 5, 3],
             [5, 3, 2], [6, 5, 3]
         ],
         query.numpy()
     )
Beispiel #3
0
 def test_eval(self):
     entries = [Environment(
         {"text_query": "ab test", "ground_truth": "y = x + 1"},
         set(["ground_truth"])
     )]
     dataset = ListDataset(entries)
     d = get_samples(dataset, MockParserWithoutVariadicArgs())
     aencoder = ActionSequenceEncoder(d, 0)
     action_sequence = GroundTruthToActionSequence(MockParserWithoutVariadicArgs())(
         "y = x + 1"
     )
     transform = AddActionSequenceAsTree(aencoder,)
     matrix, depth = transform(
         reference=[Token(None, "ab", "ab"), Token(None, "test", "test")],
         action_sequence=action_sequence,
         train=False
     )
     assert np.array_equal(
         [0, 1, 2, 3, 2, 3, 3, 4, 3, 4],
         depth.numpy()
     )
     assert np.array_equal(
         [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
          [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 1, 1, 0, 1, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
         matrix.numpy()
     )
Beispiel #4
0
 def test_n_dependent(self):
     entries = [Environment(
         {"text_query": "ab test", "ground_truth": "y = x + 1"},
         set(["ground_truth"])
     )]
     dataset = ListDataset(entries)
     d = get_samples(dataset, MockParserWithoutVariadicArgs())
     aencoder = ActionSequenceEncoder(d, 0)
     action_sequence = GroundTruthToActionSequence(MockParserWithoutVariadicArgs())(
         "y = x + 1"
     )
     transform = AddPreviousActionRules(aencoder, 2, n_dependent=3)
     prev_rule_action = transform(
         reference=[Token(None, "ab", "ab"), Token(None, "test", "test")],
         action_sequence=action_sequence,
         train=False,
     )
     assert np.array_equal(
         [
             # str -> "y"
             [[-1, -1, -1], [-1, 3, -1], [-1, -1, -1]],
             # Number -> number
             [[8, -1, -1], [9, -1, -1], [-1, -1, -1]],
             [[-1, -1, -1], [-1, 4, -1], [-1, -1, -1]],
         ],
         prev_rule_action.numpy()
     )
Beispiel #5
0
    def test_eval(self):
        entries = [Environment(
            {"text_query": "foo bar", "ground_truth": "y = x + 1"},
            set(["ground_truth"])
        )]
        dataset = ListDataset(entries)
        d = get_samples(dataset, MockParser())
        aencoder = ActionSequenceEncoder(d, 0)
        action_sequence = GroundTruthToActionSequence(MockParser())(
            "y = x + 1"
        )
        transform = AddActions(aencoder)
        action_tensor = transform(
            reference=[Token(None, "foo", "foo"), Token(None, "bar", "bar")],
            action_sequence=action_sequence,
            train=False
        )

        assert np.array_equal(
            [
                [2, 2, 0], [4, 3, 1], [6, 4, 2], [6, 4, 2], [5, 3, 1],
                [6, 5, 5], [6, 5, 5], [5, 5, 5], [6, 4, 8], [6, 4, 8],
                [5, 5, 5], [9, 6, 11], [9, 6, 11], [-1, -1, -1]
            ],
            action_tensor.numpy()
        )
Beispiel #6
0
    def test_eval(self):
        entries = [Environment(
            {"ground_truth": "y = x + 1"},
            set(["ground_truth"])
        )]
        dataset = ListDataset(entries)
        d = get_samples(dataset, MockParser())
        aencoder = ActionSequenceEncoder(d, 0)
        action_sequence = GroundTruthToActionSequence(MockParser())(
            "y = x + 1"
        )
        transform = AddPreviousActions(aencoder)
        prev_action_tensor = transform(
            reference=[Token(None, "foo", "foo"), Token(None, "bar", "bar")],
            action_sequence=action_sequence,
            train=False
        )

        assert np.array_equal(
            [
                [2, -1, -1], [3, -1, -1], [4, -1, -1], [-1, 1, -1],
                [1, -1, -1], [5, -1, -1], [-1, 2, -1], [1, -1, -1],
                [4, -1, -1], [-1, 3, -1], [1, -1, -1], [6, -1, -1],
                [-1, 4, -1], [1, -1, -1]
            ],
            prev_action_tensor.numpy()
        )
Beispiel #7
0
 def transform_cls(self, qencoder, cencoder, aencoder, parser):
     return Sequence(
         OrderedDict([("extract_reference",
                       Apply(module=mlprogram.nn.Function(tokenize),
                             in_keys=[["text_query", "str"]],
                             out_key="reference")),
                      ("encode_word_query",
                       Apply(module=EncodeWordQuery(qencoder),
                             in_keys=["reference"],
                             out_key="word_nl_query")),
                      ("encode_char",
                       Apply(module=EncodeCharacterQuery(cencoder, 10),
                             in_keys=["reference"],
                             out_key="char_nl_query")),
                      ("f2",
                       Apply(
                           module=GroundTruthToActionSequence(parser),
                           in_keys=["ground_truth"],
                           out_key="action_sequence",
                       )),
                      ("add_previous_action",
                       Apply(
                           module=AddPreviousActions(aencoder),
                           in_keys=["action_sequence", "reference"],
                           constants={"train": True},
                           out_key="previous_actions",
                       )),
                      ("add_previous_action_rule",
                       Apply(
                           module=AddPreviousActionRules(aencoder, 4),
                           in_keys=["action_sequence", "reference"],
                           constants={"train": True},
                           out_key="previous_action_rules",
                       )),
                      ("add_tree",
                       Apply(
                           module=AddActionSequenceAsTree(aencoder),
                           in_keys=["action_sequence", "reference"],
                           constants={"train": True},
                           out_key=["adjacency_matrix", "depthes"],
                       )),
                      ("add_query",
                       Apply(
                           module=AddQueryForTreeGenDecoder(aencoder, 4),
                           in_keys=["action_sequence", "reference"],
                           constants={"train": True},
                           out_key="action_queries",
                       )),
                      ("f4",
                       Apply(
                           module=EncodeActionSequence(aencoder),
                           in_keys=["action_sequence", "reference"],
                           out_key="ground_truth_actions",
                       ))]))
Beispiel #8
0
 def test_impossible_case(self):
     entries = [Environment(
         {"ground_truth": "y = x + 1"},
         set(["ground_truth"])
     )]
     dataset = ListDataset(entries)
     d = get_samples(dataset, MockParser())
     d.tokens = [("", "y"), ("", "1")]
     aencoder = ActionSequenceEncoder(d, 0)
     action_sequence = GroundTruthToActionSequence(MockParser())(
         ground_truth="y = x + 1"
     )
     transform = EncodeActionSequence(aencoder)
     with pytest.raises(RuntimeError):
         transform(
             reference=[Token(None, "foo", "foo"), Token(None, "bar", "bar")],
             action_sequence=action_sequence,
         )
Beispiel #9
0
 def test_eval(self):
     entries = [Environment(
         {"text_query": "ab test", "ground_truth": "y = x + 1"},
         set(["ground_truth"])
     )]
     dataset = ListDataset(entries)
     d = get_samples(dataset, MockParserWithoutVariadicArgs())
     aencoder = ActionSequenceEncoder(d, 0)
     action_sequence = GroundTruthToActionSequence(MockParserWithoutVariadicArgs())(
         "y = x + 1"
     )
     transform = AddPreviousActionRules(aencoder, 2)
     prev_rule_action = transform(
         reference=[Token(None, "ab", "ab"), Token(None, "test", "test")],
         action_sequence=action_sequence,
         train=False
     )
     assert np.array_equal(
         [
             # None -> Root
             [[1, -1, -1], [2, -1, -1], [-1, -1, -1]],
             # Assign -> Name, expr
             [[3, -1, -1], [4, -1, -1], [5, -1, -1]],
             # Name -> str
             [[4, -1, -1], [6, -1, -1], [-1, -1, -1]],
             # str -> "x"
             [[-1, -1, -1], [-1, 1, -1], [-1, -1, -1]],
             # Op -> str, expr, expr
             [[7, -1, -1], [6, -1, -1], [5, -1, -1]],
             # str -> "+"
             [[-1, -1, -1], [-1, 2, -1], [-1, -1, -1]],
             # Name -> str
             [[4, -1, -1], [6, -1, -1], [-1, -1, -1]],
             # str -> "y"
             [[-1, -1, -1], [-1, 3, -1], [-1, -1, -1]],
             # Number -> number
             [[8, -1, -1], [9, -1, -1], [-1, -1, -1]],
             [[-1, -1, -1], [-1, 4, -1], [-1, -1, -1]],
         ],
         prev_rule_action.numpy()
     )
Beispiel #10
0
 def transform_cls(self, qencoder, aencoder, parser):
     return Sequence(
         OrderedDict([("extract_reference",
                       Apply(module=mlprogram.nn.Function(tokenize),
                             in_keys=[["text_query", "str"]],
                             out_key="reference")),
                      ("encode_word_query",
                       Apply(module=EncodeWordQuery(qencoder),
                             in_keys=["reference"],
                             out_key="word_nl_query")),
                      ("f2",
                       Apply(
                           module=GroundTruthToActionSequence(parser),
                           in_keys=["ground_truth"],
                           out_key="action_sequence",
                       )),
                      ("add_previous_action",
                       Apply(
                           module=AddPreviousActions(aencoder,
                                                     n_dependent=1),
                           in_keys=["action_sequence", "reference"],
                           constants={"train": True},
                           out_key="previous_actions",
                       )),
                      ("add_action",
                       Apply(
                           module=AddActions(aencoder, n_dependent=1),
                           in_keys=["action_sequence", "reference"],
                           constants={"train": True},
                           out_key="actions",
                       )), ("add_state", AddState("state")),
                      ("add_hidden_state", AddState("hidden_state")),
                      ("add_history", AddState("history")),
                      ("f4",
                       Apply(
                           module=EncodeActionSequence(aencoder),
                           in_keys=["action_sequence", "reference"],
                           out_key="ground_truth_actions",
                       ))]))
 def transform(self, encoder, interpreter, parser):
     tcode = Apply(
         module=GroundTruthToActionSequence(parser),
         in_keys=["ground_truth"],
         out_key="action_sequence"
     )
     aaction = Apply(
         module=AddPreviousActions(encoder, n_dependent=1),
         in_keys=["action_sequence", "reference"],
         constants={"train": True},
         out_key="previous_actions",
     )
     tgt = Apply(
         module=EncodeActionSequence(encoder),
         in_keys=["action_sequence", "reference"],
         out_key="ground_truth_actions",
     )
     return Sequence(
         OrderedDict([
             ("tinput",
              Apply(
                  module=TransformInputs(),
                  in_keys=["test_cases"],
                  out_key="test_case_tensor",
              )),
             ("tvariable",
              Apply(
                  module=TransformVariables(),
                  in_keys=["variables", "test_case_tensor"],
                  out_key="variables_tensor"
              )),
             ("tcode", tcode),
             ("aaction", aaction),
             ("add_state", AddState("state")),
             ("add_hidden_state", AddState("hidden_state")),
             ("tgt", tgt)
         ])
     )
Beispiel #12
0
 def test_simple_case(self):
     transform = GroundTruthToActionSequence(MockParser())
     action_sequence = transform(ground_truth="y = x + 1")
     assert action_sequence.head is None