def __init__(self, input_size, hidden_size, optimization=None, wpresent=[], hidden_activation=elu, hidden_deactivation=delu, output_activation=tanh, output_deactivation=dtanh, log_file='log.txt', log=1, vector=None): self.model_type = 'RAE' self.i_size = input_size self.h_size = hidden_size self.optimum = optimization self.hidactivation = hidden_activation self.hiddeactivation = hidden_deactivation self.outactivation = output_activation self.outdeactivation = output_deactivation self.en_de_coder = autoencoder(hidden_activation, hidden_deactivation, output_activation, output_deactivation) self.w, self.b, self.dw, self.db, self.count = self.init_weights( input_size, hidden_size, wpresent) self.count2 = 0.0 self.wpresent = wpresent self.log = log self.logg = logger('text', log_file) self.vector = vector
def msrp_train_test(var_file, stp, parse_type): #create result_directory res_dir = '/'.join(var_file.split('/')[:-1]) + '/results/' log_file = '/'.join(var_file.split('/')[:-1]) + '/msrp_train_log.txt' if not os.path.isdir(res_dir): os.mkdir(res_dir) # getting data that is processed for testing porpose train, train_label, test, test_label = get_msrp_data(stp) #initalizing model isize = 50 osize = 2 wvect = Word_vector(isize, vtype='msrp') # a.optimum.opt_var.g=g # preprocessing for train and test set data_processing = preprocess(parsing_type=parse_type, structure_type='h', stopword=stp, wvect=wvect) train_set, _ = data_processing.process_words_data(train) test_set, _ = data_processing.process_words_data(test) train_label = [ np.array([[0.0], [1.0]]) if i == 1 else np.array([[1.0], [0.0]]) for i in train_label ] train = [[train_set[i], train_set[i + 1]] for i in range(0, len(train_set), 2)] test = [[test_set[i], test_set[i + 1]] for i in range(0, len(test_set), 2)] # model layers initalization isize, hsize, w, b, g = pickle.load(open(var_file, 'rb')) opt_var = Optimization_variable(method='rmsprop', isize=isize, osize=hsize, model_type='RAE', option={'wpresent': []}) opt = Optimization(optimization_variable=opt_var, learning_rate=0.0001) rae_layer = stack_RAE(input_size=isize, hidden_size=hsize, optimization=opt, hidden_activation=tanh, hidden_deactivation=dtanh) rae_layer.w = w rae_layer.b = b rae_layer.optimum.opt_var.g = g rae_layer.init_dw(w, b) nn_layer = NN(2 * hsize, osize) nn_layer.optimum = Optimization( optimization_variable=Optimization_variable(method='rmsprop', isize=2 * hsize, osize=osize), learning_rate=0.0001) logg = logger('text', log_file) # trainning of model epoch = 10000 batch_size = 50 for ep in range(epoch): batches = mini_batch(zip(train, train_label), len(train_set), batch_size) cost = 0.0 ecount = 0 for batch in range(len(batches)): for data in batches[batch]: # forward pass of RAE vect1, v1 = rae_layer.encoding(data[0][0]) vect2, v2 = rae_layer.encoding(data[0][1]) data[0][0]['vects'] = vect1 data[0][1]['vects'] = vect2 # forward pass of NN o, vnn = nn_layer.forward_pass( np.concatenate( (vect1[len(vect1) - 1], vect2[len(vect2) - 1]), axis=0)) # cost calculation cost += ce_erro(o, data[1]) ecount += 1 grad = o - data[1] # backward pass of NN nngrad = nn_layer.backward_pass(grad, vnn) # backward padd of RAE nngrad1, nngrad2 = np.split(nngrad, [hsize], axis=0) grad1 = rae_layer.encoding_back(nngrad1, v1, data[0][0]) grad2 = rae_layer.encoding_back(nngrad2, v2, data[0][1]) # calculating weight update nn_layer.calc_dw( grad, np.concatenate( (vect1[len(vect1) - 1], vect2[len(vect2) - 1]), axis=0)) rae_layer.calc_dw(grad1, data[0][0]) rae_layer.calc_dw(grad2, data[0][1]) # updating weights nn_layer.update_weights() rae_layer.update_weights() if (ep + 1) % 50 == 0: score = msrp_test([rae_layer, nn_layer], test) tp, tn, fp, fn, acc, f1 = get_results(score, test_label) print 'stopword : %d, Tp : %d, Tn : %d, Fp : %d Fn : %d,acc : %ff1 score : %f' % ( stp, tp, tn, fp, fn, acc, f1) logg.log_text( 'stopword : %d, Tp : %d, Tn : %d, Fp : %d Fn : %d, acc : %f, f1 score : %f' % (stp, tp, tn, fp, fn, acc, f1)) pickle.dump([rae_layer.w, rae_layer.b, nn_layer.w], open( '/'.join(var_file.split('/')[:-1]) + '/results/weights' + str(ep) + '.pickle', 'wb')) print "%d/%d epoch completed .... error : %f" % (ep + 1, epoch, cost / ecount) logg.log_text("%d/%d epoch completed ....\n" % (ep + 1, epoch)) if cost / ecount < 0.01: break # getting results score = msrp_test([rae_layer, nn_layer], test) tp, tn, fp, fn, acc, f1 = get_results(score, test_label) print '\nstopword : %d\n\tTrue positive : %d\n\tTrue negative : %d\n\tFalse positive : %d\n\tFalse negatie : %d\n\taccuracy : %f\n\tf1 score : %f\n' % ( stp, tp, tn, fp, fn, acc, f1) # logging result in file open(res_dir + 'res.txt', 'a').write( '\nstopword : %d\n\tTrue positive : %d\n\tTrue negative : %d\n\tFalse positive : %d\n\tFalse negatie : %d\n\taccuracy : %f\n\tf1 score : %f\n' % (stp, tp, tn, fp, fn, acc, f1))
def msrp_train_test(var_file, stp, parse_type): #create result_directory out_dir = '/'.join(var_file.split('/')[:-1]) res_dir = out_dir + '/results/' log_file = out_dir + '/msrp_train_log.txt' if not os.path.isdir(out_dir): os.mkdir(out_dir) if not os.path.isdir(res_dir): os.mkdir(res_dir) # getting data that is processed for testing porpose train, train_label, test, test_label = get_msrp_data(stp) #initalizing model isize = 50 hsize = 200 rosize = 50 osize = 2 wvect = Word_vector(isize, vtype='msrp') # preprocessing for train and test set train_set = preprocess(train, wvect) test_set = preprocess(test, wvect) train_label = [ np.array([[0.0], [1.0]]) if i == 1 else np.array([[1.0], [0.0]]) for i in train_label ] train = [[train_set[i], train_set[i + 1]] for i in range(0, len(train_set), 2)] test = [[test_set[i], test_set[i + 1]] for i in range(0, len(test_set), 2)] # model layers initalization opt_var = Optimization_variable(method='rmsprop', isize=isize, osize=rosize, model_type='GRU', option={'hsize': hsize}) opt = Optimization(optimization_variable=opt_var, learning_rate=0.00001) rnn_layer = gru(i_size=isize, h_size=hsize, o_size=rosize, activation=tanh, deactivation=dtanh, optimization=opt) nn_layer = NN(isize=2 * rosize, osize=osize) nn_layer.optimum = Optimization( optimization_variable=Optimization_variable(method='rmsprop', isize=2 * rosize, osize=osize), learning_rate=0.0001) logg = logger('text', log_file) # trainning of model epoch = 1000 batch_size = 50 for ep in range(epoch): batches = mini_batch(zip(train, train_label), len(train_set), batch_size) cost = 0.0 ecount = 0 for batch in range(len(batches)): for data in batches[batch]: # forward pass of RNN vect1, v1 = rnn_layer.forward_pass(data[0][0]) vect2, v2 = rnn_layer.forward_pass(data[0][1]) # forward pass of NN o, vnn = nn_layer.forward_pass( np.concatenate((vect1, vect2), axis=0)) # cost calculation cost += ce_erro(o, data[1]) ecount += 1 grad = o - data[1] # backward pass of NN nngrad = nn_layer.backward_pass( grad, vnn, np.concatenate((vect1, vect2), axis=0)) # backward padd of RNN nngrad1, nngrad2 = np.split(nngrad, [rosize], axis=0) grad1 = rnn_layer.backward_pass(nngrad1, v1, data[0][0]) grad2 = rnn_layer.backward_pass(nngrad2, v2, data[0][1]) # updating weights nn_layer.update_weights() rnn_layer.update_weights() if (ep + 1) % 20 == 0: score = msrp_test([rnn_layer, nn_layer], test) tp, tn, fp, fn, acc, f1 = get_results(score, test_label) print 'stopword : %d, Tp : %d, Tn : %d, Fp : %d Fn : %d,acc : %ff1 score : %f' % ( stp, tp, tn, fp, fn, acc, f1) logg.log_text( 'stopword : %d, Tp : %d, Tn : %d, Fp : %d Fn : %d, acc : %f, f1 score : %f' % (stp, tp, tn, fp, fn, acc, f1)) pickle.dump([rnn_layer.w, rnn_layer.b, nn_layer.w], open( '/'.join(var_file.split('/')[:-1]) + '/results/weights' + str(ep) + '.pickle', 'wb')) print "%d/%d epoch completed .... error : %f" % (ep + 1, epoch, cost / ecount) logg.log_text("%d/%d epoch completed ....\n" % (ep + 1, epoch)) # if cost/ecount < 0.01: # break # getting results score = msrp_test([rnn_layer, nn_layer], test) tp, tn, fp, fn, acc, f1 = get_results(score, test_label) print '\nstopword : %d\n\tTrue positive : %d\n\tTrue negative : %d\n\tFalse positive : %d\n\tFalse negatie : %d\n\taccuracy : %f\n\tf1 score : %f\n' % ( stp, tp, tn, fp, fn, acc, f1) # logging result in file open(res_dir + 'res.txt', 'a').write( '\nstopword : %d\n\tTrue positive : %d\n\tTrue negative : %d\n\tFalse positive : %d\n\tFalse negatie : %d\n\taccuracy : %f\n\tf1 score : %f\n' % (stp, tp, tn, fp, fn, acc, f1))