|
|
|
@ -25,7 +25,6 @@ from paddle.fluid.dygraph.base import switch_to_static_graph
|
|
|
|
|
from paddle.fluid.dygraph import to_variable
|
|
|
|
|
from paddle.fluid.dygraph.nn import Conv2D, Linear, Pool2D
|
|
|
|
|
from paddle.fluid.optimizer import AdamOptimizer
|
|
|
|
|
from paddle.fluid.dygraph.jit import declarative
|
|
|
|
|
from paddle.fluid.dygraph.io import VARIABLE_FILENAME
|
|
|
|
|
from paddle.fluid.dygraph.dygraph_to_static import ProgramTranslator
|
|
|
|
|
|
|
|
|
@ -102,7 +101,7 @@ class MNIST(fluid.dygraph.Layer):
|
|
|
|
|
loc=0.0, scale=scale)),
|
|
|
|
|
act="softmax")
|
|
|
|
|
|
|
|
|
|
@declarative
|
|
|
|
|
@paddle.jit.to_static
|
|
|
|
|
def forward(self, inputs, label=None):
|
|
|
|
|
x = self.inference(inputs)
|
|
|
|
|
if label is not None:
|
|
|
|
@ -134,7 +133,7 @@ class TestMNIST(unittest.TestCase):
|
|
|
|
|
drop_last=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMNISTWithDeclarative(TestMNIST):
|
|
|
|
|
class TestMNISTWithToStatic(TestMNIST):
|
|
|
|
|
"""
|
|
|
|
|
Tests model if doesn't change the layers while decorated
|
|
|
|
|
by `dygraph_to_static_output`. In this case, everything should
|
|
|
|
@ -147,7 +146,7 @@ class TestMNISTWithDeclarative(TestMNIST):
|
|
|
|
|
def train_dygraph(self):
|
|
|
|
|
return self.train(to_static=False)
|
|
|
|
|
|
|
|
|
|
def test_mnist_declarative(self):
|
|
|
|
|
def test_mnist_to_static(self):
|
|
|
|
|
dygraph_loss = self.train_dygraph()
|
|
|
|
|
static_loss = self.train_static()
|
|
|
|
|
self.assertTrue(
|
|
|
|
|