提交 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,6 +228,8 @@ def infer(use_cuda, save_dirname=None): ...@@ -228,6 +228,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
......
...@@ -72,6 +72,8 @@ def infer(use_cuda, save_dirname=None): ...@@ -72,6 +72,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
...@@ -82,7 +84,8 @@ def infer(use_cuda, save_dirname=None): ...@@ -82,7 +84,8 @@ def infer(use_cuda, save_dirname=None):
# The input's dimension should be 2-D and the second dim is 13 # The input's dimension should be 2-D and the second dim is 13
# The input data should be >= 0 # The input data should be >= 0
batch_size = 10 batch_size = 10
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32") tensor_x = numpy.random.uniform(0, 10,
[batch_size, 13]).astype("float32")
assert feed_target_names[0] == 'x' assert feed_target_names[0] == 'x'
results = exe.run(inference_program, results = exe.run(inference_program,
feed={feed_target_names[0]: tensor_x}, feed={feed_target_names[0]: tensor_x},
......
...@@ -174,6 +174,8 @@ def infer(use_cuda, save_dirname=None): ...@@ -174,6 +174,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
......
...@@ -252,6 +252,8 @@ def infer(use_cuda, save_dirname=None): ...@@ -252,6 +252,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
...@@ -260,14 +262,22 @@ def infer(use_cuda, save_dirname=None): ...@@ -260,14 +262,22 @@ def infer(use_cuda, save_dirname=None):
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
lod = [0, 4, 10] lod = [0, 4, 10]
word = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) word = create_random_lodtensor(
pred = create_random_lodtensor(lod, place, low=0, high=pred_dict_len - 1) lod, place, low=0, high=word_dict_len - 1)
ctx_n2 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) pred = create_random_lodtensor(
ctx_n1 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) lod, place, low=0, high=pred_dict_len - 1)
ctx_0 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) ctx_n2 = create_random_lodtensor(
ctx_p1 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) lod, place, low=0, high=word_dict_len - 1)
ctx_p2 = create_random_lodtensor(lod, place, low=0, high=word_dict_len - 1) ctx_n1 = 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_0 = create_random_lodtensor(
lod, place, low=0, high=word_dict_len - 1)
ctx_p1 = create_random_lodtensor(
lod, place, low=0, high=word_dict_len - 1)
ctx_p2 = create_random_lodtensor(
lod, place, low=0, high=word_dict_len - 1)
mark = create_random_lodtensor(
lod, place, low=0, high=mark_dict_len - 1)
# Construct feed as a dictionary of {feed_target_name: feed_target_data} # Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets. # and results will contain a list of data corresponding to fetch_targets.
......
...@@ -165,6 +165,8 @@ def infer(use_cuda, save_dirname=None, param_filename=None): ...@@ -165,6 +165,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
...@@ -175,8 +177,8 @@ def infer(use_cuda, save_dirname=None, param_filename=None): ...@@ -175,8 +177,8 @@ def infer(use_cuda, save_dirname=None, param_filename=None):
# The input's dimension of conv should be 4-D or 5-D. # The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range [-1.0, 1.0]. # Use normilized image pixels as input data, which should be in the range [-1.0, 1.0].
batch_size = 1 batch_size = 1
tensor_img = numpy.random.uniform(-1.0, 1.0, tensor_img = numpy.random.uniform(
[batch_size, 1, 28, 28]).astype("float32") -1.0, 1.0, [batch_size, 1, 28, 28]).astype("float32")
# Construct feed as a dictionary of {feed_target_name: feed_target_data} # Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets. # and results will contain a list of data corresponding to fetch_targets.
......
...@@ -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,6 +268,15 @@ def infer(use_cuda, save_dirname=None): ...@@ -275,6 +268,15 @@ def infer(use_cuda, save_dirname=None):
tensor.set(flattened_data, place) tensor.set(flattened_data, place)
return tensor return tensor
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# 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)
# Use the first data from paddle.dataset.movielens.test() as input # Use the first data from paddle.dataset.movielens.test() as input
assert feed_target_names[0] == "user_id" assert feed_target_names[0] == "user_id"
user_id = create_lod_tensor([[1]]) user_id = create_lod_tensor([[1]])
......
...@@ -144,6 +144,8 @@ def infer(word_dict, use_cuda, save_dirname=None): ...@@ -144,6 +144,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
......
...@@ -138,6 +138,8 @@ def infer(use_cuda, save_dirname=None): ...@@ -138,6 +138,8 @@ 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)
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc, # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded # the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that # data using feed operators), and the fetch_targets (variables that
...@@ -150,10 +152,14 @@ def infer(use_cuda, save_dirname=None): ...@@ -150,10 +152,14 @@ def infer(use_cuda, save_dirname=None):
# Setup inputs, by creating 4 words, the lod of which should be [0, 1] # Setup inputs, by creating 4 words, the lod of which should be [0, 1]
lod = [0, 1] lod = [0, 1]
first_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) first_word = create_random_lodtensor(
second_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) lod, place, low=0, high=dict_size - 1)
third_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1) second_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)
third_word = create_random_lodtensor(
lod, place, low=0, high=dict_size - 1)
fourth_word = create_random_lodtensor(
lod, place, low=0, high=dict_size - 1)
assert feed_target_names[0] == 'firstw' assert feed_target_names[0] == 'firstw'
assert feed_target_names[1] == 'secondw' assert feed_target_names[1] == 'secondw'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册