def ner_net_train(data_reader=train_data_reader, num_passes=5): # define network topology crf_cost, crf_dec, target = ner_net(is_train=True) 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) print "total word vocab in word vector: ", len(word_vector_values) 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) reader = paddle.batch(paddle.reader.shuffle(data_reader, buf_size=8192), batch_size=64) feeding = {'word': 0, 'mark': 1, 'target': 2} 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 event.batch_id % 1000 == 0: result = trainer.test(reader=reader, feeding=feeding) print "\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) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) trainer.train(reader=reader, event_handler=event_handler, num_passes=num_passes, feeding=feeding) return parameters
def main(train_data_file, test_data_file, vocab_file, target_file, emb_file, model_save_dir, num_passes=10, batch_size=32): 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 % 1 == 0: logger.info("Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics)) if event.batch_id % 1 == 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: parameters.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() # 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(cloud_reader( ["/pfs/dlnel/public/dataset/conll05/conl105_train-*"], etcd_endpoint), 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: print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if event.batch_id % 1000 == 0: result = trainer.test(reader=reader, feeding=feeding) print "\nTest with Pass %d, Batch %d, %s" % ( event.pass_id, event.batch_id, result.metrics) 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(conll05.test(), 10), feeding=feeding) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) trainer.train(reader=reader, event_handler=event_handler, num_passes=1, feeding=feeding) test_creator = paddle.dataset.conll05.test() test_data = [] for item in test_creator(): test_data.append(item[0:8]) if len(test_data) == 1: break predict = paddle.layer.crf_decoding( size=label_dict_len, input=feature_out, param_attr=paddle.attr.Param(name='crfw')) probs = paddle.infer(output_layer=predict, parameters=parameters, input=test_data, feeding=feeding, field='id') assert len(probs) == len(test_data[0][0]) labels_reverse = {} for (k, v) in label_dict.items(): labels_reverse[v] = k pre_lab = [labels_reverse[i] for i in probs] print pre_lab
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)