未验证 提交 44e3306b 编写于 作者: Z zhouzj 提交者: GitHub

fix demo's bug (#1651)

* fix demo's bug

* fix code style
上级 210308ac
import argparse import argparse
import sys import sys
import time import time
import math
import unittest
import contextlib
import numpy as np import numpy as np
import six import os
import paddle import paddle
import net import net
import utils import utils
...@@ -72,7 +69,7 @@ def infer_epoch(args, vocab_size, test_reader, use_cuda, i2w): ...@@ -72,7 +69,7 @@ def infer_epoch(args, vocab_size, test_reader, use_cuda, i2w):
values, pred = net.infer_network(vocab_size, emb_size) values, pred = net.infer_network(vocab_size, emb_size)
for epoch in range(start_index, last_index + 1): for epoch in range(start_index, last_index + 1):
copy_program = main_program.clone() copy_program = main_program.clone()
model_path = model_dir + "/pass-" + str(epoch) model_path = os.path.join(model_dir, "pass-" + str(epoch))
paddle.static.load(copy_program, model_path, exe) paddle.static.load(copy_program, model_path, exe)
if args.emb_quant: if args.emb_quant:
config = { config = {
...@@ -92,29 +89,33 @@ def infer_epoch(args, vocab_size, test_reader, use_cuda, i2w): ...@@ -92,29 +89,33 @@ def infer_epoch(args, vocab_size, test_reader, use_cuda, i2w):
for data in test_reader(): for data in test_reader():
step_id += 1 step_id += 1
b_size = len([dat[0] for dat in data]) b_size = len([dat[0] for dat in data])
wa = np.array( wa = np.array([dat[0]
[dat[0] for dat in data]).astype("int64").reshape( for dat in data]).astype("int64").reshape(
b_size, 1) b_size, 1)
wb = np.array( wb = np.array([dat[1]
[dat[1] for dat in data]).astype("int64").reshape( for dat in data]).astype("int64").reshape(
b_size, 1) b_size, 1)
wc = np.array( wc = np.array([dat[2]
[dat[2] for dat in data]).astype("int64").reshape( for dat in data]).astype("int64").reshape(
b_size, 1) b_size, 1)
label = [dat[3] for dat in data] label = [dat[3] for dat in data]
input_word = [dat[4] for dat in data] input_word = [dat[4] for dat in data]
para = exe.run(copy_program, para = exe.run(
feed={ copy_program,
"analogy_a": wa, feed={
"analogy_b": wb, "analogy_a":
"analogy_c": wc, wa,
"all_label": "analogy_b":
np.arange(vocab_size).reshape( wb,
vocab_size, 1).astype("int64"), "analogy_c":
}, wc,
fetch_list=[pred.name, values], "all_label":
return_numpy=False) np.arange(vocab_size).reshape(vocab_size,
1).astype("int64"),
},
fetch_list=[pred.name, values],
return_numpy=False)
pre = np.array(para[0]) pre = np.array(para[0])
val = np.array(para[1]) val = np.array(para[1])
for ii in range(len(label)): for ii in range(len(label)):
...@@ -156,24 +157,27 @@ def infer_step(args, vocab_size, test_reader, use_cuda, i2w): ...@@ -156,24 +157,27 @@ def infer_step(args, vocab_size, test_reader, use_cuda, i2w):
for data in test_reader(): for data in test_reader():
step_id += 1 step_id += 1
b_size = len([dat[0] for dat in data]) b_size = len([dat[0] for dat in data])
wa = np.array( wa = np.array([dat[0] for dat in
[dat[0] for dat in data]).astype("int64").reshape( data]).astype("int64").reshape(
b_size, 1) b_size, 1)
wb = np.array( wb = np.array([dat[1] for dat in
[dat[1] for dat in data]).astype("int64").reshape( data]).astype("int64").reshape(
b_size, 1) b_size, 1)
wc = np.array( wc = np.array([dat[2] for dat in
[dat[2] for dat in data]).astype("int64").reshape( data]).astype("int64").reshape(
b_size, 1) b_size, 1)
label = [dat[3] for dat in data] label = [dat[3] for dat in data]
input_word = [dat[4] for dat in data] input_word = [dat[4] for dat in data]
para = exe.run( para = exe.run(
copy_program, copy_program,
feed={ feed={
"analogy_a": wa, "analogy_a":
"analogy_b": wb, wa,
"analogy_c": wc, "analogy_b":
wb,
"analogy_c":
wc,
"all_label": "all_label":
np.arange(vocab_size).reshape(vocab_size, 1), np.arange(vocab_size).reshape(vocab_size, 1),
}, },
......
...@@ -131,7 +131,7 @@ def infer_network(vocab_size, emb_size): ...@@ -131,7 +131,7 @@ def infer_network(vocab_size, emb_size):
emb_c = paddle.static.nn.embedding( emb_c = paddle.static.nn.embedding(
input=analogy_c, size=[vocab_size, emb_size], param_attr="emb") input=analogy_c, size=[vocab_size, emb_size], param_attr="emb")
target = paddle.add(paddle.add(emb_b, -emb_a), emb_c) target = paddle.add(paddle.add(emb_b, -emb_a), emb_c)
emb_all_label_l2 = paddle.linalg.norm(emb_all_label, p=2, axis=1) emb_all_label_l2 = F.normalize(emb_all_label, p=2, axis=1)
dist = paddle.matmul(x=target, y=emb_all_label_l2, transpose_y=True) dist = paddle.matmul(x=target, y=emb_all_label_l2, transpose_y=True)
values, pred_idx = paddle.topk(x=dist, k=4) values, pred_idx = paddle.topk(x=dist, k=4)
return values, pred_idx return values, pred_idx
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册