提交 23416e81 编写于 作者: R root

modify ce file code style of 01.fit_a_line and 02.recognize_digits

上级 e9abf856
...@@ -9,28 +9,31 @@ from kpi import CostKpi ...@@ -9,28 +9,31 @@ from kpi import CostKpi
train_cost_kpi = CostKpi('train_cost', 0.02, 0, actived=True, desc='train cost') train_cost_kpi = CostKpi('train_cost', 0.02, 0, actived=True, desc='train cost')
test_cost_kpi = CostKpi('test_cost', 0.02, 0, actived=True, desc='test cost') test_cost_kpi = CostKpi('test_cost', 0.02, 0, actived=True, desc='test cost')
tracking_kpis=[train_cost_kpi,test_cost_kpi] tracking_kpis = [train_cost_kpi, test_cost_kpi]
def parse_log(log): def parse_log(log):
for line in log.split('\n'): for line in log.split('\n'):
fs = line.strip().split('\t') fs = line.strip().split('\t')
print(fs) print(fs)
if len(fs) == 3 and fs[0] =='kpis': if len(fs) == 3 and fs[0] == 'kpis':
print("-----%s" % fs) print("-----%s" % fs)
kpi_name = fs[1] kpi_name = fs[1]
kpi_value = float(fs[2]) kpi_value = float(fs[2])
yield kpi_name,kpi_value yield kpi_name, kpi_value
def log_to_ce(log): def log_to_ce(log):
kpi_tracker = {} kpi_tracker = {}
for kpi in tracking_kpis: for kpi in tracking_kpis:
kpi_tracker[kpi.name]=kpi kpi_tracker[kpi.name] = kpi
for (kpi_name, kpi_value) in parse_log(log): for (kpi_name, kpi_value) in parse_log(log):
print(kpi_name,kpi_value) print(kpi_name, kpi_value)
kpi_tracker[kpi_name].add_record(kpi_value) kpi_tracker[kpi_name].add_record(kpi_value)
kpi_tracker[kpi_name].persist() kpi_tracker[kpi_name].persist()
if __name__ == '__main__':
log = sys.stdin.read()
log_to_ce(log)
if __name__ == '__main__':
log = sys.stdin.read()
log_to_ce(log)
...@@ -23,14 +23,24 @@ import numpy ...@@ -23,14 +23,24 @@ import numpy
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
def parse_args(): def parse_args():
parser = argparse.ArgumentParser("fit_a_line") parser = argparse.ArgumentParser("fit_a_line")
parser.add_argument('--enable_ce', action='store_true', help="If set, run the task with continuous evaluation logs." ) parser.add_argument(
parser.add_argument('--use_gpu', type=bool, default=False, help="Whether to use GPU or not." ) '--enable_ce',
parser.add_argument('--num_epochs', type=int, default=100, help="number of epochs." ) action='store_true',
help="If set, run the task with continuous evaluation logs.")
parser.add_argument(
'--use_gpu',
type=bool,
default=False,
help="Whether to use GPU or not.")
parser.add_argument(
'--num_epochs', type=int, default=100, help="number of epochs.")
args = parser.parse_args() args = parser.parse_args()
return args return args
# For training test cost # For training test cost
def train_test(executor, program, reader, feeder, fetch_list): def train_test(executor, program, reader, feeder, fetch_list):
accumulated = 1 * [0] accumulated = 1 * [0]
...@@ -62,14 +72,18 @@ def main(): ...@@ -62,14 +72,18 @@ def main():
batch_size = 20 batch_size = 20
if args.enable_ce: if args.enable_ce:
train_reader = paddle.batch(paddle.dataset.uci_housing.train(), batch_size=batch_size) train_reader = paddle.batch(
test_reader = paddle.batch(paddle.dataset.uci_housing.test(), batch_size=batch_size) paddle.dataset.uci_housing.train(), batch_size=batch_size)
else : test_reader = paddle.batch(
train_reader = paddle.batch( paddle.dataset.uci_housing.test(), batch_size=batch_size)
paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500), else:
train_reader = paddle.batch(
paddle.reader.shuffle(
paddle.dataset.uci_housing.train(), buf_size=500),
batch_size=batch_size) batch_size=batch_size)
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.reader.shuffle(paddle.dataset.uci_housing.test(), buf_size=500), paddle.reader.shuffle(
paddle.dataset.uci_housing.test(), buf_size=500),
batch_size=batch_size) batch_size=batch_size)
# feature vector of length 13 # feature vector of length 13
...@@ -78,11 +92,11 @@ def main(): ...@@ -78,11 +92,11 @@ def main():
main_program = fluid.default_main_program() main_program = fluid.default_main_program()
startup_program = fluid.default_startup_program() startup_program = fluid.default_startup_program()
if args.enable_ce: if args.enable_ce:
main_program.random_seed = 90 main_program.random_seed = 90
startup_program.random_seed = 90 startup_program.random_seed = 90
y_predict = fluid.layers.fc(input=x, size=1, act=None) y_predict = fluid.layers.fc(input=x, size=1, act=None)
cost = fluid.layers.square_error_cost(input=y_predict, label=y) cost = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_loss = fluid.layers.mean(cost) avg_loss = fluid.layers.mean(cost)
...@@ -140,12 +154,13 @@ def main(): ...@@ -140,12 +154,13 @@ def main():
sys.exit("got NaN loss, training failed.") sys.exit("got NaN loss, training failed.")
if params_dirname is not None: if params_dirname is not None:
# We can save the trained parameters for the inferences later # We can save the trained parameters for the inferences later
fluid.io.save_inference_model(params_dirname, ['x'], [y_predict], exe) fluid.io.save_inference_model(params_dirname, ['x'], [y_predict],
exe)
if args.enable_ce and pass_id == args.num_epochs - 1: if args.enable_ce and pass_id == args.num_epochs - 1:
print("kpis\ttrain_cost\t%f" % avg_loss_value[0]) print("kpis\ttrain_cost\t%f" % avg_loss_value[0])
print("kpis\ttest_cost\t%f" % test_metics[0]) print("kpis\ttest_cost\t%f" % test_metics[0])
infer_exe = fluid.Executor(place) infer_exe = fluid.Executor(place)
inference_scope = fluid.core.Scope() inference_scope = fluid.core.Scope()
...@@ -182,5 +197,5 @@ def main(): ...@@ -182,5 +197,5 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
args=parse_args() args = parse_args()
main() main()
...@@ -10,28 +10,30 @@ from kpi import AccKpi ...@@ -10,28 +10,30 @@ from kpi import AccKpi
train_cost_kpi = CostKpi('train_cost', 0.02, 0, actived=True, desc='train cost') train_cost_kpi = CostKpi('train_cost', 0.02, 0, actived=True, desc='train cost')
test_cost_kpi = CostKpi('test_cost', 0.02, 0, actived=True, desc='test cost') test_cost_kpi = CostKpi('test_cost', 0.02, 0, actived=True, desc='test cost')
test_acc_kpi= AccKpi('test_acc', 0.02, 0, actived=True, desc='test acc') test_acc_kpi = AccKpi('test_acc', 0.02, 0, actived=True, desc='test acc')
tracking_kpis=[train_cost_kpi, test_cost_kpi, test_acc_kpi] tracking_kpis = [train_cost_kpi, test_cost_kpi, test_acc_kpi]
def parse_log(log): def parse_log(log):
for line in log.split('\n'): for line in log.split('\n'):
fs = line.strip().split('\t') fs = line.strip().split('\t')
print(fs) print(fs)
if len(fs) == 3 and fs[0] =='kpis': if len(fs) == 3 and fs[0] == 'kpis':
kpi_name = fs[1] kpi_name = fs[1]
kpi_value = float(fs[2]) kpi_value = float(fs[2])
yield kpi_name,kpi_value yield kpi_name, kpi_value
def log_to_ce(log): def log_to_ce(log):
kpi_tracker = {} kpi_tracker = {}
for kpi in tracking_kpis: for kpi in tracking_kpis:
kpi_tracker[kpi.name]=kpi kpi_tracker[kpi.name] = kpi
for (kpi_name, kpi_value) in parse_log(log): for (kpi_name, kpi_value) in parse_log(log):
print(kpi_name,kpi_value) print(kpi_name, kpi_value)
kpi_tracker[kpi_name].add_record(kpi_value) kpi_tracker[kpi_name].add_record(kpi_value)
kpi_tracker[kpi_name].persist() kpi_tracker[kpi_name].persist()
if __name__ == '__main__':
log = sys.stdin.read()
log_to_ce(log)
if __name__ == '__main__':
log = sys.stdin.read()
log_to_ce(log)
...@@ -21,14 +21,24 @@ import numpy ...@@ -21,14 +21,24 @@ import numpy
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
def parse_args(): def parse_args():
parser = argparse.ArgumentParser("mnist") parser = argparse.ArgumentParser("mnist")
parser.add_argument('--enable_ce', action='store_true', help="If set, run the task with continuous evaluation logs.") parser.add_argument(
parser.add_argument('--use_gpu', type=bool, default=False, help="Whether to use GPU or not.") '--enable_ce',
parser.add_argument('--num_epochs', type=int, default=5, help="number of epochs.") action='store_true',
args=parser.parse_args() help="If set, run the task with continuous evaluation logs.")
parser.add_argument(
'--use_gpu',
type=bool,
default=False,
help="Whether to use GPU or not.")
parser.add_argument(
'--num_epochs', type=int, default=5, help="number of epochs.")
args = parser.parse_args()
return args return args
def loss_net(hidden, label): def loss_net(hidden, label):
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax') prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label) loss = fluid.layers.cross_entropy(input=prediction, label=label)
...@@ -73,18 +83,21 @@ def train(nn_type, ...@@ -73,18 +83,21 @@ def train(nn_type,
params_filename=None): params_filename=None):
if use_cuda and not fluid.core.is_compiled_with_cuda(): if use_cuda and not fluid.core.is_compiled_with_cuda():
return return
startup_program = fluid.default_startup_program() startup_program = fluid.default_startup_program()
main_program = fluid.default_main_program() main_program = fluid.default_main_program()
if args.enable_ce: if args.enable_ce:
train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=BATCH_SIZE) train_reader = paddle.batch(
test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=BATCH_SIZE) paddle.dataset.mnist.train(), batch_size=BATCH_SIZE)
test_reader = paddle.batch(
paddle.dataset.mnist.test(), batch_size=BATCH_SIZE)
startup_program.random_seed = 90 startup_program.random_seed = 90
main_program.random_seed = 90 main_program.random_seed = 90
else : else:
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), batch_size=BATCH_SIZE) paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500),
batch_size=BATCH_SIZE)
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.dataset.mnist.test(), batch_size=BATCH_SIZE) paddle.dataset.mnist.test(), batch_size=BATCH_SIZE)
...@@ -122,7 +135,7 @@ def train(nn_type, ...@@ -122,7 +135,7 @@ def train(nn_type,
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
feeder = fluid.DataFeeder(feed_list=[img, label], place=place) feeder = fluid.DataFeeder(feed_list=[img, label], place=place)
exe.run(startup_program) exe.run(startup_program)
epochs = [epoch_id for epoch_id in range(PASS_NUM)] epochs = [epoch_id for epoch_id in range(PASS_NUM)]
...@@ -154,17 +167,17 @@ def train(nn_type, ...@@ -154,17 +167,17 @@ def train(nn_type,
exe, exe,
model_filename=model_filename, model_filename=model_filename,
params_filename=params_filename) params_filename=params_filename)
if args.enable_ce: if args.enable_ce:
print("kpis\ttrain_cost\t%f" % metrics[0] ) print("kpis\ttrain_cost\t%f" % metrics[0])
print("kpis\ttest_cost\t%s" % avg_loss_val) print("kpis\ttest_cost\t%s" % avg_loss_val)
print("kpis\ttest_acc\t%s" % acc_val) print("kpis\ttest_acc\t%s" % acc_val)
# find the best pass # find the best pass
best = sorted(lists, key=lambda list: float(list[1]))[0] best = sorted(lists, key=lambda list: float(list[1]))[0]
print('Best pass is %s, testing Avgcost is %s' % (best[0], best[1])) print('Best pass is %s, testing Avgcost is %s' % (best[0], best[1]))
print('The classification accuracy is %.2f%%' % (float(best[2]) * 100)) print('The classification accuracy is %.2f%%' % (float(best[2]) * 100))
def infer(use_cuda, def infer(use_cuda,
save_dirname=None, save_dirname=None,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册