Created by: Aurelius84
Adds ProgramCache
In this PR, FunctionCache
and ProgramCache
are added to avoid transforming code of dygrah function by AST analyze and inserting ops into main_program, respectively.
That means program will be built once, even users put myNet(input)
into for
loop.
code example:
def train_mnist():
with fluid.program_guard(fluid.Program()):
mnist = MNIST()
for epoch in range(self.epoch_num):
for batch_id, data in enumerate(self.train_reader()):
dy_x_data = np.array([
x[0].reshape(1, 28, 28) for x in data
]).astype('float32')
y_data = np.array([x[1] for x in data
]).astype('int64').reshape(-1, 1)
# fetch outputs dynamically as dygraph model.
# Program will be built only once.
prediction, acc, avg_loss = mnist(dy_x_data, label=y_data)
Moreover, code related to exe.run
is moved into ProgramCache
to allow users specifying optimizer
which used to minimize loss.