diff --git a/fluid/deep_attention_matching_net/model.py b/fluid/deep_attention_matching_net/model.py index 1aab7cf28ce5198ba6cbf47b791180f5d799319a..a558d149ff51e7ef1905efe5bdbff0878d5e2c75 100644 --- a/fluid/deep_attention_matching_net/model.py +++ b/fluid/deep_attention_matching_net/model.py @@ -1,4 +1,4 @@ -import cPickle as pickle +import six import numpy as np import paddle.fluid as fluid import utils.layers as layers @@ -22,7 +22,7 @@ class Net(object): mask_cache = dict() if self.use_mask_cache else None turns_data = [] - for i in xrange(self._max_turn_num): + for i in six.moves.xrange(self._max_turn_num): turn = fluid.layers.data( name="turn_%d" % i, shape=[self._max_turn_len, 1], @@ -30,7 +30,7 @@ class Net(object): turns_data.append(turn) turns_mask = [] - for i in xrange(self._max_turn_num): + for i in six.moves.xrange(self._max_turn_num): turn_mask = fluid.layers.data( name="turn_mask_%d" % i, shape=[self._max_turn_len, 1], @@ -57,7 +57,7 @@ class Net(object): Hr = response_emb Hr_stack = [Hr] - for index in range(self._stack_num): + for index in six.moves.xrange(self._stack_num): Hr = layers.block( name="response_self_stack" + str(index), query=Hr, @@ -71,7 +71,7 @@ class Net(object): # context part sim_turns = [] - for t in xrange(self._max_turn_num): + for t in six.moves.xrange(self._max_turn_num): Hu = fluid.layers.embedding( input=turns_data[t], size=[self._vocab_size + 1, self._emb_size], @@ -81,7 +81,7 @@ class Net(object): initializer=fluid.initializer.Normal(scale=0.1))) Hu_stack = [Hu] - for index in range(self._stack_num): + for index in six.moves.range(self._stack_num): # share parameters Hu = layers.block( name="turn_self_stack" + str(index), @@ -97,7 +97,7 @@ class Net(object): # cross attention r_a_t_stack = [] t_a_r_stack = [] - for index in range(self._stack_num + 1): + for index in six.moves.range(self._stack_num + 1): t_a_r = layers.block( name="t_attend_r_" + str(index), query=Hu_stack[index], @@ -127,7 +127,7 @@ class Net(object): t_a_r = fluid.layers.stack(t_a_r_stack, axis=1) r_a_t = fluid.layers.stack(r_a_t_stack, axis=1) else: - for index in xrange(len(t_a_r_stack)): + for index in six.moves.xrange(len(t_a_r_stack)): t_a_r_stack[index] = fluid.layers.unsqueeze( input=t_a_r_stack[index], axes=[1]) r_a_t_stack[index] = fluid.layers.unsqueeze( @@ -144,7 +144,7 @@ class Net(object): if self.use_stack_op: sim = fluid.layers.stack(sim_turns, axis=2) else: - for index in xrange(len(sim_turns)): + for index in six.moves.xrange(len(sim_turns)): sim_turns[index] = fluid.layers.unsqueeze( input=sim_turns[index], axes=[2]) # sim shape: [batch_size, 2*(stack_num+2), max_turn_num, max_turn_len, max_turn_len] diff --git a/fluid/deep_attention_matching_net/test_and_evaluate.py b/fluid/deep_attention_matching_net/test_and_evaluate.py index 13450db196e1c8e2017a44ac23d29fd164a1c29f..4531f6b11df36ed97f1c1c7ac508f14e9800d67e 100644 --- a/fluid/deep_attention_matching_net/test_and_evaluate.py +++ b/fluid/deep_attention_matching_net/test_and_evaluate.py @@ -1,4 +1,5 @@ import os +import six import numpy as np import time import argparse @@ -167,7 +168,7 @@ def test(args): score_path = os.path.join(args.save_path, 'score.txt') score_file = open(score_path, 'w') - for it in xrange(test_batch_num // dev_count): + for it in six.moves.xrange(test_batch_num // dev_count): feed_list = [] for dev in xrange(dev_count): index = it * dev_count + dev diff --git a/fluid/deep_attention_matching_net/train_and_evaluate.py b/fluid/deep_attention_matching_net/train_and_evaluate.py index 3e46d6e1fad148ffa56245d7aa8b62e81bb98033..d07073f8489227a38e7e622fdb67fbb8ef314aab 100644 --- a/fluid/deep_attention_matching_net/train_and_evaluate.py +++ b/fluid/deep_attention_matching_net/train_and_evaluate.py @@ -1,4 +1,5 @@ import os +import six import numpy as np import time import argparse @@ -6,9 +7,13 @@ import multiprocessing import paddle import paddle.fluid as fluid import utils.reader as reader -import cPickle as pickle from utils.util import print_arguments +try: + import cPickle as pickle #python 2 +except ImportError as e: + import pickle #python 3 + from model import Net @@ -172,12 +177,12 @@ def train(args): print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) step = 0 - for epoch in xrange(args.num_scan_data): + for epoch in six.moves.xrange(args.num_scan_data): shuffle_train = reader.unison_shuffle(train_data) train_batches = reader.build_batches(shuffle_train, data_conf) ave_cost = 0.0 - for it in xrange(batch_num // dev_count): + for it in six.moves.xrange(batch_num // dev_count): feed_list = [] for dev in xrange(dev_count): index = it * dev_count + dev @@ -200,8 +205,9 @@ def train(args): if (args.save_path is not None) and (step % save_step == 0): save_path = os.path.join(args.save_path, "step_" + str(step)) print("Save model at step %d ... " % step) - print(time.strftime('%Y-%m-%d %H:%M:%S', - time.localtime(time.time()))) + print( + time.strftime('%Y-%m-%d %H:%M:%S', + time.localtime(time.time()))) fluid.io.save_persistables(exe, save_path) score_path = os.path.join(args.save_path, 'score.' + str(step)) @@ -235,8 +241,9 @@ def train(args): for p_at in result: out_file.write(str(p_at) + '\n') print('finish evaluation') - print(time.strftime('%Y-%m-%d %H:%M:%S', - time.localtime(time.time()))) + print( + time.strftime('%Y-%m-%d %H:%M:%S', + time.localtime(time.time()))) if __name__ == '__main__': diff --git a/fluid/deep_attention_matching_net/utils/reader.py b/fluid/deep_attention_matching_net/utils/reader.py index 4d17082e5590523a2a4ed5b238370a2100f53eb3..d141fe87df8a05135342e6020c044021a788db20 100644 --- a/fluid/deep_attention_matching_net/utils/reader.py +++ b/fluid/deep_attention_matching_net/utils/reader.py @@ -1,6 +1,10 @@ -import cPickle as pickle import numpy as np +try: + import cPickle as pickle #python 2 +except ImportError as e: + import pickle #python 3 + def unison_shuffle(data, seed=None): if seed is not None: diff --git a/fluid/deep_attention_matching_net/utils/util.py b/fluid/deep_attention_matching_net/utils/util.py index f1c8c8765821737ce85dea000dcc7741630f81d9..10206b1deffbe5cecdfbdba9232e4ce110eb4d21 100644 --- a/fluid/deep_attention_matching_net/utils/util.py +++ b/fluid/deep_attention_matching_net/utils/util.py @@ -1,6 +1,9 @@ +import six + + def print_arguments(args): print('----------- Configuration Arguments -----------') - for arg, value in sorted(vars(args).iteritems()): + for arg, value in sorted(six.iteritems(vars(args))): print('%s: %s' % (arg, value)) print('------------------------------------------------')