未验证 提交 25dad426 编写于 作者: L LiuChiachi 提交者: GitHub

fix sample code for hapi.model.save (#26667)

* fix sample code for hapi.model.save, test=document_fix

* test=document_fix

* update usage of 2.0 API, test=document_fix

* fix bugs, return dygraph back to users while using model.save in dygraph

* fix code style
上级 7afb1df1
......@@ -891,10 +891,11 @@ class Model(object):
class Mnist(paddle.nn.Layer):
def __init__(self):
super(MyNet, self).__init__()
self._fc = Linear(784, 1, act='softmax')
super(Mnist, self).__init__()
self._fc = Linear(784, 10, act='softmax')
@paddle.jit.to_static # If save for inference in dygraph, need this
# If save for inference in dygraph, need this
@paddle.jit.to_static
def forward(self, x):
y = self._fc(x)
return y
......@@ -903,21 +904,18 @@ class Model(object):
device = hapi.set_device('cpu')
# if use static graph, do not set
paddle.disable_static(device) if dynamic else None
# inputs and labels are not required for dynamic graph.
input = hapi.Input([None, 784], 'float32', 'x')
label = hapi.Input([None, 1], 'int64', 'label')
model = hapi.Model(Mnist(), input, label)
optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameter_list=model.parameters())
model.prepare(optim,
paddle.nn.CrossEntropyLoss(),
hapi.metrics.Accuracy())
model.prepare(optim, paddle.nn.CrossEntropyLoss())
mnist_data = hapi.datasets.MNIST(mode='train', chw_format=False)
model.fit(mnist_data, epochs=1, batch_size=32, verbose=0)
model.save('checkpoint/test') # save for training
model.save('inference_model', False) # save for inference
"""
if ParallelEnv().local_rank == 0:
......@@ -1534,47 +1532,6 @@ class Model(object):
Returns:
list: The fetch variables' name list
Examples:
.. code-block:: python
import numpy as np
import paddle
from paddle.static import InputSpec
import paddle.incubate.hapi as hapi
from paddle.nn import Linear
from paddle.incubate.hapi.datasets.mnist import MNIST as MnistDataset
class Mnist(Layer):
def __init__(self, classifier_act=None):
super(Mnist, self).__init__()
self.fc = Linear(input_dim=784, output_dim=10, act="softmax")
@paddle.jit.to_static # In static mode, you need to delete this.
def forward(self, inputs):
outputs = self.fc(inputs)
return outputs
dynamic = True # False
device = hapi.set_device('gpu')
# if use static graph, do not set
paddle.disable_static(device) if dynamic else None
# inputs and labels are not required for dynamic graph.
input = InputSpec([None, 784], 'float32', 'x')
label = InputSpec([None, 1], 'int64', 'label')
model = hapi.Model(Mnist(), input, label)
optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameter_list=model.parameters())
model.prepare(optim,
paddle.nn.CrossEntropyLoss(),
hapi.metrics.Accuracy())
mnist_data = hapi.datasets.MNIST(mode='train', chw_format=False)
model.fit(mnist_data, epochs=1, batch_size=32, verbose=0)
model.save_inference_model('inference_model')
"""
def get_inout_spec(all_vars, return_name=False):
......@@ -1592,8 +1549,8 @@ class Model(object):
# the inputs of the model in running.
# 3. Make it Unnecessary to add `@paddle.jit.to_static` for users in dynamic mode.
if fluid.in_dygraph_mode():
with fluid.framework._dygraph_guard(None):
layer = self.network
fluid.disable_dygraph()
# 1. input check
prog_translator = ProgramTranslator()
......@@ -1631,7 +1588,8 @@ class Model(object):
if param_or_buffer.name in state_names_dict:
extra_info_dict['structured_name'] = state_names_dict[
param_or_buffer.name]
extra_info_dict['stop_gradient'] = param_or_buffer.stop_gradient
extra_info_dict[
'stop_gradient'] = param_or_buffer.stop_gradient
if isinstance(param_or_buffer, ParamBase):
extra_info_dict['trainable'] = param_or_buffer.trainable
extra_var_info[param_or_buffer.name] = extra_info_dict
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册