From cff7e36b938bff5e8cdf4823bf0385642374b3ff Mon Sep 17 00:00:00 2001 From: JiabinYang Date: Tue, 8 Jan 2019 06:22:43 +0000 Subject: [PATCH] refine readme and fix net conf bug --- fluid/PaddleRec/word2vec/README.md | 2 +- fluid/PaddleRec/word2vec/network_conf.py | 18 ++++++++++++------ fluid/PaddleRec/word2vec/train.py | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/fluid/PaddleRec/word2vec/README.md b/fluid/PaddleRec/word2vec/README.md index 01e0696a..3534fa71 100644 --- a/fluid/PaddleRec/word2vec/README.md +++ b/fluid/PaddleRec/word2vec/README.md @@ -29,7 +29,7 @@ This model implement a skip-gram model of word2vector. Preprocess the training data to generate a word dict. ```bash -python preprocess.py --data_path ./data/1-billion-word-language-modeling-benchmark-r13output/training-monolingual.tokenized.shuffled --dict_path data/1-billion_dict +python preprocess.py --data_path ./data/1-billion-word-language-modeling-benchmark-r13output/training-monolingual.tokenized.shuffled --is_local --dict_path data/1-billion_dict ``` if you would like to use our supported third party vocab, please set --other_dict_path as the directory of where you save the vocab you will use and set --with_other_dict flag on to using it. diff --git a/fluid/PaddleRec/word2vec/network_conf.py b/fluid/PaddleRec/word2vec/network_conf.py index 5b8e9513..16178c33 100644 --- a/fluid/PaddleRec/word2vec/network_conf.py +++ b/fluid/PaddleRec/word2vec/network_conf.py @@ -95,8 +95,7 @@ def skip_gram_word2vec(dict_size, capacity=64, feed_list=datas, name='py_reader', use_double_buffer=True) words = fluid.layers.read_file(py_reader) - - emb = fluid.layers.embedding( + target_emb = fluid.layers.embedding( input=words[0], is_sparse=is_sparse, size=[dict_size, embedding_size], @@ -104,16 +103,23 @@ def skip_gram_word2vec(dict_size, name='embeding', initializer=fluid.initializer.Normal(scale=1 / math.sqrt(dict_size)))) - + context_emb = fluid.layers.embedding( + input=words[1], + is_sparse=is_sparse, + size=[dict_size, embedding_size], + param_attr=fluid.ParamAttr( + name='embeding', + initializer=fluid.initializer.Normal(scale=1 / + math.sqrt(dict_size)))) cost, cost_nce, cost_hs = None, None, None if with_nce: - cost_nce = nce_layer(emb, words[1], embedding_size, dict_size, 5, + cost_nce = nce_layer(target_emb, words[1], embedding_size, dict_size, 5, "uniform", word_frequencys, None) cost = cost_nce if with_hsigmoid: - cost_hs = hsigmoid_layer(emb, words[1], words[2], words[3], dict_size, - is_sparse) + cost_hs = hsigmoid_layer(context_emb, words[0], words[2], words[3], + dict_size, is_sparse) cost = cost_hs if with_nce and with_hsigmoid: cost = fluid.layers.elementwise_add(cost_nce, cost_hs) diff --git a/fluid/PaddleRec/word2vec/train.py b/fluid/PaddleRec/word2vec/train.py index 40f7729f..ec4be60f 100644 --- a/fluid/PaddleRec/word2vec/train.py +++ b/fluid/PaddleRec/word2vec/train.py @@ -278,7 +278,7 @@ def train(args): optimizer = None if args.with_Adam: - optimizer = fluid.optimizer.Adam(learning_rate=1e-4) + optimizer = fluid.optimizer.Adam(learning_rate=1e-4, lazy_mode=True) else: optimizer = fluid.optimizer.SGD(learning_rate=1e-4) -- GitLab