Remove Input requirement in dygraph for Model#27557
Remove Input requirement in dygraph for Model#27557guoshengCS merged 21 commits intoPaddlePaddle:developfrom
Conversation
… remove-input-requirment-in-dygraph-CModel
|
Thanks for your contribution! |
… remove-input-requirment-in-dygraph-CModel
… remove-input-requirment-in-dygraph-CModel
… remove-input-requirment-in-dygraph-CModel
python/paddle/hapi/model.py
Outdated
| if isinstance(inputs, list): | ||
| self._shapes = [list(input.shape) for input in inputs] | ||
| elif isinstance(inputs, dict): | ||
| self._shapes = [list(inputs[name]) for name in inputs] |
There was a problem hiding this comment.
这里是否是inputs[name].shape呢
python/paddle/hapi/model.py
Outdated
| for i in range(len(data) - 1) | ||
| ] | ||
| self._is_shape_inferred = True | ||
| self._inputs = self._verify_spec(None, self._shapes, True) |
There was a problem hiding this comment.
_run_one_epoch 会调用train_batch等方法,应该不用在这里也另外实现这个功能
… remove-input-requirment-in-dygraph-CModel
python/paddle/hapi/model.py
Outdated
| 'test_batch': 0 | ||
| } | ||
|
|
||
| self._shapes = None |
There was a problem hiding this comment.
建议使用一个更达义的变量名,比如self._input_shapes
| self._optimizer = None | ||
| self._optimizer = None | ||
| self._shapes = None | ||
| self._is_shape_inferred = False |
There was a problem hiding this comment.
建议一个更达义的变量名替换self._shapes。这里如果成员变量inputs初始化后是不变的话,是否不需要一个额外的shape变量。只需要一个成员函数解析一下self._inputs即可?
如果保留这个shape变量的话,建议修改下变量名。
There was a problem hiding this comment.
动、静态图下在Model初始化时都需要对self._inputs进行初始化,因为目前train_batch, eval_batch等也需要用到self._inputs。因此需要在动态图下用户没提供inputs时用self._input_shapes记录下在运行模型推导出的输入shape,以便能通过此次更新后的self._verify_spec根据shape获取一个可传递给paddle.to_static的较为合理的self._inputs
| self._shapes = [list(input.shape) for input in inputs] | ||
| elif isinstance(inputs, dict): | ||
| self._shapes = [list(inputs[name].shape) for name in inputs] | ||
|
|
There was a problem hiding this comment.
如上个comment,elif的这部分逻辑其实可以抽离到一个成员函数里,用于解析inputs的shape
| print(loss) | ||
| """ | ||
| return self._adapter.eval_batch(inputs, labels) | ||
| loss = self._adapter.eval_batch(inputs, labels) |
| print(out) | ||
| """ | ||
| return self._adapter.test_batch(inputs) | ||
| loss = self._adapter.test_batch(inputs) |
python/paddle/hapi/model.py
Outdated
| layer = self.network | ||
| if self._shapes is None: # No provided or inferred | ||
| raise RuntimeError( | ||
| "Saving inference model needs `inputs` or running before saving." |
There was a problem hiding this comment.
报错文案可以优化下。这里只给出了原因,也可以给出一些解决方式。比如:
- 提示用户指定inputs
- 可以输入数据,执行一次训练用于shape的推导。
There was a problem hiding this comment.
感谢!已修改,在报错信息里进行了更详细的说明
python/paddle/hapi/model.py
Outdated
| ) | ||
| if self._is_shape_inferred: | ||
| warnings.warn( | ||
| 'Saving actual input shapes only if `inputs` is provided, otherwise variable input dimension is immutable.' |
There was a problem hiding this comment.
这个报错文案是否可以优化下。这里warning信息里有两点要提示用户:
- 提醒用户Model里没有指定inputs,这里保存时将使用从实际数据推导出来的shape信息保存模型
- 打印出推导出来的shape信息,让用户知道自己保存的输入shape
| for i, n in enumerate(arg_names) | ||
| ] | ||
| else: | ||
| out_specs = [Input(name=n, shape=[None]) for n in arg_names] |
There was a problem hiding this comment.
这里的else分支应该可以去掉吧。直接返回空就可以了,后续依靠推导来得到。
There was a problem hiding this comment.
train_batch, eval_batch目前会用到self._inputs,因此Model初始化时所初始化的self._inputs不能是None。
… remove-input-requirment-in-dygraph-CModel
PR types
New features
PR changes
APIs
Describe
1.移除了动态图下
Model对inputs的需求, #27272(如果动态图下提供
inputs,保存预测模型将能够保存正确的输入shape,是完全正确的状态;如果动态图下不提供inputs也不需要保存预测模型,也不会有任何影响)2.动态图下未指定
inputs,并需要保存预测模型时:1)未运行模型,直接保存:报错
RuntimeError2)运行
model.fit,model.train_batch,model.eval_batch,或者model.test_batch之后可以保存预测模型,不报错,但报UserWarning,提示用户保存的不是确切的shape(不支持保存batch_size等可变输入维度),并提示用户输入inputs以保存更准确的input shape