def test_graphnet(self, X, Y, coefs): X = simple_normalize(X) accuracies = [] for i, coefset in enumerate(coefs): correct = [] print 'Checking accuracy for test group' if self.problemkey == 'RobustGraphNet': coefset = coefset[:-self.trainX_shape[0]] for trial, outcome in zip(X, Y): predict = trial * coefset #print np.sum(predict) Ypredsign = np.sign(np.sum(predict)) if Ypredsign < 0.: Ypredsign = 0. else: Ypredsign = 1. #print Ypredsign, outcome, (Ypredsign == outcome) correct.append(Ypredsign == outcome) fold_accuracy = np.sum(correct) * 1. / len(correct) print 'coef number:', i print 'fold accuracy: ', fold_accuracy accuracies.append(fold_accuracy) return accuracies
def test_graphnet(self, X, Y, coefs): X = simple_normalize(X) accuracies = [] for i, coefset in enumerate(coefs): correct = [] print 'Checking accuracy for test group' if self.problemkey == 'RobustGraphNet': coefset = coefset[:-self.trainX_shape[0]] for trial, outcome in zip(X, Y): predict = trial*coefset #print np.sum(predict) Ypredsign = np.sign(np.sum(predict)) if Ypredsign < 0.: Ypredsign = 0. else: Ypredsign = 1. #print Ypredsign, outcome, (Ypredsign == outcome) correct.append(Ypredsign == outcome) fold_accuracy = np.sum(correct) * 1. / len(correct) print 'coef number:', i print 'fold accuracy: ', fold_accuracy accuracies.append(fold_accuracy) return accuracies
def train_graphnet(self, X, Y, trial_mask=None, G=None, l1=None, l2=None, l3=None, delta=None, svmdelta=None, initial=None, adaptive=False, svm=False, scipy_compare=False, tol=1e-5, greymatter_mask=None, initial_l1weights=None, use_adj_time=True): if not type(l1) in [list, tuple]: l1 = [l1] X = simple_normalize(X) tic = time.clock() #problemkey = self.regression_type_selector(*[bool(x) for x in [l1, l2, l3, delta, svmdelta]]) problemkey = self.regression_type_selector(l1, l2, l3, delta, svmdelta) self.problemkey = problemkey self.trainX_shape = X.shape if problemkey in ('HuberSVMGraphNet', 'RobustGraphNet', 'NaiveGraphNet'): if G is None: #nx = 60 #ny = 60 #A, Afull = construct_adjacency_list(nx, ny, 1, return_full=True) #A, Afull = self.gen_adj(X.shape[1]) #if greymatter_mask is not None: # A, GMA = prepare_adj(trial_mask, numt=1, gm_mask=greymatter_mask) #else: # A = prepare_adj(trial_mask, numt=1) # GMA = None if use_adj_time: A = prepare_adj(trial_mask, numt=1, gm_mask=greymatter_mask) else: A = prepare_adj(trial_mask, numt=0, gm_mask=greymatter_mask) else: A = G.copy() if initial_l1weights is not None: newl1 = l1 else: newl1 = None if problemkey is 'RobustGraphNet': problemtype = graphnet.RobustGraphNet print 'Robust GraphNet with penalties (l1, l2, l3, delta): ', l1, l2, l3, delta l = cwpath.CoordWise((X, Y, A), problemtype, initial_coefs=initial) #, gma=GMA) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, delta=delta, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'HuberSVMGraphNet': problemtype = graphnet.GraphSVM print 'HuberSVM GraphNet with penalties (l1, l2, l3, delta): ', l1, l2, l3, delta Y = 2 * np.round(np.random.uniform(0, 1, len(Y))) - 1 l = cwpath.CoordWise((X, Y, A), problemtype) #, gma=GMA) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, delta=delta, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'NaiveGraphNet': problemtype = graphnet.NaiveGraphNet print 'Testing GraphNet with penalties (l1, l2, l3): ', l1, l2, l3 l = cwpath.CoordWise((X, Y, A), problemtype, initial_coefs=initial) #, gma=GMA) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'NaiveENet': problemtype = graphnet.NaiveENet print 'Testing ENET with penalties (l1, l2): ', l1, l2 l = cwpath.CoordWise((X, Y), problemtype, initial_coefs=initial) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'Lasso': problemtype = graphnet.Lasso print 'Testing LASSO with penalty (l1): ', l1 l = cwpath.CoordWise((X, Y), problemtype, initial_coefs=initial) l.problem.assign_penalty(path_key='l1', l1=l1, l1weights=initial_l1weights, newl1=newl1) else: print 'Incorrect parameters set (no problem key).' return False # Solve the problem: print 'Solving the problem...' coefficients, residuals = l.fit(tol=tol, initial=initial) self.coefficients = coefficients self.residuals = residuals print '\t---> Fitting problem with coordinate decesnt took: ', time.clock( ) - tic, 'seconds.' if adaptive: tic = time.clock() safety = 1e-5 l1weights = 1. / (self.coefficients[-1] + safety) l = cwpath.CoordWise((X, Y, A), problemtype, initial_coefs=initial) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, delta=delta, l1weights=l1weights, newl1=l1) adaptive_coefficients, adaptive_residuals = l.fit(tol=tol, initial=initial) print '\t---> Fitting Adaptive GraphNet problem with coordinate descent took: ', time.clock( ) - tic, 'seconds.' self.firstpass_coefficients = self.coefficients self.firstpass_residuals = self.residuals self.coefficients = adaptive_coefficients self.residuals = adaptive_residuals ''' if scipy_compare: l1 = l1[-1] beta = self.coefficients[-1] print '\t---> Fitting with scipy for comparison...' tic = time.clock() if problemkey is 'RobustGraphNet': def f(beta): huber_sum = self.huber(Y - np.dot(X, beta), delta).sum()/2 beta_l1 = l1*np.dot(np.fabs(beta), l1weights) beta_l2 = l2*np.linalg.norm(beta)**2/2 beta_l3 = l3*np.dot(beta, np.dot(Afull, beta))/2 return huber_sum + beta_l1 + beta_l2 + beta_l3 elif problemkey is 'HuberSVMGraphNet': Xp2 = np.hstack([np.ones(X.shape[0])[:,np.newaxis], X]) def f(beta): ind = range(1, len(beta)) huber_err_sum = self.huber_svm_error(beta, Y, Xp2, delta).sum() beta_l1 = np.fabs(beta[ind]).sum()*l1 beta_l2 = l2*(np.linalg.norm(beta[ind])**2/2) beta_l3 = l3*(np.dot(beta[ind], np.dot(Afull, beta[ind])))/2 return huber_error_sum + beta_l1 + beta_l2 + beta_l3 elif problemkey is 'NaiveGraphNet': def f(beta): beta_XY = np.linalg.norm(Y - np.dot(X, beta))**2/2 beta_l1 = l1*np.fabs(beta).sum() beta_l2 = l2*np.linalg.norm(beta)**2/2 beta_l3 = l3*np.dot(beta, np.dot(Afull, beta))/2 return beta_XY + beta_l1 + beta_l2 + beta_l3 elif problemkey is 'NaiveENet': def f(beta): beta_XY = np.linalg.norm(Y - np.dot(X, beta))**2/2 beta_l1 = l1*np.fabs(beta).sum() beta_l2 = np.linalg.norm(beta)**2/2 elif problemkey is 'Lasso': def f(beta): beta_XY = np.linalg.norm(Y - np.dot(X, beta))**2/2 beta_l1 = l1*np.fabs(beta).sum() if problemkey is 'HuberSVMGraphNet': v = scipy.optimize.fmin_powell(f, np.zeros(Xp2.shape[1]), ftol=1.0e-14, xtol=1.0e-14, maxfun=100000) else: v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) print '\t---> Fitting GraphNet with scipy took: ', time.clock()-tic, 'seconds.' assert_true(np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < tol) if np.linalg.norm(v) > 1e-8: assert_true(np.linalg.norm(v - beta) / np.linalg.norm(v) < tol) else: assert_true(np.linalg.norm(beta) < 1e-8) print '\t---> Coordinate-wise and Scipy optimization agree.' ''' return self.coefficients
def train_graphnet(self, X, Y, trial_mask=None, G=None, l1=None, l2=None, l3=None, delta=None, svmdelta=None, initial=None, adaptive=False, svm=False, scipy_compare=False, tol=1e-5, greymatter_mask=None, initial_l1weights=None, use_adj_time=True): if not type(l1) in [list, tuple]: l1 = [l1] X = simple_normalize(X) tic = time.clock() #problemkey = self.regression_type_selector(*[bool(x) for x in [l1, l2, l3, delta, svmdelta]]) problemkey = self.regression_type_selector(l1, l2, l3, delta, svmdelta) self.problemkey = problemkey self.trainX_shape = X.shape if problemkey in ('HuberSVMGraphNet', 'RobustGraphNet', 'NaiveGraphNet'): if G is None: #nx = 60 #ny = 60 #A, Afull = construct_adjacency_list(nx, ny, 1, return_full=True) #A, Afull = self.gen_adj(X.shape[1]) #if greymatter_mask is not None: # A, GMA = prepare_adj(trial_mask, numt=1, gm_mask=greymatter_mask) #else: # A = prepare_adj(trial_mask, numt=1) # GMA = None if use_adj_time: A = prepare_adj(trial_mask, numt=1, gm_mask=greymatter_mask) else: A = prepare_adj(trial_mask, numt=0, gm_mask=greymatter_mask) else: A = G.copy() if initial_l1weights is not None: newl1 = l1 else: newl1 = None if problemkey is 'RobustGraphNet': problemtype = graphnet.RobustGraphNet print 'Robust GraphNet with penalties (l1, l2, l3, delta): ', l1, l2, l3, delta l = cwpath.CoordWise((X, Y, A), problemtype, initial_coefs=initial)#, gma=GMA) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, delta=delta, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'HuberSVMGraphNet': problemtype = graphnet.GraphSVM print 'HuberSVM GraphNet with penalties (l1, l2, l3, delta): ', l1, l2, l3, delta Y = 2*np.round(np.random.uniform(0, 1, len(Y)))-1 l = cwpath.CoordWise((X, Y, A), problemtype)#, gma=GMA) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, delta=delta, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'NaiveGraphNet': problemtype = graphnet.NaiveGraphNet print 'Testing GraphNet with penalties (l1, l2, l3): ', l1, l2, l3 l = cwpath.CoordWise((X, Y, A), problemtype, initial_coefs=initial)#, gma=GMA) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'NaiveENet': problemtype = graphnet.NaiveENet print 'Testing ENET with penalties (l1, l2): ', l1, l2 l = cwpath.CoordWise((X, Y), problemtype, initial_coefs=initial) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l1weights=initial_l1weights, newl1=newl1) elif problemkey is 'Lasso': problemtype = graphnet.Lasso print 'Testing LASSO with penalty (l1): ', l1 l = cwpath.CoordWise((X, Y), problemtype, initial_coefs=initial) l.problem.assign_penalty(path_key='l1', l1=l1, l1weights=initial_l1weights, newl1=newl1) else: print 'Incorrect parameters set (no problem key).' return False # Solve the problem: print 'Solving the problem...' coefficients, residuals = l.fit(tol=tol, initial=initial) self.coefficients = coefficients self.residuals = residuals print '\t---> Fitting problem with coordinate decesnt took: ', time.clock()-tic, 'seconds.' if adaptive: tic = time.clock() safety = 1e-5 l1weights = 1./(self.coefficients[-1]+safety) l = cwpath.CoordWise((X, Y, A), problemtype, initial_coefs=initial) l.problem.assign_penalty(path_key='l1', l1=l1, l2=l2, l3=l3, delta=delta, l1weights=l1weights, newl1=l1) adaptive_coefficients, adaptive_residuals = l.fit(tol=tol, initial=initial) print '\t---> Fitting Adaptive GraphNet problem with coordinate descent took: ', time.clock()-tic, 'seconds.' self.firstpass_coefficients = self.coefficients self.firstpass_residuals = self.residuals self.coefficients = adaptive_coefficients self.residuals = adaptive_residuals ''' if scipy_compare: l1 = l1[-1] beta = self.coefficients[-1] print '\t---> Fitting with scipy for comparison...' tic = time.clock() if problemkey is 'RobustGraphNet': def f(beta): huber_sum = self.huber(Y - np.dot(X, beta), delta).sum()/2 beta_l1 = l1*np.dot(np.fabs(beta), l1weights) beta_l2 = l2*np.linalg.norm(beta)**2/2 beta_l3 = l3*np.dot(beta, np.dot(Afull, beta))/2 return huber_sum + beta_l1 + beta_l2 + beta_l3 elif problemkey is 'HuberSVMGraphNet': Xp2 = np.hstack([np.ones(X.shape[0])[:,np.newaxis], X]) def f(beta): ind = range(1, len(beta)) huber_err_sum = self.huber_svm_error(beta, Y, Xp2, delta).sum() beta_l1 = np.fabs(beta[ind]).sum()*l1 beta_l2 = l2*(np.linalg.norm(beta[ind])**2/2) beta_l3 = l3*(np.dot(beta[ind], np.dot(Afull, beta[ind])))/2 return huber_error_sum + beta_l1 + beta_l2 + beta_l3 elif problemkey is 'NaiveGraphNet': def f(beta): beta_XY = np.linalg.norm(Y - np.dot(X, beta))**2/2 beta_l1 = l1*np.fabs(beta).sum() beta_l2 = l2*np.linalg.norm(beta)**2/2 beta_l3 = l3*np.dot(beta, np.dot(Afull, beta))/2 return beta_XY + beta_l1 + beta_l2 + beta_l3 elif problemkey is 'NaiveENet': def f(beta): beta_XY = np.linalg.norm(Y - np.dot(X, beta))**2/2 beta_l1 = l1*np.fabs(beta).sum() beta_l2 = np.linalg.norm(beta)**2/2 elif problemkey is 'Lasso': def f(beta): beta_XY = np.linalg.norm(Y - np.dot(X, beta))**2/2 beta_l1 = l1*np.fabs(beta).sum() if problemkey is 'HuberSVMGraphNet': v = scipy.optimize.fmin_powell(f, np.zeros(Xp2.shape[1]), ftol=1.0e-14, xtol=1.0e-14, maxfun=100000) else: v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) print '\t---> Fitting GraphNet with scipy took: ', time.clock()-tic, 'seconds.' assert_true(np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < tol) if np.linalg.norm(v) > 1e-8: assert_true(np.linalg.norm(v - beta) / np.linalg.norm(v) < tol) else: assert_true(np.linalg.norm(beta) < 1e-8) print '\t---> Coordinate-wise and Scipy optimization agree.' ''' return self.coefficients