コード例 #1
0
def test_metric_tracker_patient():
    metric_tracker = MetricTracker(patient=1)

    for metric in METRICS:
        metric_tracker.add_metric(**metric)

        if metric["epoch"] > 4:
            ASSERT.assertTrue(metric_tracker.early_stopping(metric["epoch"]))
        else:
            ASSERT.assertFalse(metric_tracker.early_stopping(metric["epoch"]))

        if metric_tracker.early_stopping(metric["epoch"]):
            break

    expect = {"epoch": 3,
              "train_metric": {"acc": 0.85},
              "train_model_target_metric": ModelTargetMetric(metric_name="acc", metric_value=0.85),
              "validation_metric": {"acc": 0.60},
              "validation_model_target_metric": ModelTargetMetric(metric_name="acc", metric_value=0.60)}

    best = metric_tracker.best()
    ASSERT.assertEqual(expect["epoch"], best.epoch)

    ASSERT.assertDictEqual(expect["train_metric"], best.train_metric)
    ASSERT.assertDictEqual(expect["validation_metric"], best.validation_metric)
    ASSERT.assertEqual(expect["train_model_target_metric"].name, best.train_model_target_metric.name)
    ASSERT.assertEqual(expect["train_model_target_metric"].value, best.train_model_target_metric.value)
    ASSERT.assertEqual(expect["validation_model_target_metric"].name, best.validation_model_target_metric.name)
    ASSERT.assertEqual(expect["validation_model_target_metric"].value, best.validation_model_target_metric.value)
コード例 #2
0
def test_dynamic_rnn(sequence_embedding):
    sequence, mask = sequence_embedding

    hidden_size = 4
    batch_size = 3
    sequence_len = 3

    rnn = RNN(input_size=2,
              hidden_size=4,
              num_layers=2,
              batch_first=True,
              bidirectional=True)

    dynamic_rnn = DynamicRnn(rnn=rnn)

    rnn_output: DynamicRnnOutput = dynamic_rnn(sequence=sequence, mask=mask)

    logging.info(json2str(rnn_output))

    last_layer_h_n: torch.Tensor = rnn_output.last_layer_h_n

    last_layer_h_n_expect_size = (batch_size, hidden_size * 2)

    ASSERT.assertEqual(last_layer_h_n_expect_size, last_layer_h_n.size())

    ASSERT.assertTrue(rnn_output.last_layer_c_n is None)

    sequence_encoding_expect_size = (batch_size, sequence_len, hidden_size * 2)
    senquence_encoding = rnn_output.output
    ASSERT.assertEqual(sequence_encoding_expect_size,
                       senquence_encoding.size())
コード例 #3
0
def test_metric_tracker_save_and_load():
    metric_tracker = MetricTracker(patient=1)

    for metric in METRICS:
        metric_tracker.add_metric(**metric)

        if metric["epoch"] > 4:
            ASSERT.assertTrue(metric_tracker.early_stopping(metric["epoch"]))
        else:
            ASSERT.assertFalse(metric_tracker.early_stopping(metric["epoch"]))

        if metric_tracker.early_stopping(metric["epoch"]):
            break

    saved_file_path = os.path.join(ROOT_PATH, "data/easytext/tests/trainer/metric_tracker.json")

    metric_tracker.save(saved_file_path)

    loaded_metric_tracker = MetricTracker.from_file(saved_file_path)

    best = metric_tracker.best()
    loaded_best = loaded_metric_tracker.best()
    ASSERT.assertEqual(best.epoch, loaded_best.epoch)

    ASSERT.assertDictEqual(best.train_metric, loaded_best.train_metric)
    ASSERT.assertDictEqual(best.validation_metric, loaded_best.validation_metric)
    ASSERT.assertEqual(best.train_model_target_metric.name, loaded_best.train_model_target_metric.name)
    ASSERT.assertEqual(best.train_model_target_metric.value, loaded_best.train_model_target_metric.value)
    ASSERT.assertEqual(best.validation_model_target_metric.name, loaded_best.validation_model_target_metric.name)
    ASSERT.assertEqual(best.validation_model_target_metric.value, loaded_best.validation_model_target_metric.value)
コード例 #4
0
def test_is_tensor_equal():
    """
    测试两个 tensor 是否相等
    :return:
    """

    x = torch.tensor([1, 2, 3])
    y = torch.tensor([1, 2, 3])

    equal = tensor_util.is_tensor_equal(tensor1=x, tensor2=y, epsilon=0)

    ASSERT.assertTrue(equal)

    x = torch.tensor([1, 2, 3])
    y = torch.tensor([2, 2, 3])

    equal = tensor_util.is_tensor_equal(tensor1=x, tensor2=y, epsilon=0)

    ASSERT.assertFalse(equal)

    x = torch.tensor([1.0001, 2.0001, 3.0001])
    y = torch.tensor([1., 2., 3.])

    equal = tensor_util.is_tensor_equal(tensor1=x, tensor2=y, epsilon=1e-3)

    ASSERT.assertTrue(equal)

    equal = tensor_util.is_tensor_equal(tensor1=x, tensor2=y, epsilon=1e-4)

    ASSERT.assertFalse(equal)
コード例 #5
0
def test_component_factory():
    Registry().clear_objects()

    model_json_file_path = "data/easytext/tests/component/model.json"
    model_json_file_path = os.path.join(ROOT_PATH, model_json_file_path)
    with open(model_json_file_path, encoding="utf-8") as f:
        config = json.load(f, object_pairs_hook=OrderedDict)

    factory = ComponentFactory(is_training=True)

    parserd_dict = factory.create(config=config)

    model = parserd_dict["model"]

    ASSERT.assertTrue(model.linear is not None)
    ASSERT.assertEqual((2, 4),
                       (model.linear.in_features, model.linear.out_features))
コード例 #6
0
ファイル: test_config.py プロジェクト: piaoxue88/easytext
def test_config():

    config_file_path = "data/easytext/tests/config/config.json"
    config_file_path = os.path.join(ROOT_PATH, config_file_path)

    config = Config(is_training=True, config_file_path=config_file_path)

    ASSERT.assertTrue(config.model is not None)
    ASSERT.assertTrue(isinstance(config.model, _MyModel))

    ASSERT.assertTrue(config.optimizer is not None)
    ASSERT.assertTrue(isinstance(config.optimizer, _MyOpitmizer))
コード例 #7
0
def test_gat_without_hidden():
    """
    测试 gat
    :return:
    """

    torch.manual_seed(7)
    torch.cuda.manual_seed_all(7)

    in_features = 2
    out_features = 4

    gat = GAT(in_features=in_features,
              out_features=out_features,
              dropout=0.,
              alpha=0.1,
              num_heads=3,
              hidden_size=None)

    nodes = torch.tensor([[[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]],
                          [[0.7, 0.8], [0.9, 0.10], [0.11, 0.12]]],
                         dtype=torch.float)

    adj = torch.tensor(
        [[[0, 1, 0], [1, 0, 0], [0, 0, 0]], [[0, 1, 1], [1, 0, 1], [1, 1, 0]]],
        dtype=torch.long)

    output_nodes: torch.Tensor = gat(nodes=nodes, adj=adj)

    expect_size = (nodes.size(0), nodes.size(1), out_features)
    ASSERT.assertEqual(expect_size, output_nodes.size())

    expect = torch.tensor([[[-1.6478, -0.3935, -2.6613, -2.7653],
                            [-1.3204, -0.8394, -1.8519, -1.9375],
                            [-1.6478, -0.3935, -2.6613, -2.7653]],
                           [[-1.9897, -0.4203, -2.4447, -2.1232],
                            [-2.1944, -0.1897, -3.4053, -3.5697],
                            [-2.9364, -0.0878, -4.1695, -4.1617]]],
                          dtype=torch.float)

    ASSERT.assertTrue(
        tensor_util.is_tensor_equal(expect, output_nodes, epsilon=1e-4))
コード例 #8
0
def test_gat_with_hidden():
    """
    测试 gat
    :return:
    """

    torch.manual_seed(7)
    torch.cuda.manual_seed_all(7)

    in_features = 2
    out_features = 4

    gat = GAT(in_features=in_features,
              out_features=out_features,
              dropout=0.,
              alpha=0.1,
              num_heads=3,
              hidden_size=3)

    nodes = torch.tensor([[[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]],
                          [[0.7, 0.8], [0.9, 0.10], [0.11, 0.12]]],
                         dtype=torch.float)

    adj = torch.tensor(
        [[[0, 1, 0], [1, 0, 0], [0, 0, 0]], [[0, 1, 1], [1, 0, 1], [1, 1, 0]]],
        dtype=torch.long)

    output_nodes: torch.Tensor = gat(nodes=nodes, adj=adj)

    expect_size = (nodes.size(0), nodes.size(1), out_features)
    ASSERT.assertEqual(expect_size, output_nodes.size())

    expect = torch.tensor([[[-1.3835, -1.4764, -1.2033, -1.5113],
                            [-1.3316, -1.5785, -1.1564, -1.5368],
                            [-1.3475, -1.5467, -1.1706, -1.5279]],
                           [[-1.3388, -1.6693, -1.4427, -1.1610],
                            [-1.4288, -1.6525, -1.6607, -0.9707],
                            [-1.4320, -1.4422, -1.6465, -1.1025]]])

    ASSERT.assertTrue(
        tensor_util.is_tensor_equal(expect, output_nodes, epsilon=1e-4))
コード例 #9
0
ファイル: test_vocabulary.py プロジェクト: piaoxue88/easytext
def test_vocabulary():
    """

    :return:
    """

    batch_tokens = [["我", "和", "你"], ["在", "我"]]
    vocabulary = Vocabulary(batch_tokens,
                            padding="",
                            unk="",
                            special_first=True,
                            min_frequency=1,
                            max_size=None)

    ASSERT.assertEqual(vocabulary.size, 4)

    ASSERT.assertTrue(not vocabulary.padding)
    ASSERT.assertTrue(not vocabulary.unk)

    ASSERT.assertEqual(vocabulary.index("我"), 0)
    ASSERT.assertEqual(vocabulary.index("和"), 1)
コード例 #10
0
def test_graph_attention_layer():
    torch.manual_seed(7)
    torch.cuda.manual_seed_all(7)

    in_features = 2
    out_features = 4

    gat_layer = GraphAttentionLayer(in_features=in_features,
                                    out_features=out_features,
                                    dropout=0.0,
                                    alpha=0.1)

    nodes = torch.tensor([[[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]],
                          [[0.7, 0.8], [0.9, 0.10], [0.11, 0.12]]],
                         dtype=torch.float)

    adj = torch.tensor(
        [[[0, 1, 0], [1, 0, 0], [0, 0, 0]], [[0, 1, 1], [1, 0, 1], [1, 1, 0]]],
        dtype=torch.long)

    outputs: torch.Tensor = gat_layer(input=nodes, adj=adj)

    expect_size = (nodes.size(0), nodes.size(1), out_features)

    ASSERT.assertEqual(expect_size, outputs.size())

    # 下面的 expect 是从原论文中测试得到的结果,直接拿来用
    expect = torch.tensor([[[0.2831, 0.3588, -0.5131, -0.2058],
                            [0.1606, 0.1292, -0.2264, -0.0951],
                            [0.2831, 0.3588, -0.5131, -0.2058]],
                           [[-0.0748, 0.5025, -0.3840, -0.1192],
                            [0.2959, 0.4624, -0.6123, -0.2405],
                            [0.1505, 0.8668, -0.8609, -0.3059]]],
                          dtype=torch.float)

    ASSERT.assertTrue(
        tensor_util.is_tensor_equal(expect, outputs, epsilon=1e-4))
コード例 #11
0
def test_component_with_object():
    """
    测试,当 component 构建的时候,某个参数是 object
    :return:
    """
    Registry().clear_objects()
    config_json_file_path = "data/easytext/tests/component/component_with_obj.json"
    config_json_file_path = os.path.join(ROOT_PATH, config_json_file_path)
    with open(config_json_file_path, encoding="utf-8") as f:
        param_dict = json.load(f, object_pairs_hook=OrderedDict)

    factory = ComponentFactory(is_training=False)

    parsed_dict = factory.create(config=param_dict)

    my_obj = parsed_dict["my_obj"]

    ASSERT.assertEqual(10, my_obj.value)

    my_component: _ModelWithObjParam = parsed_dict["my_component"]

    ASSERT.assertEqual(4, my_component.sub_model.in_features)
    ASSERT.assertEqual(2, my_component.sub_model.out_features)

    ASSERT.assertTrue(id(my_obj) == id(my_component.customer_obj))

    another_component: _ModelWithObjParam = parsed_dict["another_component"]

    ASSERT.assertTrue(id(my_component) != id(another_component))

    another_obj: _CustomerObj = parsed_dict["another_obj"]
    ASSERT.assertTrue(id(another_obj) == id(another_component.customer_obj))

    ASSERT.assertEqual(20, another_obj.value)

    dict_param_component: _DictParamComponent = parsed_dict[
        "dict_param_component"]
    ASSERT.assertTrue(
        id(dict_param_component.curstomer_obj) == id(another_obj))

    ASSERT.assertEqual(1, dict_param_component.dict_value["a"])
    ASSERT.assertEqual(2, dict_param_component.dict_value["b"])
    ASSERT.assertEqual(30, dict_param_component.dict_value["c_obj"].value)

    my_object = parsed_dict["my_object"]
    ASSERT.assertEqual("my_test_value", my_object)