Skip to content

Commit

Permalink
nb iter as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
vzantedeschi committed Feb 27, 2017
1 parent 5e63bc0 commit 203255c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
32 changes: 19 additions & 13 deletions l3svms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NORM = args.norm # default False
LIN = args.linear # default True
PCA_BOOL = args.pca # default False
ITER = args.nb_iterations # default 1

print("learning on {}: {} clusters, {} landmarks".format(DATASET,CLUS,LAND))

Expand All @@ -32,7 +33,7 @@
# load dataset
train_y,train_x,test_y,test_x = load_dataset(DATASET,norm=NORM)
t2 = time.time()
print("dataset loading time:",t2-t1)
print("dataset loading time:",t2-t1,"s")

if PCA_BOOL:
if LAND > train_x.shape[1]:
Expand All @@ -41,8 +42,9 @@
else:
print("random landmarks")

print("--------------------\n")
acc_list = []
for it in range(2):
for it in range(ITER):

t2 = time.time()
if CLUS > 1:
Expand All @@ -55,7 +57,7 @@
train_clusters = None
test_clusters = None
t3 = time.time()
print("clustering time:",t3-t2)
print("clustering time:",t3-t2,"s")

# select landmarks
if PCA_BOOL:
Expand All @@ -66,40 +68,44 @@
u = None

t2 = time.time()
print("landmarks selection time:",t2-t3)
print("landmarks selection time:",t2-t3,"s")
t2 = time.time()

# project data
# tr_x = project(train_x,landmarks,clusters=train_clusters,unit_vectors=u,linear=LIN)
tr_x = parallelized_projection(-1,train_x,landmarks,clusters=train_clusters,unit_vectors=u,linear=LIN)

t3 = time.time()
print("projection time:",t3-t2)
print("projection time:",t3-t2,"s")
t3 = time.time()

# tuning
best_C,_ = train(train_y, tr_x, '-C -s 2 -B 1')
best_C,_ = train(train_y, tr_x, '-C -s 2 -B 1 -q')

t4 = time.time()
print("tuning time:",t4-t3)
print("tuning time:",t4-t3,"s")
# training
m = train(train_y, tr_x, '-c {} -s 2 -B 1'.format(best_C))
assert m.nr_feature == LAND*CLUS
model = train(train_y, tr_x, '-c {} -s 2 -B 1 -q'.format(best_C))
assert model.nr_feature == LAND*CLUS

t5 = time.time()
print("training time:",t5-t4)
print("training time:",t5-t4,"s")

te_x = parallelized_projection(-1,test_x,landmarks,clusters=test_clusters,unit_vectors=u,linear=LIN)
# te_x = project(test_x,landmarks,clusters=test_clusters,unit_vectors=u,linear=LIN)
p_label,p_acc,p_val = predict(test_y, te_x, m)
p_label,p_acc,p_val = predict(test_y, te_x, model)

acc_list.append(p_acc[0])

t6 = time.time()
print("testing time:",t6-t5)
print("testing time:",t6-t5,"s")

print(evaluations(test_y,p_label),t6-t1)
print("iteration {} results: (accuracy,mean squared error,squared correlation coefficient), learning time".format(it))
print(evaluations(test_y,p_label),t6-t2)

print("-------------------\n")

print("Mean accuracy (%), mean stdev (%) over {} iterations:".format(ITER))
try:
print(statistics.mean(acc_list),statistics.stdev(acc_list))
except:
Expand Down
1 change: 1 addition & 0 deletions src/landmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from scipy.sparse import csr_matrix
from sklearn.decomposition import PCA

# ----------------------------------------------------------- LAND SELECTION
def random_landmarks(x,n):
m = x.shape[0]
landmarks = x[random.sample(range(m),min(m,n))]
Expand Down
6 changes: 3 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def load_dense_dataset(dataset_name,norm=False):

# ------------------------------------------------------------------- ARG PARSER

def get_args(prog,dataset_name="svmguide1",nb_clusters=1,nb_landmarks=10,linear=True,pca=False):
def get_args(prog,dataset_name="svmguide1",nb_clusters=1,nb_landmarks=10,linear=True,pca=False,nb_iterations=1):

parser = argparse.ArgumentParser(prog=prog,formatter_class=argparse.ArgumentDefaultsHelpFormatter)

Expand All @@ -176,8 +176,8 @@ def get_args(prog,dataset_name="svmguide1",nb_clusters=1,nb_landmarks=10,linear=
help='number of landmarks')
parser.add_argument("-o", "--normalize", dest='norm', action="store_true",
help='if set, the dataset is normalized')
parser.add_argument("-c", "--centeredk", dest='centered', action="store_true",
help='if set, the centered linear kernel is used instead of the std linear')
parser.add_argument("-i", "--nbiter", type=int, dest='nb_iterations', default=nb_iterations,
help='number of times the learning is repeated')
parser.add_argument("-r", "--rbfk", dest='linear', action="store_false",
help='if set, the rbf kernel is used')
parser.add_argument("-p", "--pca", dest='pca', action="store_true",
Expand Down

0 comments on commit 203255c

Please sign in to comment.