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

Adapt dam to py3

上级 d65c9edf
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]
......
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
......
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,7 +205,8 @@ 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',
print(
time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time())))
fluid.io.save_persistables(exe, save_path)
......@@ -235,7 +241,8 @@ 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',
print(
time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time())))
......
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:
......
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('------------------------------------------------')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册