diff --git a/tests/test_downloader.py b/tests/test_downloader.py index a5a393bcb4948a8d8a855b6ab6268f909da1bc6b..2df20b3726f705b9c8dc994bff355408901bd09a 100644 --- a/tests/test_downloader.py +++ b/tests/test_downloader.py @@ -18,7 +18,7 @@ import paddle_hub as hub class TestDownloader(unittest.TestCase): def test_download(self): - link = "http://paddlehub.bj.bcebos.com/word2vec/word2vec-dim16-simple-example-2.tar.gz" + link = "https://paddlehub.cdn.bcebos.com/word2vec/word2vec_test_module.tar.gz" module_path = hub.download_and_uncompress(link) diff --git a/tests/test_export_n_load_module.py b/tests/test_export_n_load_module.py index c0f88f2cbec14b52250ec6938347a7c1cc3c033a..4ddd5ae2bfec7eff795fef847fbfcdf5779c68d0 100644 --- a/tests/test_export_n_load_module.py +++ b/tests/test_export_n_load_module.py @@ -154,36 +154,40 @@ def test_create_w2v_module(use_gpu=False): main_program.global_block().var("fourthw"), ] signature = hub.create_signature( - "default", inputs=module_inputs, outputs=[pred_prob]) + "default", + inputs=module_inputs, + outputs=[pred_prob], + feed_names=["firstw", "secondw", "thirdw", "fourthw"], + fetch_names=["pred_prob"]) hub.create_module( - sign_arr=signature, - program=fluid.default_main_program(), - module_dir=saved_module_dir, - word_dict=dictionary) + sign_arr=signature, module_dir=saved_module_dir, word_dict=dictionary) def test_load_w2v_module(use_gpu=False): saved_module_dir = "./tmp/word2vec_test_module" w2v_module = hub.Module(module_dir=saved_module_dir) - feed_list, fetch_list, program, generator = w2v_module( + feed_dict, fetch_dict, program = w2v_module( sign_name="default", trainable=False) with fluid.program_guard(main_program=program): - with fluid.unique_name.guard(generator): - pred_prob = fetch_list[0] - pred_word = fluid.layers.argmax(x=pred_prob, axis=1) - # set place, executor, datafeeder - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(place=place, feed_list=feed_list) - - word_ids = [[1, 2, 3, 4]] - result = exe.run( - fluid.default_main_program(), - feed=feeder.feed(word_ids), - fetch_list=[pred_word], - return_numpy=True) - - print(result) + pred_prob = fetch_dict["pred_prob"] + pred_word = fluid.layers.argmax(x=pred_prob, axis=1) + # set place, executor, datafeeder + place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() + exe = fluid.Executor(place) + feed_vars = [ + feed_dict["firstw"], feed_dict["secondw"], feed_dict["thirdw"], + feed_dict["fourthw"] + ] + feeder = fluid.DataFeeder(place=place, feed_list=feed_vars) + + word_ids = [[1, 2, 3, 4]] + result = exe.run( + fluid.default_main_program(), + feed=feeder.feed(word_ids), + fetch_list=[pred_word], + return_numpy=True) + + print(result) if __name__ == "__main__": diff --git a/tests/test_module.py b/tests/test_module.py index 6bb3788e074b970088a1fdd137d8f3d9df5c7d52..76791c927503eec05cae82ef08d2011b62964161 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -15,19 +15,36 @@ import unittest import paddle_hub as hub +import paddle.fluid as fluid class TestModule(unittest.TestCase): #TODO(ZeyuChen): add setup for test envrinoment prepration def test_word2vec_module_usage(self): - pass - # url = "http://paddlehub.cdn.bcebos.com/word2vec/word2vec-dim16-simple-example-2.tar.gz" - # module = Module(module_url=url) - # inputs = [["it", "is", "new"], ["hello", "world"]] - # tensor = module._process_input(inputs) - # print(tensor) - # result = module(inputs) - # print(result) + url = "https://paddlehub.cdn.bcebos.com/word2vec/word2vec_test_module.tar.gz" + w2v_module = hub.Module(module_url=url) + feed_dict, fetch_dict, program = w2v_module( + sign_name="default", trainable=False) + with fluid.program_guard(main_program=program): + pred_prob = fetch_dict["pred_prob"] + pred_word = fluid.layers.argmax(x=pred_prob, axis=1) + # set place, executor, datafeeder + place = fluid.CPUPlace() + exe = fluid.Executor(place) + feed_vars = [ + feed_dict["firstw"], feed_dict["secondw"], feed_dict["thirdw"], + feed_dict["fourthw"] + ] + feeder = fluid.DataFeeder(place=place, feed_list=feed_vars) + + word_ids = [[1, 2, 3, 4]] + result = exe.run( + fluid.default_main_program(), + feed=feeder.feed(word_ids), + fetch_list=[pred_word], + return_numpy=True) + + self.assertEqual(result[0], 5) def test_senta_module_usage(self): pass