def _prepare(self): # prepare reader self.test_reader = paddle.batch( reader=self.test_reader.create_reader(), batch_size=self.args.batch_size) # init paddle paddle.init(use_gpu=self.args.use_gpu, trainer_count=self.args.trainer_count) # create parameters and trainer model_out = self.model() out_names = [x.name for x in model_out] \ if isinstance(model_out, collections.Iterable) \ else model_out.name self.logger.info("out type: {}".format(model_out)) self.logger.info("out names: {}".format(out_names)) try: self.parameters = paddle.parameters.Parameters.from_tar( gzip.open(self.args.model_file, 'r')) except IOError: raise IOError('can not find: {}'.format(self.args.model_file)) self.inferer = paddle.inference.Inference( output_layer=model_out, parameters=self.parameters)
def main(): # init paddle.init(use_gpu=False, trainer_count=1) # network config x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) y_predict = paddle.layer.fc(input=x, param_attr=paddle.attr.Param(name='w'), size=1, act=paddle.activation.Linear(), bias_attr=paddle.attr.Param(name='b')) y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.mse_cost(input=y_predict, label=y) # create parameters parameters = paddle.parameters.create(cost) # create optimizer of new remote updater to pserver optimizer = paddle.optimizer.Momentum(momentum=0, learning_rate=1e-3) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer, is_local=False, pserver_spec=etcd_endpoints, use_etcd=True) # event_handler to print training and testing info def event_handler(event): if isinstance(event, paddle.event.EndIteration): # FIXME: for cloud data reader, pass number is managed by master # should print the server side pass number if event.batch_id % 100 == 0: print "Pass %d, Batch %d, Cost %f" % ( event.pass_id, event.batch_id, event.cost) if isinstance(event, paddle.event.EndPass): if (event.pass_id + 1) % 10 == 0: result = trainer.test( reader=paddle.batch( uci_housing.test(), batch_size=2), feeding={'x': 0, 'y': 1}) print "Test %d, %.2f" % (event.pass_id, result.cost) # training # NOTE: use uci_housing.train() as reader for non-paddlecloud training trainer.train( reader=paddle.batch( paddle.reader.shuffle( cloud_reader( ["/pfs/dlnel/public/dataset/uci_housing/uci_housing*"], etcd_endpoints), buf_size=500), batch_size=2), feeding={'x': 0, 'y': 1}, event_handler=event_handler, num_passes=30)
def main(): paddle.init(use_gpu=False, trainer_count=1) word_dict = paddle.dataset.imikolov.build_dict() dict_size = len(word_dict) firstword = paddle.layer.data( name="firstw", type=paddle.data_type.integer_value(dict_size)) secondword = paddle.layer.data( name="secondw", type=paddle.data_type.integer_value(dict_size)) thirdword = paddle.layer.data( name="thirdw", type=paddle.data_type.integer_value(dict_size)) fourthword = paddle.layer.data( name="fourthw", type=paddle.data_type.integer_value(dict_size)) nextword = paddle.layer.data( name="fifthw", type=paddle.data_type.integer_value(dict_size)) Efirst = wordemb(firstword) Esecond = wordemb(secondword) Ethird = wordemb(thirdword) Efourth = wordemb(fourthword) contextemb = paddle.layer.concat(input=[Efirst, Esecond, Ethird, Efourth]) hidden1 = paddle.layer.fc(input=contextemb, size=hiddensize, act=paddle.activation.Sigmoid(), layer_attr=paddle.attr.Extra(drop_rate=0.5), bias_attr=paddle.attr.Param(learning_rate=2), param_attr=paddle.attr.Param( initial_std=1. / math.sqrt(embsize * 8), learning_rate=1)) predictword = paddle.layer.fc(input=hidden1, size=dict_size, bias_attr=paddle.attr.Param(learning_rate=2), act=paddle.activation.Softmax()) def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: result = trainer.test( paddle.batch( paddle.dataset.imikolov.test(word_dict, N), 32)) print "Pass %d, Batch %d, Cost %f, %s, Testing metrics %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics, result.metrics) cost = paddle.layer.classification_cost(input=predictword, label=nextword) parameters = paddle.parameters.create(cost) adam_optimizer = paddle.optimizer.Adam( learning_rate=3e-3, regularization=paddle.optimizer.L2Regularization(8e-4)) trainer = paddle.trainer.SGD(cost, parameters, adam_optimizer) trainer.train( paddle.batch(paddle.dataset.imikolov.train(word_dict, N), 32), num_passes=30, event_handler=event_handler)
def main(): # init paddle.init(use_gpu=False, trainer_count=1) # network config x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) y_predict = paddle.layer.fc(input=x, param_attr=paddle.attr.Param(name='w'), size=1, act=paddle.activation.Linear(), bias_attr=paddle.attr.Param(name='b')) y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.mse_cost(input=y_predict, label=y) # create parameters parameters = paddle.parameters.create(cost) # create optimizer optimizer = paddle.optimizer.Momentum(momentum=0) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer) # event_handler to print training and testing info def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "Pass %d, Batch %d, Cost %f" % ( event.pass_id, event.batch_id, event.cost) if isinstance(event, paddle.event.EndPass): if (event.pass_id + 1) % 10 == 0: result = trainer.test( reader=paddle.batch( uci_housing.test(), batch_size=2), feeding={'x': 0, 'y': 1}) print "Test %d, %.2f" % (event.pass_id, result.cost) # training trainer.train( reader=paddle.batch( paddle.reader.shuffle( uci_housing.train(), buf_size=500), batch_size=2), feeding={'x': 0, 'y': 1}, event_handler=event_handler, num_passes=30)
def test_param_conf_order(self): paddle.init() cost = train() parameters = paddle.parameters.create(cost) adagrad = paddle.optimizer.AdaGrad( learning_rate=3e-3, regularization=paddle.optimizer.L2Regularization(rate=8e-4)) trainer = paddle.trainer.SGD(cost, parameters, adagrad) for p in trainer.get_topology_proto().parameters: if p.name == "_fc1.w0": self.assertEqual(p.decay_rate, 6e-4) else: self.assertEqual(p.decay_rate, 8e-4)
def main(): paddle.init(use_gpu=False, trainer_count=1) # define network topology crf_cost, crf_dec = db_lstm() # create parameters parameters = paddle.parameters.create([crf_cost, crf_dec]) # create optimizer optimizer = paddle.optimizer.Momentum( momentum=0, learning_rate=2e-2, regularization=paddle.optimizer.L2Regularization(rate=8e-4), model_average=paddle.optimizer.ModelAverage( average_window=0.5, max_average_window=10000), ) def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) trainer = paddle.trainer.SGD(cost=crf_cost, parameters=parameters, update_equation=optimizer) parameters.set('emb', load_parameter(conll05.get_embedding(), 44068, 32)) trn_reader = paddle.batch( paddle.reader.shuffle( conll05.test(), buf_size=8192), batch_size=10) feeding = { 'word_data': 0, 'ctx_n2_data': 1, 'ctx_n1_data': 2, 'ctx_0_data': 3, 'ctx_p1_data': 4, 'ctx_p2_data': 5, 'verb_data': 6, 'mark_data': 7, 'target': 8 } trainer.train( reader=trn_reader, event_handler=event_handler, num_passes=10000, feeding=feeding)
def main(task="train", use_gpu=False, trainer_count=1, save_dir="models"): if task == "train": if not os.path.exists(save_dir): os.mkdir(save_dir) paddle.init(use_gpu=use_gpu, trainer_count=trainer_count) cost = network() parameters = paddle.parameters.create(cost) optimizer = paddle.optimizer.Momentum( learning_rate=0.1 / 128.0, momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128)) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer) def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: logger.info("Pass %d, Batch %d, Cost %f, %s" % (event.pass_id, event.batch_id, event.cost, event.metrics)) if isinstance(event, paddle.event.EndPass): with gzip.open( os.path.join(save_dir, "params_pass_%d.tar" % event.pass_id), "w") as f: trainer.save_parameter_to_tar(f) trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler, num_passes=5) elif task == "dump_config": predict = network(is_infer=True) dump_v2_config(predict, "trainer_config.bin", True) else: raise RuntimeError(("Error value for parameter task. " "Available options are: train and dump_config."))
def _prepare(self): # prepare reader self.train_reader = paddle.batch( reader=self.train_reader.create_reader(), batch_size=self.args.batch_size) self.test_reader = paddle.batch( reader=self.test_reader.create_reader(), batch_size=self.args.batch_size) # init paddle paddle.init(use_gpu=self.args.use_gpu, trainer_count=self.args.trainer_count) # create optimizer optimizer = paddle.optimizer.RMSProp( learning_rate=self.args.learning_rate, regularization=opt.L2Regularization(rate=self.args.l2)) # create parameters and trainer model_out = self.model() if self.args.init_from: self.parameters = paddle.parameters.Parameters.from_tar( gzip.open(self.args.init_from, 'r')) else: self.parameters = paddle.parameters.create(model_out) if self.args.pre_emb.strip() != '': embeddings = np.loadtxt(self.args.pre_emb, dtype=float)[:self.args.vocab_size] self.parameters.set(self.model.name + '.embs', embeddings) self.logger.info('init emb from {} to {}'.format( self.args.pre_emb, self.model.name + '.embs')) self.trainer = paddle.trainer.SGD(cost=model_out, parameters=self.parameters, update_equation=optimizer)
def test_demo(): # PaddlePaddle init paddle.init(use_gpu=True, gpu_id=FLAGS.gpu_id) params = sun3d.set_params() inputs = d_net.get_demon_inputs(params) # Add neural network config outputs_bs = d_net.bootstrap_net(inputs, params) outputs_it = d_net.iterative_net(inputs, params) outputs_re = d_net.refine_net(inputs, params) out_fields = ['flow', 'depth_inv', 'normal', 'rotation', 'translation'] my_g_layer_map = {} parameters_bs, topo_bs = paddle.parameters.create( [outputs_bs[x] for x in out_fields]) my_g_layer_map.update(cp.g_layer_map) parameters_it, topo_it = paddle.parameters.create( [outputs_it[x] for x in out_fields]) my_g_layer_map.update(cp.g_layer_map) parameters_re, topo_re = paddle.parameters.create(outputs_re['depth_0']) my_g_layer_map.update(cp.g_layer_map) print('load parameters') with gzip.open(FLAGS.model, 'r') as f: parameters_init = paddle.parameters.Parameters.from_tar(f) for name in parameters_bs.names(): parameters_bs.set(name, parameters_init.get(name)) for name in parameters_it.names(): parameters_it.set(name, parameters_init.get(name)) for name in parameters_re.names(): parameters_re.set(name, parameters_init.get(name)) # Read image pair 1, 2 flow for scene_name in params['train_scene'][1:]: image_list = preprocess_util.list_files(params['flow_path'] + scene_name + '/flow/') image2depth = sun3d.get_image_depth_matching(scene_name) for pair_name in image_list[0:2]: image1, image2, flow_gt, depth1_gt, normal1_gt = \ sun3d.load_image_pair(scene_name, pair_name, image2depth) #transform and yield image1_new = uts.transform(image1.copy(), height=params['size'][0], width=params['size'][1]) image2_new = uts.transform(image2.copy(), height=params['size'][0], width=params['size'][1]) intrinsic = np.array([0.89115971, 1.18821287, 0.5, 0.5]) test_data_bs = [(image1_new, image2_new)] feeding_bs = {'image1': 0, 'image2': 1} flow, depth_inv, normal, rotation, translation = paddle.infer( output=topo_bs, parameters=parameters_bs, input=test_data_bs, feeding=feeding_bs) for i in range(3): test_data_it = [(image1_new, image2_new, intrinsic, rotation, translation, depth_inv, normal)] feeding_it = { 'image1': 0, 'image2': 1, 'intrinsic': 2, 'rotation': 3, 'translation': 4, 'depth_inv': 5, 'normal': 6 } flow, depth_inv, normal, rotation, translation = paddle.infer( output=topo_it, parameters=parameters_it, input=test_data_it, feeding=feeding_it) test_data_re = [(image1_new, image2_new, depth_inv)] feeding_re = {'image1': 0, 'image2': 1, 'depth_inv': 2} depth = paddle.infer(output=topo_re, parameters=parameters_re, input=test_data_re, feeding=feeding_re) layer_names = [ outputs_it['flow'].name, outputs_it['normal'].name, outputs_re['depth_0'].name ] height_list = [my_g_layer_map[x].height for x in layer_names] width_list = [my_g_layer_map[x].width for x in layer_names] flow, normal, depth = vec2img(inputs=[flow, normal, depth], height=height_list, width=width_list) # visualize depth in 3D # image1_down = cv2.resize(image1, # (depth.shape[1], depth.shape[0])) # visualize_prediction( # depth=depth, # image=np.uint8(image1_down.transpose([2, 0, 1])), # rotation=rotation, # translation=translation) with open('./test/depth_gt.npy', 'wb') as f: np.save(f, depth1_gt) with open('./test/depth_res.npy', 'wb') as f: np.save(f, depth) uts.plot_images(OrderedDict([ ('image1', image1), ('image2', image2), ('flow', flow), ('flow_gt', flow_gt), ('depth', depth), ('depth_gt', depth1_gt), ('normal', (normal + 1.0) / 2.), ('normal_gt', (normal1_gt + 1.0) / 2) ]), layout=[4, 2])
def main(): datadim = 3 * 32 * 32 classdim = 10 # PaddlePaddle init paddle.init(use_gpu=False, trainer_count=1) image = paddle.layer.data( name="image", type=paddle.data_type.dense_vector(datadim)) # Add neural network config # option 1. resnet # net = resnet_cifar10(image, depth=32) # option 2. vgg net = vgg_bn_drop(image) out = paddle.layer.fc(input=net, size=classdim, act=paddle.activation.Softmax()) lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(classdim)) cost = paddle.layer.classification_cost(input=out, label=lbl) # Create parameters parameters = paddle.parameters.create(cost) # Create optimizer momentum_optimizer = paddle.optimizer.Momentum( momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0002 * 128), learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, learning_rate_schedule='discexp', batch_size=128) # End batch and end pass event handler def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "\nPass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) else: sys.stdout.write('.') sys.stdout.flush() if isinstance(event, paddle.event.EndPass): result = trainer.test( reader=paddle.batch( paddle.dataset.cifar.test10(), batch_size=128), feeding={'image': 0, 'label': 1}) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) # Create trainer trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=momentum_optimizer) trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.cifar.train10(), buf_size=50000), batch_size=128), num_passes=5, event_handler=event_handler, feeding={'image': 0, 'label': 1})
seq = [] prob = beam_result[0] for i in xrange(gen_num): print "\n*******************************************************\n" print "src:", ' '.join([src_dict.get(w) for w in gen_data[i][0]]), "\n" for j in xrange(beam_size): print "prob = %f:" % (prob[i][j]), seq_list[i * beam_size + j] if __name__ == '__main__': args = parse_args() dict_size = 30000 paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count) # use the first 3 samples for generation gen_creator = paddle.dataset.wmt14.gen(dict_size) gen_data = [] gen_num = 3 for item in gen_creator(): gen_data.append((item[0], )) if len(gen_data) == gen_num: break generate(gen_data, dict_size=dict_size, model_path=args.model_path, beam_size=args.beam_size)
lstm_last = paddle.layer.pooling(input=inputs[1], pooling_type=paddle.pooling.Max()) output = paddle.layer.fc(input=[fc_last, lstm_last], size=class_dim, act=paddle.activation.Softmax(), bias_attr=bias_attr, param_attr=para_attr) lbl = paddle.layer.data("label", paddle.data_type.integer_value(2)) cost = paddle.layer.classification_cost(input=output, label=lbl) return cost if __name__ == '__main__': # init paddle.init(use_gpu=False) #data print 'load dictionary...' word_dict = paddle.dataset.imdb.word_dict() dict_dim = len(word_dict) class_dim = 2 train_reader = paddle.batch(paddle.reader.shuffle( lambda: paddle.dataset.imdb.train(word_dict), buf_size=1000), batch_size=100) test_reader = paddle.batch(lambda: paddle.dataset.imdb.test(word_dict), batch_size=100) feeding = {'word': 0, 'label': 1} # network config
def main(): # 初始化定义跑模型的设备 paddle.init(use_gpu=with_gpu, trainer_count=1) # 读取数据 images = paddle.layer.data( name='pixel', type=paddle.data_type.dense_vector(784)) label = paddle.layer.data( name='label', type=paddle.data_type.integer_value(10)) # 调用之前定义的网络结构 predict = convolutional_neural_network_org(images)#原网络结构 # predict = convolutional_neural_network(images) # 定义损失函数 cost = paddle.layer.classification_cost(input=predict, label=label) # 指定训练相关的参数 parameters = paddle.parameters.create(cost) # 定义训练方法 optimizer = paddle.optimizer.Momentum( learning_rate=0.1 / 128.0, momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128)) # 训练模型 trainer = paddle.trainer.SGD( cost=cost, parameters=parameters, update_equation=optimizer) lists = [] # 定义event_handler,输出训练过程中的结果 def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): # 保存参数 with open('params_pass_%d.tar' % event.pass_id, 'w') as f: parameters.to_tar(f) result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % ( event.pass_id, result.cost, result.metrics) lists.append((event.pass_id, result.cost, result.metrics['classification_error_evaluator'])) trainer.train( reader=paddle.batch( paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192), batch_size=64), event_handler=event_handler, num_passes=50) # 找到训练误差最小的一次结果 best = sorted(lists, key=lambda list: float(list[1]))[0] print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1]) print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100) # 加载数据 def load_image(file): im = Image.open(file).convert('L') im = im.resize((28, 28), Image.ANTIALIAS) im = np.array(im).astype(np.float32).flatten() im = im / 255.0 return im # 测试结果 test_data = [] cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/image/infer_3.png'), )) probs = paddle.infer( output_layer=predict, parameters=parameters, input=test_data) lab = np.argsort(-probs) # probs and lab are the results of one batch data print "Label of image/infer_3.png is: %d" % lab[0][0]
def main(train_data_file, test_data_file, vocab_file, target_file, emb_file, model_save_dir, num_passes=100, batch_size=64): if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) word_dict = load_dict(vocab_file) label_dict = load_dict(target_file) word_vector_values = get_embedding(emb_file) word_dict_len = len(word_dict) label_dict_len = len(label_dict) paddle.init(use_gpu=False, trainer_count=1) # define network topology crf_cost, crf_dec, target = ner_net(word_dict_len, label_dict_len) evaluator.sum(name="error", input=crf_dec) evaluator.chunk(name="ner_chunk", input=crf_dec, label=target, chunk_scheme="IOB", num_chunk_types=(label_dict_len - 1) / 2) # create parameters parameters = paddle.parameters.create(crf_cost) parameters.set("emb", word_vector_values) # create optimizer optimizer = paddle.optimizer.Momentum( momentum=0, learning_rate=2e-4, regularization=paddle.optimizer.L2Regularization(rate=8e-4), gradient_clipping_threshold=25, model_average=paddle.optimizer.ModelAverage(average_window=0.5, max_average_window=10000), ) trainer = paddle.trainer.SGD(cost=crf_cost, parameters=parameters, update_equation=optimizer, extra_layers=crf_dec) train_reader = paddle.batch(paddle.reader.shuffle(reader.data_reader( train_data_file, word_dict, label_dict), buf_size=1000), batch_size=batch_size) test_reader = paddle.batch(paddle.reader.shuffle(reader.data_reader( test_data_file, word_dict, label_dict), buf_size=1000), batch_size=batch_size) feeding = {"word": 0, "mark": 1, "target": 2} def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 5 == 0: logger.info( "Pass %d, Batch %d, Cost %f, %s" % (event.pass_id, event.batch_id, event.cost, event.metrics)) if event.batch_id % 50 == 0: result = trainer.test(reader=test_reader, feeding=feeding) logger.info("\nTest with Pass %d, Batch %d, %s" % (event.pass_id, event.batch_id, result.metrics)) if isinstance(event, paddle.event.EndPass): # save parameters with gzip.open( os.path.join(model_save_dir, "params_pass_%d.tar.gz" % event.pass_id), "w") as f: trainer.save_parameter_to_tar(f) result = trainer.test(reader=test_reader, feeding=feeding) logger.info("\nTest with Pass %d, %s" % (event.pass_id, result.metrics)) trainer.train(reader=train_reader, event_handler=event_handler, num_passes=num_passes, feeding=feeding)
def main(): paddle.init(use_gpu=False, trainer_count=1) # define network topology images = paddle.layer.data( name='pixel', type=paddle.data_type.dense_vector(784)) label = paddle.layer.data( name='label', type=paddle.data_type.integer_value(10)) # Here we can build the prediction network in different ways. Please # choose one by uncomment corresponding line. predict = softmax_regression(images) #predict = multilayer_perceptron(images) #predict = convolutional_neural_network(images) cost = paddle.layer.classification_cost(input=predict, label=label) parameters = paddle.parameters.create(cost) optimizer = paddle.optimizer.Momentum( learning_rate=0.1 / 128.0, momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128)) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer, is_local=False, pserver_spec="localhost:3000") lists = [] def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 1000 == 0: print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) elif isinstance(event, paddle.event.EndPass): result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % ( event.pass_id, result.cost, result.metrics) lists.append((event.pass_id, result.cost, result.metrics['classification_error_evaluator'])) trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler, num_passes=100) # find the best pass best = sorted(lists, key=lambda list: float(list[1]))[0] print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1]) print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100) test_creator = paddle.dataset.mnist.test() test_data = [] for item in test_creator(): test_data.append((item[0], )) if len(test_data) == 100: break # output is a softmax layer. It returns probabilities. # Shape should be (100, 10) probs = paddle.infer( output_layer=predict, parameters=parameters, input=test_data) print probs.shape
def main(): # get arguments from env # for local training TRUTH = ["true", "True", "TRUE", "1", "yes", "Yes", "YES"] cluster_train = os.getenv('PADDLE_CLUSTER_TRAIN', "False") in TRUTH use_gpu = os.getenv('PADDLE_INIT_USE_GPU', "False") if not cluster_train: paddle.init( use_gpu=use_gpu, trainer_count=int(os.getenv("PADDLE_INIT_TRAINER_COUNT", "1"))) else: paddle.init( use_gpu=use_gpu, trainer_count=int(os.getenv("PADDLE_INIT_TRAINER_COUNT", "1")), port=int(os.getenv("PADDLE_INIT_PORT", "7164")), ports_num=int(os.getenv("PADDLE_INIT_PORTS_NUM", "1")), ports_num_for_sparse=int( os.getenv("PADDLE_INIT_PORTS_NUM_FOR_SPARSE", "1")), num_gradient_servers=int( os.getenv("PADDLE_INIT_NUM_GRADIENT_SERVERS", "1")), trainer_id=int(os.getenv("PADDLE_INIT_TRAINER_ID", "0")), pservers=os.getenv("PADDLE_INIT_PSERVERS", "127.0.0.1")) fn = open("thirdparty/wuyi_train_thdpty/word_dict.pickle", "r") word_dict = pickle.load(fn) fn.close() dict_size = len(word_dict) firstword = paddle.layer.data( name="firstw", type=paddle.data_type.integer_value(dict_size)) secondword = paddle.layer.data( name="secondw", type=paddle.data_type.integer_value(dict_size)) thirdword = paddle.layer.data( name="thirdw", type=paddle.data_type.integer_value(dict_size)) fourthword = paddle.layer.data( name="fourthw", type=paddle.data_type.integer_value(dict_size)) nextword = paddle.layer.data( name="fifthw", type=paddle.data_type.integer_value(dict_size)) Efirst = wordemb(firstword) Esecond = wordemb(secondword) Ethird = wordemb(thirdword) Efourth = wordemb(fourthword) contextemb = paddle.layer.concat(input=[Efirst, Esecond, Ethird, Efourth]) hidden1 = paddle.layer.fc(input=contextemb, size=hiddensize, act=paddle.activation.Sigmoid(), layer_attr=paddle.attr.Extra(drop_rate=0.5), bias_attr=paddle.attr.Param(learning_rate=2), param_attr=paddle.attr.Param( initial_std=1. / math.sqrt(embsize * 8), learning_rate=1)) predictword = paddle.layer.fc(input=hidden1, size=dict_size, bias_attr=paddle.attr.Param(learning_rate=2), act=paddle.activation.Softmax()) def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: result = trainer.test( paddle.batch( cluster_reader_cluster(cluster_test_file, node_id), 32)) print "Pass %d, Batch %d, Cost %f, %s, Testing metrics %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics, result.metrics) cost = paddle.layer.classification_cost(input=predictword, label=nextword) parameters = paddle.parameters.create(cost) adagrad = paddle.optimizer.AdaGrad( learning_rate=3e-3, regularization=paddle.optimizer.L2Regularization(8e-4)) trainer = paddle.trainer.SGD(cost, parameters, adagrad, is_local=not cluster_train) trainer.train( paddle.batch(cluster_reader_cluster(cluster_train_file, node_id), 32), num_passes=30, event_handler=event_handler)
def main(): datadim = 3 * 32 * 32 classdim = 10 # PaddlePaddle init paddle.init(use_gpu=with_gpu, trainer_count=7) image = paddle.layer.data( name="image", type=paddle.data_type.dense_vector(datadim)) # Add neural network config # option 1. resnet # net = resnet_cifar10(image, depth=32) # option 2. vgg net = simple_cnn(image) out = paddle.layer.fc( input=net, size=classdim, act=paddle.activation.Softmax()) lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(classdim)) cost = paddle.layer.classification_cost(input=out, label=lbl) # Create parameters parameters = paddle.parameters.create(cost) # Create optimizer momentum_optimizer = paddle.optimizer.Momentum( momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0002 * 128), learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, learning_rate_schedule='discexp') # End batch and end pass event handler def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "\nPass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) else: sys.stdout.write('.') sys.stdout.flush() if isinstance(event, paddle.event.EndPass): # save parameters with open('params_pass_%d.tar' % event.pass_id, 'w') as f: parameters.to_tar(f) result = trainer.test( reader=paddle.batch( paddle.dataset.cifar.test10(), batch_size=128), feeding={'image': 0, 'label': 1}) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) # Create trainer trainer = paddle.trainer.SGD( cost=cost, parameters=parameters, update_equation=momentum_optimizer) # Save the inference topology to protobuf. inference_topology = paddle.topology.Topology(layers=out) with open("inference_topology.pkl", 'wb') as f: inference_topology.serialize_for_inference(f) trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.cifar.train10(), buf_size=50000), batch_size=128), num_passes=200, event_handler=event_handler, feeding={'image': 0, 'label': 1}) # inference from PIL import Image import numpy as np import os def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) im = np.array(im).astype(np.float32) # The storage order of the loaded image is W(widht), # H(height), C(channel). PaddlePaddle requires # the CHW order, so transpose them. im = im.transpose((2, 0, 1)) # CHW # In the training phase, the channel order of CIFAR # image is B(Blue), G(green), R(Red). But PIL open # image in RGB mode. It must swap the channel order. im = im[(2, 1, 0), :, :] # BGR im = im.flatten() im = im / 255.0 return im test_data = [] cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/image/dog.png'), )) # users can remove the comments and change the model name # with open('params_pass_50.tar', 'r') as f: # parameters = paddle.parameters.Parameters.from_tar(f) probs = paddle.infer( output_layer=out, parameters=parameters, input=test_data) lab = np.argsort(-probs) # probs and lab are the results of one batch data print "Label of image/dog.png is: %d" % lab[0][0]
def train(train_file_list_path, test_file_list_path, label_dict_path, model_save_dir): # 获取训练列表 train_file_list = get_file_list(train_file_list_path) # 获取测试列表 test_file_list = get_file_list(test_file_list_path) # 使用训练数据生成标记字典 if not os.path.exists(label_dict_path): print(("Label dictionary is not given, the dictionary " "is automatically built from the training data.")) build_label_dict(train_file_list, label_dict_path) # 获取标签字典 char_dict = load_dict(label_dict_path) # 获取字典大小 dict_size = len(char_dict) # 定义网络拓扑 model = Model(dict_size, IMAGE_SHAPE, is_infer=False) # 初始化PaddlePaddle paddle.init(use_gpu=True, trainer_count=1) # 创建优化方法 optimizer = paddle.optimizer.Momentum(momentum=0) # 创建训练参数 params = paddle.parameters.create(model.cost) # 定义训练器 trainer = paddle.trainer.SGD(cost=model.cost, parameters=params, update_equation=optimizer, extra_layers=model.eval) # 获取reader my_reader = Reader(char_dict=char_dict, image_shape=IMAGE_SHAPE) # 说明数据层之间的关系 feeding = {'image': 0, 'label': 1} # 训练事件 def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print("Pass %d, batch %d, Samples %d, Cost %f, Eval %s" % (event.pass_id, event.batch_id, event.batch_id * BATCH_SIZE, event.cost, event.metrics)) if isinstance(event, paddle.event.EndPass): # 这里由于训练和测试数据共享相同的格式 # 我们仍然使用reader.train_reader来读取测试数据 test_reader = paddle.batch(my_reader.train_reader(test_file_list), batch_size=BATCH_SIZE) result = trainer.test(reader=test_reader, feeding=feeding) print("Test %d, Cost %f, Eval %s" % (event.pass_id, result.cost, result.metrics)) # 检查保存model的路径是否存在,如果不存在就创建 if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) with gzip.open(os.path.join(model_save_dir, "params_pass.tar.gz"), "w") as f: trainer.save_parameter_to_tar(f) # 获取训练数据的reader train_reader = paddle.batch(paddle.reader.shuffle( my_reader.train_reader(train_file_list), buf_size=1000), batch_size=BATCH_SIZE) # 开始训练 trainer.train(reader=train_reader, feeding=feeding, event_handler=event_handler, num_passes=5000)
def train(): paddle.init(use_gpu=False, trainer_count=1) # define network topology feature_out = db_lstm() target = paddle.layer.data(name='target', type=d_type(label_dict_len)) crf_cost = paddle.layer.crf(size=label_dict_len, input=feature_out, label=target, param_attr=paddle.attr.Param( name='crfw', initial_std=default_std, learning_rate=mix_hidden_lr)) crf_dec = paddle.layer.crf_decoding( size=label_dict_len, input=feature_out, label=target, param_attr=paddle.attr.Param(name='crfw')) evaluator.sum(input=crf_dec) # create parameters parameters = paddle.parameters.create(crf_cost) parameters.set('emb', load_parameter(conll05.get_embedding(), 44068, 32)) # create optimizer optimizer = paddle.optimizer.Momentum( momentum=0, learning_rate=2e-2, regularization=paddle.optimizer.L2Regularization(rate=8e-4), model_average=paddle.optimizer.ModelAverage(average_window=0.5, max_average_window=10000), ) trainer = paddle.trainer.SGD(cost=crf_cost, parameters=parameters, update_equation=optimizer, extra_layers=crf_dec) reader = paddle.batch(paddle.reader.shuffle(conll05.test(), buf_size=8192), batch_size=10) feeding = { 'word_data': 0, 'ctx_n2_data': 1, 'ctx_n1_data': 2, 'ctx_0_data': 3, 'ctx_p1_data': 4, 'ctx_p2_data': 5, 'verb_data': 6, 'mark_data': 7, 'target': 8 } def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: logger.info( "Pass %d, Batch %d, Cost %f, %s" % (event.pass_id, event.batch_id, event.cost, event.metrics)) if event.batch_id and event.batch_id % 1000 == 0: result = trainer.test(reader=reader, feeding=feeding) logger.info("\nTest with Pass %d, Batch %d, %s" % (event.pass_id, event.batch_id, result.metrics)) if isinstance(event, paddle.event.EndPass): # save parameters with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: parameters.to_tar(f) result = trainer.test(reader=reader, feeding=feeding) logger.info("\nTest with Pass %d, %s" % (event.pass_id, result.metrics)) trainer.train(reader=reader, event_handler=event_handler, num_passes=10, feeding=feeding)
def cbow_main(cost_config, window_size=5, prefix="./output/cbow_softmax/", cpu_num=3, word_dict_limit=20000, emb_size=32, num_passes=2, batch_size_per_cpu=2000): assert word_dict_limit < MAX_WORDS assert window_size % 2 == 1 paddle.init(use_gpu=False, trainer_count=cpu_num) words = [] word_limit = word_dict_limit + 2 for i in xrange(window_size): words.append(paddle.layer.data(name='word_%d' % i, type=paddle.data_type.integer_value( word_limit))) embs = [] for w in words[:window_size / 2] + words[-window_size / 2 + 1:]: embs.append( paddle.layer.embedding(input=w, size=emb_size, param_attr= paddle.attr.Param(name='emb', sparse_update=True))) with paddle.layer.mixed(size=emb_size) as sum_emb: for emb in embs: sum_emb += paddle.layer.identity_projection(input=emb) label = words[window_size / 2] cost = cost_config(sum_emb, label, word_limit) parameters = paddle.parameters.create(cost) adam_optimizer = paddle.optimizer.RMSProp( learning_rate=1e-3) trainer = paddle.trainer.SGD(cost, parameters, adam_optimizer) counter = [0] total_cost = [0.0] def event_handler(event): if isinstance(event, paddle.event.EndIteration): total_cost[0] += event.cost counter[0] += 1 sys.stdout.write('.') if event.batch_id % 100 == 0: print "Pass %d, Batch %d, AvgCost %f" % ( event.pass_id, event.batch_id, total_cost[0] / counter[0]) if event.batch_id % 1000000 == 0: with gzip.open(os.path.join(prefix, "model_%d_%d.tar.gz" % ( event.pass_id, event.batch_id)), 'w') as f: parameters.to_tar(f) if isinstance(event, paddle.event.EndPass): print "Pass %d" % event.pass_id with gzip.open(os.path.join(prefix, "model_%d.tar.gz" % event.pass_id), 'w') as f: parameters.to_tar(f) trainer.train( paddle.batch( paddle.reader.buffered( reader_creator(window_size=window_size, word_limit=word_dict_limit, path="./preprocessed"), 16 * cpu_num * 4000), batch_size_per_cpu*cpu_num), num_passes=num_passes, event_handler=event_handler, feeding=[w.name for w in words])
def test_demo(): # PaddlePaddle init paddle.init(use_gpu=True, gpu_id=FLAGS.gpu_id) params = sun3d.set_params() inputs = d_net.get_demon_inputs(params) params['stage'] = 2 # Add neural network config outputs, out_field = d_net.get_demon_outputs(inputs, params, ext_inputs=None) parameters = paddle.parameters.create(outputs[out_field]) print('load parameters') with gzip.open(FLAGS.model, 'r') as f: parameters_init = paddle.parameters.Parameters.from_tar(f) for name in parameters.names(): parameters.set(name, parameters_init.get(name)) # Read image pair 1, 2 flow for scene_name in params['train_scene'][1:]: image_list = preprocess_util.list_files(params['flow_path'] + scene_name + '/flow/') image2depth = sun3d.get_image_depth_matching(scene_name) for pair_name in image_list[0:2]: image1, image2, flow_gt, depth1_gt, normal1_gt = \ sun3d.load_image_pair(scene_name, pair_name, image2depth) #transform and yield image1_new = uts.transform(image1.copy(), height=params['size'][0], width=params['size'][1]) image2_new = uts.transform(image2.copy(), height=params['size'][0], width=params['size'][1]) intrinsic = np.array([0.89115971, 1.18821287, 0.5, 0.5]) test_data = [(image1_new, image2_new, intrinsic)] depth_name = 'depth' if params['stage'] < 5 else 'depth_0' out_fields = [ 'flow', depth_name, 'normal', 'rotation', 'translation' ] output_list = [outputs[x] for x in out_fields] flow, depth, normal, rotation, translation = paddle.infer( output_layer=output_list, parameters=parameters, input=test_data, feeding={ 'image1': 0, 'image2': 1, 'intrinsic': 2 }) height_list = [cp.g_layer_map[outputs[x].name].height \ for x in ['flow', 'depth','normal']] width_list = [cp.g_layer_map[outputs[x].name].width \ for x in ['flow', 'depth','normal']] flow, depth, normal = vec2img(inputs=[flow, depth, normal], height=height_list, width=width_list) # visualize depth in 3D image1_down = cv2.resize(image1, (depth.shape[1], depth.shape[0])) visualize_prediction(depth=depth, image=np.uint8( image1_down.transpose([2, 0, 1])), rotation=rotation, translation=translation) uts.plot_images(OrderedDict([ ('image1', image1), ('image2', image2), ('flow', flow), ('flow_gt', flow_gt), ('depth', depth), ('depth_gt', depth1_gt), ('normal', (normal + 1.0) / 2.), ('normal_gt', (normal1_gt + 1.0) / 2) ]), layout=[4, 2])
def main(): paddle.init(use_gpu=False, trainer_count=1) is_generating = False # source and target dict dim. dict_size = 30000 source_dict_dim = target_dict_dim = dict_size # train the network if not is_generating: cost = seqToseq_net(source_dict_dim, target_dict_dim) parameters = paddle.parameters.create(cost) # define optimize method and trainer optimizer = paddle.optimizer.Adam( learning_rate=5e-5, regularization=paddle.optimizer.L2Regularization(rate=8e-4)) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer) # define data reader wmt14_reader = paddle.batch( paddle.reader.shuffle( paddle.dataset.wmt14.train(dict_size), buf_size=8192), batch_size=5) # define event_handler callback def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 10 == 0: print "\nPass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) else: sys.stdout.write('.') sys.stdout.flush() # start to train trainer.train( reader=wmt14_reader, event_handler=event_handler, num_passes=2) # generate a english sequence to french else: # use the first 3 samples for generation gen_creator = paddle.dataset.wmt14.gen(dict_size) gen_data = [] gen_num = 3 for item in gen_creator(): gen_data.append((item[0], )) if len(gen_data) == gen_num: break beam_gen = seqToseq_net(source_dict_dim, target_dict_dim, is_generating) # get the pretrained model, whose bleu = 26.92 parameters = paddle.dataset.wmt14.model() # prob is the prediction probabilities, and id is the prediction word. beam_result = paddle.infer( output_layer=beam_gen, parameters=parameters, input=gen_data, field=['prob', 'id']) # get the dictionary src_dict, trg_dict = paddle.dataset.wmt14.get_dict(dict_size) # the delimited element of generated sequences is -1, # the first element of each generated sequence is the sequence length seq_list = [] seq = [] for w in beam_result[1]: if w != -1: seq.append(w) else: seq_list.append(' '.join([trg_dict.get(w) for w in seq[1:]])) seq = [] prob = beam_result[0] beam_size = 3 for i in xrange(gen_num): print "\n*******************************************************\n" print "src:", ' '.join( [src_dict.get(w) for w in gen_data[i][0]]), "\n" for j in xrange(beam_size): print "prob = %f:" % (prob[i][j]), seq_list[i * beam_size + j]
import paddle.v2 as paddle # Initialize PaddlePaddle. paddle.init(use_gpu=False, trainer_count=1) # Configure the neural network. x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear()) with open('fit_a_line.tar', 'r') as f: parameters = paddle.parameters.Parameters.from_tar(f) # Infer using provided test data. probs = paddle.infer( output_layer=y_predict, parameters=parameters, input=[item for item in paddle.dataset.uci_housing.test()()]) for i in xrange(len(probs)): print 'Predicted price: ${:,.2f}'.format(probs[i][0] * 1000)
beam_size = 500 num_proc_bsearch = 1 num_proc_data = 1 alpha = 2.5 beta = 0.3 cutoff_prob = 1.0 cutoff_top_n = 40 decoding_method = 'ctc_beam_search' error_rate_type = 'wer' num_conv_layers = 2 num_rnn_layers = 3 rnn_layer_size = 2048 share_rnn_weights = True specgram_type = 'linear' paddle.init(use_gpu=USING_GPU, rnn_use_batch=True, trainer_count=trainer_count) data_generator = DataGenerator(vocab_filepath=vocab_path, mean_std_filepath=mean_std_path, augmentation_config='{}', specgram_type=specgram_type, num_threads=num_proc_data, keep_transcription_text=True) batch_reader = data_generator.batch_reader_creator(manifest_path=test_manifest, batch_size=batch_size, min_batch_size=1, sortagrad=False, shuffle_method=None) print('Loading inference model from files {}'.format(model_path)) log_file.write('Loading inference model from files {}'.format(model_path))
def main(): # get arguments from env # for local training TRUTH = ["true", "True", "TRUE", "1", "yes", "Yes", "YES"] cluster_train = os.getenv('PADDLE_CLUSTER_TRAIN', "False") in TRUTH use_gpu = os.getenv('PADDLE_INIT_USE_GPU', "False") if not cluster_train: paddle.init(use_gpu=use_gpu, trainer_count=int( os.getenv("PADDLE_INIT_TRAINER_COUNT", "1"))) else: paddle.init(use_gpu=use_gpu, trainer_count=int( os.getenv("PADDLE_INIT_TRAINER_COUNT", "1")), port=int(os.getenv("PADDLE_INIT_PORT", "7164")), ports_num=int(os.getenv("PADDLE_INIT_PORTS_NUM", "1")), ports_num_for_sparse=int( os.getenv("PADDLE_INIT_PORTS_NUM_FOR_SPARSE", "1")), num_gradient_servers=int( os.getenv("PADDLE_INIT_NUM_GRADIENT_SERVERS", "1")), trainer_id=int(os.getenv("PADDLE_INIT_TRAINER_ID", "0")), pservers=os.getenv("PADDLE_INIT_PSERVERS", "127.0.0.1")) fn = open("thirdparty/wuyi_train_thdpty/word_dict.pickle", "r") word_dict = pickle.load(fn) fn.close() dict_size = len(word_dict) firstword = paddle.layer.data( name="firstw", type=paddle.data_type.integer_value(dict_size)) secondword = paddle.layer.data( name="secondw", type=paddle.data_type.integer_value(dict_size)) thirdword = paddle.layer.data( name="thirdw", type=paddle.data_type.integer_value(dict_size)) fourthword = paddle.layer.data( name="fourthw", type=paddle.data_type.integer_value(dict_size)) nextword = paddle.layer.data( name="fifthw", type=paddle.data_type.integer_value(dict_size)) Efirst = wordemb(firstword) Esecond = wordemb(secondword) Ethird = wordemb(thirdword) Efourth = wordemb(fourthword) contextemb = paddle.layer.concat(input=[Efirst, Esecond, Ethird, Efourth]) hidden1 = paddle.layer.fc(input=contextemb, size=hiddensize, act=paddle.activation.Sigmoid(), layer_attr=paddle.attr.Extra(drop_rate=0.5), bias_attr=paddle.attr.Param(learning_rate=2), param_attr=paddle.attr.Param( initial_std=1. / math.sqrt(embsize * 8), learning_rate=1)) predictword = paddle.layer.fc(input=hidden1, size=dict_size, bias_attr=paddle.attr.Param(learning_rate=2), act=paddle.activation.Softmax()) def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: result = trainer.test( paddle.batch( cluster_reader_cluster(cluster_test_file, node_id), 32)) print "Pass %d, Batch %d, Cost %f, %s, Testing metrics %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics, result.metrics) cost = paddle.layer.classification_cost(input=predictword, label=nextword) parameters = paddle.parameters.create(cost) adagrad = paddle.optimizer.AdaGrad( learning_rate=3e-3, regularization=paddle.optimizer.L2Regularization(8e-4)) trainer = paddle.trainer.SGD(cost, parameters, adagrad, is_local=not cluster_train) trainer.train(paddle.batch( cluster_reader_cluster(cluster_train_file, node_id), 32), num_passes=30, event_handler=event_handler)
def infer(data_path, model_path, word_dict_path, batch_size, label_dict_path): def _infer_a_batch(inferer, test_batch, ids_2_word, ids_2_label): probs = inferer.infer(input=test_batch, field=["value"]) assert len(probs) == len(test_batch) for word_ids, prob in zip(test_batch, probs): sent_ids = [] for sent in word_ids[0]: sent_ids.extend(sent) word_text = " ".join([ids_2_word[id] for id in sent_ids]) print("%s\t%s\t%s" % (ids_2_label[prob.argmax()], " ".join(["{:0.4f}".format(p) for p in prob]), word_text)) assert os.path.exists(model_path), "The trained model does not exist." logger.info("Begin to predict...") use_default_data = (data_path is None) if use_default_data: word_dict = reader.imdb_word_dict() word_reverse_dict = dict((value, key) for key, value in word_dict.iteritems()) # The reversed label dict of the imdb dataset label_reverse_dict = {0: "positive", 1: "negative"} test_reader = reader.imdb_test(word_dict) class_num = 2 else: assert os.path.exists( word_dict_path), "The word dictionary file does not exist" assert os.path.exists( label_dict_path), "The label dictionary file does not exist" word_dict = load_dict(word_dict_path) word_reverse_dict = dict((value, key) for key, value in word_dict.iteritems()) label_reverse_dict = load_reverse_dict(label_dict_path) class_num = len(label_reverse_dict) test_reader = reader.infer_reader(data_path, word_dict)() dict_dim = len(word_dict) # initialize PaddlePaddle. paddle.init(use_gpu=False, trainer_count=1) prob_layer = nested_net(dict_dim, class_num, is_infer=True) # load the trained models. parameters = paddle.parameters.Parameters.from_tar( gzip.open(model_path, "r")) inferer = paddle.inference.Inference( output_layer=prob_layer, parameters=parameters) test_batch = [] for idx, item in enumerate(test_reader): test_batch.append([item[0]]) if len(test_batch) == batch_size: _infer_a_batch(inferer, test_batch, word_reverse_dict, label_reverse_dict) test_batch = [] if len(test_batch): _infer_a_batch(inferer, test_batch, word_reverse_dict, label_reverse_dict) test_batch = []
def main(): paddle.init(use_gpu=False) movie_title_dict = paddle.dataset.movielens.get_movie_title_dict() uid = paddle.layer.data( name='user_id', type=paddle.data_type.integer_value( paddle.dataset.movielens.max_user_id() + 1)) usr_emb = paddle.layer.embedding(input=uid, size=32) usr_gender_id = paddle.layer.data( name='gender_id', type=paddle.data_type.integer_value(2)) usr_gender_emb = paddle.layer.embedding(input=usr_gender_id, size=16) usr_age_id = paddle.layer.data( name='age_id', type=paddle.data_type.integer_value( len(paddle.dataset.movielens.age_table))) usr_age_emb = paddle.layer.embedding(input=usr_age_id, size=16) usr_job_id = paddle.layer.data( name='job_id', type=paddle.data_type.integer_value(paddle.dataset.movielens.max_job_id( ) + 1)) usr_job_emb = paddle.layer.embedding(input=usr_job_id, size=16) usr_combined_features = paddle.layer.fc( input=[usr_emb, usr_gender_emb, usr_age_emb, usr_job_emb], size=200, act=paddle.activation.Tanh()) mov_id = paddle.layer.data( name='movie_id', type=paddle.data_type.integer_value( paddle.dataset.movielens.max_movie_id() + 1)) mov_emb = paddle.layer.embedding(input=mov_id, size=32) mov_categories = paddle.layer.data( name='category_id', type=paddle.data_type.sparse_binary_vector( len(paddle.dataset.movielens.movie_categories()))) mov_categories_hidden = paddle.layer.fc(input=mov_categories, size=32) mov_title_id = paddle.layer.data( name='movie_title', type=paddle.data_type.integer_value_sequence(len(movie_title_dict))) mov_title_emb = paddle.layer.embedding(input=mov_title_id, size=32) mov_title_conv = paddle.networks.sequence_conv_pool( input=mov_title_emb, hidden_size=32, context_len=3) mov_combined_features = paddle.layer.fc( input=[mov_emb, mov_categories_hidden, mov_title_conv], size=200, act=paddle.activation.Tanh()) inference = paddle.layer.cos_sim( a=usr_combined_features, b=mov_combined_features, size=1, scale=5) cost = paddle.layer.mse_cost( input=inference, label=paddle.layer.data( name='score', type=paddle.data_type.dense_vector(1))) parameters = paddle.parameters.create(cost) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=paddle.optimizer.Adam( learning_rate=1e-4)) feeding = { 'user_id': 0, 'gender_id': 1, 'age_id': 2, 'job_id': 3, 'movie_id': 4, 'category_id': 5, 'movie_title': 6, 'score': 7 } def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "Pass %d Batch %d Cost %.2f" % ( event.pass_id, event.batch_id, event.cost) trainer.train( reader=paddle.batch( paddle.reader.shuffle( paddle.dataset.movielens.train(), buf_size=8192), batch_size=256), event_handler=event_handler, feeding=feeding, num_passes=1) user_id = 234 movie_id = 345 user = paddle.dataset.movielens.user_info()[user_id] movie = paddle.dataset.movielens.movie_info()[movie_id] feature = user.value() + movie.value() def reader(): yield feature infer_dict = copy.copy(feeding) del infer_dict['score'] prediction = paddle.infer( output=inference, parameters=parameters, reader=paddle.batch( reader, batch_size=32), feeding=infer_dict) print(prediction + 5) / 2
def train(): """ model train """ # init use_gpu_flag = os.getenv("PADDLE_USE_GPU", "0") print("use_gpu_flag", use_gpu_flag) trainer_id_str = os.getenv("PADDLE_TRAINER_ID", "0") paddle.init( use_gpu=use_gpu_flag, trainer_count=int(os.getenv("PADDLE_TRAINER_COUNT", "1")), port=int(os.getenv("PADDLE_PORT", "7164")), ports_num=int(os.getenv("PADDLE_PORTS_NUM", "1")), ports_num_for_sparse=int(os.getenv("PADDLE_PORTS_NUM_FOR_SPARSE", "1")), num_gradient_servers=int(os.getenv("PADDLE_NUM_GRADIENT_SERVERS", "1")), trainer_id=int(trainer_id_str), pservers=os.getenv("PADDLE_PSERVERS", "127.0.0.1")) # network config x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear()) y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.square_error_cost(input=y_predict, label=y) # Save the inference topology to protobuf. inference_topology = paddle.topology.Topology(layers=y_predict) with open("./output/inference_topology.pkl", 'wb') as f: inference_topology.serialize_for_inference(f) # create parameters parameters = paddle.parameters.create(cost) # create optimizer optimizer = paddle.optimizer.Momentum(momentum=0) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer, is_local=False) feeding = {'x': 0, 'y': 1} #create reader train_reader = paddle.batch(paddle.reader.shuffle( cluster_data_reader(cluster_train_dir), buf_size=500), batch_size=2) # event_handler to print training and testing info def event_handler(event): """ handle paddle event """ if isinstance(event, paddle.event.EndIteration): if event.batch_id % 100 == 0: print "Pass %d, Batch %d, Cost %f" % ( event.pass_id, event.batch_id, event.cost) if isinstance(event, paddle.event.EndPass): if event.pass_id % 10 == 0: filename = './output/model/params_pass_%d_%s.tar' % ( event.pass_id, trainer_id_str) with open(filename, "w") as f: trainer.save_parameter_to_tar(f) test_reader = paddle.batch(paddle.reader.shuffle( cluster_data_reader(cluster_test_dir), buf_size=500), batch_size=2) result = trainer.test(reader=test_reader, feeding=feeding) print "Test %d, Cost %f" % (event.pass_id, result.cost) # training trainer.train(reader=train_reader, feeding=feeding, event_handler=event_handler, num_passes=30)
#encoding=utf8 import paddle.v2 as paddle import paddle.v2.dataset.uci_housing as uci_housing # 模型初始化 paddle.init(use_gpu=False, trainer_count=1) # 数据 x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear()) y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) # cost = paddle.layer.mse_cost(input=y_predict, label=y) cost = paddle.layer.square_error_cost(input=y_predict, label=y) # 参数,优化器,训练器 parameters = paddle.parameters.create(cost) optimizer = paddle.optimizer.Momentum(momentum=0) trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer)
def main(): # parse the argument parser = argparse.ArgumentParser() parser.add_argument( 'model', help='The model for image classification', choices=['alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet']) args = parser.parse_args() # PaddlePaddle init paddle.init(use_gpu=True, trainer_count=7) image = paddle.layer.data( name="image", type=paddle.data_type.dense_vector(DATA_DIM)) lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(CLASS_DIM)) extra_layers = None learning_rate = 0.01 if args.model == 'alexnet': out = alexnet.alexnet(image, class_dim=CLASS_DIM) elif args.model == 'vgg13': out = vgg.vgg13(image, class_dim=CLASS_DIM) elif args.model == 'vgg16': out = vgg.vgg16(image, class_dim=CLASS_DIM) elif args.model == 'vgg19': out = vgg.vgg19(image, class_dim=CLASS_DIM) elif args.model == 'resnet': out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM) learning_rate = 0.1 elif args.model == 'googlenet': out, out1, out2 = googlenet.googlenet(image, class_dim=CLASS_DIM) loss1 = paddle.layer.cross_entropy_cost( input=out1, label=lbl, coeff=0.3) paddle.evaluator.classification_error(input=out1, label=lbl) loss2 = paddle.layer.cross_entropy_cost( input=out2, label=lbl, coeff=0.3) paddle.evaluator.classification_error(input=out2, label=lbl) extra_layers = [loss1, loss2] cost = paddle.layer.classification_cost(input=out, label=lbl) # Create parameters parameters = paddle.parameters.create(cost) # Create optimizer optimizer = paddle.optimizer.Momentum( momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * BATCH_SIZE), learning_rate=learning_rate / BATCH_SIZE, learning_rate_decay_a=0.1, learning_rate_decay_b=128000 * 35, learning_rate_schedule="discexp", ) train_reader = paddle.batch( paddle.reader.shuffle( flowers.train(), # To use other data, replace the above line with: # reader.train_reader('train.list'), buf_size=1000), batch_size=BATCH_SIZE) test_reader = paddle.batch( flowers.valid(), # To use other data, replace the above line with: # reader.test_reader('val.list'), batch_size=BATCH_SIZE) # Create trainer trainer = paddle.trainer.SGD( cost=cost, parameters=parameters, update_equation=optimizer, extra_layers=extra_layers) # End batch and end pass event handler def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 1 == 0: print "\nPass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: trainer.save_parameter_to_tar(f) result = trainer.test(reader=test_reader) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) trainer.train( reader=train_reader, num_passes=200, event_handler=event_handler)
lstm_last = paddle.layer.pooling( input=inputs[1], pooling_type=paddle.pooling.Max()) output = paddle.layer.fc(input=[fc_last, lstm_last], size=class_dim, act=paddle.activation.Softmax(), bias_attr=bias_attr, param_attr=para_attr) lbl = paddle.layer.data("label", paddle.data_type.integer_value(2)) cost = paddle.layer.classification_cost(input=output, label=lbl) return cost if __name__ == '__main__': # init paddle.init(use_gpu=False) #data print 'load dictionary...' word_dict = paddle.dataset.imdb.word_dict() dict_dim = len(word_dict) class_dim = 2 train_reader = paddle.batch( paddle.reader.shuffle( lambda: paddle.dataset.imdb.train(word_dict), buf_size=1000), batch_size=100) test_reader = paddle.batch( lambda: paddle.dataset.imdb.test(word_dict), batch_size=100) feeding = {'word': 0, 'label': 1}
def main(): # parse the argument parser = argparse.ArgumentParser() parser.add_argument( 'model', help='The model for image classification', choices=['alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet']) args = parser.parse_args() # PaddlePaddle init paddle.init(use_gpu=True, trainer_count=7) image = paddle.layer.data(name="image", type=paddle.data_type.dense_vector(DATA_DIM)) lbl = paddle.layer.data(name="label", type=paddle.data_type.integer_value(CLASS_DIM)) extra_layers = None learning_rate = 0.01 if args.model == 'alexnet': out = alexnet.alexnet(image, class_dim=CLASS_DIM) elif args.model == 'vgg13': out = vgg.vgg13(image, class_dim=CLASS_DIM) elif args.model == 'vgg16': out = vgg.vgg16(image, class_dim=CLASS_DIM) elif args.model == 'vgg19': out = vgg.vgg19(image, class_dim=CLASS_DIM) elif args.model == 'resnet': out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM) learning_rate = 0.1 elif args.model == 'googlenet': out, out1, out2 = googlenet.googlenet(image, class_dim=CLASS_DIM) loss1 = paddle.layer.cross_entropy_cost(input=out1, label=lbl, coeff=0.3) paddle.evaluator.classification_error(input=out1, label=lbl) loss2 = paddle.layer.cross_entropy_cost(input=out2, label=lbl, coeff=0.3) paddle.evaluator.classification_error(input=out2, label=lbl) extra_layers = [loss1, loss2] cost = paddle.layer.classification_cost(input=out, label=lbl) # Create parameters parameters = paddle.parameters.create(cost) # Create optimizer optimizer = paddle.optimizer.Momentum( momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * BATCH_SIZE), learning_rate=learning_rate / BATCH_SIZE, learning_rate_decay_a=0.1, learning_rate_decay_b=128000 * 35, learning_rate_schedule="discexp", ) train_reader = paddle.batch( paddle.reader.shuffle( flowers.train(), # To use other data, replace the above line with: # reader.train_reader('train.list'), buf_size=1000), batch_size=BATCH_SIZE) test_reader = paddle.batch( flowers.valid(), # To use other data, replace the above line with: # reader.test_reader('val.list'), batch_size=BATCH_SIZE) # Create trainer trainer = paddle.trainer.SGD(cost=cost, parameters=parameters, update_equation=optimizer, extra_layers=extra_layers) # End batch and end pass event handler def event_handler(event): if isinstance(event, paddle.event.EndIteration): if event.batch_id % 1 == 0: print "\nPass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: trainer.save_parameter_to_tar(f) result = trainer.test(reader=test_reader) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) trainer.train(reader=train_reader, num_passes=200, event_handler=event_handler)