-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Closed
Description
Pytorch uses Python to call each operator, the following API uses Python to call each compiled function.
fit_a_line.py
class FitALine(fluid.Program):
DEFAULT_READER_FILE_PATH = './data.recordio'
DEFAULT_READER_BATCH_SIZE = 128
# @network decorator will be used by the compiler to generate a
# ProgramDesc block. It can optionally take inputs, which represents a
# mapping from the input vars to vars in the block
@network()
def train_step(self):
reader = fluid.batch_reader(file=DEFAULT_READER_FILE_PATH,
batch_size=DEFAULT_READER_BATCH_SIZE,
shape=[[13], [1]],
dtype=['float32', 'float32'],
format='recordio')
x, y = self.reader.next_item()
with fluid.var_scope('prediction'):
# Since we want to be able to access the same weights and bias during
# inference, we need to namespace the variables.
# fluid.var_scope block guard will create a new UniqueNameGenerator when
# we enter the block, and rollback to the previous UniqueNameGenerator when
# the block exits.
y_predict = fluid.layers.fc(input=x, size=1, act=None)
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_cost = fluid.layers.mean(cost, name='avg_cost')
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
sgd_optimizer.minimize(avg_cost)
return avg_cost
@network("x")
def infer(self):
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
with fluid.var_scope('prediction'):
return fluid.layers.fc(input=x, size=1, act=None)main.py
fit_a_line = FitALine(batch_size=256).Compile()
while i in range(1000):
avg_cost = fit_a_line.train_step()
y_results = fit_a_line.infer([3,4,6,3,5,7,8,6,5,4,1,5,8])Transpiler
Trainer
> TRAINING_ROLE=TRAINER python main.py --distributed main=train_step
PServer
> TRAINING_ROLE=PSERVER paddle run train.py --distributed main=train_step
Metadata
Metadata
Assignees
Labels
No labels