def svm(self, h, name=""): # Function that trains an SVM on the representation # h given in argument. print '--- Loading data' self.jobman.save() f = theano.function([self.x], [h]) def fix_theano(X, l): return f(X)[0] self.svm_data = root(self.jobman.state['dataset'], prepro_fct_x=[(fix_theano, [])]) res = eval_fold(3, self.svm_data) cPickle.dump(res, open('results.pkl', 'w')) self.jobman.state['train_' + name] = res['train'] if self.jobman.state['valid_' + name] <= res['valid']: self.jobman.state['valid_' + name] = res['valid'] self.jobman.state['test_' + name] = res['test'] self.jobman.save() print name, res return True
def __init__(self,jobman): self.jobman = jobman self.x = T.matrix() self.y = T.imatrix() self.data = root(self.jobman.state['dataset'],prepro_fct_x=[],prepro_fct_y=[]) self.shared_x,self.shared_y = self.data.get_train_labeled() self.n_i,self.n_h = (self.data.get_sample_size(),self.jobman.state['n_h']) self.featsplit = int(self.n_h * self.jobman.state['featsplit']) self.cae = cae(self.n_i,self.n_h) self.logreg = logistic(self.featsplit,self.jobman.state['nclass'])
def evalreg(self, name=""): # This function is used only for monitoring purposes print "--- Loading data" self.jobman.save() o = self.logrege.get_hidden(self.cae_emotions.get_hidden(self.x)) f = theano.function([self.x, self.y], hardmax(o, self.y)) def fix_label(Y, l): return numpy.array(Y - 1, dtype="int32").flatten() data = root(self.jobman.state["dataset"], prepro_fct_x=[], prepro_fct_y=[(fix_label, [])]) X, Y = data.get_train_labeled(shared_array=False) print "--- Logreg Valid Accuracy:", f(X, Y) return True
def evalreg(self,name=""): # Function to evaluate the classification error # when using the output of the logistic regression. # This function is used only for monitoring purposes print '--- Loading data' self.jobman.save() o = self.logrege.get_hidden(self.cae_emotions.get_hidden(self.x)) f = theano.function([self.x,self.y],hardmax(o,self.y)) data = root(self.jobman.state['dataset'],prepro_fct_x=[],prepro_fct_y=[]) X,Y = data.get_train_labeled(shared_array=False) print '--- Logreg Valid Accuracy:',f(X,Y) return True
def __init__(self, jobman): self.jobman = jobman self.x = T.matrix() self.y = T.imatrix() self.data = root(self.jobman.state['dataset'], prepro_fct_x=[], prepro_fct_y=[]) self.shared_x, self.shared_y = self.data.get_train_labeled() self.n_i, self.n_h = (self.data.get_sample_size(), self.jobman.state['n_h']) self.featsplit = int(self.n_h * self.jobman.state['featsplit']) self.cae = cae(self.n_i, self.n_h) self.logreg = logistic(self.featsplit, self.jobman.state['nclass'])
def __init__(self, jobman): self.jobman = jobman self.x = T.matrix() self.y = T.ivector() def fix_label(Y, l): return numpy.array(Y - 1, dtype="int32").flatten() self.data = root(self.jobman.state["dataset"], prepro_fct_x=[], prepro_fct_y=[(fix_label, [])]) dataset_list = [ "/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold0_files_dict.pkl", "/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold1_files_dict.pkl", "/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold2_files_dict.pkl", "/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold3_files_dict.pkl", "/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold4_files_dict.pkl", ] csplit = self.jobman.state["split"] assert csplit < 5 self.data.fusion(dataset_list[csplit : csplit + 1]) x, y = shuffle_array( self.data.get_train_unlabeled(shared_array=False), self.data.get_train_labeled(shared_array=False) ) self.shared_x, self.shared_y = (theano.shared(value=x), theano.shared(value=y)) self.n_i, self.n_hi, self.n_he = ( self.data.get_sample_size(), self.jobman.state["n_hi"], self.jobman.state["n_he"], ) self.cae_emotions = cae(self.n_i, self.n_he) self.cae_identity = cae(self.n_i, self.n_hi) self.logrege = logistic(self.n_he, 7) if self.jobman.state["identity"]: self.logregi = logistic(self.n_hi, 900)
def __init__(self, jobman): self.jobman = jobman self.x = T.matrix() self.y = T.ivector() def fix_label(Y, l): return numpy.array(Y - 1, dtype='int32').flatten() self.data = root(self.jobman.state['dataset'], prepro_fct_x=[], prepro_fct_y=[(fix_label, [])]) dataset_list = [ '/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold0_files_dict.pkl', '/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold1_files_dict.pkl', '/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold2_files_dict.pkl', '/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold3_files_dict.pkl', '/scratch/rifaisal/data/tfdconv/convfeat_patch14_1024_33_fold4_files_dict.pkl' ] csplit = self.jobman.state['split'] assert csplit < 5 self.data.fusion(dataset_list[csplit:csplit + 1]) x, y = shuffle_array(self.data.get_train_unlabeled(shared_array=False), self.data.get_train_labeled(shared_array=False)) self.shared_x, self.shared_y = (theano.shared(value=x), theano.shared(value=y)) self.n_i, self.n_hi, self.n_he = (self.data.get_sample_size(), self.jobman.state['n_hi'], self.jobman.state['n_he']) self.cae_emotions = cae(self.n_i, self.n_he) self.cae_identity = cae(self.n_i, self.n_hi) self.logrege = logistic(self.n_he, 7) if self.jobman.state['identity']: self.logregi = logistic(self.n_hi, 900)
def evalreg(self, name=""): # This function is used only for monitoring purposes print '--- Loading data' self.jobman.save() o = self.logrege.get_hidden(self.cae_emotions.get_hidden(self.x)) f = theano.function([self.x, self.y], hardmax(o, self.y)) def fix_label(Y, l): return numpy.array(Y - 1, dtype='int32').flatten() data = root(self.jobman.state['dataset'], prepro_fct_x=[], prepro_fct_y=[(fix_label, [])]) X, Y = data.get_train_labeled(shared_array=False) print '--- Logreg Valid Accuracy:', f(X, Y) return True
def evalreg(self, name=""): # Function to evaluate the classification error # when using the output of the logistic regression. # This function is used only for monitoring purposes print '--- Loading data' self.jobman.save() o = self.logrege.get_hidden(self.cae_emotions.get_hidden(self.x)) f = theano.function([self.x, self.y], hardmax(o, self.y)) data = root(self.jobman.state['dataset'], prepro_fct_x=[], prepro_fct_y=[]) X, Y = data.get_train_labeled(shared_array=False) print '--- Logreg Valid Accuracy:', f(X, Y) return True
def svm(self, h, name=""): # Function that trains an SVM on the representation # h given in argument. print "--- Loading data" self.jobman.save() f = theano.function([self.x], [h]) def fix_theano(X, l): return f(X)[0] self.svm_data = root(self.jobman.state["dataset"], prepro_fct_x=[(fix_theano, [])]) res = eval_fold(3, self.svm_data) cPickle.dump(res, open("results.pkl", "w")) self.jobman.state["train_" + name] = res["train"] if self.jobman.state["valid_" + name] <= res["valid"]: self.jobman.state["valid_" + name] = res["valid"] self.jobman.state["test_" + name] = res["test"] self.jobman.save() print name, res return True
def svm(self,h,name=""): # Function that trains an SVM on the representation # h given in argument. print '--- Loading data' self.jobman.save() f = theano.function([self.x],[h]) def fix_theano(X,l): return f(X)[0] self.svm_data = root(self.jobman.state['dataset'],prepro_fct_x=[(fix_theano,[])]) res = eval_fold(3,self.svm_data) cPickle.dump(res,open('results.pkl','w')) self.jobman.state['train_'+name] = res['train'] if self.jobman.state['valid_'+name] <= res['valid']: self.jobman.state['valid_'+name] = res['valid'] self.jobman.state['test_'+name] = res['test'] self.jobman.save() print name,res return True
valid_set_x, valid_set_y = dataset.get_all_valid(shared_array=False) test_set_x, test_set_y = dataset.get_all_test(shared_array=False) train_set_y = train_set_y.flatten() valid_set_y = valid_set_y.flatten() test_set_y = test_set_y.flatten() bestC, res = OptimizeLinearSVM(([train_set_x, train_set_y],\ [valid_set_x, valid_set_y],\ [test_set_x, test_set_y]),\ MAXSTEPS=nsteps) best = { 'C': bestC, 'train': res[bestC]['train_acc'], 'valid': res[bestC]['valid_acc'], 'test': res[bestC]['test_acc'], 'Optimize': res } return best if __name__ == '__main__': data = root( '/scratch/rifaisal/data/paradata/msrparaphrasefullconvl2_files_dict.pkl', prepro_fct_x=[(scale, [])]) print 'Starting evaluation...' eval_fold(nsteps=30, dataset=data) print dic
''' print "Loading data..." train_set_x,train_set_y = dataset.get_all_train_labeled(shared_array=False) valid_set_x,valid_set_y = dataset.get_all_valid(shared_array=False) test_set_x,test_set_y = dataset.get_all_test(shared_array=False) train_set_y = train_set_y.flatten() valid_set_y = valid_set_y.flatten() test_set_y = test_set_y.flatten() bestC, res = OptimizeLinearSVM(([train_set_x, train_set_y],\ [valid_set_x, valid_set_y],\ [test_set_x, test_set_y]),\ MAXSTEPS=nsteps) best = {'C':bestC,'train':res[bestC]['train_acc'], 'valid':res[bestC]['valid_acc'], 'test':res[bestC]['test_acc'], 'Optimize':res} return best if __name__ == '__main__': data = root('/scratch/rifaisal/data/paradata/msrparaphrasefullconvl2_files_dict.pkl',prepro_fct_x=[(scale,[])]) print 'Starting evaluation...' eval_fold(nsteps=30, dataset = data) print dic