未验证 提交 0c3b3698 编写于 作者: N Nyakku Shigure 提交者: GitHub

[xdoctest] reformat example code with google style in `paddle/jit` (#55645)

* [xdoctest] reformat example code for paddle.jit.api

* test=docs_preview

* add some ... for decorator

* skip some example, test=docs_preview

* add ..., test=docs_preview

* skip some test, test=docs_preview

* more jit files, test=docs_preview

* remove some empty lines, test=docs_preview

* format program translator, test=docs_preview

* remove a blank line, test=docs_preview

* skip translated layer.program, test=docs_preview

* fix doc format, test=docs_preview
上级 eee4b8fb
...@@ -126,29 +126,28 @@ def _dygraph_to_static_func_(dygraph_func): ...@@ -126,29 +126,28 @@ def _dygraph_to_static_func_(dygraph_func):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.fluid as fluid >>> from paddle.jit.api import dygraph_to_static_func
import numpy as np
from paddle.jit.api import dygraph_to_static_func >>> @dygraph_to_static_func
... def func(x):
@dygraph_to_static_func ... if paddle.mean(x) < 0:
def func(x): ... x_v = x - 1
if paddle.mean(x) < 0: ... else:
x_v = x - 1 ... x_v = x + 1
else: ...
x_v = x + 1 ... return x_v
...
return x_v >>> paddle.enable_static()
>>> x = paddle.full(shape=[3, 3], fill_value=0, dtype='float64')
x = paddle.full(shape=[3, 3], fill_value=0, dtype='float64')
>>> x_v = func(x)
x_v = func(x) >>> exe = paddle.static.Executor(paddle.CPUPlace())
exe = fluid.Executor(fluid.CPUPlace()) >>> out = exe.run(fetch_list=[x_v])
out = exe.run(fetch_list=[x_v]) >>> print(out[0])
print(out[0]) [[1. 1. 1.]
# [[1. 1. 1.] [1. 1. 1.]
# [1. 1. 1.] [1. 1. 1.]]
# [1. 1. 1.]]
""" """
...@@ -202,18 +201,16 @@ def ignore_module(modules: list[Any]): ...@@ -202,18 +201,16 @@ def ignore_module(modules: list[Any]):
Examples: Examples:
.. code-block:: python .. code-block:: python
import scipy >>> import scipy
import astor >>> import astor
import paddle
from paddle.jit import ignore_module
modules = [
scipy,
astor
]
ignore_module(modules) >>> import paddle
>>> from paddle.jit import ignore_module
>>> modules = [
... scipy,
... astor,
... ]
>>> ignore_module(modules)
""" """
add_ignore_module(modules) add_ignore_module(modules)
...@@ -263,20 +260,23 @@ def to_static( ...@@ -263,20 +260,23 @@ def to_static(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
from paddle.jit import to_static >>> import paddle
>>> from paddle.jit import to_static
@to_static
def func(x): >>> @to_static
if paddle.mean(x) < 0: >>> def func(x):
x_v = x - 1 ... if paddle.mean(x) < 0:
else: ... x_v = x - 1
x_v = x + 1 ... else:
return x_v ... x_v = x + 1
... return x_v
x = paddle.ones([1, 2], dtype='float32') ...
x_v = func(x) >>> x = paddle.ones([1, 2], dtype='float32')
print(x_v) # [[2. 2.]] >>> x_v = func(x)
>>> print(x_v)
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2., 2.]])
""" """
property = kwargs.get("property", False) property = kwargs.get("property", False)
...@@ -343,24 +343,27 @@ def not_to_static(func=None): ...@@ -343,24 +343,27 @@ def not_to_static(func=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
@paddle.jit.not_to_static
def func_not_to_static(x): >>> @paddle.jit.not_to_static
res = x - 1 ... def func_not_to_static(x):
return res ... res = x - 1
... return res
@paddle.jit.to_static
def func(x): >>> @paddle.jit.to_static
if paddle.mean(x) < 0: ... def func(x):
out = func_not_to_static(x) ... if paddle.mean(x) < 0:
else: ... out = func_not_to_static(x)
out = x + 1 ... else:
return out ... out = x + 1
... return out
x = paddle.ones([1, 2], dtype='float32') ...
out = func(x) >>> x = paddle.ones([1, 2], dtype='float32')
print(out) # [[2. 2.]] >>> out = func(x)
>>> print(out)
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2., 2.]])
""" """
if func is None: if func is None:
return not_to_static return not_to_static
...@@ -688,34 +691,36 @@ def _register_save_pre_hook(hook): ...@@ -688,34 +691,36 @@ def _register_save_pre_hook(hook):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle >>> import paddle
IMAGE_SIZE = 256 >>> IMAGE_SIZE = 256
CLASS_NUM = 10 >>> CLASS_NUM = 10
class LinearNet(paddle.nn.Layer): >>> class LinearNet(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self._linear = paddle.nn.Linear(IMAGE_SIZE, CLASS_NUM) ... self._linear = paddle.nn.Linear(IMAGE_SIZE, CLASS_NUM)
...
def forward(self, x): ... def forward(self, x):
return self._linear(x) ... return self._linear(x)
...
saving_count = 0 >>> saving_count = 0
def save_pre_hook(layer, input_spec, configs): >>> def save_pre_hook(layer, input_spec, configs):
global saving_count ... global saving_count
saving_count += 1 ... saving_count += 1
...
remove_handler = paddle.jit.register_save_pre_hook(save_pre_hook) >>> remove_handler = paddle.jit.api._register_save_pre_hook(save_pre_hook)
layer = LinearNet() >>> layer = LinearNet()
paddle.jit.save(layer, "/tmp", [paddle.static.InputSpec(shape=[-1, IMAGE_SIZE])]) >>> paddle.jit.save(layer, "/tmp", [paddle.static.InputSpec(shape=[-1, IMAGE_SIZE])])
# saving_count == 1 >>> print(saving_count)
1
remove_handler.remove()
paddle.jit.save(layer, "/tmp", [paddle.static.InputSpec(shape=[-1, IMAGE_SIZE])]) >>> remove_handler.remove()
# saving_count == 1 >>> paddle.jit.save(layer, "/tmp", [paddle.static.InputSpec(shape=[-1, IMAGE_SIZE])])
>>> print(saving_count)
1
""" """
global _save_pre_hooks_lock global _save_pre_hooks_lock
global _save_pre_hooks global _save_pre_hooks
...@@ -834,95 +839,97 @@ def save(layer, path, input_spec=None, **configs): ...@@ -834,95 +839,97 @@ def save(layer, path, input_spec=None, **configs):
Examples: Examples:
.. code-block:: python .. code-block:: python
# example 1: save layer >>> # doctest: +SKIP
import numpy as np >>> # example 1: save layer
import paddle >>> import numpy as np
import paddle.nn as nn >>> import paddle
import paddle.optimizer as opt >>> import paddle.nn as nn
>>> import paddle.optimizer as opt
BATCH_SIZE = 16
BATCH_NUM = 4 >>> BATCH_SIZE = 16
EPOCH_NUM = 4 >>> BATCH_NUM = 4
>>> EPOCH_NUM = 4
IMAGE_SIZE = 784
CLASS_NUM = 10 >>> IMAGE_SIZE = 784
>>> CLASS_NUM = 10
# define a random dataset
class RandomDataset(paddle.io.Dataset): >>> # define a random dataset
def __init__(self, num_samples): >>> class RandomDataset(paddle.io.Dataset):
self.num_samples = num_samples ... def __init__(self, num_samples):
... self.num_samples = num_samples
def __getitem__(self, idx): ...
image = np.random.random([IMAGE_SIZE]).astype('float32') ... def __getitem__(self, idx):
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') ... image = np.random.random([IMAGE_SIZE]).astype('float32')
return image, label ... label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
... return image, label
def __len__(self): ...
return self.num_samples ... def __len__(self):
... return self.num_samples
class LinearNet(nn.Layer):
def __init__(self): >>> class LinearNet(nn.Layer):
super().__init__() ... def __init__(self):
self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) ... super().__init__()
... self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM)
@paddle.jit.to_static ...
def forward(self, x): ... @paddle.jit.to_static
return self._linear(x) ... def forward(self, x):
... return self._linear(x)
def train(layer, loader, loss_fn, opt):
for epoch_id in range(EPOCH_NUM): >>> def train(layer, loader, loss_fn, opt):
for batch_id, (image, label) in enumerate(loader()): ... for epoch_id in range(EPOCH_NUM):
out = layer(image) ... for batch_id, (image, label) in enumerate(loader()):
loss = loss_fn(out, label) ... out = layer(image)
loss.backward() ... loss = loss_fn(out, label)
opt.step() ... loss.backward()
opt.clear_grad() ... opt.step()
print("Epoch {} batch {}: loss = {}".format( ... opt.clear_grad()
epoch_id, batch_id, np.mean(loss.numpy()))) ... print("Epoch {} batch {}: loss = {}".format(
... epoch_id, batch_id, np.mean(loss.numpy())))
# 1. train & save model.
>>> # 1. train & save model.
# create network
layer = LinearNet() >>> # create network
loss_fn = nn.CrossEntropyLoss() >>> layer = LinearNet()
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters()) >>> loss_fn = nn.CrossEntropyLoss()
>>> adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())
# create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) >>> # create data loader
loader = paddle.io.DataLoader(dataset, >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
batch_size=BATCH_SIZE, >>> loader = paddle.io.DataLoader(dataset,
shuffle=True, ... batch_size=BATCH_SIZE,
drop_last=True, ... shuffle=True,
num_workers=2) ... drop_last=True,
... num_workers=2
# train ... )
train(layer, loader, loss_fn, adam)
>>> # train
# save >>> train(layer, loader, loss_fn, adam)
path = "example_model/linear"
paddle.jit.save(layer, path) >>> # save
>>> path = "example_model/linear"
# example 2: save function >>> paddle.jit.save(layer, path)
import paddle
from paddle.static import InputSpec >>> # example 2: save function
>>> import paddle
>>> from paddle.static import InputSpec
def save_function():
@paddle.jit.to_static
def fun(inputs): >>> def save_function():
return paddle.tanh(inputs) ... @paddle.jit.to_static
... def fun(inputs):
path = 'test_jit_save_load_function_1/func' ... return paddle.tanh(inputs)
inps = paddle.rand([3, 6]) ...
origin = fun(inps) ... path = 'test_jit_save_load_function_1/func'
... inps = paddle.rand([3, 6])
paddle.jit.save(fun, path) ... origin = fun(inps)
load_func = paddle.jit.load(path) ...
... paddle.jit.save(fun, path)
load_result = load_func(inps) ... load_func = paddle.jit.load(path)
print((load_result - origin).abs().max() < 1e-10) ...
... load_result = load_func(inps)
save_function() ... print((load_result - origin).abs().max() < 1e-10)
>>> save_function()
""" """
# 1. input build & check # 1. input build & check
...@@ -1310,87 +1317,90 @@ def load(path, **configs): ...@@ -1310,87 +1317,90 @@ def load(path, **configs):
.. code-block:: python .. code-block:: python
:name: code-example1 :name: code-example1
import numpy as np >>> # doctest: +SKIP
import paddle >>> import numpy as np
import paddle.nn as nn >>> import paddle
import paddle.optimizer as opt >>> import paddle.nn as nn
>>> import paddle.optimizer as opt
BATCH_SIZE = 16
BATCH_NUM = 4 >>> BATCH_SIZE = 16
EPOCH_NUM = 4 >>> BATCH_NUM = 4
>>> EPOCH_NUM = 4
IMAGE_SIZE = 784
CLASS_NUM = 10 >>> IMAGE_SIZE = 784
>>> CLASS_NUM = 10
# define a random dataset
class RandomDataset(paddle.io.Dataset): >>> # define a random dataset
def __init__(self, num_samples): >>> class RandomDataset(paddle.io.Dataset):
self.num_samples = num_samples ... def __init__(self, num_samples):
... self.num_samples = num_samples
def __getitem__(self, idx): ...
image = np.random.random([IMAGE_SIZE]).astype('float32') ... def __getitem__(self, idx):
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') ... image = np.random.random([IMAGE_SIZE]).astype('float32')
return image, label ... label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
... return image, label
def __len__(self): ...
return self.num_samples ... def __len__(self):
... return self.num_samples
class LinearNet(nn.Layer):
def __init__(self): >>> class LinearNet(nn.Layer):
super().__init__() ... def __init__(self):
self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) ... super().__init__()
... self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM)
@paddle.jit.to_static ...
def forward(self, x): ... @paddle.jit.to_static
return self._linear(x) ... def forward(self, x):
... return self._linear(x)
def train(layer, loader, loss_fn, opt): ...
for epoch_id in range(EPOCH_NUM): >>> def train(layer, loader, loss_fn, opt):
for batch_id, (image, label) in enumerate(loader()): ... for epoch_id in range(EPOCH_NUM):
out = layer(image) ... for batch_id, (image, label) in enumerate(loader()):
loss = loss_fn(out, label) ... out = layer(image)
loss.backward() ... loss = loss_fn(out, label)
opt.step() ... loss.backward()
opt.clear_grad() ... opt.step()
print("Epoch {} batch {}: loss = {}".format( ... opt.clear_grad()
epoch_id, batch_id, np.mean(loss.numpy()))) ... print("Epoch {} batch {}: loss = {}".format(
... epoch_id, batch_id, np.mean(loss.numpy())))
# 1. train & save model.
>>> # 1. train & save model.
# create network
layer = LinearNet() >>> # create network
loss_fn = nn.CrossEntropyLoss() >>> layer = LinearNet()
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters()) >>> loss_fn = nn.CrossEntropyLoss()
>>> adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())
# create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) >>> # create data loader
loader = paddle.io.DataLoader(dataset, >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
batch_size=BATCH_SIZE, >>> loader = paddle.io.DataLoader(
shuffle=True, ... dataset,
drop_last=True, ... batch_size=BATCH_SIZE,
num_workers=2) ... shuffle=True,
... drop_last=True,
# train ... num_workers=2
train(layer, loader, loss_fn, adam) ... )
# save >>> # train
path = "example_model/linear" >>> train(layer, loader, loss_fn, adam)
paddle.jit.save(layer, path)
>>> # save
# 2. load model >>> path = "example_model/linear"
>>> paddle.jit.save(layer, path)
# load
loaded_layer = paddle.jit.load(path) >>> # 2. load model
# inference >>> # load
loaded_layer.eval() >>> loaded_layer = paddle.jit.load(path)
x = paddle.randn([1, IMAGE_SIZE], 'float32')
pred = loaded_layer(x) >>> # inference
>>> loaded_layer.eval()
# fine-tune >>> x = paddle.randn([1, IMAGE_SIZE], 'float32')
loaded_layer.train() >>> pred = loaded_layer(x)
adam = opt.Adam(learning_rate=0.001, parameters=loaded_layer.parameters())
train(loaded_layer, loader, loss_fn, adam) >>> # fine-tune
>>> loaded_layer.train()
>>> adam = opt.Adam(learning_rate=0.001, parameters=loaded_layer.parameters())
>>> train(loaded_layer, loader, loss_fn, adam)
2. Load model saved by ``paddle.fluid.io.save_inference_model`` then performing and fine-tune training. 2. Load model saved by ``paddle.fluid.io.save_inference_model`` then performing and fine-tune training.
...@@ -1398,102 +1408,105 @@ def load(path, **configs): ...@@ -1398,102 +1408,105 @@ def load(path, **configs):
.. code-block:: python .. code-block:: python
:name: code-example2 :name: code-example2
import numpy as np >>> import numpy as np
import paddle >>> import paddle
import paddle.static as static >>> import paddle.static as static
import paddle.nn as nn >>> import paddle.nn as nn
import paddle.optimizer as opt >>> import paddle.optimizer as opt
import paddle.nn.functional as F >>> import paddle.nn.functional as F
BATCH_SIZE = 16 >>> BATCH_SIZE = 16
BATCH_NUM = 4 >>> BATCH_NUM = 4
EPOCH_NUM = 4 >>> EPOCH_NUM = 4
IMAGE_SIZE = 784 >>> IMAGE_SIZE = 784
CLASS_NUM = 10 >>> CLASS_NUM = 10
# define a random dataset >>> # define a random dataset
class RandomDataset(paddle.io.Dataset): >>> class RandomDataset(paddle.io.Dataset):
def __init__(self, num_samples): ... def __init__(self, num_samples):
self.num_samples = num_samples ... self.num_samples = num_samples
...
def __getitem__(self, idx): ... def __getitem__(self, idx):
image = np.random.random([IMAGE_SIZE]).astype('float32') ... image = np.random.random([IMAGE_SIZE]).astype('float32')
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') ... label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
return image, label ... return image, label
...
def __len__(self): ... def __len__(self):
return self.num_samples ... return self.num_samples
paddle.enable_static() >>> paddle.enable_static()
image = static.data(name='image', shape=[None, 784], dtype='float32') >>> image = static.data(name='image', shape=[None, 784], dtype='float32')
label = static.data(name='label', shape=[None, 1], dtype='int64') >>> label = static.data(name='label', shape=[None, 1], dtype='int64')
pred = static.nn.fc(x=image, size=10, activation='softmax') >>> pred = static.nn.fc(x=image, size=10, activation='softmax')
loss = F.cross_entropy(input=pred, label=label) >>> loss = F.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(loss) >>> avg_loss = paddle.mean(loss)
optimizer = paddle.optimizer.SGD(learning_rate=0.001) >>> optimizer = paddle.optimizer.SGD(learning_rate=0.001)
optimizer.minimize(avg_loss) >>> optimizer.minimize(avg_loss)
place = paddle.CPUPlace() >>> place = paddle.CPUPlace()
exe = static.Executor(place) >>> exe = static.Executor(place)
exe.run(static.default_startup_program()) >>> exe.run(static.default_startup_program())
# create data loader >>> # create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
loader = paddle.io.DataLoader(dataset, >>> loader = paddle.io.DataLoader(dataset,
feed_list=[image, label], ... feed_list=[image, label],
places=place, ... places=place,
batch_size=BATCH_SIZE, ... batch_size=BATCH_SIZE,
shuffle=True, ... shuffle=True,
drop_last=True, ... drop_last=True,
return_list=False, ... return_list=False,
num_workers=2) ... num_workers=2
... )
# 1. train and save inference model
for data in loader(): >>> # 1. train and save inference model
exe.run( >>> for data in loader():
static.default_main_program(), >>> exe.run(
feed=data, ... static.default_main_program(),
fetch_list=[avg_loss]) ... feed=data,
... fetch_list=[avg_loss]
model_path = "fc.example.model" ... )
paddle.fluid.io.save_inference_model(
model_path, ["image"], [pred], exe) >>> model_path = "fc.example.model"
>>> paddle.fluid.io.save_inference_model(
# 2. load model >>> model_path, ["image"], [pred], exe)
# enable dygraph mode >>> # 2. load model
paddle.disable_static(place)
>>> # enable dygraph mode
# load >>> paddle.disable_static(place)
fc = paddle.jit.load(model_path)
>>> # load
# inference >>> fc = paddle.jit.load(model_path)
fc.eval()
x = paddle.randn([1, IMAGE_SIZE], 'float32') >>> # inference
pred = fc(x) >>> fc.eval()
>>> x = paddle.randn([1, IMAGE_SIZE], 'float32')
# fine-tune >>> pred = fc(x)
fc.train()
loss_fn = nn.CrossEntropyLoss() >>> # fine-tune
adam = opt.Adam(learning_rate=0.001, parameters=fc.parameters()) >>> fc.train()
loader = paddle.io.DataLoader(dataset, >>> loss_fn = nn.CrossEntropyLoss()
places=place, >>> adam = opt.Adam(learning_rate=0.001, parameters=fc.parameters())
batch_size=BATCH_SIZE, >>> loader = paddle.io.DataLoader(dataset,
shuffle=True, ... places=place,
drop_last=True, ... batch_size=BATCH_SIZE,
num_workers=2) ... shuffle=True,
for epoch_id in range(EPOCH_NUM): ... drop_last=True,
for batch_id, (image, label) in enumerate(loader()): ... num_workers=2
out = fc(image) ... )
loss = loss_fn(out, label) >>> for epoch_id in range(EPOCH_NUM):
loss.backward() ... for batch_id, (image, label) in enumerate(loader()):
adam.step() ... out = fc(image)
adam.clear_grad() ... loss = loss_fn(out, label)
print("Epoch {} batch {}: loss = {}".format( ... loss.backward()
epoch_id, batch_id, np.mean(loss.numpy()))) ... adam.step()
... adam.clear_grad()
... print("Epoch {} batch {}: loss = {}".format(
... epoch_id, batch_id, np.mean(loss.numpy())))
""" """
# 1. construct correct config # 1. construct correct config
config = _parse_load_config(configs) config = _parse_load_config(configs)
...@@ -1612,29 +1625,29 @@ class TracedLayer: ...@@ -1612,29 +1625,29 @@ class TracedLayer:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
class ExampleLayer(paddle.nn.Layer): >>> class ExampleLayer(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self._fc = paddle.nn.Linear(3, 10) ... self._fc = paddle.nn.Linear(3, 10)
...
def forward(self, input): ... def forward(self, input):
return self._fc(input) ... return self._fc(input)
layer = ExampleLayer() >>> layer = ExampleLayer()
in_var = paddle.uniform(shape=[2, 3], dtype='float32') >>> in_var = paddle.uniform(shape=[2, 3], dtype='float32')
out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var]) >>> out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
# run the static graph model using Executor inside >>> # run the static graph model using Executor inside
out_static_graph = static_layer([in_var]) >>> out_static_graph = static_layer([in_var])
print(len(out_static_graph)) # 1 >>> print(len(out_static_graph)) # 1
print(out_static_graph[0].shape) # (2, 10) >>> print(out_static_graph[0].shape) # (2, 10)
# save the static graph model for inference >>> # save the static graph model for inference
static_layer.save_inference_model('./saved_infer_model') >>> static_layer.save_inference_model('./saved_infer_model')
""" """
assert isinstance( assert isinstance(
...@@ -1662,29 +1675,29 @@ class TracedLayer: ...@@ -1662,29 +1675,29 @@ class TracedLayer:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
class ExampleLayer(paddle.nn.Layer):
def __init__(self):
super().__init__()
self._fc = paddle.nn.Linear(3, 10)
def forward(self, input): >>> class ExampleLayer(paddle.nn.Layer):
return self._fc(input) ... def __init__(self):
... super().__init__()
... self._fc = paddle.nn.Linear(3, 10)
...
... def forward(self, input):
... return self._fc(input)
layer = ExampleLayer() >>> layer = ExampleLayer()
in_var = paddle.uniform(shape=[2, 3], dtype='float32') >>> in_var = paddle.uniform(shape=[2, 3], dtype='float32')
out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var]) >>> out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
build_strategy = paddle.static.BuildStrategy() >>> build_strategy = paddle.static.BuildStrategy()
build_strategy.enable_inplace = True >>> build_strategy.enable_inplace = True
exec_strategy = paddle.static.ExecutionStrategy() >>> exec_strategy = paddle.static.ExecutionStrategy()
exec_strategy.num_threads = 2 >>> exec_strategy.num_threads = 2
static_layer.set_strategy(build_strategy=build_strategy, exec_strategy=exec_strategy) >>> static_layer.set_strategy(build_strategy=build_strategy, exec_strategy=exec_strategy)
out_static_graph = static_layer([in_var]) >>> out_static_graph = static_layer([in_var])
""" """
assert self._compiled_program is None, "Cannot set strategy after run" assert self._compiled_program is None, "Cannot set strategy after run"
...@@ -1765,33 +1778,36 @@ class TracedLayer: ...@@ -1765,33 +1778,36 @@ class TracedLayer:
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle >>> import paddle
class ExampleLayer(paddle.nn.Layer): >>> class ExampleLayer(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self._fc = paddle.nn.Linear(3, 10) ... self._fc = paddle.nn.Linear(3, 10)
...
def forward(self, input): ... def forward(self, input):
return self._fc(input) ... return self._fc(input)
save_dirname = './saved_infer_model' >>> save_dirname = './saved_infer_model'
in_np = np.random.random([2, 3]).astype('float32') >>> in_np = np.random.random([2, 3]).astype('float32')
in_var = paddle.to_tensor(in_np) >>> in_var = paddle.to_tensor(in_np)
layer = ExampleLayer() >>> layer = ExampleLayer()
out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var]) >>> out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0]) >>> static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0])
paddle.enable_static() >>> paddle.enable_static()
place = paddle.CPUPlace() >>> place = paddle.CPUPlace()
exe = paddle.static.Executor(place) >>> exe = paddle.static.Executor(place)
program, feed_vars, fetch_vars = paddle.static.load_inference_model(save_dirname, >>> program, feed_vars, fetch_vars = paddle.static.load_inference_model(
exe) ... save_dirname,
... exe
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars) ... )
print(fetch.shape) # (2, 10)
>>> fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
>>> print(fetch.shape)
[2, 10]
""" """
check_type( check_type(
path, path,
......
...@@ -162,27 +162,28 @@ def convert_call(func): ...@@ -162,27 +162,28 @@ def convert_call(func):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
from paddle.jit.dy2static import Call >>> import paddle
>>> from paddle.jit.dy2static import Call
paddle.enable_static()
def dyfunc(x): >>> paddle.enable_static()
if paddle.mean(x) < 0: >>> def dyfunc(x):
x_v = x - 1 ... if paddle.mean(x) < 0:
else: ... x_v = x - 1
x_v = x + 1 ... else:
return x_v ... x_v = x + 1
... return x_v
new_func = Call(dyfunc) ...
x = paddle.tensor.manipulation.fill_constant(shape=[3, 3], value=0, dtype='float64') >>> new_func = Call(dyfunc)
x_v = new_func(x) >>> x = paddle.tensor.manipulation.fill_constant(shape=[3, 3], value=0, dtype='float64')
>>> x_v = new_func(x)
exe = paddle.static.Executor(paddle.CPUPlace())
out = exe.run(fetch_list=[x_v]) >>> exe = paddle.static.Executor(paddle.CPUPlace())
print(out[0]) >>> out = exe.run(fetch_list=[x_v])
# [[1. 1. 1.] >>> print(out[0])
# [1. 1. 1.] [[1. 1. 1.]
# [1. 1. 1.]] [1. 1. 1.]
[1. 1. 1.]]
""" """
translator_logger.log(1, f"Convert callable object: convert {func}.") translator_logger.log(1, f"Convert callable object: convert {func}.")
......
...@@ -206,14 +206,14 @@ def set_verbosity(level=0, also_to_stdout=False): ...@@ -206,14 +206,14 @@ def set_verbosity(level=0, also_to_stdout=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import os >>> import os
import paddle >>> import paddle
paddle.jit.set_verbosity(1) >>> paddle.jit.set_verbosity(1)
# The verbosity level is now 1 >>> # The verbosity level is now 1
os.environ['TRANSLATOR_VERBOSITY'] = '3' >>> os.environ['TRANSLATOR_VERBOSITY'] = '3'
# The verbosity level is now 3, but it has no effect because it has a lower priority than `set_verbosity` >>> # The verbosity level is now 3, but it has no effect because it has a lower priority than `set_verbosity`
""" """
_TRANSLATOR_LOGGER.verbosity_level = level _TRANSLATOR_LOGGER.verbosity_level = level
_TRANSLATOR_LOGGER.need_to_echo_log_to_stdout = also_to_stdout _TRANSLATOR_LOGGER.need_to_echo_log_to_stdout = also_to_stdout
...@@ -244,14 +244,15 @@ def set_code_level(level=LOG_AllTransformer, also_to_stdout=False): ...@@ -244,14 +244,15 @@ def set_code_level(level=LOG_AllTransformer, also_to_stdout=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import os
>>> import paddle
paddle.jit.set_code_level(2) >>> paddle.jit.set_code_level(2)
# It will print the transformed code at level 2, which means to print the code after second transformer, >>> # It will print the transformed code at level 2, which means to print the code after second transformer,
# as the date of August 28, 2020, it is CastTransformer. >>> # as the date of August 28, 2020, it is CastTransformer.
os.environ['TRANSLATOR_CODE_LEVEL'] = '3' >>> os.environ['TRANSLATOR_CODE_LEVEL'] = '3'
# The code level is now 3, but it has no effect because it has a lower priority than `set_code_level` >>> # The code level is now 3, but it has no effect because it has a lower priority than `set_code_level`
""" """
_TRANSLATOR_LOGGER.transformed_code_level = level _TRANSLATOR_LOGGER.transformed_code_level = level
......
...@@ -641,24 +641,25 @@ class StaticFunction: ...@@ -641,24 +641,25 @@ class StaticFunction:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
from paddle.jit import to_static >>> import paddle
from paddle.static import InputSpec >>> from paddle.jit import to_static
>>> from paddle.static import InputSpec
paddle.disable_static()
>>> paddle.disable_static()
def foo(x, y):
z = x + y >>> def foo(x, y):
return z ... z = x + y
... return z
# usage 1: ...
decorated_foo = to_static(foo, input_spec=[InputSpec([10], name='x'), InputSpec([10], name='y')]) >>> # usage 1:
print(decorated_foo.concrete_program) >>> decorated_foo = to_static(foo, input_spec=[InputSpec([10], name='x'), InputSpec([10], name='y')])
>>> print(decorated_foo.concrete_program)
# usage 2:
decorated_foo = to_static(foo) >>> # usage 2:
out_foo = decorated_foo(paddle.rand([10]), paddle.rand([10])) >>> decorated_foo = to_static(foo)
print(decorated_foo.concrete_program) >>> out_foo = decorated_foo(paddle.rand([10]), paddle.rand([10]))
>>> print(decorated_foo.concrete_program)
""" """
return self.concrete_program_specify_input_spec(input_spec=None) return self.concrete_program_specify_input_spec(input_spec=None)
...@@ -760,25 +761,26 @@ class StaticFunction: ...@@ -760,25 +761,26 @@ class StaticFunction:
Example:: Example::
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
class Net(paddle.nn.Layer): >>> class Net(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
...
def forward(self, x, flag=True): ... def forward(self, x, flag=True):
if flag: ... if flag:
out = x + 1 ... out = x + 1
else: ... else:
out = x - 1 ... out = x - 1
return out ... return out
...
x = paddle.randn([10, 1], 'float32') >>> x = paddle.randn([10, 1], 'float32')
net = paddle.jit.to_static(Net()) # convert into static graph mode >>> net = paddle.jit.to_static(Net()) # convert into static graph mode
out = net(x) >>> out = net(x)
net.forward.rollback() # rollback into dygraph mode >>> net.forward.rollback() # rollback into dygraph mode
out = net(x) >>> out = net(x)
""" """
def rollback_impl(class_instance): def rollback_impl(class_instance):
...@@ -819,24 +821,24 @@ class StaticFunction: ...@@ -819,24 +821,24 @@ class StaticFunction:
Example:: Example::
.. code-block:: python .. code-block:: python
import copy >>> import copy
import paddle >>> import paddle
class Net(paddle.nn.Layer): >>> class Net(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
...
def forward(self, x, flag=True): ... def forward(self, x, flag=True):
if flag: ... if flag:
out = x + 1 ... out = x + 1
else: ... else:
out = x - 1 ... out = x - 1
return out ... return out
...
x = paddle.randn([10, 1], 'float32') >>> x = paddle.randn([10, 1], 'float32')
net = paddle.jit.to_static(Net()) # convert into static graph mode >>> net = paddle.jit.to_static(Net()) # convert into static graph mode
copy_net = copy.deepcopy(net) # deepcopy a new net without @to_static >>> copy_net = copy.deepcopy(net) # deepcopy a new net without @to_static
Please attention that original 'net' will unwrap @to_static and rollback into simple Layer. Please attention that original 'net' will unwrap @to_static and rollback into simple Layer.
""" """
...@@ -1378,11 +1380,11 @@ class ProgramTranslator: ...@@ -1378,11 +1380,11 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# Two methods get same object because ProgramTranslator is a singleton >>> # Two methods get same object because ProgramTranslator is a singleton
paddle.jit.ProgramTranslator() >>> paddle.jit.dy2static.program_translator.ProgramTranslator()
paddle.jit.ProgramTranslator.get_instance() >>> paddle.jit.dy2static.program_translator.ProgramTranslator.get_instance()
""" """
...@@ -1433,24 +1435,23 @@ class ProgramTranslator: ...@@ -1433,24 +1435,23 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
>>> def func(x):
@paddle.jit.to_static ... if paddle.mean(x) > 0:
def func(x): ... x_v = x - 1
if paddle.mean(x) > 0: ... else:
x_v = x - 1 ... x_v = x + 1
else: ... return x_v
x_v = x + 1 ...
return x_v ...
>>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
paddle.jit.enable_to_static(False) >>> x = paddle.ones([1, 2])
>>> x_v = prog_trans.get_output(func, x)
x = paddle.ones([1, 2]) >>> print(x_v)
# ProgramTranslator is disabled so the func is run in dygraph Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
print(func(x)) # [[0. 0.]] [[0., 0.]])
""" """
check_type( check_type(
enable_to_static, enable_to_static,
...@@ -1477,23 +1478,23 @@ class ProgramTranslator: ...@@ -1477,23 +1478,23 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
>>> def func(x):
def func(x): ... if paddle.mean(x) > 0:
if paddle.mean(x) > 0: ... x_v = x - 1
x_v = x - 1 ... else:
else: ... x_v = x + 1
x_v = x + 1 ... return x_v
return x_v ...
...
>>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
prog_trans = paddle.jit.ProgramTranslator()
>>> x = paddle.ones([1, 2])
x = paddle.ones([1, 2]) >>> x_v = prog_trans.get_output(func, x)
x_v = prog_trans.get_output(func, x) >>> print(x_v)
print(x_v) # [[0. 0.]] Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0.]])
""" """
assert callable( assert callable(
dygraph_func dygraph_func
...@@ -1560,21 +1561,19 @@ class ProgramTranslator: ...@@ -1560,21 +1561,19 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
>>> def func(x):
def func(x): ... if paddle.mean(x) > 0:
if paddle.mean(x) > 0: ... x_v = x - 1
x_v = x - 1 ... else:
else: ... x_v = x + 1
x_v = x + 1 ... return x_v
return x_v ...
>>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
>>> static_func = prog_trans.get_func(func)
prog_trans = paddle.jit.ProgramTranslator() >>> print(callable(static_func))
static_func = prog_trans.get_func(func) True
print(callable(static_func)) # True
""" """
assert callable( assert callable(
dygraph_func dygraph_func
...@@ -1611,25 +1610,22 @@ class ProgramTranslator: ...@@ -1611,25 +1610,22 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
>>> def func(x):
def func(x): ... if paddle.mean(x) > 0:
if paddle.mean(x) > 0: ... x_v = x - 1
x_v = x - 1 ... else:
else: ... x_v = x + 1
x_v = x + 1 ... return x_v
return x_v ...
>>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
>>> x = paddle.ones([1, 2])
prog_trans = paddle.jit.ProgramTranslator() >>> main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x)
x = paddle.ones([1, 2]) >>> print([i.name for i in inputs])
main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) >>> # [u'generated_tensor_0'] the feed input Tensor name representing x
print([i.name for i in inputs]) >>> print([o.name for o in outputs])
# [u'generated_tensor_0'] the feed input Tensor name representing x >>> # [u'_generated_var_4'] the fetch output Tensor name representing x_v
print([o.name for o in outputs])
# [u'_generated_var_4'] the fetch output Tensor name representing x_v
""" """
assert callable( assert callable(
dygraph_func dygraph_func
...@@ -1681,22 +1677,20 @@ class ProgramTranslator: ...@@ -1681,22 +1677,20 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP
>>> import paddle
>>> def func(x):
def func(x): ... if paddle.mean(x) > 0:
if paddle.mean(x) > 0: ... x_v = x - 1
x_v = x - 1 ... else:
else: ... x_v = x + 1
x_v = x + 1 ... return x_v
return x_v ...
>>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
prog_trans = paddle.jit.ProgramTranslator() >>> code = prog_trans.get_code(func)
>>> print(type(code))
code = prog_trans.get_code(func) <class 'str'>
print(type(code)) # <class 'str'>
""" """
assert callable( assert callable(
dygraph_func dygraph_func
...@@ -1728,11 +1722,10 @@ class ProgramTranslator: ...@@ -1728,11 +1722,10 @@ class ProgramTranslator:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
prog_trans = paddle.jit.ProgramTranslator()
prog_cache = prog_trans.get_program_cache()
>>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
>>> prog_cache = prog_trans.get_program_cache()
""" """
return self._program_cache return self._program_cache
...@@ -1751,23 +1744,22 @@ def enable_to_static(enable_to_static_bool): ...@@ -1751,23 +1744,22 @@ def enable_to_static(enable_to_static_bool):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> @paddle.jit.to_static
>>> def func(x):
@paddle.jit.to_static ... if paddle.mean(x) > 0:
def func(x): ... x_v = x - 1
if paddle.mean(x) > 0: ... else:
x_v = x - 1 ... x_v = x + 1
else: ... return x_v
x_v = x + 1 ...
return x_v >>> paddle.jit.enable_to_static(False)
>>> x = paddle.ones([1, 2])
paddle.jit.enable_to_static(False) >>> # ProgramTranslator is disabled so the func is run in dygraph
>>> print(func(x))
x = paddle.ones([1, 2]) Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# ProgramTranslator is disabled so the func is run in dygraph [[0., 0.]])
print(func(x)) # [[0. 0.]]
""" """
check_type( check_type(
......
...@@ -1312,87 +1312,86 @@ class TranslatedLayer(layers.Layer): ...@@ -1312,87 +1312,86 @@ class TranslatedLayer(layers.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> # doctest: +SKIP
import paddle >>> import numpy as np
import paddle.nn as nn >>> import paddle
import paddle.optimizer as opt >>> import paddle.nn as nn
>>> import paddle.optimizer as opt
BATCH_SIZE = 16
BATCH_NUM = 4 >>> BATCH_SIZE = 16
EPOCH_NUM = 4 >>> BATCH_NUM = 4
>>> EPOCH_NUM = 4
IMAGE_SIZE = 784
CLASS_NUM = 10 >>> IMAGE_SIZE = 784
>>> CLASS_NUM = 10
# define a random dataset
class RandomDataset(paddle.io.Dataset): >>> # define a random dataset
def __init__(self, num_samples): >>> class RandomDataset(paddle.io.Dataset):
self.num_samples = num_samples ... def __init__(self, num_samples):
... self.num_samples = num_samples
def __getitem__(self, idx): ...
image = np.random.random([IMAGE_SIZE]).astype('float32') ... def __getitem__(self, idx):
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') ... image = np.random.random([IMAGE_SIZE]).astype('float32')
return image, label ... label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
... return image, label
def __len__(self): ...
return self.num_samples ... def __len__(self):
... return self.num_samples
class LinearNet(nn.Layer): ...
def __init__(self): >>> class LinearNet(nn.Layer):
super().__init__() ... def __init__(self):
self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) ... super().__init__()
... self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM)
@paddle.jit.to_static ...
def forward(self, x): ... @paddle.jit.to_static
return self._linear(x) ... def forward(self, x):
... return self._linear(x)
def train(layer, loader, loss_fn, opt): ...
for epoch_id in range(EPOCH_NUM): >>> def train(layer, loader, loss_fn, opt):
for batch_id, (image, label) in enumerate(loader()): ... for epoch_id in range(EPOCH_NUM):
out = layer(image) ... for batch_id, (image, label) in enumerate(loader()):
loss = loss_fn(out, label) ... out = layer(image)
loss.backward() ... loss = loss_fn(out, label)
opt.step() ... loss.backward()
opt.clear_grad() ... opt.step()
print("Epoch {} batch {}: loss = {}".format( ... opt.clear_grad()
epoch_id, batch_id, np.mean(loss.numpy()))) ... print("Epoch {} batch {}: loss = {}".format(
... epoch_id, batch_id, np.mean(loss.numpy())))
# 1. train & save model. ...
>>> # 1. train & save model.
# create network >>> # create network
layer = LinearNet() >>> layer = LinearNet()
loss_fn = nn.CrossEntropyLoss() >>> loss_fn = nn.CrossEntropyLoss()
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters()) >>> adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())
# create data loader >>> # create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
loader = paddle.io.DataLoader(dataset, >>> loader = paddle.io.DataLoader(dataset,
batch_size=BATCH_SIZE, ... batch_size=BATCH_SIZE,
shuffle=True, ... shuffle=True,
drop_last=True, ... drop_last=True,
num_workers=2) ... num_workers=2
... )
# train >>> # train
train(layer, loader, loss_fn, adam) >>> train(layer, loader, loss_fn, adam)
# save >>> # save
model_path = "linear.example.model" >>> model_path = "linear.example.model"
paddle.jit.save(layer, model_path) >>> paddle.jit.save(layer, model_path)
# 2. load model as TranslatedLayer >>> # 2. load model as TranslatedLayer
>>> # load
# load >>> translated_layer = paddle.jit.load(model_path)
translated_layer = paddle.jit.load(model_path)
>>> # inference
# inference >>> translated_layer.eval()
translated_layer.eval() >>> x = paddle.randn([1, IMAGE_SIZE], 'float32')
x = paddle.randn([1, IMAGE_SIZE], 'float32') >>> pred = translated_layer(x)
pred = translated_layer(x)
>>> # fine-tune
# fine-tune >>> translated_layer.train()
translated_layer.train() >>> adam = opt.Adam(learning_rate=0.001, parameters=translated_layer.parameters())
adam = opt.Adam(learning_rate=0.001, parameters=translated_layer.parameters()) >>> train(translated_layer, loader, loss_fn, adam)
train(translated_layer, loader, loss_fn, adam)
""" """
...@@ -1523,76 +1522,76 @@ class TranslatedLayer(layers.Layer): ...@@ -1523,76 +1522,76 @@ class TranslatedLayer(layers.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> # doctest: +SKIP
import paddle >>> import numpy as np
import paddle.nn as nn >>> import paddle
import paddle.optimizer as opt >>> from paddle import nn
>>> import paddle.optimizer as opt
BATCH_SIZE = 16
BATCH_NUM = 4 >>> BATCH_SIZE = 16
EPOCH_NUM = 4 >>> BATCH_NUM = 4
>>> EPOCH_NUM = 4
IMAGE_SIZE = 784
CLASS_NUM = 10 >>> IMAGE_SIZE = 784
>>> CLASS_NUM = 10
# define a random dataset
class RandomDataset(paddle.io.Dataset): >>> # define a random dataset
def __init__(self, num_samples): >>> class RandomDataset(paddle.io.Dataset):
self.num_samples = num_samples ... def __init__(self, num_samples):
... self.num_samples = num_samples
def __getitem__(self, idx): ...
image = np.random.random([IMAGE_SIZE]).astype('float32') ... def __getitem__(self, idx):
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') ... image = np.random.random([IMAGE_SIZE]).astype('float32')
return image, label ... label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
... return image, label
def __len__(self): ...
return self.num_samples ... def __len__(self):
... return self.num_samples
class LinearNet(nn.Layer): ...
def __init__(self): >>> class LinearNet(nn.Layer):
super().__init__() ... def __init__(self):
self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) ... super().__init__()
... self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM)
@paddle.jit.to_static ...
def forward(self, x): ... @paddle.jit.to_static
return self._linear(x) ... def forward(self, x):
... return self._linear(x)
def train(layer, loader, loss_fn, opt): ...
for epoch_id in range(EPOCH_NUM): >>> def train(layer, loader, loss_fn, opt):
for batch_id, (image, label) in enumerate(loader()): ... for epoch_id in range(EPOCH_NUM):
out = layer(image) ... for batch_id, (image, label) in enumerate(loader()):
loss = loss_fn(out, label) ... out = layer(image)
loss.backward() ... loss = loss_fn(out, label)
opt.step() ... loss.backward()
opt.clear_grad() ... opt.step()
print("Epoch {} batch {}: loss = {}".format( ... opt.clear_grad()
epoch_id, batch_id, np.mean(loss.numpy()))) ... print("Epoch {} batch {}: loss = {}".format(
... epoch_id, batch_id, np.mean(loss.numpy())))
# create network ...
layer = LinearNet() >>> # create network
loss_fn = nn.CrossEntropyLoss() >>> layer = LinearNet()
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters()) >>> loss_fn = nn.CrossEntropyLoss()
>>> adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())
# create data loader >>> # create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
loader = paddle.io.DataLoader(dataset, >>> loader = paddle.io.DataLoader(dataset,
batch_size=BATCH_SIZE, ... batch_size=BATCH_SIZE,
shuffle=True, ... shuffle=True,
drop_last=True, ... drop_last=True,
num_workers=2) ... num_workers=2
... )
# train >>> # train
train(layer, loader, loss_fn, adam) >>> train(layer, loader, loss_fn, adam)
# save >>> # save
model_path = "linear.example.model" >>> model_path = "linear.example.model"
paddle.jit.save(layer, model_path) >>> paddle.jit.save(layer, model_path)
# load >>> # load
translated_layer = paddle.jit.load(model_path) >>> translated_layer = paddle.jit.load(model_path)
# get program >>> # get program
program = translated_layer.program() >>> program = translated_layer.program()
""" """
# 1. get program holder # 1. get program holder
program_holder = self._get_program_holder(method_name) program_holder = self._get_program_holder(method_name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册