コード例 #1
0
def load_model(path_to_model,path_to_tables):
    """
    Load the model with saved tables
    """
    # Load model options
    print ('Loading model parameters...')
    with open('%s.pkl'%path_to_model, 'rb') as f:
        options = pkl.load(f)
    
    # Load parameters
    params = init_params(options)
    params = load_params(path_to_model, params)
    tparams = init_tparams(params)

    # Extractor functions
    print ('Compiling encoders...')
    embedding, x_mask, ctxw2v = build_encoder(tparams, options)
    f_w2v = theano.function([embedding, x_mask], ctxw2v, name='f_w2v')

    # Tables
    print ('Loading tables...')
    table = load_tables() 

    # Store everything we need in a dictionary
    print ('Packing up...')
    model = {}
    model['options'] = options
    model['table'] = table
    model['f_w2v'] = f_w2v

    return model
コード例 #2
0
def load_model(embed_map=None):
    """
    Load all model components + apply vocab expansion
    """
    # Load the worddict
    print('Loading dictionary...')
    with open(config.paths['dictionary'], 'rb') as f:
        worddict = pkl.load(f)

    # Create inverted dictionary
    print('Creating inverted dictionary...')
    word_idict = dict()
    for kk, vv in worddict.items():
        word_idict[vv] = kk
    word_idict[0] = '<eos>'
    word_idict[1] = 'UNK'

    # Load model options
    print('Loading model options...')
    with open('%s.pkl' % config.paths['skmodels'], 'rb') as f:
        options = pkl.load(f)

    # Load parameters
    print('Loading model parameters...')
    params = init_params(options)
    params = load_params(config.paths['skmodels'], params)
    tparams = init_tparams(params)

    # Extractor functions
    print('Compiling encoder...')
    trng = RandomStreams(1234)
    trng, x, x_mask, ctx, emb = build_encoder(tparams, options)
    f_enc = theano.function([x, x_mask], ctx, name='f_enc')
    f_emb = theano.function([x], emb, name='f_emb')
    trng, embedding, x_mask, ctxw2v = build_encoder_w2v(tparams, options)
    f_w2v = theano.function([embedding, x_mask], ctxw2v, name='f_w2v')

    # Load word2vec, if applicable
    if embed_map == None:
        print('Loading word2vec embeddings...')
        embed_map = load_googlenews_vectors(config.paths['v_expansion'])

    # Lookup table using vocab expansion trick
    print('Creating word lookup tables...')
    table = lookup_table(options, embed_map, worddict, word_idict, f_emb)

    # Store everything we need in a dictionary
    print('Packing up...')
    model = {}
    model['options'] = options
    model['table'] = table
    model['f_w2v'] = f_w2v

    return model
コード例 #3
0
def load_model():
    """
    Load a trained model for decoding
    """
    # Load the worddict
    print('Loading dictionary...')
    with open(config.paths['dictionary'], 'rb') as f:
        worddict = pkl.load(f)

    # Create inverted dictionary
    print('Creating inverted dictionary...')
    word_idict = dict()
    for kk, vv in worddict.items():
        word_idict[vv] = kk
    word_idict[0] = '<eos>'
    word_idict[1] = 'UNK'

    # Load model options
    print('Loading model options...')
    with open('%s.pkl' % config.paths['skvmodels'], 'rb') as f:
        options = pkl.load(f)

    # Load parameters
    print('Loading model parameters...')
    params = init_params(options)
    params = load_params(config.paths['skvmodels'], params)
    tparams = init_tparams(params)

    # Sampler.
    trng = RandomStreams(1234)
    f_init, f_next = build_sampler(tparams, options, trng)

    # Pack everything up
    dec = dict()
    dec['options'] = options
    dec['trng'] = trng
    dec['worddict'] = worddict
    dec['word_idict'] = word_idict
    dec['tparams'] = tparams
    dec['f_init'] = f_init
    dec['f_next'] = f_next
    return dec
コード例 #4
0
ファイル: decoder.py プロジェクト: zzcandor/ailab
def load_model(path_to_model, path_to_dictionary):
    """
    Load a trained model for decoding
    """
    # Load the worddict
    with open(path_to_dictionary, 'rb') as f:
        worddict = pkl.load(f)

    # Create inverted dictionary
    word_idict = dict()
    for kk, vv in worddict.items():
        word_idict[vv] = kk
    word_idict[0] = '<eos>'
    word_idict[1] = 'UNK'

    # Load model options
    with open('%s.pkl'%path_to_model, 'rb') as f:
        options = pkl.load(f)
    if 'doutput' not in options.keys():
        options['doutput'] = True

    # Load parameters
    params = init_params(options)
    params = load_params(path_to_model, params)
    tparams = init_tparams(params)

    # Sampler.
    trng = RandomStreams(1234)
    f_init, f_next = build_sampler(tparams, options, trng)

    # Pack everything up
    dec = dict()
    dec['options'] = options
    dec['trng'] = trng
    dec['worddict'] = worddict
    dec['word_idict'] = word_idict
    dec['tparams'] = tparams
    dec['f_init'] = f_init
    dec['f_next'] = f_next
    return dec
コード例 #5
0
def load_model(path_to_model):
    """
    Load all model components
    """
    # Load the worddict
    with open('%s.dictionary.pkl' % path_to_model, 'rb') as f:
        worddict = pkl.load(f)

    # Create inverted dictionary
    word_idict = dict()
    for kk, vv in worddict.items():
        word_idict[vv] = kk
    word_idict[0] = '<eos>'
    word_idict[1] = 'UNK'

    # Load model options
    with open('%s.pkl' % path_to_model, 'rb') as f:
        options = pkl.load(f)

    # Load parameters
    params = init_params(options)
    params = load_params(path_to_model, params)
    tparams = init_tparams(params)

    # Extractor functions
    trng = RandomStreams(1234)
    trng, [x, x_mask], sentences = build_sentence_encoder(tparams, options)
    f_senc = theano.function([x, x_mask], sentences, name='f_senc')

    trng, [im], images = build_image_encoder(tparams, options)
    f_ienc = theano.function([im], images, name='f_ienc')

    # Store everything we need in a dictionary
    model = {}
    model['options'] = options
    model['worddict'] = worddict
    model['word_idict'] = word_idict
    model['f_senc'] = f_senc
    model['f_ienc'] = f_ienc
    return model