diff --git a/WORKSPACE b/WORKSPACE index d6ae2af8eb678a2e399220abefe825ab3975ff69..0b8299905abb844bfbd8b27f47b8fafded31ef7a 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,17 +1,15 @@ # External dependency to Google protobuf. http_archive( - name = "protobuf", - url = "http://github.com/google/protobuf/archive/v3.1.0.tar.gz", - sha256 = "0a0ae63cbffc274efb573bdde9a253e3f32e458c41261df51c5dbc5ad541e8f7", - strip_prefix = "protobuf-3.1.0", -) + name="protobuf", + url="http://github.com/google/protobuf/archive/v3.1.0.tar.gz", + sha256="0a0ae63cbffc274efb573bdde9a253e3f32e458c41261df51c5dbc5ad541e8f7", + strip_prefix="protobuf-3.1.0", ) # External dependency to gtest 1.7.0. This method comes from # https://www.bazel.io/versions/master/docs/tutorial/cpp.html. new_http_archive( - name = "gtest", - url = "https://github.com/google/googletest/archive/release-1.7.0.zip", - sha256 = "b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0", - build_file = "third_party/gtest.BUILD", - strip_prefix = "googletest-release-1.7.0", -) + name="gtest", + url="https://github.com/google/googletest/archive/release-1.7.0.zip", + sha256="b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0", + build_file="third_party/gtest.BUILD", + strip_prefix="googletest-release-1.7.0", ) diff --git a/demo/sentiment/predict.py b/demo/sentiment/predict.py index 0095c6f7272a2191ea39e042a836f7d6038032aa..8ec490f64691924013200a3d0038d39aa834b038 100755 --- a/demo/sentiment/predict.py +++ b/demo/sentiment/predict.py @@ -71,9 +71,7 @@ class SentimentPrediction(): transform word into integer index according to the dictionary. """ words = data.strip().split() - word_slot = [ - self.word_dict[w] for w in words if w in self.word_dict - ] + word_slot = [self.word_dict[w] for w in words if w in self.word_dict] return word_slot def batch_predict(self, data_batch): @@ -85,8 +83,8 @@ class SentimentPrediction(): if self.label is None: print("predicting label is %d" % (lab[0])) else: - print("predicting label is %s" % - (self.label[lab[0]])) + print("predicting label is %s" % (self.label[lab[0]])) + def option_parser(): usage = "python predict.py -n config -w model_dir -d dictionary -i input_file " @@ -143,9 +141,10 @@ def main(): batch.append([predict.get_index(line)]) if len(batch) == batch_size: predict.batch_predict(batch) - batch=[] + batch = [] if len(batch) > 0: predict.batch_predict(batch) + if __name__ == '__main__': main() diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index 42a7a29403f7dde2c3d1d1f090a1885104e15e64..5b7f4d85e2c3343013938e38492be8985a8cd11f 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -3364,7 +3364,10 @@ def my_fatal(s): logger.critical(s) raise Exception() + _parse_config_hooks = set() + + def register_parse_config_hook(f): """ Register a hook function for parse_config. parse_config will invoke the hook @@ -3373,6 +3376,7 @@ def register_parse_config_hook(f): """ _parse_config_hooks.add(f) + def parse_config(config_file, config_arg_str): ''' @param config_arg_str: a string of the form var1=val1,var2=val2. It will be diff --git a/python/paddle/trainer_config_helpers/default_decorators.py b/python/paddle/trainer_config_helpers/default_decorators.py index 13712aad7b03e561f86101070a3140324b51a4e3..ad3efcbf369411b9c42b2a32ed05b04f86bf7de6 100644 --- a/python/paddle/trainer_config_helpers/default_decorators.py +++ b/python/paddle/trainer_config_helpers/default_decorators.py @@ -84,12 +84,15 @@ class DefaultNameFactory(object): _name_factories = [] + def reset_hook(): for factory in _name_factories: factory.reset() + register_parse_config_hook(reset_hook) + def wrap_name_default(name_prefix=None): """ Decorator to set "name" arguments default to "{name_prefix}_{invoke_count}". diff --git a/python/paddle/trainer_config_helpers/tests/configs/test_config_parser_for_non_file_config.py b/python/paddle/trainer_config_helpers/tests/configs/test_config_parser_for_non_file_config.py index 87a607acf4219945b602411cd689afba3298696f..9b791a0222dd60e9ae2fca8b2798cddd13ed1d1c 100644 --- a/python/paddle/trainer_config_helpers/tests/configs/test_config_parser_for_non_file_config.py +++ b/python/paddle/trainer_config_helpers/tests/configs/test_config_parser_for_non_file_config.py @@ -17,33 +17,35 @@ import sys import re import getopt + def main(print_whole_config, globals, locals): - ''' + ''' this test will all test_config.py ''' - cmdstr = """from paddle.trainer.config_parser import parse_config\n""" - importstr = "" - functionstr = "" + cmdstr = """from paddle.trainer.config_parser import parse_config\n""" + importstr = "" + functionstr = "" + + for line in sys.stdin: + if re.match("^import", line) or re.match("^from.*import", line): + importstr = importstr + line + else: + functionstr = functionstr + " " + line - for line in sys.stdin: - if re.match("^import", line) or re.match("^from.*import", line): - importstr = importstr + line + cmdstr = cmdstr + importstr + """def configs():\n""" + functionstr + #cmdstr = cmdstr + """def configs():\n""" + importstr + functionstr + if print_whole_config: + cmdstr = cmdstr + """print parse_config(configs, "")""" else: - functionstr = functionstr + " " + line + cmdstr = cmdstr + """print parse_config(configs, "").model_config""" - cmdstr = cmdstr + importstr + """def configs():\n""" + functionstr - #cmdstr = cmdstr + """def configs():\n""" + importstr + functionstr - if print_whole_config: - cmdstr = cmdstr + """print parse_config(configs, "")""" - else: - cmdstr = cmdstr + """print parse_config(configs, "").model_config""" + exec (cmdstr, globals, locals) - exec(cmdstr, globals, locals) if __name__ == '__main__': - whole = False - opts, args = getopt.getopt(sys.argv[1:], "", ["whole"]) - for op, value in opts: - if op == "--whole": - whole = True - main(whole, globals(), locals()) + whole = False + opts, args = getopt.getopt(sys.argv[1:], "", ["whole"]) + for op, value in opts: + if op == "--whole": + whole = True + main(whole, globals(), locals()) diff --git a/python/paddle/trainer_config_helpers/tests/test_reset_hook.py b/python/paddle/trainer_config_helpers/tests/test_reset_hook.py index dc494d0eef22c927fe8afea5af2f8c36ff405173..0423babdb720191d8e9dfc67f1af3be339dbe27d 100644 --- a/python/paddle/trainer_config_helpers/tests/test_reset_hook.py +++ b/python/paddle/trainer_config_helpers/tests/test_reset_hook.py @@ -14,13 +14,13 @@ import unittest from paddle.trainer.config_parser import parse_config -class TestParse(unittest.TestCase): +class TestParse(unittest.TestCase): def test_parse(self): - a = parse_config( - 'trainer_config_helpers/tests/layers_test_config.py', '') - b = parse_config( - 'trainer_config_helpers/tests/layers_test_config.py', '') + a = parse_config('trainer_config_helpers/tests/layers_test_config.py', + '') + b = parse_config('trainer_config_helpers/tests/layers_test_config.py', + '') self.assertEqual(a, b) diff --git a/third_party/gtest.BUILD b/third_party/gtest.BUILD index 3e68a1d879311de905fab2f4cd3486a4c72a3532..71c74af513ca8c511dd273d089722bc7fbffd84c 100644 --- a/third_party/gtest.BUILD +++ b/third_party/gtest.BUILD @@ -1,14 +1,8 @@ cc_library( - name = "main", - srcs = glob( - ["src/*.cc"], - exclude = ["src/gtest-all.cc"] - ), - hdrs = glob([ - "include/**/*.h", - "src/*.h" - ]), - copts = ["-Iexternal/gtest/include"], - linkopts = ["-pthread"], - visibility = ["//visibility:public"], -) + name="main", + srcs=glob( + ["src/*.cc"], exclude=["src/gtest-all.cc"]), + hdrs=glob(["include/**/*.h", "src/*.h"]), + copts=["-Iexternal/gtest/include"], + linkopts=["-pthread"], + visibility=["//visibility:public"], ) diff --git a/third_party/protobuf_test/BUILD b/third_party/protobuf_test/BUILD index 46f769da5f5004ef2c5f6ecee43bc8965c1d811d..95a687a35629315398c7046ca7dc8d526892e227 100644 --- a/third_party/protobuf_test/BUILD +++ b/third_party/protobuf_test/BUILD @@ -3,25 +3,22 @@ licenses(["notice"]) # Apache 2.0 load("@protobuf//:protobuf.bzl", "cc_proto_library") cc_proto_library( - name = "example_proto", - srcs = ["example.proto"], - protoc = "@protobuf//:protoc", - default_runtime = "@protobuf//:protobuf", -) + name="example_proto", + srcs=["example.proto"], + protoc="@protobuf//:protoc", + default_runtime="@protobuf//:protobuf", ) cc_library( - name = "example_lib", - srcs = ["example_lib.cc"], - hdrs = ["example_lib.h"], - deps = [":example_proto"], -) + name="example_lib", + srcs=["example_lib.cc"], + hdrs=["example_lib.h"], + deps=[":example_proto"], ) cc_test( - name = "example_lib_test", - srcs = ["example_lib_test.cc"], - copts = ["-Iexternal/gtest/include"], - deps =[ + name="example_lib_test", + srcs=["example_lib_test.cc"], + copts=["-Iexternal/gtest/include"], + deps=[ "@gtest//:main", ":example_lib", - ], -) + ], ) diff --git a/third_party/protobuf_test/example_lib.cc b/third_party/protobuf_test/example_lib.cc index 56341a0124c0c22897aad8f5e1b85f9e28567a22..ced377bc0a17dde31c5c853dec1a852fa0be7223 100644 --- a/third_party/protobuf_test/example_lib.cc +++ b/third_party/protobuf_test/example_lib.cc @@ -3,9 +3,7 @@ namespace third_party { namespace protobuf_test { -std::string get_greet(const Greeting& who) { - return "Hello " + who.name(); -} +std::string get_greet(const Greeting& who) { return "Hello " + who.name(); } } // namespace protobuf_test } // namespace thrid_party