Beispiel #1
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gurobipy as gp
import codecs

from numpy import random
from fei.model.feat_vec import FeatureVector
from fei.model.feat_extr import FeatureExtractor
from fei.model.utils import getLogger

logger = getLogger()


class Decoder(object):
    """
    Implement of decoder for structured prediction
    """
    def __init__(self):
        self.gilp = GurobiILP()
        self.weights = FeatureVector()
        self.feat_extr = FeatureExtractor()
        return

    def decode(self,
               instance,
               oracle_len='nolen',
               node_cost=None,
               edge_cost=None):
        """
        an instance includes:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import sys
from fei.model.utils import getLogger

LOG_FILE = 'semantic_summ.log'
if len(sys.argv) > 4: LOG_FILE = 'log_%s_%s_passes_len_%s_exp_%s' % (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
logger = getLogger(log_file=LOG_FILE)

from fei.model.corpus import buildCorpus
from fei.model.decoder import Decoder
from fei.model.learning import ParamEstimator

def train(body_file, summ_file, param_file, loss_func, num_passes, oracle_len, w_exp):
    """
    run summarizer, learn structured prediction parameters
    """    
    logger.debug('start training...')
    logger.debug('[settings]: %s_%d_passes_len_%s_exp_%d' % (loss_func, num_passes, oracle_len, w_exp))
    corpus = buildCorpus(body_file, summ_file, w_exp)
    
    # learn parameters
    decoder = Decoder()
    estimator = ParamEstimator()
    final_weights = estimator.learnParamsAdaGrad(decoder, corpus, param_file, loss_func, num_passes, oracle_len)
    
    # output parameters to file
    with codecs.open(param_file, 'w', 'utf-8') as outfile:
        outfile.write('#num_passes#: %d\n' % num_passes)
Beispiel #3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-



import re

from collections import namedtuple
from collections import Counter
from fei.model.utils import getLogger

logger = getLogger()


# information obtained from file
NodeSource = namedtuple('NodeSource', 'graph_idx, start_idx, end_idx, word_str, filename, line_num, sentence')
EdgeSource = namedtuple('EdgeSource', 'relation, filename, line_num, sentence')

class AmrNode(object):
    def __init__(self, graph_idx=None, short_hnd=None, concept=None):
        self.graph_idx = graph_idx
        self.short_hnd = short_hnd
        self.concept = concept
        self.sources = [] # list of NodeSource
        
    def __repr__(self):
        return '%s %s %s' % (self.graph_idx, self.short_hnd, self.concept)
    
    def toString(self):
        """
        convert AmrNode to string, include full node information
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import sys
from fei.model.utils import getLogger

LOG_FILE = 'semantic_summ.log'
if len(sys.argv) > 4: LOG_FILE = 'log_%s_%s_passes_len_%s_exp_%s' % (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
logger = getLogger(log_file=LOG_FILE)

from fei.model.corpus import buildCorpus
from fei.model.decoder import Decoder
from fei.model.learning import ParamEstimator

def train(body_file, summ_file, param_file, loss_func, num_passes, oracle_len, w_exp):
    """
    run summarizer, learn structured prediction parameters
    """    
    logger.debug('start training...')
    logger.debug('[settings]: %s_%d_passes_len_%s_exp_%d' % (loss_func, num_passes, oracle_len, w_exp))
    corpus = buildCorpus(body_file, summ_file, w_exp)
    
    # learn parameters
    decoder = Decoder()
    estimator = ParamEstimator()
    final_weights = estimator.learnParamsAdaGrad(decoder, corpus, param_file, loss_func, num_passes, oracle_len)
    
    # output parameters to file
    with codecs.open(param_file, 'w', 'utf-8') as outfile:
        outfile.write('#num_passes#: %d\n' % num_passes)