Skip to content

Commit

Permalink
Update API to 2.0
Browse files Browse the repository at this point in the history
test=develop
  • Loading branch information
qingqing01 committed Aug 18, 2020
1 parent a6235a4 commit 362d914
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
52 changes: 26 additions & 26 deletions python/paddle/incubate/hapi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
]


class Input(fluid.dygraph.Layer):
class Input(paddle.nn.Layer):
"""
Define inputs the model.
Expand Down Expand Up @@ -665,14 +665,14 @@ class Model(object):
"""
An Model object is network with training and inference features.
Dynamic graph and static graph are supported at the same time,
switched by `fluid.enable_dygraph()`. The usage is as follows.
switched by `paddle.disable_static()`. The usage is as follows.
But note, the switching between dynamic and static should be before
instantiating a Model. The input description, i.e, hapi.Input,
must be required for static graph.
Args:
network (fluid.dygraph.Layer): The network is an instance of
fluid.dygraph.Layer.
network (paddle.nn.Layer): The network is an instance of
paddle.nn.Layer.
inputs (Input|list|dict|None): `inputs`, entry points of network,
could be a Input layer, or lits of Input layers,
or dict (name: Input), or None. For static graph,
Expand All @@ -690,7 +690,7 @@ class Model(object):
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self, classifier_act=None):
super(MyNet, self).__init__()
self._fc1 = fluid.dygraph.Linear(784, 200, act=classifier_act)
Expand All @@ -701,7 +701,7 @@ def forward(self, x):
device = hapi.set_device('gpu')
# if use static graph, do not set
fluid.enable_dygraph(device)
paddle.disable_static(device)
# inputs and labels are not required for dynamic graph.
input = hapi.Input('x', [None, 784], 'float32')
Expand Down Expand Up @@ -775,7 +775,7 @@ def train_batch(self, inputs, labels=None):
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self, classifier_act=None):
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 10, act=classifier_act)
Expand All @@ -785,7 +785,7 @@ def forward(self, x):
return y
device = hapi.set_device('gpu')
fluid.enable_dygraph(device)
paddle.disable_static(device)
input = hapi.Input('x', [None, 784], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
Expand Down Expand Up @@ -824,7 +824,7 @@ def eval_batch(self, inputs, labels=None):
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self, classifier_act=None):
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 10, act=classifier_act)
Expand All @@ -834,7 +834,7 @@ def forward(self, x):
return y
device = hapi.set_device('gpu')
fluid.enable_dygraph(device)
paddle.disable_static(device)
input = hapi.Input('x', [None, 784], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
Expand Down Expand Up @@ -870,7 +870,7 @@ def test_batch(self, inputs):
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self):
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
Expand All @@ -879,7 +879,7 @@ def forward(self, x):
return y
device = hapi.set_device('gpu')
fluid.enable_dygraph(device)
paddle.disable_static(device)
model = hapi.Model(MyNet())
model.prepare()
Expand Down Expand Up @@ -918,7 +918,7 @@ def save(self, path):
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self):
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
Expand All @@ -927,7 +927,7 @@ def forward(self, x):
return y
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
paddle.disable_static(device)
model = hapi.Model(MyNet())
model.save('checkpoint/test')
"""
Expand Down Expand Up @@ -970,7 +970,7 @@ def load(self, path, skip_mismatch=False, reset_optimizer=False):
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self):
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
Expand All @@ -979,7 +979,7 @@ def forward(self, x):
return y
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
paddle.disable_static(device)
model = hapi.Model(MyNet())
model.load('checkpoint/test')
"""
Expand Down Expand Up @@ -1045,15 +1045,15 @@ def parameters(self, *args, **kwargs):
import paddle.fluid as fluid
from paddle.incubate.hapi import Model
class MyNet(fluid.dygraph.Layer):
class MyNet(paddle.nn.Layer):
def __init__(self):
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(20, 10, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
fluid.enable_dygraph()
paddle.disable_static()
model = Model(MyNet())
params = model.parameters()
"""
Expand All @@ -1068,7 +1068,7 @@ def prepare(self, optimizer=None, loss=None, metrics=None):
and should be a Optimizer instance. It can be None in eval
and test mode.
loss (Loss|callable function|None): Loss function can
be a `fluid.dygraph.Layer` instance or any callable function
be a `paddle.nn.Layer` instance or any callable function
taken the predicted values and ground truth values as input.
It can be None when there is no loss.
metrics (Metric|list of Metric|None): If metrics is set, all
Expand All @@ -1087,7 +1087,7 @@ def prepare(self, optimizer=None, loss=None, metrics=None):
startup_prog_seed = fluid.default_startup_program(
).random_seed
fluid.disable_dygraph()
fluid.enable_dygraph(self._place)
paddle.disable_static(self._place)
# enable_dygraph would create and switch to a new program,
# thus also copy seed to the new program
fluid.default_main_program().random_seed = main_prog_seed
Expand All @@ -1100,9 +1100,9 @@ def prepare(self, optimizer=None, loss=None, metrics=None):

self._optimizer = optimizer
if loss is not None:
if not isinstance(loss, fluid.dygraph.Layer) and not callable(loss):
if not isinstance(loss, paddle.nn.Layer) and not callable(loss):
raise TypeError("'loss' must be sub classes of " \
"`fluid.dygraph.Layer` or any callable function.")
"`paddle.nn.Layer` or any callable function.")
self._loss = loss

metrics = metrics or []
Expand Down Expand Up @@ -1188,7 +1188,7 @@ def fit(
dynamic = True
device = hapi.set_device('gpu')
fluid.enable_dygraph(device) if dynamic else None
paddle.disable_static(device) if dynamic else None
train_dataset = hapi.datasets.MNIST(mode='train')
val_dataset = hapi.datasets.MNIST(mode='test')
Expand Down Expand Up @@ -1221,7 +1221,7 @@ def fit(
dynamic = True
device = hapi.set_device('gpu')
fluid.enable_dygraph(device) if dynamic else None
paddle.disable_static(device) if dynamic else None
train_dataset = hapi.datasets.MNIST(mode='train')
train_loader = fluid.io.DataLoader(train_dataset,
Expand Down Expand Up @@ -1368,7 +1368,7 @@ def evaluate(
print(result)
# imperative mode
fluid.enable_dygraph()
paddle.disable_static()
model = hapi.Model(hapi.vision.LeNet())
model.prepare(metrics=paddle.metric.Accuracy())
result = model.evaluate(val_dataset, batch_size=64)
Expand Down Expand Up @@ -1475,7 +1475,7 @@ def __len__(self):
# imperative mode
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
paddle.disable_static(device)
model = hapi.Model(hapi.vision.LeNet())
model.prepare()
result = model.predict(test_dataset, batch_size=64)
Expand Down
27 changes: 14 additions & 13 deletions python/paddle/metric/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ class Metric(object):
.. code-block:: python
def compute(pred, label):
# sort prediction and slice the top-5 scores
pred = fluid.layers.argsort(pred, descending=True)[1][:, :5]
pred = paddle.argsort(pred, descending=True)[1][:, :5]
# calculate whether the predictions are correct
correct = pred == label
return fluid.layers.cast(correct, dtype='float32')
return paddle.cast(correct, dtype='float32')
With the :code:`compute`, we split some calculations to OPs (which
may run on GPU devices, will be faster), and only fetch 1 tensor with
Expand Down Expand Up @@ -184,12 +184,12 @@ def __init__(self, topk=(1, ), name='acc', *args, **kwargs):
import paddle
paddle.disable_static()
x = paddle.to_variable(np.array([
x = paddle.to_tensor(np.array([
[0.1, 0.2, 0.3, 0.4],
[0.1, 0.4, 0.3, 0.2],
[0.1, 0.2, 0.4, 0.3],
[0.1, 0.2, 0.3, 0.4]]))
y = paddle.to_variable(np.array([[0], [1], [2], [3]]))
y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
m = paddle.metric.Accuracy()
correct = m.compute(x, y)
Expand All @@ -203,14 +203,13 @@ def __init__(self, topk=(1, ), name='acc', *args, **kwargs):
.. code-block:: python
import paddle
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
paddle.disable_static()
train_dataset = hapi.datasets.MNIST(mode='train')
model = hapi.Model(hapi.vision.LeNet(classifier_activation=None))
optim = fluid.optimizer.Adam(
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameter_list=model.parameters())
model.prepare(
optim,
Expand Down Expand Up @@ -241,9 +240,9 @@ def compute(self, pred, label, *args):
Return:
Tensor: Correct mask, a tensor with shape [batch_size, topk].
"""
pred = fluid.layers.argsort(pred, descending=True)[1][:, :self.maxk]
pred = paddle.argsort(pred, descending=True)[1][:, :self.maxk]
correct = pred == label
return fluid.layers.cast(correct, dtype='float32')
return paddle.cast(correct, dtype='float32')

def update(self, correct, *args):
"""
Expand Down Expand Up @@ -446,7 +445,7 @@ class Recall(Metric):
Args:
name (str, optional): String name of the metric instance.
Default is `precision`.
Default is `recall`.
Example by standalone:
Expand Down Expand Up @@ -583,10 +582,13 @@ class Auc(Metric):
computed using the height of the precision values by the recall.
Args:
name (str, optional): String name of the metric instance. Default
is `acc`.
curve (str): Specifies the mode of the curve to be computed,
'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'.
num_thresholds (int): The number of thresholds to use when
discretizing the roc curve. Default is 4095.
'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'.
name (str, optional): String name of the metric instance. Default
is `auc`.
"NOTE: only implement the ROC curve type via Python now."
Expand Down Expand Up @@ -615,7 +617,6 @@ class Auc(Metric):
import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.nn as nn
import paddle.incubate.hapi as hapi
Expand All @@ -640,7 +641,7 @@ def __len__(self):
learning_rate=0.001, parameter_list=model.parameters())
def loss(x, y):
return fluid.layers.cross_entropy(x, y)
return nn.functional.nll_loss(paddle.log(x), y)
model.prepare(
optim,
Expand Down

0 comments on commit 362d914

Please sign in to comment.