Evaluate model while training

fluid.Program for model test and evaluation is different from the one for training. In the test and evalution phase:

  1. There is no back propagation and no process of optimizing and updating parameters in evaluation and test.

  2. Operations in model evaluation can be different.

    • Take the operator BatchNorm for example, algorithms are different in train and test.

    • Evaluation model and training model can be totally different.

Generate fluid.Program for test

Generate test fluid.Program by cloning training fluid.Program

System Message: WARNING/2 (/home/work/paddledoc/FluidDoc/doc/fluid/beginners_guide/coding_practice/test_while_training_en.rst, line 20)

Title underline too short.

Generate test :code:`fluid.Program` by cloning training :code:`fluid.Program`

Program.clone() can generate a copied new fluid.Program . You can generate a copy of Program with operations applied for test by setting Program.clone(for_test=True) . Simple usage is as follows:

import paddle.fluid as fluid

image = fluid.data(name="image", shape=[None, 784], dtype='float32')
label = fluid.data(name="label", shape=[None, 1], dtype="int64")
prediction = fluid.layers.fc(
  input=fluid.layers.fc(input=image, size=100, act='relu'),
loss = fluid.layers.mean(fluid.layers.cross_entropy(input=prediction, label=label))
acc = fluid.layers.accuracy(input=prediction, label=label)

test_program = fluid.default_main_program().clone(for_test=True)

adam = fluid.optimizer.Adam(learning_rate=0.001)

Before using Optimizer , please copy fluid.default_main_program() into a test_program . Then you can pass test data to test_program so that you can run test program without influencing training result.

Configure training fluid.Program and test fluid.Program individually

If the training program is largely different from test program, you can define two totally different fluid.Program , and perform training and test individually. In PaddlePaddle Fluid, all parameters are named. If two different operations or even two different networks use parameters with the same name, the value and memory space of these parameters are shared.

Fluid adopts fluid.unique_name package to randomly initialize the names of unnamed parameters. fluid.unique_name.guard can keep the initialized names consistent across multiple times of calling some function.

For example:

import paddle.fluid as fluid

def network(is_test):
    image = fluid.data(name="image", shape=[None, 784], dtype='float32')
    label = fluid.data(name="label", shape=[None, 1], dtype="int64")
    hidden = fluid.layers.fc(input=image, size=100, act="relu")
    hidden = fluid.layers.batch_norm(input=hidden, is_test=is_test)
    return loss

with fluid.unique_name.guard():
    train_loss = network(is_test=False)
    sgd = fluid.optimizer.SGD(0.001)

test_program = fluid.Program()
with fluid.unique_name.guard():
    with fluid.program_guard(test_program, fluid.Program()):
        test_loss = network(is_test=True)

# fluid.default_main_program() is the train program
# fluid.test_program is the test program

Perform test fluid.Program

Run test fluid.Program with Executor

You can run test fluid.Program with Executor.run(program=...) .

For example:

exe = fluid.Executor(fluid.CPUPlace())
test_acc = exe.run(program=test_program, feed=test_data_batch, fetch_list=[acc])
print 'Test accuracy is ', test_acc

Run test fluid.Program with ParallelExecutor

You can use ParallelExecutor for training and fluid.Program for test to create a new test ParallelExecutor ; then use test ParallelExecutor.run to run test process.

For example:

train_exec = fluid.ParallelExecutor(use_cuda=True, loss_name=loss.name)

test_exec = fluid.ParallelExecutor(use_cuda=True, share_vars_from=train_exec,
test_acc = test_exec.run(fetch_list=[acc], ...)