提交 5e23ec05 编写于 作者: C chenxuyi 提交者: Meiyim

fix paddleTensor and paddleInference

上级 e5d95cb9
......@@ -22,13 +22,16 @@ import argparse
import numpy as np
import multiprocessing
import logging
import paddle.fluid as fluid
import reader.task_reader as task_reader
from model.ernie import ErnieConfig, ErnieModel
from utils.args import ArgumentGroup, print_arguments
from model.ernie_v1 import ErnieConfig, ErnieModel
from utils.args import ArgumentGroup, print_arguments, prepare_logger
from utils.init import init_pretraining_params
log = logging.getLogger()
# yapf: disable
parser = argparse.ArgumentParser(__doc__)
model_g = ArgumentGroup(parser, "model", "model configuration and paths.")
......@@ -67,7 +70,6 @@ def create_model(args, pyreader_name, ernie_config):
src_ids=src_ids,
position_ids=pos_ids,
sentence_ids=sent_ids,
task_ids=task_ids,
input_mask=input_mask,
config=ernie_config)
......@@ -179,6 +181,7 @@ def main(args):
if __name__ == '__main__':
prepare_logger(log)
args = parser.parse_args()
print_arguments(args)
......
......@@ -52,7 +52,7 @@ def create_model(args,
elif is_regression:
labels = fluid.layers.data(name='6', shape=[-1, 1], dtype='float32')
pyreader = fluid.io.DataLoader.from_generator(feed_list=[src_ids, sent_ids, pos_ids, task_ids, input_mask, qids],
pyreader = fluid.io.DataLoader.from_generator(feed_list=[src_ids, sent_ids, pos_ids, task_ids, input_mask, labels, qids],
capacity=70,
iterable=False)
......
......@@ -130,6 +130,7 @@ def main(args):
if not args.use_cuda:
log.info("disable gpu")
config.disable_gpu()
config.switch_ir_optim(True)
else:
log.info("using gpu")
config.enable_use_gpu(1024)
......@@ -162,8 +163,7 @@ def main(args):
# parse outputs
output = outputs[0]
output_data = output.data.float_data()
batch_result = np.array(output_data).reshape(output.shape)
batch_result = output.as_ndarray()
for single_example_probs in batch_result:
print('\t'.join(map(str, single_example_probs.tolist())))
index += 1
......@@ -173,13 +173,7 @@ def main(args):
def array2tensor(ndarray):
""" convert numpy array to PaddleTensor"""
assert isinstance(ndarray, np.ndarray), "input type must be np.ndarray"
if ndarray.dtype == np.float32:
dtype = PaddleDType.FLOAT32
elif ndarray.dtype == np.int64:
dtype = PaddleDType.INT64
else:
raise ValueError("{} type ndarray is unsupported".format(ndarray.dtype))
tensor = PaddleTensor(data=ndarray, name="data")
tensor = PaddleTensor(data=ndarray)
return tensor
if __name__ == '__main__':
......
......@@ -73,7 +73,7 @@ def slot_to_paddlearray(slot):
raise RuntimeError('know type %s' % slot.type)
num = len(slot.data) // struct.calcsize(type_str)
arr = struct.unpack('%d%s' % (num, type_str), slot.data)
ret = core.PaddleTensor(name='1', data=np.array(arr, dtype=dtype).reshape(slot.dims))
ret = core.PaddleTensor(np.array(arr, dtype=dtype).reshape(slot.dims))
return ret
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册