From f096af83a0d7f55aaf153f923049fa547c002380 Mon Sep 17 00:00:00 2001 From: Shibo Tao <62922815+T8T9@users.noreply.github.com> Date: Thu, 19 Nov 2020 10:20:23 +0800 Subject: [PATCH] fix document sample. test=develop (#28721) --- python/paddle/static/io.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/python/paddle/static/io.py b/python/paddle/static/io.py index b30dfa8429..14536b880f 100644 --- a/python/paddle/static/io.py +++ b/python/paddle/static/io.py @@ -78,23 +78,21 @@ def save_inference_model(path_prefix, feed_vars, fetch_vars, executor): .. code-block:: python import paddle - import paddle.fluid as fluid paddle.enable_static() path_prefix = "./infer_model" # User defined network, here a softmax regession example - image = fluid.data(name='img', shape=[None, 28, 28], dtype='float32') - label = fluid.data(name='label', shape=[None, 1], dtype='int64') - feeder = fluid.DataFeeder(feed_list=[image, label], place=fluid.CPUPlace()) - predict = fluid.layers.fc(input=image, size=10, act='softmax') + image = paddle.static.data(name='img', shape=[None, 28, 28], dtype='float32') + label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') + predict = paddle.static.nn.fc(image, 10, activation='softmax') - loss = fluid.layers.cross_entropy(input=predict, label=label) - avg_loss = fluid.layers.mean(loss) + loss = paddle.nn.functional.cross_entropy(predict, label) + avg_loss = paddle.tensor.stat.mean(loss) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = paddle.static.Executor(paddle.CPUPlace()) + exe.run(paddle.static.default_startup_program()) # Feed data and train process @@ -223,22 +221,20 @@ def load_inference_model(path_prefix, executor, **configs): .. code-block:: python import paddle - import paddle.fluid as fluid import numpy as np paddle.enable_static() # Build the model - startup_prog = fluid.default_startup_program() - main_prog = fluid.default_main_program() - with fluid.program_guard(main_prog, startup_prog): - image = fluid.layers.data(name="img", shape=[64, 784], append_batch_size=False) - w = fluid.layers.create_parameter(shape=[784, 200], dtype='float32') - b = fluid.layers.create_parameter(shape=[200], dtype='float32') - hidden_w = fluid.layers.matmul(x=image, y=w) - hidden_b = fluid.layers.elementwise_add(hidden_w, b) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + startup_prog = paddle.static.default_startup_program() + main_prog = paddle.static.default_main_program() + with paddle.static.program_guard(main_prog, startup_prog): + image = paddle.static.data(name="img", shape=[64, 784]) + w = paddle.create_parameter(shape=[784, 200], dtype='float32') + b = paddle.create_parameter(shape=[200], dtype='float32') + hidden_w = paddle.matmul(x=image, y=w) + hidden_b = paddle.add(hidden_w, b) + exe = paddle.static.Executor(paddle.CPUPlace()) exe.run(startup_prog) # Save the inference model @@ -247,7 +243,7 @@ def load_inference_model(path_prefix, executor, **configs): [inference_program, feed_target_names, fetch_targets] = ( paddle.static.io.load_inference_model(path_prefix, exe)) - tensor_img = np.array(np.random.random((1, 64, 784)), dtype=np.float32) + tensor_img = np.array(np.random.random((64, 784)), dtype=np.float32) results = exe.run(inference_program, feed={feed_target_names[0]: tensor_img}, fetch_list=fetch_targets) -- GitLab