Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

Should have a unique type for the return value of program_func

Open jacquesqiao opened this issue 8 years ago • 3 comments

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.

jacquesqiao avatar May 08 '18 02:05 jacquesqiao

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.

jetfuel avatar May 09 '18 01:05 jetfuel

After a discussion with @wangkuiyi and @daming-lu , we have reached two conclusions:

  1. All return values of inference_program are 'predictions'.

  2. train_program can return two kinds of variables: 'loss' and 'metric'. The first return value is the loss, and the others(if any) are metrics.

JiayiFeng avatar May 09 '18 03:05 JiayiFeng

@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}

jetfuel avatar May 10 '18 20:05 jetfuel