Should have a unique type for the return value of program_func
Currently, in new API design, the traininig program_func only return loss Variable for Trainer.
https://github.com/PaddlePaddle/Paddle/blob/3356fb3c6eab41cdfcd745a09f182547822bb3b2/python/paddle/fluid/trainer.py#L78-L78
But this is not enough, because Trainer also need predict Variable to save model, and maybe there are also some other requirement, for example to return the acurrucy Variable. Maybe we need a return type for traininig program_func, like:
class TrainProgram(object):
def __init__(self, loss, predict, accuracy):
self.loss = loss
self.predict = predict
self.accuracy = accuracy
so Trainer can get these information.
Yes, the train network needs to return more information. Since inter network could also use this helper class, should we re-name this to TrainProgram to NetworkProgram
And the usage as followed.
def inference_network():
network_program = NetworkProgram()
data_shape = [3, 32, 32]
images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
network_program.predict = resnet_cifar10(images, 32)
return network_program
def train_network():
network_program = inference_network()
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
cost = fluid.layers.cross_entropy(input=network_program.predict, label=label)
avg_cost = fluid.layers.mean(cost)
accuracy = fluid.layers.accuracy(input=predict, label=label)
network_program.avg_cost = avg_cost
network_program.accuracy = accuracy
return network_program
Hope this is not too complex.
After a discussion with @wangkuiyi and @daming-lu , we have reached two conclusions:
-
All return values of
inference_programare 'predictions'. -
train_programcan return two kinds of variables: 'loss' and 'metric'. The first return value is the loss, and the others(if any) are metrics.
@JiayiFeng Is this the proposed syntax?
def train_program():
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
predict = inference_program()
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(cost)
acc = fluid.layers.accuracy(input=predict, label=label)
return avg_cost, {"acc": acc}