Example #1
0
import cPickle as pickle
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

from trans_model import Translate
# Get the arguments

if __name__ == "__main__":
    config = getattr(configurations, 'get_config_cs2en')()

    source = T.lmatrix('source')
    target = T.lmatrix('target')
    source_mask = T.matrix('source_mask')
    target_mask = T.matrix('target_mask')

    trans = Translate(**config)
    trans.apply(source.T, source_mask.T, target.T, target_mask.T)
    cost = trans.cost

    #trans.load(config['saveto']+'/params.npz')
    params = trans.params

    for value in params:
        logger.info('    {:15}: {}'.format(value.get_value().shape, value.name))

    grade = T.grad(cost, params)

    # add step clipping
    if config['step_clipping'] > 0.:
        grad = step_clipping(params, grade, config['step_clipping'])
    ltopk_trg_vocab_idx = []
    if config['use_mv']:
        # no need to use the whole vocabulary
        v_part = T.vector('batch_target_vocab', dtype='int64')
        v_true = T.matrix('v_true', dtype='int64')

        from manvocab import topk_target_vcab_list
        ltopk_trg_vocab_idx = topk_target_vcab_list(**config)
        log('{}'.format(ltopk_trg_vocab_idx))
        log('{}'.format([tv_i2w[i] for i in ltopk_trg_vocab_idx]))

    one_model = config['one_model']
    log('Build lookup table, bi-directional encoder and decoder ... ', nl=False)

    trans = Translate(**config)
    # transpose all the input matrix into shape (sent_len * batch_size)
    if config['use_mv']:
        trans.apply(source.T, source_mask.T, target.T,
                    target_mask.T, v_part, v_true)
    else:
        trans.apply(source.T, source_mask.T, target.T, target_mask.T)
    log('Done\n')

    if config['reload']:
        log('Reload model {}'.format(config['one_model']))
        trans.load(one_model)

    # actually the average cross entropy (cost) per sentence in a batch
    cost = trans.mean_cost
    log_norm = trans.mean_abs_log_norm
Example #3
0
import logging
import configurations
from sample import multi_process_sample, gen_sample
import cPickle as pickle

logger = logging.getLogger(__name__)

from trans_model import Translate

# Get the arguments
import sys

if __name__ == "__main__":
    config = getattr(configurations, "get_config_cs2en")()

    trans = Translate(**config)
    params = trans.params
    print params[0].get_value().sum()

    logger.info("begin to build sample model : f_init, f_next")
    f_init, f_next = trans.build_sample()
    logger.info("end build sample model : f_init, f_next")

    src_vocab = pickle.load(open(config["src_vocab"]))
    trg_vocab = pickle.load(open(config["trg_vocab"]))
    src_vocab = ensure_special_tokens(
        src_vocab, bos_idx=0, eos_idx=config["src_vocab_size"] - 1, unk_idx=config["unk_id"]
    )
    trg_vocab = ensure_special_tokens(
        trg_vocab, bos_idx=0, eos_idx=config["src_vocab_size"] - 1, unk_idx=config["unk_id"]
    )
Example #4
0
               args.watch_adist, args.merge_way, args.ifapprox_dist, args.ifapprox_att,
               args.add_lmscore, args.ifsplit]
    valid_set = args.valid_set
    kl = args.m_threshold
    nprocess = args.n_process
    lmpath = args.lm_path if args.lm_path is not None else None
    ngram = args.ngram

    alpha = args.length_norm
    beta = args.cover_penalty

    dec_conf(switchs, beam_size, search_mode, kl, nprocess, lmpath, ngram, alpha, beta, valid_set)

    config = getattr(configurations, 'get_config_cs2en')()
    _log('init decoder ... ')
    trans = Translate(**config)
    _log('done')

    _log('build decode funcs: f_init f_nh f_na f_ns f_mo f_ws f_ps f_ce f_next f_emb ... ', nl=False)
    fs = trans.build_sample()
    _log('done')

    y_im1 = [2]
    npy = np.asarray(y_im1)
    if npy.ndim == 1:
        x = npy[None, :]
    # context = np.random.sample((7, 1, 2048)).astype(np.float32)
    # s_im1 = np.random.sample((1, 1024)).astype(np.float32)
    debug('............. time testing ..............................')
    s = time.time()
    s_im1, ctx, c_x = fs[0](x)