提交 790d5226 编写于 作者: G Ghost Under Moon 提交者: hong

give warnings when save a model without any parameters (#19931)

* give warnings when save a model without any parameters test=develop

* delete one line comment test=develop
上级 f254b477
...@@ -217,6 +217,13 @@ def save_vars(executor, ...@@ -217,6 +217,13 @@ def save_vars(executor,
vars=list(filter(predicate, main_program.list_vars())), vars=list(filter(predicate, main_program.list_vars())),
filename=filename) filename=filename)
else: else:
# give warning when there is no var in model
if len(list(vars)) == 0:
warnings.warn(
"no variable in your model, please ensure there are any variables in your model to save"
)
return None
save_program = Program() save_program = Program()
save_block = save_program.global_block() save_block = save_program.global_block()
......
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import warnings
import unittest
import paddle
import paddle.fluid as fluid
from paddle.fluid.layers.device import get_places
from paddle.fluid.executor import as_numpy
class TestSaveModelWithoutVar(unittest.TestCase):
def test_no_var_save(self):
data = fluid.layers.data(
name='data',
shape=[-1, 1],
dtype='float32',
append_batch_size=False)
data_plus = data + 1
if fluid.core.is_compiled_with_cuda():
place = fluid.core.CUDAPlace(0)
else:
place = fluid.core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
fluid.io.save_inference_model(
dirname='test',
feeded_var_names=['data'],
target_vars=[data_plus],
executor=exe,
model_filename='model',
params_filename='params')
expected_warn = "no variable in your model, please ensure there are any variables in your model to save"
self.assertTrue(len(w) > 0)
self.assertTrue(expected_warn == str(w[0].message))
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册