提交 865dfbe5 编写于 作者: L Liu Yiqun

Use a new scope for inference in python unittest to avoid changing the value...

Use a new scope for inference in python unittest to avoid changing the value of variables for training.
上级 f95e05a3
...@@ -228,32 +228,34 @@ def infer(use_cuda, save_dirname=None): ...@@ -228,32 +228,34 @@ def infer(use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, # data using feed operators), and the fetch_targets (variables that
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
lod = [0, 4, 10] fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
word_data = create_random_lodtensor(lod, place, low=0, high=1)
trg_word = create_random_lodtensor(lod, place, low=0, high=1) lod = [0, 4, 10]
word_data = create_random_lodtensor(lod, place, low=0, high=1)
# Construct feed as a dictionary of {feed_target_name: feed_target_data} trg_word = create_random_lodtensor(lod, place, low=0, high=1)
# and results will contain a list of data corresponding to fetch_targets.
assert feed_target_names[0] == 'source_sequence' # Construct feed as a dictionary of {feed_target_name: feed_target_data}
assert feed_target_names[1] == 'target_sequence' # and results will contain a list of data corresponding to fetch_targets.
results = exe.run(inference_program, assert feed_target_names[0] == 'source_sequence'
feed={ assert feed_target_names[1] == 'target_sequence'
feed_target_names[0]: word_data, results = exe.run(inference_program,
feed_target_names[1]: trg_word, feed={
}, feed_target_names[0]: word_data,
fetch_list=fetch_targets, feed_target_names[1]: trg_word,
return_numpy=False) },
print(results[0].lod()) fetch_list=fetch_targets,
np_data = np.array(results[0]) return_numpy=False)
print("Inference shape: ", np_data.shape) print(results[0].lod())
print("Inference results: ", np_data) np_data = np.array(results[0])
print("Inference shape: ", np_data.shape)
print("Inference results: ", np_data)
def main(use_cuda): def main(use_cuda):
......
...@@ -72,23 +72,26 @@ def infer(use_cuda, save_dirname=None): ...@@ -72,23 +72,26 @@ def infer(use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, # data using feed operators), and the fetch_targets (variables that
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
# The input's dimension should be 2-D and the second dim is 13 fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
# The input data should be >= 0
batch_size = 10 # The input's dimension should be 2-D and the second dim is 13
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32") # The input data should be >= 0
assert feed_target_names[0] == 'x' batch_size = 10
results = exe.run(inference_program, tensor_x = numpy.random.uniform(0, 10,
feed={feed_target_names[0]: tensor_x}, [batch_size, 13]).astype("float32")
fetch_list=fetch_targets) assert feed_target_names[0] == 'x'
print("infer shape: ", results[0].shape) results = exe.run(inference_program,
print("infer results: ", results[0]) feed={feed_target_names[0]: tensor_x},
fetch_list=fetch_targets)
print("infer shape: ", results[0].shape)
print("infer results: ", results[0])
def main(use_cuda): def main(use_cuda):
......
...@@ -174,24 +174,26 @@ def infer(use_cuda, save_dirname=None): ...@@ -174,24 +174,26 @@ def infer(use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, # data using feed operators), and the fetch_targets (variables that
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
# The input's dimension of conv should be 4-D or 5-D. fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
# Use normilized image pixels as input data, which should be in the range [0, 1.0].
batch_size = 1 # The input's dimension of conv should be 4-D or 5-D.
tensor_img = numpy.random.rand(batch_size, 3, 32, 32).astype("float32") # Use normilized image pixels as input data, which should be in the range [0, 1.0].
batch_size = 1
# Construct feed as a dictionary of {feed_target_name: feed_target_data} tensor_img = numpy.random.rand(batch_size, 3, 32, 32).astype("float32")
# and results will contain a list of data corresponding to fetch_targets.
results = exe.run(inference_program, # Construct feed as a dictionary of {feed_target_name: feed_target_data}
feed={feed_target_names[0]: tensor_img}, # and results will contain a list of data corresponding to fetch_targets.
fetch_list=fetch_targets) results = exe.run(inference_program,
print("infer results: ", results[0]) feed={feed_target_names[0]: tensor_img},
fetch_list=fetch_targets)
print("infer results: ", results[0])
def main(net_type, use_cuda): def main(net_type, use_cuda):
......
...@@ -252,50 +252,60 @@ def infer(use_cuda, save_dirname=None): ...@@ -252,50 +252,60 @@ def infer(use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, # data using feed operators), and the fetch_targets (variables that
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
lod = [0, 4, 10] fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
word = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1)
pred = create_random_lodtensor(lod, place, low=0, high=pred_dict_len - 1) lod = [0, 4, 10]
ctx_n2 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) word = create_random_lodtensor(
ctx_n1 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) lod, place, low=0, high=word_dict_len - 1)
ctx_0 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) pred = create_random_lodtensor(
ctx_p1 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) lod, place, low=0, high=pred_dict_len - 1)
ctx_p2 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) ctx_n2 = create_random_lodtensor(
mark = create_random_lodtensor(lod, place, low=0, high=mark_dict_len - 1) lod, place, low=0, high=word_dict_len - 1)
ctx_n1 = create_random_lodtensor(
# Construct feed as a dictionary of {feed_target_name: feed_target_data} lod, place, low=0, high=word_dict_len - 1)
# and results will contain a list of data corresponding to fetch_targets. ctx_0 = create_random_lodtensor(
assert feed_target_names[0] == 'word_data' lod, place, low=0, high=word_dict_len - 1)
assert feed_target_names[1] == 'verb_data' ctx_p1 = create_random_lodtensor(
assert feed_target_names[2] == 'ctx_n2_data' lod, place, low=0, high=word_dict_len - 1)
assert feed_target_names[3] == 'ctx_n1_data' ctx_p2 = create_random_lodtensor(
assert feed_target_names[4] == 'ctx_0_data' lod, place, low=0, high=word_dict_len - 1)
assert feed_target_names[5] == 'ctx_p1_data' mark = create_random_lodtensor(
assert feed_target_names[6] == 'ctx_p2_data' lod, place, low=0, high=mark_dict_len - 1)
assert feed_target_names[7] == 'mark_data'
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
results = exe.run(inference_program, # and results will contain a list of data corresponding to fetch_targets.
feed={ assert feed_target_names[0] == 'word_data'
feed_target_names[0]: word, assert feed_target_names[1] == 'verb_data'
feed_target_names[1]: pred, assert feed_target_names[2] == 'ctx_n2_data'
feed_target_names[2]: ctx_n2, assert feed_target_names[3] == 'ctx_n1_data'
feed_target_names[3]: ctx_n1, assert feed_target_names[4] == 'ctx_0_data'
feed_target_names[4]: ctx_0, assert feed_target_names[5] == 'ctx_p1_data'
feed_target_names[5]: ctx_p1, assert feed_target_names[6] == 'ctx_p2_data'
feed_target_names[6]: ctx_p2, assert feed_target_names[7] == 'mark_data'
feed_target_names[7]: mark
}, results = exe.run(inference_program,
fetch_list=fetch_targets, feed={
return_numpy=False) feed_target_names[0]: word,
print(results[0].lod()) feed_target_names[1]: pred,
np_data = np.array(results[0]) feed_target_names[2]: ctx_n2,
print("Inference Shape: ", np_data.shape) feed_target_names[3]: ctx_n1,
feed_target_names[4]: ctx_0,
feed_target_names[5]: ctx_p1,
feed_target_names[6]: ctx_p2,
feed_target_names[7]: mark
},
fetch_list=fetch_targets,
return_numpy=False)
print(results[0].lod())
np_data = np.array(results[0])
print("Inference Shape: ", np_data.shape)
def main(use_cuda): def main(use_cuda):
......
...@@ -165,25 +165,27 @@ def infer(use_cuda, save_dirname=None, param_filename=None): ...@@ -165,25 +165,27 @@ def infer(use_cuda, save_dirname=None, param_filename=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, fetch_targets # data using feed operators), and the fetch_targets (variables that
] = fluid.io.load_inference_model(save_dirname, exe, param_filename) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names, fetch_targets
# The input's dimension of conv should be 4-D or 5-D. ] = fluid.io.load_inference_model(save_dirname, exe, param_filename)
# Use normilized image pixels as input data, which should be in the range [-1.0, 1.0].
batch_size = 1 # The input's dimension of conv should be 4-D or 5-D.
tensor_img = numpy.random.uniform(-1.0, 1.0, # Use normilized image pixels as input data, which should be in the range [-1.0, 1.0].
[batch_size, 1, 28, 28]).astype("float32") batch_size = 1
tensor_img = numpy.random.uniform(
# Construct feed as a dictionary of {feed_target_name: feed_target_data} -1.0, 1.0, [batch_size, 1, 28, 28]).astype("float32")
# and results will contain a list of data corresponding to fetch_targets.
results = exe.run(inference_program, # Construct feed as a dictionary of {feed_target_name: feed_target_data}
feed={feed_target_names[0]: tensor_img}, # and results will contain a list of data corresponding to fetch_targets.
fetch_list=fetch_targets) results = exe.run(inference_program,
print("infer results: ", results[0]) feed={feed_target_names[0]: tensor_img},
fetch_list=fetch_targets)
print("infer results: ", results[0])
def main(use_cuda, parallel, nn_type, combine): def main(use_cuda, parallel, nn_type, combine):
......
...@@ -251,13 +251,6 @@ def infer(use_cuda, save_dirname=None): ...@@ -251,13 +251,6 @@ def infer(use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that
# we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
def create_lod_tensor(data, lod=None): def create_lod_tensor(data, lod=None):
tensor = fluid.LoDTensor() tensor = fluid.LoDTensor()
if lod is None: if lod is None:
...@@ -275,44 +268,53 @@ def infer(use_cuda, save_dirname=None): ...@@ -275,44 +268,53 @@ def infer(use_cuda, save_dirname=None):
tensor.set(flattened_data, place) tensor.set(flattened_data, place)
return tensor return tensor
# Use the first data from paddle.dataset.movielens.test() as input inference_scope = fluid.core.Scope()
assert feed_target_names[0] == "user_id" with fluid.scope_guard(inference_scope):
user_id = create_lod_tensor([[1]]) # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded
assert feed_target_names[1] == "gender_id" # data using feed operators), and the fetch_targets (variables that
gender_id = create_lod_tensor([[1]]) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
assert feed_target_names[2] == "age_id" fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
age_id = create_lod_tensor([[0]])
# Use the first data from paddle.dataset.movielens.test() as input
assert feed_target_names[3] == "job_id" assert feed_target_names[0] == "user_id"
job_id = create_lod_tensor([[10]]) user_id = create_lod_tensor([[1]])
assert feed_target_names[4] == "movie_id" assert feed_target_names[1] == "gender_id"
movie_id = create_lod_tensor([[783]]) gender_id = create_lod_tensor([[1]])
assert feed_target_names[5] == "category_id" assert feed_target_names[2] == "age_id"
category_id = create_lod_tensor([[10], [8], [9]], [[0, 3]]) age_id = create_lod_tensor([[0]])
assert feed_target_names[6] == "movie_title" assert feed_target_names[3] == "job_id"
movie_title = create_lod_tensor([[1069], [4140], [2923], [710], [988]], job_id = create_lod_tensor([[10]])
[[0, 5]])
assert feed_target_names[4] == "movie_id"
# Construct feed as a dictionary of {feed_target_name: feed_target_data} movie_id = create_lod_tensor([[783]])
# and results will contain a list of data corresponding to fetch_targets.
results = exe.run(inference_program, assert feed_target_names[5] == "category_id"
feed={ category_id = create_lod_tensor([[10], [8], [9]], [[0, 3]])
feed_target_names[0]: user_id,
feed_target_names[1]: gender_id, assert feed_target_names[6] == "movie_title"
feed_target_names[2]: age_id, movie_title = create_lod_tensor([[1069], [4140], [2923], [710], [988]],
feed_target_names[3]: job_id, [[0, 5]])
feed_target_names[4]: movie_id,
feed_target_names[5]: category_id, # Construct feed as a dictionary of {feed_target_name: feed_target_data}
feed_target_names[6]: movie_title # and results will contain a list of data corresponding to fetch_targets.
}, results = exe.run(inference_program,
fetch_list=fetch_targets, feed={
return_numpy=False) feed_target_names[0]: user_id,
print("inferred score: ", np.array(results[0])) feed_target_names[1]: gender_id,
feed_target_names[2]: age_id,
feed_target_names[3]: job_id,
feed_target_names[4]: movie_id,
feed_target_names[5]: category_id,
feed_target_names[6]: movie_title
},
fetch_list=fetch_targets,
return_numpy=False)
print("inferred score: ", np.array(results[0]))
def main(use_cuda): def main(use_cuda):
......
...@@ -144,30 +144,32 @@ def infer(word_dict, use_cuda, save_dirname=None): ...@@ -144,30 +144,32 @@ def infer(word_dict, use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, # data using feed operators), and the fetch_targets (variables that
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
word_dict_len = len(word_dict) fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
lod = [0, 4, 10] word_dict_len = len(word_dict)
tensor_words = create_random_lodtensor(
lod, place, low=0, high=word_dict_len - 1) lod = [0, 4, 10]
tensor_words = create_random_lodtensor(
# Construct feed as a dictionary of {feed_target_name: feed_target_data} lod, place, low=0, high=word_dict_len - 1)
# and results will contain a list of data corresponding to fetch_targets.
assert feed_target_names[0] == "words" # Construct feed as a dictionary of {feed_target_name: feed_target_data}
results = exe.run(inference_program, # and results will contain a list of data corresponding to fetch_targets.
feed={feed_target_names[0]: tensor_words}, assert feed_target_names[0] == "words"
fetch_list=fetch_targets, results = exe.run(inference_program,
return_numpy=False) feed={feed_target_names[0]: tensor_words},
print(results[0].lod()) fetch_list=fetch_targets,
np_data = np.array(results[0]) return_numpy=False)
print("Inference Shape: ", np_data.shape) print(results[0].lod())
print("Inference results: ", np_data) np_data = np.array(results[0])
print("Inference Shape: ", np_data.shape)
print("Inference results: ", np_data)
def main(word_dict, nn_type, use_cuda): def main(word_dict, nn_type, use_cuda):
......
...@@ -138,42 +138,48 @@ def infer(use_cuda, save_dirname=None): ...@@ -138,42 +138,48 @@ def infer(use_cuda, save_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
# Use fluid.io.load_inference_model to obtain the inference program desc, inference_scope = fluid.core.Scope()
# the feed_target_names (the names of variables that will be feeded with fluid.scope_guard(inference_scope):
# data using feed operators), and the fetch_targets (variables that # Use fluid.io.load_inference_model to obtain the inference program desc,
# we want to obtain data from using fetch operators). # the feed_target_names (the names of variables that will be feeded
[inference_program, feed_target_names, # data using feed operators), and the fetch_targets (variables that
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) # we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
word_dict = paddle.dataset.imikolov.build_dict() fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
dict_size = len(word_dict)
word_dict = paddle.dataset.imikolov.build_dict()
# Setup inputs, by creating 4 words, the lod of which should be [0, 1] dict_size = len(word_dict)
lod = [0, 1]
first_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) # Setup inputs, by creating 4 words, the lod of which should be [0, 1]
second_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) lod = [0, 1]
third_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) first_word = create_random_lodtensor(
fourth_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) lod, place, low=0, high=dict_size - 1)
second_word = create_random_lodtensor(
assert feed_target_names[0] == 'firstw' lod, place, low=0, high=dict_size - 1)
assert feed_target_names[1] == 'secondw' third_word = create_random_lodtensor(
assert feed_target_names[2] == 'thirdw' lod, place, low=0, high=dict_size - 1)
assert feed_target_names[3] == 'forthw' fourth_word = create_random_lodtensor(
lod, place, low=0, high=dict_size - 1)
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets. assert feed_target_names[0] == 'firstw'
results = exe.run(inference_program, assert feed_target_names[1] == 'secondw'
feed={ assert feed_target_names[2] == 'thirdw'
feed_target_names[0]: first_word, assert feed_target_names[3] == 'forthw'
feed_target_names[1]: second_word,
feed_target_names[2]: third_word, # Construct feed as a dictionary of {feed_target_name: feed_target_data}
feed_target_names[3]: fourth_word # and results will contain a list of data corresponding to fetch_targets.
}, results = exe.run(inference_program,
fetch_list=fetch_targets, feed={
return_numpy=False) feed_target_names[0]: first_word,
print(results[0].lod()) feed_target_names[1]: second_word,
np_data = np.array(results[0]) feed_target_names[2]: third_word,
print("Inference Shape: ", np_data.shape) feed_target_names[3]: fourth_word
},
fetch_list=fetch_targets,
return_numpy=False)
print(results[0].lod())
np_data = np.array(results[0])
print("Inference Shape: ", np_data.shape)
def main(use_cuda, is_sparse, is_parallel): def main(use_cuda, is_sparse, is_parallel):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册