Exemplo n.º 1
0
def find_sent_embedding(n_words=21102, img_w=300, img_h=48, feature_maps=200, 
    filter_hs=[3,4,5],n_x=300, n_h=600):

    options = {}
    options['n_words'] = n_words
    options['img_w'] = img_w
    options['img_h'] = img_h
    options['feature_maps'] = feature_maps
    options['filter_hs'] = filter_hs
    options['n_x'] = n_x
    options['n_h'] = n_h
    
    filter_w = img_w
    filter_shapes = []
    pool_sizes = []
    for filter_h in filter_hs:
        filter_shapes.append((feature_maps, 1, filter_h, filter_w))
        pool_sizes.append((img_h-filter_h+1, img_w-filter_w+1))
        
    options['filter_shapes'] = filter_shapes
    options['pool_sizes'] = pool_sizes
    
    params = init_params(options)
    tparams = init_tparams(params)
    
    data = np.load('./bookcorpus_result.npz')  
    
    for kk, pp in params.iteritems():
        params[kk] = data[kk]
    
    for kk, pp in params.iteritems():
        tparams[kk].set_value(params[kk])

    x = tensor.matrix('x', dtype='int32')
    
    layer0_input = tparams['Wemb'][tensor.cast(x.flatten(),dtype='int32')].reshape((x.shape[0],1,x.shape[1],tparams['Wemb'].shape[1])) 
 
    layer1_inputs = []
    for i in xrange(len(options['filter_hs'])):
        filter_shape = options['filter_shapes'][i]
        pool_size = options['pool_sizes'][i]
        conv_layer = encoder(tparams, layer0_input,filter_shape, pool_size,prefix=_p('cnn_encoder',i))                          
        layer1_input = conv_layer
        layer1_inputs.append(layer1_input)
    layer1_input = tensor.concatenate(layer1_inputs,1)
                                 
    f_embed = theano.function([x], layer1_input, name='f_embed')
    
    return f_embed, params
Exemplo n.º 2
0
def find_sent_embedding(whole,
                        n_words=21102,
                        img_w=300,
                        img_h=48,
                        feature_maps=200,
                        filter_hs=[3, 4, 5],
                        n_x=300,
                        n_h=600):

    options = {}
    options['n_words'] = n_words
    options['img_w'] = img_w
    options['img_h'] = img_h
    options['feature_maps'] = feature_maps
    options['filter_hs'] = filter_hs
    options['n_x'] = n_x
    options['n_h'] = n_h

    filter_w = img_w
    filter_shapes = []
    pool_sizes = []
    for filter_h in filter_hs:
        filter_shapes.append((feature_maps, 1, filter_h, filter_w))
        pool_sizes.append((img_h - filter_h + 1, img_w - filter_w + 1))

    options['filter_shapes'] = filter_shapes
    options['pool_sizes'] = pool_sizes

    params = init_params(options)
    tparams = init_tparams(params)

    data = np.load('./bookcorpus_result.npz')

    for kk, pp in params.iteritems():
        params[kk] = data[kk]

    for kk, pp in params.iteritems():
        tparams[kk].set_value(params[kk])

    x = tensor.matrix('x', dtype='int32')

    layer0_input = tparams['Wemb'][tensor.cast(x.flatten(),
                                               dtype='int32')].reshape(
                                                   (x.shape[0], 1, x.shape[1],
                                                    tparams['Wemb'].shape[1]))

    layer1_inputs = []
    for i in xrange(len(options['filter_hs'])):
        filter_shape = options['filter_shapes'][i]
        pool_size = options['pool_sizes'][i]
        conv_layer = encoder(tparams,
                             layer0_input,
                             filter_shape,
                             pool_size,
                             prefix=_p('cnn_encoder', i))
        layer1_input = conv_layer
        layer1_inputs.append(layer1_input)
    layer1_input = tensor.concatenate(layer1_inputs, 1)

    f_embed = theano.function([x], layer1_input, name='f_embed')

    kf = get_minibatches_idx(len(whole), 100)
    sent_emb = np.zeros((len(whole), 600))

    for i, train_index in kf:
        sents = [whole[t] for t in train_index]
        x = prepare_data_for_cnn(sents)
        sent_emb[train_index[0]:train_index[-1] + 1] = f_embed(x)
        if i % 500 == 0:
            print i,

    np.savez('./bookcorpus_embedding.npz', sent_emb=sent_emb)

    return sent_emb
Exemplo n.º 3
0
def find_sent_embedding(testset,
                        params_file_path,
                        n_words=22153,
                        period=887,
                        img_w=300,
                        img_h=148,
                        feature_maps=300,
                        filter_hs=[3, 4, 5],
                        n_x=300,
                        n_h=500,
                        n_h2=200,
                        p_lambda_q=0,
                        p_lambda_fm=1,
                        n_codes=2,
                        max_epochs=16,
                        lr_d=0.0001,
                        lr_g=0.00006,
                        kde_sigma=1.,
                        batch_size=64,
                        valid_batch_size=64,
                        dim_mmd=32,
                        dispFreq=10,
                        encodeDisFreq=1,
                        Large=1e3,
                        validFreq=5000,
                        saveFreq=1000):
    data = np.load(params_file_path)

    # n_h2 = data['dis_decoder_W1'].shape[0]
    options = {}

    options['n_words'] = n_words
    options['img_w'] = img_w
    options['img_h'] = img_h
    options['feature_maps'] = feature_maps
    options['filter_hs'] = filter_hs  #band width
    options['n_x'] = n_x
    options['n_h'] = n_h
    options['n_h2'] = n_h2
    options['max_epochs'] = max_epochs
    options['batch_size'] = batch_size
    options['valid_batch_size'] = valid_batch_size
    options['dispFreq'] = dispFreq
    options['validFreq'] = validFreq
    options['saveFreq'] = saveFreq
    options['encodeDisFreq'] = encodeDisFreq
    options['cnn_activation'] = 'tanh'

    # options['label_sizes'] = n_label

    # options['input_shape'] = (n_h2, n_dis*len(options['filter_hs']))
    # options['pred_shape'] = (n_label, n_h2)

    # options['dis_encoder_input_shape'] = (n_h2, options['feature_maps']*len(options['filter_hs'])+n_label)
    #  options['dis_encoder_pred_shape'] = (n_dis*len(options['filter_hs']), n_h2)

    # options['gan_input_shape'] = (n_h2, n_gan)
    # options['gan_pred_shape'] = (n_dis*len(options['filter_hs']), n_h2)
    # options['pred_d_shape'] = (2, n_h2)

    filter_w = img_w
    filter_shapes = []
    pool_sizes = []
    for filter_h in filter_hs:
        filter_shapes.append((feature_maps, 1, filter_h, filter_w))
        pool_sizes.append((img_h - filter_h + 1, img_w - filter_w + 1))

    options['filter_shapes'] = filter_shapes
    options['pool_sizes'] = pool_sizes

    # params = init_params(options)
    tparams = OrderedDict()

    # tparams = init_tparams(data)

    for kk, pp in data.iteritems():
        tparams[kk] = theano.shared(data[kk], name=kk)

    # for kk, pp in params.iteritems():
    #     params[kk] = data[kk]
    #
    # for kk, pp in params.iteritems():
    #     tparams[kk].set_value(params[kk])

    x = tensor.matrix('x', dtype='int32')

    layer0_input = tparams['Wemb'][tensor.cast(x.flatten(),
                                               dtype='int32')].reshape(
                                                   (x.shape[0], 1, x.shape[1],
                                                    tparams['Wemb'].shape[1]))

    layer1_inputs = []
    for i in xrange(len(options['filter_hs'])):
        filter_shape = options['filter_shapes'][i]
        pool_size = options['pool_sizes'][i]
        conv_layer = encoder(tparams,
                             layer0_input,
                             filter_shape,
                             pool_size,
                             options,
                             prefix=_p('cnn_d', i))
        layer1_input = conv_layer
        layer1_inputs.append(layer1_input)
    layer1_input = tensor.concatenate(layer1_inputs, 1)

    f_embed = theano.function([x], [layer1_input], name='f_embed')

    #print testset
    #x = prepare_data_for_cnn(testset)

    [sent_emb] = f_embed(testset)

    #print str(testset.shape) + " " + str(x.shape) + " " + str(sent_emb.shape) + " " + str(sent_emb_dis.shape)
    return sent_emb