提交 468c8269 编写于 作者: Y Yibing Liu

Adapt dam to py3

上级 d65c9edf
import cPickle as pickle import six
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle.fluid as fluid
import utils.layers as layers import utils.layers as layers
...@@ -22,7 +22,7 @@ class Net(object): ...@@ -22,7 +22,7 @@ class Net(object):
mask_cache = dict() if self.use_mask_cache else None mask_cache = dict() if self.use_mask_cache else None
turns_data = [] turns_data = []
for i in xrange(self._max_turn_num): for i in six.moves.xrange(self._max_turn_num):
turn = fluid.layers.data( turn = fluid.layers.data(
name="turn_%d" % i, name="turn_%d" % i,
shape=[self._max_turn_len, 1], shape=[self._max_turn_len, 1],
...@@ -30,7 +30,7 @@ class Net(object): ...@@ -30,7 +30,7 @@ class Net(object):
turns_data.append(turn) turns_data.append(turn)
turns_mask = [] 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( turn_mask = fluid.layers.data(
name="turn_mask_%d" % i, name="turn_mask_%d" % i,
shape=[self._max_turn_len, 1], shape=[self._max_turn_len, 1],
...@@ -57,7 +57,7 @@ class Net(object): ...@@ -57,7 +57,7 @@ class Net(object):
Hr = response_emb Hr = response_emb
Hr_stack = [Hr] Hr_stack = [Hr]
for index in range(self._stack_num): for index in six.moves.xrange(self._stack_num):
Hr = layers.block( Hr = layers.block(
name="response_self_stack" + str(index), name="response_self_stack" + str(index),
query=Hr, query=Hr,
...@@ -71,7 +71,7 @@ class Net(object): ...@@ -71,7 +71,7 @@ class Net(object):
# context part # context part
sim_turns = [] sim_turns = []
for t in xrange(self._max_turn_num): for t in six.moves.xrange(self._max_turn_num):
Hu = fluid.layers.embedding( Hu = fluid.layers.embedding(
input=turns_data[t], input=turns_data[t],
size=[self._vocab_size + 1, self._emb_size], size=[self._vocab_size + 1, self._emb_size],
...@@ -81,7 +81,7 @@ class Net(object): ...@@ -81,7 +81,7 @@ class Net(object):
initializer=fluid.initializer.Normal(scale=0.1))) initializer=fluid.initializer.Normal(scale=0.1)))
Hu_stack = [Hu] Hu_stack = [Hu]
for index in range(self._stack_num): for index in six.moves.range(self._stack_num):
# share parameters # share parameters
Hu = layers.block( Hu = layers.block(
name="turn_self_stack" + str(index), name="turn_self_stack" + str(index),
...@@ -97,7 +97,7 @@ class Net(object): ...@@ -97,7 +97,7 @@ class Net(object):
# cross attention # cross attention
r_a_t_stack = [] r_a_t_stack = []
t_a_r_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( t_a_r = layers.block(
name="t_attend_r_" + str(index), name="t_attend_r_" + str(index),
query=Hu_stack[index], query=Hu_stack[index],
...@@ -127,7 +127,7 @@ class Net(object): ...@@ -127,7 +127,7 @@ class Net(object):
t_a_r = fluid.layers.stack(t_a_r_stack, axis=1) t_a_r = fluid.layers.stack(t_a_r_stack, axis=1)
r_a_t = fluid.layers.stack(r_a_t_stack, axis=1) r_a_t = fluid.layers.stack(r_a_t_stack, axis=1)
else: 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( t_a_r_stack[index] = fluid.layers.unsqueeze(
input=t_a_r_stack[index], axes=[1]) input=t_a_r_stack[index], axes=[1])
r_a_t_stack[index] = fluid.layers.unsqueeze( r_a_t_stack[index] = fluid.layers.unsqueeze(
...@@ -144,7 +144,7 @@ class Net(object): ...@@ -144,7 +144,7 @@ class Net(object):
if self.use_stack_op: if self.use_stack_op:
sim = fluid.layers.stack(sim_turns, axis=2) sim = fluid.layers.stack(sim_turns, axis=2)
else: else:
for index in xrange(len(sim_turns)): for index in six.moves.xrange(len(sim_turns)):
sim_turns[index] = fluid.layers.unsqueeze( sim_turns[index] = fluid.layers.unsqueeze(
input=sim_turns[index], axes=[2]) input=sim_turns[index], axes=[2])
# sim shape: [batch_size, 2*(stack_num+2), max_turn_num, max_turn_len, max_turn_len] # sim shape: [batch_size, 2*(stack_num+2), max_turn_num, max_turn_len, max_turn_len]
......
import os import os
import six
import numpy as np import numpy as np
import time import time
import argparse import argparse
...@@ -167,7 +168,7 @@ def test(args): ...@@ -167,7 +168,7 @@ def test(args):
score_path = os.path.join(args.save_path, 'score.txt') score_path = os.path.join(args.save_path, 'score.txt')
score_file = open(score_path, 'w') 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 = [] feed_list = []
for dev in xrange(dev_count): for dev in xrange(dev_count):
index = it * dev_count + dev index = it * dev_count + dev
......
import os import os
import six
import numpy as np import numpy as np
import time import time
import argparse import argparse
...@@ -6,9 +7,13 @@ import multiprocessing ...@@ -6,9 +7,13 @@ import multiprocessing
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import utils.reader as reader import utils.reader as reader
import cPickle as pickle
from utils.util import print_arguments 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 from model import Net
...@@ -172,12 +177,12 @@ def train(args): ...@@ -172,12 +177,12 @@ def train(args):
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())))
step = 0 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) shuffle_train = reader.unison_shuffle(train_data)
train_batches = reader.build_batches(shuffle_train, data_conf) train_batches = reader.build_batches(shuffle_train, data_conf)
ave_cost = 0.0 ave_cost = 0.0
for it in xrange(batch_num // dev_count): for it in six.moves.xrange(batch_num // dev_count):
feed_list = [] feed_list = []
for dev in xrange(dev_count): for dev in xrange(dev_count):
index = it * dev_count + dev index = it * dev_count + dev
...@@ -200,7 +205,8 @@ def train(args): ...@@ -200,7 +205,8 @@ def train(args):
if (args.save_path is not None) and (step % save_step == 0): if (args.save_path is not None) and (step % save_step == 0):
save_path = os.path.join(args.save_path, "step_" + str(step)) save_path = os.path.join(args.save_path, "step_" + str(step))
print("Save model at step %d ... " % step) print("Save model at step %d ... " % step)
print(time.strftime('%Y-%m-%d %H:%M:%S', print(
time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time()))) time.localtime(time.time())))
fluid.io.save_persistables(exe, save_path) fluid.io.save_persistables(exe, save_path)
...@@ -235,7 +241,8 @@ def train(args): ...@@ -235,7 +241,8 @@ def train(args):
for p_at in result: for p_at in result:
out_file.write(str(p_at) + '\n') out_file.write(str(p_at) + '\n')
print('finish evaluation') print('finish evaluation')
print(time.strftime('%Y-%m-%d %H:%M:%S', print(
time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time()))) time.localtime(time.time())))
......
import cPickle as pickle
import numpy as np 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): def unison_shuffle(data, seed=None):
if seed is not None: if seed is not None:
......
import six
def print_arguments(args): def print_arguments(args):
print('----------- Configuration Arguments -----------') 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('%s: %s' % (arg, value))
print('------------------------------------------------') print('------------------------------------------------')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册