Beispiel #1
0
def param_init_encoder(options, params, prefix='lstm_encoder'):

    n_x = options['n_x']
    n_h = options['n_h']

    W = np.concatenate([
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h)
    ],
                       axis=1)
    params[_p(prefix, 'W')] = W

    U = np.concatenate([
        ortho_weight(n_h),
        ortho_weight(n_h),
        ortho_weight(n_h),
        ortho_weight(n_h)
    ],
                       axis=1)
    params[_p(prefix, 'U')] = U

    params[_p(prefix, 'b')] = zero_bias(4 * n_h)

    # It is observed that setting a high initial forget gate bias for LSTMs can
    # give slighly better results (Le et al., 2015). Hence, the initial forget
    # gate bias is set to 3.
    params[_p(prefix, 'b')][n_h:2 * n_h] = 3 * np.ones(
        (n_h, )).astype(theano.config.floatX)

    return params
Beispiel #2
0
def init_params(options, prefix):
    temp = prefix
    params = OrderedDict()
    selected = np.random.permutation(options['C0'])
    params['r'] = options['Lx'][selected[0:options['C']], :]
    params['gp_beta'] = np.float32(1.)
    params['gp_alpha'] = np.float32(2.)
    if prefix == 'e' or temp == 'all':
        prefix = 'e'
        params[_p(prefix, 'b')] = uniform_weight(options[_p(prefix, 'K')],
                                                 options['C'])
        params[_p(prefix, 'phi')] = uniform_weight(options[_p(prefix, 'K')],
                                                   options['C'] - 1)
        params[_p(prefix, 'beta')] = uniform_weight(options[_p(prefix, 'K')],
                                                    1, 0, 0.05)
        params[_p(prefix, 'omega')] = uniform_weight(options[_p(prefix, 'K')],
                                                     1, 0, 1.)
        params[_p(prefix, 'bias')] = np.zeros((options[_p(prefix, 'K')]),
                                              dtype=theano.config.floatX)

    if prefix == 'cl' or temp == 'all':
        prefix = 'cl'
        params[_p(prefix, 'Wy')] = uniform_weight(options[_p(prefix, 'Wy')],
                                                  options[_p(prefix, 'ny')])
        params[_p(prefix, 'by')] = np.zeros((options[_p(prefix, 'ny')]),
                                            dtype=theano.config.floatX)
    return params
Beispiel #3
0
def param_init_decoder(options, params, prefix='decoder_gru'):

    n_x = options['n_x']
    n_h = options['n_h']

    W = np.concatenate([uniform_weight(n_x, n_h),
                        uniform_weight(n_x, n_h)],
                       axis=1)
    params[_p(prefix, 'W')] = W

    U = np.concatenate([ortho_weight(n_h), ortho_weight(n_h)], axis=1)
    params[_p(prefix, 'U')] = U

    params[_p(prefix, 'b')] = zero_bias(2 * n_h)

    Wx = uniform_weight(n_x, n_h)
    params[_p(prefix, 'Wx')] = Wx

    Ux = ortho_weight(n_h)
    params[_p(prefix, 'Ux')] = Ux

    params[_p(prefix, 'bx')] = zero_bias(n_h)

    params[_p(prefix, 'b0')] = zero_bias(n_h)

    return params
Beispiel #4
0
def param_init_decoder(options, params, prefix='decoder_gru'):
    
    n_x = options['n_x']
    n_h = options['n_h']
    
    W = np.concatenate([uniform_weight(n_x,n_h),
                        uniform_weight(n_x,n_h)], axis=1)
    params[_p(prefix,'W')] = W
    
    U = np.concatenate([ortho_weight(n_h),
                        ortho_weight(n_h)], axis=1)
    params[_p(prefix,'U')] = U
    
    params[_p(prefix,'b')] = zero_bias(2*n_h)

    Wx = uniform_weight(n_x, n_h)
    params[_p(prefix,'Wx')] = Wx
    
    Ux = ortho_weight(n_h)
    params[_p(prefix,'Ux')] = Ux
    
    params[_p(prefix,'bx')] = zero_bias(n_h)
    
    params[_p(prefix,'b0')] = zero_bias(n_h)

    return params   
Beispiel #5
0
def param_init_decoder(options, params, prefix='decoder_lstm'):

    n_x = options['n_x']
    n_h = options['n_h']

    W = np.concatenate([
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h)
    ],
                       axis=1)
    params[_p(prefix, 'W')] = W

    U = np.concatenate([
        ortho_weight(n_h),
        ortho_weight(n_h),
        ortho_weight(n_h),
        ortho_weight(n_h)
    ],
                       axis=1)
    params[_p(prefix, 'U')] = U

    params[_p(prefix, 'b')] = zero_bias(4 * n_h)
    params[_p(prefix, 'b')][n_h:2 * n_h] = 3 * np.ones(
        (n_h, )).astype(theano.config.floatX)

    return params
def param_init_encoder(options, params, prefix='lstm_encoder'):
    
    n_x = options['n_x']
    n_h = options['n_h']
    
    W = np.concatenate([uniform_weight(n_x,n_h),
                        uniform_weight(n_x,n_h),
                        uniform_weight(n_x,n_h),
                        uniform_weight(n_x,n_h)], axis=1)
    params[_p(prefix, 'W')] = W
    
    U = np.concatenate([ortho_weight(n_h),
                        ortho_weight(n_h),
                        ortho_weight(n_h),
                        ortho_weight(n_h)], axis=1)
    params[_p(prefix, 'U')] = U
    
    params[_p(prefix,'b')] = zero_bias(4*n_h)
    
    # It is observed that setting a high initial forget gate bias for LSTMs can 
    # give slighly better results (Le et al., 2015). Hence, the initial forget
    # gate bias is set to 3.
    params[_p(prefix, 'b')][n_h:2*n_h] = 3*np.ones((n_h,)).astype(theano.config.floatX)

    return params
Beispiel #7
0
def init_params(options, W):

    n_words = options['n_words']
    n_x = options['n_x']
    n_h = options['n_h']
    n_z = options['n_z']
    n_s = options['n_s']

    params = OrderedDict()
    # word embedding init or load
    # params['Wemb'] = uniform_weight(n_words,n_x)
    params['Wemb'] = W.astype(config.floatX)

    options[_p('lstm', 'n_x')] = n_x
    options[_p('lstm', 'n_h')] = n_h
    options[_p('lstm', 'n_g')] = n_s

    params = param_init_lstm(options, params, 'lstm')

    params['Vhid'] = uniform_weight(n_h, n_x)
    params['bhid'] = zero_bias(n_words)

    params['bos'] = zero_bias(n_x)
    params['Tv'] = uniform_weight(n_z, n_h)
    params['Ts'] = uniform_weight(n_x, n_h)
    params['Ta'] = uniform_weight(n_s, n_h)

    #re-z
    params['task_W'] = uniform_weight(n_h, n_z)
    params['task_b'] = zero_bias(n_z)

    return params
def init_params(options):

    n_chars = options['n_chars']
    n_h = options['n_h']

    params = OrderedDict()
    # character embedding
    params['W1'] = uniform_weight(n_chars, n_h)
    params['b1'] = zero_bias(n_h)
    params['W2'] = uniform_weight(n_h, n_h)
    params['b2'] = zero_bias(n_h)

    return params
def init_params(options):

    n_y = options['n_y']
    n_z = options['n_z']

    params = OrderedDict()

    params['Wy1'] = uniform_weight(n_z, 512)
    params['by1'] = zero_bias(512)

    params['Wy2'] = uniform_weight(512, n_y)
    params['by2'] = zero_bias(n_y)

    return params
Beispiel #10
0
def LSTM(input_x, rnn_size,
         batch_size):  #input(batch_size, steps, embedding_size)
    num_steps = int(input_x.get_shape()[1])
    embedding_size = int(input_x.get_shape()[2])
    #define parameter
    W = tf.get_variable("W",
                        initializer=tf.concat(1, [
                            uniform_weight(embedding_size, rnn_size),
                            uniform_weight(embedding_size, rnn_size)
                        ]))
    U = tf.get_variable(
        "U",
        initializer=tf.concat(1,
                              [ortho_weight(rnn_size),
                               ortho_weight(rnn_size)]))
    b = tf.get_variable("b", initializer=tf.zeros([2 * rnn_size]))
    Wx = tf.get_variable("Wx",
                         initializer=uniform_weight(embedding_size, rnn_size))
    Ux = tf.get_variable("Ux", initializer=ortho_weight(rnn_size))
    bx = tf.get_variable("bx", initializer=tf.zeros([rnn_size]))
    h_ = tf.zeros([batch_size, rnn_size])
    one = tf.fill([batch_size, rnn_size], 1.)
    state_below = tf.transpose(tf.batch_matmul(
        input_x,
        tf.tile(tf.reshape(W, [1, embedding_size, 2 * rnn_size]),
                [batch_size, 1, 1])) + b,
                               perm=[1, 0, 2])
    state_belowx = tf.transpose(tf.batch_matmul(
        input_x,
        tf.tile(tf.reshape(Wx, [1, embedding_size, rnn_size]),
                [batch_size, 1, 1])) + bx,
                                perm=[1, 0, 2])  #(steps, batch_size, rnn_size)
    output = []  #(steps, batch_size, rnn_size)
    with tf.variable_scope("GRU"):
        for time_step in range(num_steps):
            preact = tf.matmul(h_, U)
            preact = tf.add(preact, state_below[time_step])

            r = tf.nn.sigmoid(_slice(preact, 0, rnn_size))
            u = tf.nn.sigmoid(_slice(preact, 1, rnn_size))

            preactx = tf.matmul(h_, Ux)
            preactx = tf.mul(preactx, r)
            preactx = tf.add(preactx, state_belowx[time_step])
            h = tf.tanh(preactx)

            h_ = tf.add(tf.mul(u, h_), tf.mul(tf.sub(one, u), h))
            output.append(h_)
    output = tf.transpose(output, perm=[1, 0, 2])
    return output  #(batch_size, steps, rnn_size)
    def _init_gru(in_dim, hid_dim, prefix_):
        param[prefix_ + '_W'] = numpy.concatenate(
            [uniform_weight(in_dim, hid_dim),
             uniform_weight(in_dim, hid_dim)],
            axis=1)
        param[prefix_ + '_Wx'] = uniform_weight(in_dim, hid_dim)

        param[prefix_ + '_U'] = numpy.concatenate(
            [ortho_weight(hid_dim),
             ortho_weight(hid_dim)], axis=1)
        param[prefix_ + '_b'] = zero_vector(2 * hid_dim)

        param[prefix_ + '_Ux'] = ortho_weight(hid_dim)
        param[prefix_ + '_bx'] = zero_vector(hid_dim)
Beispiel #12
0
def param_init_gru(options, param, prefix='gru', nin=None, dim=None):

    param[prefix + '_W'] = numpy.concatenate(
        [uniform_weight(nin, dim),
         uniform_weight(nin, dim)], axis=1)
    param[prefix + '_U'] = numpy.concatenate(
        [ortho_weight(dim), ortho_weight(dim)], axis=1)
    param[prefix + '_b'] = zero_vector(2 * dim)

    param[prefix + '_Wx'] = uniform_weight(nin, dim)
    param[prefix + '_Ux'] = ortho_weight(dim)
    param[prefix + '_bx'] = zero_vector(dim)

    return param
def evaluateOneEpoch(inputCoor, inputGraph, inputLabel, para, sess, trainOperaion):
    test_loss = []
    test_acc = []
    test_predict = []
    for i in range(len(inputCoor)):
        xTest, graphTest, labelTest = inputCoor[i], inputGraph[i], inputLabel[i]
        graphTest = graphTest.tocsr()
        labelBinarize = label_binarize(labelTest, classes=[i for i in range(para.outputClassN)])
        test_batch_size = para.testBatchSize
        for testBatchID in range(len(labelTest) / test_batch_size):
            start = testBatchID * test_batch_size
            end = start + test_batch_size
            batchCoor, batchGraph, batchLabel = get_mini_batch(xTest, graphTest, labelBinarize, start, end)
            batchWeight = uniform_weight(batchLabel)
            batchGraph = batchGraph.todense()

            feed_dict = {trainOperaion['inputPC']: batchCoor, trainOperaion['inputGraph']: batchGraph,
                         trainOperaion['outputLabel']: batchLabel, trainOperaion['weights']: batchWeight,
                         trainOperaion['keep_prob_1']: 1.0, trainOperaion['keep_prob_2']: 1.0}

            predict, loss_test, acc_test = sess.run(
                [trainOperaion['predictLabels'], trainOperaion['loss'], trainOperaion['acc']], feed_dict=feed_dict)
            test_loss.append(loss_test)
            test_acc.append(acc_test)
            test_predict.append(predict)

    test_average_loss = np.mean(test_loss)
    test_average_acc = np.mean(test_acc)

    return test_average_loss, test_average_acc, test_predict
Beispiel #14
0
def init_params(options):

    n_words = options['n_words']
    n_x = options['n_x']
    n_h = options['n_h']

    params = OrderedDict()
    params['Wemb'] = uniform_weight(n_words, n_x)
    params = param_init_decoder(options, params, prefix='decoder_h1')

    options['n_x'] = n_h
    params = param_init_decoder(options, params, prefix='decoder_h2')
    options['n_x'] = n_x

    params['Vhid'] = uniform_weight(n_h, n_words)
    params['bhid'] = zero_bias(n_words)

    return params
Beispiel #15
0
def param_init_gru(options, param, prefix='gru', nin=None, dim=None):

    param[prefix + '_W'] = numpy.concatenate(
        [
            uniform_weight(nin, dim), uniform_weight(nin, dim)
        ],
        axis=1)
    param[prefix + '_U'] = numpy.concatenate(
        [
            ortho_weight(dim), ortho_weight(dim)
        ],
        axis=1)
    param[prefix + '_b'] = zero_vector(2 * dim)

    param[prefix + '_Wx'] = uniform_weight(nin, dim)
    param[prefix + '_Ux'] = ortho_weight(dim)
    param[prefix + '_bx'] = zero_vector(dim)

    return param
def init_params(options, W):

    n_words = options['n_words']
    n_x = options['n_x']
    n_h = options['n_h']
    n_z = options['n_z']

    params = OrderedDict()
    # word embedding
    # params['Wemb'] = uniform_weight(n_words,n_x)
    params['Wemb'] = W.astype(config.floatX)
    params = param_init_encoder(options, params)

    params['Vhid'] = uniform_weight(n_h, n_x)
    params['bhid'] = zero_bias(n_words)

    params['C0'] = uniform_weight(n_z, n_x)

    return params
def trainOneEpoch(inputCoor, inputGraph, inputLabel, para, sess, trainOperaion, weight_dict, learningRate):
    dataChunkLoss = []
    dataChunkAcc = []
    dataChunkRegLoss = []
    for i in range(len(inputCoor)):
        xTrain_1, graphTrain_1, labelTrain_1 = inputCoor[i], inputGraph[i], inputLabel[i]
        graphTrain_1 = graphTrain_1.tocsr()
        labelBinarize = label_binarize(labelTrain_1, classes=[j for j in range(para.outputClassN)])
        xTrain, graphTrain, labelTrain = shuffle(xTrain_1, graphTrain_1, labelBinarize)
        # labelBinarize = label_binarize(labelTrain, classes=[j for j in range(40)])

        batch_loss = []
        batch_acc = []
        batch_reg = []
        batchSize = para.batchSize
        for batchID in range(len(labelBinarize) / para.batchSize):
            start = batchID * batchSize
            end = start + batchSize
            batchCoor, batchGraph, batchLabel = get_mini_batch(xTrain, graphTrain, labelTrain, start, end)
            batchGraph = batchGraph.todense()


            batchCoor = add_noise(batchCoor, sigma=0.008, clip=0.02)
            if para.weighting_scheme == 'uniform':
                batchWeight = uniform_weight(batchLabel)
            elif para.weighting_scheme == 'weighted':
                batchWeight = weights_calculation(batchLabel, weight_dict)
            else:
                print 'please enter the valid weighting scheme'
	        
	    #print batchWeight

            feed_dict = {trainOperaion['inputPC']: batchCoor, trainOperaion['inputGraph']: batchGraph,
                         trainOperaion['outputLabel']: batchLabel, trainOperaion['lr']: learningRate,
                         trainOperaion['weights']: batchWeight,
                         trainOperaion['keep_prob_1']: para.keep_prob_1, trainOperaion['keep_prob_2']: para.keep_prob_2}

            opt, loss_train, acc_train, loss_reg_train = sess.run(
                [trainOperaion['train'], trainOperaion['loss_total'], trainOperaion['acc'], trainOperaion['loss_reg']],
                feed_dict=feed_dict)

            #print('The loss loss_reg and acc for this batch is {},{} and {}'.format(loss_train, loss_reg_train, acc_train))
            batch_loss.append(loss_train)
            batch_acc.append(acc_train)
            batch_reg.append(loss_reg_train)

        dataChunkLoss.append(np.mean(batch_loss))
        dataChunkAcc.append(np.mean(batch_acc))
        dataChunkRegLoss.append(np.mean(batch_reg))


    train_average_loss = np.mean(dataChunkLoss)
    train_average_acc = np.mean(dataChunkAcc)
    loss_reg_average = np.mean(dataChunkRegLoss)
    return train_average_loss, train_average_acc, loss_reg_average
Beispiel #18
0
def param_init_fflayer(options,
                       param,
                       prefix='ff',
                       nin=None,
                       nout=None,
                       ortho=True):

    param[prefix + '_W'] = uniform_weight(nin, nout)
    param[prefix + '_b'] = zero_vector(nout)

    return param
Beispiel #19
0
def param_init_fflayer(options,
                       param,
                       prefix='ff',
                       nin=None,
                       nout=None,
                       ortho=True):

    param[prefix + '_W'] = uniform_weight(nin, nout)
    param[prefix + '_b'] = zero_vector(nout)

    return param
Beispiel #20
0
def param_init_gru_cond(options,
                        param,
                        prefix='gru_cond',
                        nin=None,
                        dim=None,
                        dimctx=None,
                        nin_nonlin=None,
                        dim_nonlin=None):
    if nin_nonlin is None:
        nin_nonlin = nin
    if dim_nonlin is None:
        dim_nonlin = dim

    param = param_init_gru(options, param, prefix=prefix, nin=nin, dim=dim)

    param[prefix + '_U_nl'] = numpy.concatenate(
        [ortho_weight(dim_nonlin),
         ortho_weight(dim_nonlin)], axis=1)
    param[prefix + '_b_nl'] = zero_vector(2 * dim_nonlin)

    param[prefix + '_Ux_nl'] = ortho_weight(dim_nonlin)
    param[prefix + '_bx_nl'] = zero_vector(dim_nonlin)

    # context to LSTM
    param[prefix + '_Wc'] = uniform_weight(dimctx, dim * 2)
    param[prefix + '_Wcx'] = uniform_weight(dimctx, dim)

    # attention: combined -> hidden
    param[prefix + '_W_comb_att'] = uniform_weight(dim, dimctx)

    # attention: context -> hidden
    param[prefix + '_Wc_att'] = uniform_weight(dimctx, dimctx)

    # attention: hidden bias
    param[prefix + '_b_att'] = zero_vector(dimctx)

    # attention:
    param[prefix + '_U_att'] = uniform_weight(dimctx, 1)
    param[prefix + '_c_att'] = zero_vector(1)

    return param
Beispiel #21
0
def init_params(options):
    
    n_x = options['n_x']  
    n_h = options['n_h']
    
    params = OrderedDict()
    params = param_init_decoder(options,params)
    
    params['Vhid'] = uniform_weight(n_h,n_x)
    params['bhid'] = zero_bias(n_x)                                     

    return params
Beispiel #22
0
def param_init_decoder(options, params, prefix='decoder'):

    n_x = options['n_x']
    n_h = options['n_h']
    n_z = options['n_z']

    W = np.concatenate([
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h),
        uniform_weight(n_x, n_h)
    ],
                       axis=1)
    params[_p(prefix, 'W')] = W

    U = np.concatenate([
        ortho_weight(n_h),
        ortho_weight(n_h),
        ortho_weight(n_h),
        ortho_weight(n_h)
    ],
                       axis=1)
    params[_p(prefix, 'U')] = U

    C = np.concatenate([
        uniform_weight(n_z, n_h),
        uniform_weight(n_z, n_h),
        uniform_weight(n_z, n_h),
        uniform_weight(n_z, n_h)
    ],
                       axis=1)
    params[_p(prefix, 'C')] = C

    params[_p(prefix, 'b')] = zero_bias(4 * n_h)
    params[_p(prefix, 'b')][n_h:2 * n_h] = 3 * np.ones(
        (n_h, )).astype(theano.config.floatX)

    C0 = uniform_weight(n_z, n_h)
    params[_p(prefix, 'C0')] = C0

    params[_p(prefix, 'b0')] = zero_bias(n_h)

    #params[_p(prefix,'b_y')] = zero_bias(n_x)  # 48

    return params
def param_init_gru_cond(options,
                        param,
                        prefix='gru_cond',
                        nin=None,
                        dim=None,
                        dimctx=None):

    assert type(dim) is list
    last_dim = dim[-1]

    param = param_init_gru(options, param, prefix=prefix, nin=nin, dim=dim)

    prefix_ = prefix + '_%d' % (len(dim) - 1)
    # context to LSTM
    param[prefix_ + '_Wc'] = numpy.concatenate(
        [uniform_weight(dimctx, last_dim),
         uniform_weight(dimctx, last_dim)],
        axis=1)
    param[prefix_ + '_Wcx'] = uniform_weight(dimctx, last_dim)

    # attention: combined -> hidden
    param[prefix_ + '_W_comb_att'] = uniform_weight(last_dim, dimctx)

    # attention: context -> hidden
    param[prefix_ + '_Wc_att'] = uniform_weight(dimctx, dimctx)

    # attention: hidden bias
    param[prefix_ + '_b_att'] = zero_vector(dimctx)

    # attention:
    param[prefix_ + '_U_att'] = uniform_weight(dimctx, 1)

    return param
Beispiel #24
0
def init_params(options):

    n_chars = options['n_chars']
    n_x = options['n_x']

    params = OrderedDict()
    # character embedding
    params['Wemb'] = uniform_weight(n_chars, n_x)
    # encoding characters into words
    params = param_init_encoder(options, params, prefix='encoder_f')
    params = param_init_encoder(options, params, prefix='encoder_b')

    return params
Beispiel #25
0
def init_params(options):
    
    n_chars = options['n_chars']
    img_w = options['img_w']  
    
    params = OrderedDict()
    # character embedding 
    params['Wemb'] = uniform_weight(n_chars,img_w)
    # encoding characters into words
    length = len(options['filter_shapes'])
    for idx in range(length):
        params = param_init_encoder(options['filter_shapes'][idx],params,prefix=_p('cnn_encoder',idx))
                                    
    return params
Beispiel #26
0
def param_init_decoder(options, params, prefix='decoder_vanilla'):

    n_x = options['n_x']
    n_h = options['n_h']

    W = uniform_weight(n_x, n_h)
    params[_p(prefix, 'W')] = W

    U = ortho_weight(n_h)
    params[_p(prefix, 'U')] = U

    params[_p(prefix, 'b')] = zero_bias(n_h)

    return params
Beispiel #27
0
def init_params(options):
    
    n_words = options['n_words']
    n_x = options['n_x']  
    n_h = options['n_h']
    
    params = OrderedDict()
    # word embedding 
    params['Wemb'] = uniform_weight(n_words,n_x)
    #params['Wemb'] = W.astype(config.floatX)
    params['Wemb'][-1] = np.zeros((n_x,)).astype(theano.config.floatX)
    # encoding words into sentences
    length = len(options['filter_shapes'])
    for idx in range(length):
        params = param_init_encoder(options['filter_shapes'][idx],params,prefix=_p('cnn_encoder',idx))
    
    options['n_z'] = options['feature_maps'] * length
    params = param_init_decoder(options,params,prefix='decoder')
    
    params['Vhid'] = uniform_weight(n_h,n_x)
    params['bhid'] = zero_bias(n_words)                                    

    return params
def param_init_fflayer(options,
                       param,
                       prefix='ff',
                       nin=None,
                       nout=None,
                       ortho=True):

    if type(nin) is int and type(nout) is int:
        param[prefix + '_W'] = uniform_weight(nin, nout)
        param[prefix + '_b'] = zero_vector(nout)
    else:
        assert type(nout) is list

        if type(nin) is int:
            nin = [nin] + nout[:-1]
        elif type(nin) is list:
            assert len(nin) == len(nout)

        for l, (in_dim, out_dim) in enumerate(zip(nin, nout)):
            prefix_ = prefix + '_%d' % l
            param[prefix_ + '_W'] = uniform_weight(in_dim, out_dim)
            param[prefix_ + '_b'] = zero_vector(out_dim)

    return param
def evaluateOneEpoch(inputCoor, inputGraph, inputLabel, para, sess, trainOperaion):
    # Description: Performance on the test set data
    # Input: (1)inputCoor: input coordinates (B, N, 3) (2) inputGraph: input graph (B, N*N) (3) inputLabel: labels (B, 1)
    #        (4) para: global Parameters  (5) sess: Session (6) trainOperaion: placeholder dictionary
    # Return: average loss, acc, regularization loss for test set
    test_loss = []
    test_acc = []
    test_predict = []
    for i in range(len(inputCoor)):
        xTest, graphTest, labelTest = inputCoor[i], inputGraph[i], inputLabel[i]
        graphTest = graphTest.tocsr()
        labelBinarize = label_binarize(labelTest, classes=[j for j in range(40)])
        test_batch_size = para.testBatchSize
        for testBatchID in range(len(labelTest) / test_batch_size):
            start = testBatchID * test_batch_size
            end = start + test_batch_size
            batchCoor, batchGraph, batchLabel = get_mini_batch(xTest, graphTest, labelBinarize, start, end)
            batchWeight = uniform_weight(batchLabel)
            batchGraph = batchGraph.todense()

            batchIndexL1, centroid_coordinates = farthest_sampling_new(batchCoor, M=para.clusterNumberL1,
                                                                   k=para.nearestNeighborL1, batch_size=test_batch_size,
                                                                   nodes_n=para.pointNumber)

            batchMiddleGraph = middle_graph_generation(centroid_coordinates, batch_size = test_batch_size, M = para.clusterNumberL1)


            feed_dict = {trainOperaion['inputPC']: batchCoor, trainOperaion['inputGraph']: batchGraph,
                         trainOperaion['outputLabel']: batchLabel, trainOperaion['weights']: batchWeight,
                         trainOperaion['keep_prob_1']: 1.0, trainOperaion['keep_prob_2']: 1.0,
                         trainOperaion['batch_index_l1']: batchIndexL1,
                         trainOperaion['l2Graph']: batchMiddleGraph, trainOperaion['batch_size']: test_batch_size
                         }

            predict, loss_test, acc_test = sess.run(
                [trainOperaion['predictLabels'], trainOperaion['loss'], trainOperaion['acc']], feed_dict=feed_dict)
            test_loss.append(loss_test)
            test_acc.append(acc_test)
            test_predict.append(predict)

    test_average_loss = np.mean(test_loss)
    test_average_acc = np.mean(test_acc)

    return test_average_loss, test_average_acc, test_predict
Beispiel #30
0
def evaluateOneEpoch(inputCoor, inputGraph, inputLabel, para, sess, trainOperaion):
    # Description: Performance on the test set data
    # Input: (1)inputCoor: input coordinates (B, N, 3) (2) inputGraph: input graph (B, N*N) (3) inputLabel: labels (B, 1)
    #        (4) para: global Parameters  (5) sess: Session (6) trainOperaion: placeholder dictionary
    # Return: average loss, acc, regularization loss for test set
    test_loss = []
    test_acc = []
    test_predict = []
    for i in range(len(inputCoor)):
        xTest, graphTest, labelTest = inputCoor[i], inputGraph[i], inputLabel[i]
        graphTest = graphTest.tocsr()
        labelBinarize = label_binarize(labelTest, classes=[j for j in range(40)])
        test_batch_size = para.testBatchSize
        for testBatchID in range(len(labelTest) / test_batch_size):
            start = testBatchID * test_batch_size
            end = start + test_batch_size
            batchCoor, batchGraph, batchLabel = get_mini_batch(xTest, graphTest, labelBinarize, start, end)
            batchWeight = uniform_weight(batchLabel)
            batchGraph = batchGraph.todense()
            # select the centroid points by farthest sampling and get the index of each centroid points n nearest neighbors
            batchIndexL1, centroid_coordinates = farthest_sampling(batchCoor, M=para.clusterNumberL1,
                                                                   k=para.nearestNeighborL1, batch_size=test_batch_size,
                                                                   nodes_n=para.pointNumber)

            batchMiddleGraph = middle_graph_generation(centroid_coordinates, batch_size = test_batch_size, M = para.clusterNumberL1)


            feed_dict = {trainOperaion['inputPC']: batchCoor, trainOperaion['inputGraph']: batchGraph,
                         trainOperaion['outputLabel']: batchLabel, trainOperaion['weights']: batchWeight,
                         trainOperaion['keep_prob_1']: 1.0, trainOperaion['keep_prob_2']: 1.0,
                         trainOperaion['batch_index_l1']: batchIndexL1,
                         trainOperaion['l2Graph']: batchMiddleGraph, trainOperaion['batch_size']: test_batch_size
                         }

            predict, loss_test, acc_test = sess.run(
                [trainOperaion['predictLabels'], trainOperaion['loss'], trainOperaion['acc']], feed_dict=feed_dict)
            test_loss.append(loss_test)
            test_acc.append(acc_test)
            test_predict.append(predict)

    test_average_loss = np.mean(test_loss)
    test_average_acc = np.mean(test_acc)

    return test_average_loss, test_average_acc, test_predict
def init_params(options,W):
    
    params = OrderedDict()
    # W is initialized by the pretrained word embedding
    params['Wemb'] = W.astype(config.floatX)
    # otherwise, W will be initialized randomly
    # n_words = options['n_words']
    # n_x = options['n_x'] 
    # params['Wemb'] = uniform_weight(n_words,n_x)
    
    length = len(options['filter_shapes'])
    for idx in range(length):
        params = param_init_encoder(options['filter_shapes'][idx],params,prefix=_p('cnn_encoder',idx))
    
    n_h = options['feature_maps'] * length
    params['Wy'] = uniform_weight(n_h,options['n_y'])
    params['by'] = zero_bias(options['n_y'])                                     

    return params
Beispiel #32
0
def init_params(options, W):

    params = OrderedDict()
    # W is initialized by the pretrained word embedding
    params['Wemb'] = W.astype(config.floatX)
    # otherwise, W will be initialized randomly
    # n_words = options['n_words']
    # n_x = options['n_x']
    # params['Wemb'] = uniform_weight(n_words,n_x)

    length = len(options['filter_shapes'])
    for idx in range(length):
        params = param_init_encoder(options['filter_shapes'][idx],
                                    params,
                                    prefix=_p('cnn_encoder', idx))

    n_h = options['feature_maps'] * length
    params['Wy'] = uniform_weight(n_h, options['n_y'])
    params['by'] = zero_bias(options['n_y'])

    return params
Beispiel #33
0
def init_params(options,W):
    
    n_h = options['n_h']
    n_y = options['n_y']
    
    params = OrderedDict()
    # W is initialized by the pretrained word embedding
    params['Wemb'] = W.astype(config.floatX)
    # otherwise, W will be initialized randomly
    # n_words = options['n_words']
    # n_x = options['n_x'] 
    # params['Wemb'] = uniform_weight(n_words,n_x)
    
    # bidirectional LSTM
    params = param_init_encoder(options,params,prefix="lstm_encoder")
    params = param_init_encoder(options,params,prefix="lstm_encoder_rev")
    
    params['Wy'] = uniform_weight(2*n_h,n_y)
    params['by'] = zero_bias(n_y)                                     

    return params
Beispiel #34
0
def train_one_epoch(input_pc, input_graph, input_label, hyperparameters, sess,
                    train_operation, weight_dict, learning_rate):
    batch_loss = []
    batch_accuracy = []
    batch_reg_loss = []
    minibatch_size = hyperparameters.minibatch_size
    for minibatch_id in range(len(input_pc) / minibatch_size):
        start = minibatch_id * minibatch_size
        end = start + minibatch_size

        minibatch_coords, minibatch_graph, minibatch_label = input_pc[
            start:end], input_graph[start:end], input_label[start:label]
        minibatch_weight = utils.uniform_weight(minibatch_label)
        minibatch_graph = minibatch_graph.todense()

        feed_dict = {
            train_operation['input_pc']: minibatch_coords,
            train_operation['input_graph']: minibatch_graph,
            train_operation['output_label']: minibatch_label,
            train_operation['learning_rate']: learning_rate,
            train_operation['weights']: minibatch_weight,
            train_operation['keep_prob_1']: hyperparameters.keep_prob_1,
            train_operation['keep_prob_2']: hyperparameters.keep_prob_2
        }

        train_opt, train_loss, train_accuracy, train_reg_loss = sess.run(
            [
                train_operation['train'], train_operation['loss'],
                train_operation['accuracy'], train_operation['loss_reg']
            ],
            feed_dict=feed_dict)

        batch_loss.append(train_loss)
        batch_accuracy.append(train_accuracy)
        batch_reg_loss.append(train_reg_loss)

    train_average_loss = np.mean(batch_loss)
    train_average_accuracy = np.mean(batch_accuracy)
    train_average_reg_loss = np.mean(batch_reg_loss)
    return train_average_loss, train_average_accuracy, train_average_reg_loss
Beispiel #35
0
def init_params(options, W):

    n_p = options['n_p']
    n_h = options['n_h']
    n_y = options['n_y']

    params = OrderedDict()
    # W is initialized by the pretrained word embedding
    params['Wemb'] = W.astype(config.floatX)
    # otherwise, W will be initialized randomly
    # n_words = options['n_words']
    # n_x = options['n_x']
    # params['Wemb'] = uniform_weight(n_words,n_x)

    # bidirectional LSTM
    #params = param_init_encoder(options,params, prefix="lstm_encoder")
    #params = param_init_encoder(options,params, prefix="lstm_encoder_rev")
    params = param_init_encoder(options, params, prefix="gru_encoder")
    params = param_init_encoder(options, params, prefix="gru_encoder_rev")

    #params['Wy'] = uniform_weight(n_p, 2*n_h+1, scale=0.1)
    params['Wy'] = np.squeeze(uniform_weight(n_p, 2 * n_h, 1))
    return params
Beispiel #36
0
def evaluate_one_epoch(input_pc, input_graph, input_label, hyperparameters,
                       sess, train_operation):
    test_loss = []
    test_accuracy = []
    test_predictions = []
    minibatch_size = hyperparameters.minibatch_size
    for minibatch_id in range(len(input_pc) / minibatch_size):
        start = minibatch_id * minibatch_size
        end = start + minibatch_size

        minibatch_coords, minibatch_graph, minibatch_label = input_pc[
            start:end], input_graph[start:end], input_label[start:label]
        minibatch_weight = utils.uniform_weight(minibatch_label)
        minibatch_graph = minibatch_graph.todense()

        feed_dict = {
            train_operation['input_pc']: minibatch_coords,
            train_operation['input_graph']: minibatch_graph,
            train_operation['output_label']: minibatch_label,
            train_operation['weights']: minibatch_weight,
            train_operation['keep_prob_1']: 1.0,
            train_operation['keep_prob_2']: 1.0
        }

        predictions, loss, accuracy = sess.run([
            train_operation['predict_label'], train_operation['loss'],
            train_operation['accuracy']
        ],
                                               feed_dict=feed_dict)

        test_loss.append(loss)
        test_accuracy.append(accuracy)
        test_predictions.append(predictions)

    test_average_loss = np.mean(test_loss)
    test_average_accuracy = np.mean(test_accuracy)
    return test_average_loss, test_average_accuracy, test_predictions
def trainOneEpoch(inputCoor, inputGraph, inputLabel, para, sess, trainOperaion,
                  weight_dict, learningRate):
    # Description: training one epoch (two options to train the model, using weighted gradient descent or normal gradient descent)
    # Input: (1)inputCoor: input coordinates (B, N, 3) (2) inputGraph: input graph (B, N*N) (3) inputLabel: labels (B, 1)
    #        (4) para: global Parameters  (5) sess: Session (6) trainOperaion: placeholder dictionary
    #        (7) weight_dict: weighting scheme used of weighted gradient descnet (8)learningRate: learning rate for current epoch
    # Return: average loss, acc, regularization loss for training set
    dataChunkLoss = []
    dataChunkAcc = []
    dataChunkRegLoss = []
    for i in range(len(inputLabel)):
        xTrain_1, graphTrain_1, labelTrain_1 = inputCoor[i], inputGraph[i], inputLabel[i]

        graphTrain_1 = graphTrain_1.tocsr()
        labelBinarize = label_binarize(labelTrain_1, classes=[j for j in range(40)])
        xTrain, graphTrain, labelTrain = shuffle(xTrain_1, graphTrain_1, labelBinarize)

        batch_loss = []
        batch_acc = []
        batch_reg = []
        batchSize = para.batchSize
        for batchID in range(len(labelBinarize) / para.batchSize):
            start = batchID * batchSize
            end = start + batchSize
            batchCoor, batchGraph, batchLabel = get_mini_batch(xTrain, graphTrain, labelTrain, start, end)
            batchGraph = batchGraph.todense()
            batchCoor = add_noise(batchCoor, sigma=0.008, clip=0.02)
	    if para.weighting_scheme == 'uniform':
		batchWeight = uniform_weight(batchLabel)
	    elif para.weighting_scheme == 'weighted':
                batchWeight = weights_calculation(batchLabel, weight_dict)
            else:
                print 'please enter a valid weighting scheme'

            batchIndexL1, centroid_coordinates = farthest_sampling_new(batchCoor, M=para.clusterNumberL1,
                                                                   k=para.nearestNeighborL1, batch_size=batchSize,
                                                                   nodes_n=para.pointNumber)
            batchMiddleGraph = middle_graph_generation(centroid_coordinates, batch_size = batchSize, M = para.clusterNumberL1)

            feed_dict = {trainOperaion['inputPC']: batchCoor, trainOperaion['inputGraph']: batchGraph,
                         trainOperaion['outputLabel']: batchLabel, trainOperaion['lr']: learningRate,
                         trainOperaion['weights']: batchWeight,
                         trainOperaion['keep_prob_1']: para.keep_prob_1, trainOperaion['keep_prob_2']: para.keep_prob_2,
                         trainOperaion['batch_index_l1']: batchIndexL1,
                         trainOperaion['l2Graph']: batchMiddleGraph, trainOperaion['batch_size']: para.batchSize}

            opt, loss_train, acc_train, loss_reg_train = sess.run(
                [trainOperaion['train'], trainOperaion['loss_total'], trainOperaion['acc'], trainOperaion['loss_reg']],
                feed_dict=feed_dict)

            batch_loss.append(loss_train)
            batch_acc.append(acc_train)
            batch_reg.append(loss_reg_train)

            #print "The loss, L2 loss and acc for this batch is {}, {} and {}".format(loss_train, loss_reg_train, acc_train)

        dataChunkLoss.append(np.mean(batch_loss))
        dataChunkAcc.append(np.mean(batch_acc))
        dataChunkRegLoss.append(np.mean(batch_reg))

    train_average_loss = np.mean(dataChunkLoss)
    train_average_acc = np.mean(dataChunkAcc)
    loss_reg_average = np.mean(dataChunkRegLoss)
    return train_average_loss, train_average_acc, loss_reg_average