diff --git a/main/experiments/__init__.py b/main/experiments/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/main/experiments/build_command_line_array.py b/main/experiments/build_command_line_array.py new file mode 100644 index 0000000000000000000000000000000000000000..607460da9872e54cca5ee8d87e61ffd942061cfe --- /dev/null +++ b/main/experiments/build_command_line_array.py @@ -0,0 +1,31 @@ +import numpy as np +import math +if __name__ == '__main__': + batch_size = np.logspace(3, 9, dtype=int, base=2, num=5) + num_epoch = 50 + sigma = 5.0 + gamma = 0.003 + subsample_size = np.logspace(3, 9, dtype=int, base=2, num=5) + networks_types = ["dense", "deepfriedconvnet", "deepstorm"] + datasets = ["mnist", + "cifar"] + output_dense = 2048 + nys_layer_dim_out = np.logspace(3, int(math.log(output_dense, 2)), dtype=int, base=2, num=5) + for d in datasets: + for network in networks_types: + s_network = network + " " + "--" + str(d) + " " + "--time" + " " + "--test" + " " + "-e" + " " + str(num_epoch) + " " + for b_size in batch_size: + s_epoch = s_network + "-s " + str(b_size) + " " + if network == "deepfriedconvnet": + s_sigma = s_epoch + "-S " + str(sigma) + " " + print(s_sigma) + elif network == "deepstorm": + s_gamma = s_epoch + "-G " + str(gamma) + " " + for nys_size in subsample_size: + s_nys = s_gamma + "-m " + str(nys_size) + " " + for nys_output_size in nys_layer_dim_out: + s_nys_out = s_nys + "-w" + " " + str(nys_output_size) + print(s_nys_out) + else: + print(s_epoch) + pass \ No newline at end of file diff --git a/main/experiments/draw_graphes.py b/main/experiments/draw_graphes.py new file mode 100644 index 0000000000000000000000000000000000000000..4da631c0f72b9163c10114c25b57c65e823e8818 --- /dev/null +++ b/main/experiments/draw_graphes.py @@ -0,0 +1,107 @@ +import matplotlib.pyplot as plt +import numpy as np +import os +import pandas as pd + +pd.set_option('display.width', 1000) + +DIRNAME = "/home/luc/PycharmProjects/deepFriedConvnets/main/results_global5" +FILENAME = "gathered_results" + +batch_size = np.logspace(3, 9, dtype=int, base=2, num=5) +subsample_size = np.logspace(3, 9, dtype=int, base=2, num=5) + +if __name__ == '__main__': + filepath = os.path.join(DIRNAME, FILENAME) + field_names = ["method_name", + "dataset", + "accuracy", + "runtime", + "number_epoch", + "batch_size", + "sigma_deepstrom", + "gamma_deepfried", + "subsample_size", + "deepstrom_dim"] + + df = pd.read_csv(filepath, names=field_names) + df = df[df["dataset"] == "mnist"] + + nrows = 3 + ncols = 2 + f, axxarr = plt.subplots(nrows, ncols) + f2, axxarr2 = plt.subplots(nrows, ncols) + st = f.suptitle("Accuracy by Runtime", y=1) + st2 = f2.suptitle("Accuracy by Nystrom Size", y=1) + curr_batch_size_idx = 0 + for i in range(nrows): + for j in range(ncols): + try: + curr_batch_size = batch_size[curr_batch_size_idx] + except IndexError: + break + df_batch_size = df[df["batch_size"] == curr_batch_size] + df_batch_size_deepstrom = df_batch_size[df_batch_size["method_name"] == "Deepstrom"] + df_batch_size_deepstrom["subsample_size"] = df_batch_size_deepstrom["subsample_size"].astype(np.int) + df_batch_size_deepstrom["deepstrom_dim"] = df_batch_size_deepstrom["deepstrom_dim"].astype(np.int) + df_batch_size_dense = df_batch_size[df_batch_size["method_name"] == "Dense"] + df_batch_size_deepfriedconvnet = df_batch_size[df_batch_size["method_name"] == "DeepFriedConvnet"] + df_batch_size_deepstrom_runtime_sort = df_batch_size_deepstrom.sort_values(by=["runtime"]) + axxarr[i][j].set_title("batch size = {}".format(curr_batch_size)) + axxarr[i][j].plot(df_batch_size_deepstrom_runtime_sort["runtime"], + df_batch_size_deepstrom_runtime_sort["accuracy"], + label="Deepstrom") + axxarr[i][j].scatter(df_batch_size_dense["runtime"], + df_batch_size_dense["accuracy"], color="r", + label="Dense", + marker="x") + axxarr[i][j].scatter(df_batch_size_deepfriedconvnet["runtime"], + df_batch_size_deepfriedconvnet["accuracy"], color="g", + label="DeepFriedConvnet", + marker="x") + axxarr[i][j].legend(loc="lower right") + + curr_batch_size_idx += 1 + + # various subsample size + df_batch_size_deepstrom_subsample_sort = df_batch_size_deepstrom.sort_values(by=["subsample_size"]) + # print(df_batch_size_deepstrom) + # print(df_batch_size_deepstrom_subsample_sort) + axxarr2[i][j].set_title("batch size = {}".format(curr_batch_size)) + axxarr2[i][j].plot(df_batch_size_deepstrom_subsample_sort["subsample_size"], + df_batch_size_deepstrom_subsample_sort["accuracy"], + label="Deepstrom") + nb_val = len(df_batch_size_deepstrom_subsample_sort["subsample_size"]) + axxarr2[i][j].plot(df_batch_size_deepstrom_subsample_sort["subsample_size"], + [df_batch_size_dense["accuracy"].values[0] for _ in range(nb_val)], color="r", + label="Dense".format(curr_batch_size)) + axxarr2[i][j].plot(df_batch_size_deepstrom_subsample_sort["subsample_size"], + [df_batch_size_deepfriedconvnet["accuracy"].values[0] for _ in range(nb_val)], color="g", + label="DeepfriedConvnet".format(curr_batch_size)) + # axxarr2[i][j].legend(loc="lower right") + + f3, axxarr3 = plt.subplots(len(subsample_size)) + st3 = f3.suptitle("Accuracy by Representation dim for batch size = {}".format(curr_batch_size), y=1) + for k, nys_dim in enumerate(subsample_size): + df_batch_size_deepstrom_nys_dim = df_batch_size_deepstrom[df_batch_size_deepstrom["subsample_size"] == nys_dim] + df_batch_size_deepstrom_nys_dim_sort = df_batch_size_deepstrom_nys_dim.sort_values(by=["deepstrom_dim"]) + axxarr3[k].plot(df_batch_size_deepstrom_nys_dim_sort["deepstrom_dim"], + df_batch_size_deepstrom_nys_dim_sort["accuracy"], + label="Deepstrom") + nb_val = len(df_batch_size_deepstrom_nys_dim_sort["deepstrom_dim"]) + axxarr3[k].plot(df_batch_size_deepstrom_nys_dim_sort["deepstrom_dim"], + [df_batch_size_dense["accuracy"].values[0] for _ in range(nb_val)], color="r", + label="Dense".format(curr_batch_size)) + axxarr3[k].plot(df_batch_size_deepstrom_nys_dim_sort["deepstrom_dim"], + [df_batch_size_deepfriedconvnet["accuracy"].values[0] for _ in range(nb_val)], color="g", + label="DeepFriedConvnet".format(curr_batch_size)) + axxarr3[k].set_title("Subsample size = {}".format(nys_dim)) + # print(df_batch_size_deepstrom_subsample_sort) + f3.tight_layout() + f3.show() + + f.tight_layout() + f.show() + f2.tight_layout() + f2.show() + # print(df) diff --git a/main/experiments/gather_results.py b/main/experiments/gather_results.py new file mode 100644 index 0000000000000000000000000000000000000000..b041736d7e2892ca20d41e5c76c571ae17c6fa05 --- /dev/null +++ b/main/experiments/gather_results.py @@ -0,0 +1,21 @@ +from os import listdir +import os +from os.path import isfile, join + + +if __name__ == '__main__': + mypath = "/home/luc/PycharmProjects/deepFriedConvnets/main/results_global5" + onlyfiles = [os.path.join(mypath, f) for f in listdir(mypath) if isfile(join(mypath, f))] + count = 0 + results = [] + for f_name in onlyfiles: + if "stderr" in f_name: + continue + with open(f_name, "r") as f: + str_f = f.read().strip() + results.append(str_f) + + with open(os.path.join(mypath, "gathered_results"), 'w') as f_w: + for s in results: + f_w.write(s) + f_w.write("\n") diff --git a/main/experiments/global_speed.py b/main/experiments/global_speed.py new file mode 100644 index 0000000000000000000000000000000000000000..127b7802c096d9a33328197587f23fd855c50897 --- /dev/null +++ b/main/experiments/global_speed.py @@ -0,0 +1,377 @@ +""" +global_speed: Compute the speed of a full network training. + +The accuracy performance can also be computed. + +Usage: + global_speed (dense|deepfriedconvnet|deepstorm) [--mnist | --cifar] [-e numepoch -s batchsize] [-T] [-t] [-c] [-S sigmavalue -f stacknumber] [-G gammavalue -m subsamplesize -w dimnystrom] + global_speed -h | --help + +Options: + -h --help Show this screen. + --mnist Use mnist dataset + --cifar Use cifar10 dataset + -e numepoch --num-epoch=numepoch The number of epoch. [default: 1] + -s batchsize --batch-size=batchsize The number of example in each batch [default: 50] + -c --cycling Cycle (loop) over the dataset to have always batch of same size. + -S sigmavalue --sigma-fastfood=sigmavalue The sigma value used in fastfood. + -f stacknumber --fastfood-stack-number=stacknumber The number of fastfood stacks. [default: 1] + -G gammavalue --gamma-nystrom=gammavalue The gamma value used in nystrom. + -m subsamplesize --subsample-size-nystrom=subsamplesize The subsample size for nystrom. + -w dimnystrom --output-dim-nystrom=dimnystrom The size of the representation space. + -T --time Time the execution of the network. + -t --test Run network against test sample and show test accuracy. +""" +import tensorflow as tf +import numpy as np +from pprint import pprint + +from nystrom.nystrom_approx import nystrom_layer +from skluc.neural_networks import inference_mnist, batch_generator, convolution_mnist, classification_mnist, \ + inference_cifar10, convolution_cifar, classification_cifar +import skluc.mldatasets as dataset + +from fasfood_layer import fast_food + +import docopt + +from skluc.utils import time_fct + +np.set_printoptions(threshold=np.nan) +tf.logging.set_verbosity(tf.logging.ERROR) + + +def fct_dense(X_train, Y_train, data_shape, output_dim, batch_size, dataname, num_epoch, dataset_cycling, + X_test=None, Y_test=None): + with tf.Graph().as_default(): + + x = tf.placeholder(tf.float32, shape=[None, *data_shape], name="x") + y_ = tf.placeholder(tf.float32, shape=[None, output_dim], name="labels") + + x_image = x + if dataname == "mnist": + inference = inference_mnist + elif dataname == "cifar": + inference = inference_cifar10 + else: + exit("No valid dataname provided") + + y_conv, keep_prob = inference(x_image, output_dim) + + # calcul de la loss + with tf.name_scope("xent"): + cross_entropy = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv, name="xentropy"), + name="xentropy_mean") + + # calcul du gradient + with tf.name_scope("train"): + global_step = tf.Variable(0, name="global_step", trainable=False) + train_optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy, + global_step=global_step) + + # calcul de l'accuracy + with tf.name_scope("accuracy"): + predictions = tf.argmax(y_conv, 1) + correct_prediction = tf.equal(predictions, tf.argmax(y_, 1)) + accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + + init = tf.global_variables_initializer() + # Create a session for running Ops on the Graph. + with tf.Session() as sess: + # Initialize all Variable objects + sess.run(init) + # actual learning + for i in range(num_epoch): + for X_batch, Y_batch in batch_generator(X_train, Y_train, batch_size, dataset_cycling): + feed_dict = {x: X_batch, y_: Y_batch, keep_prob: 0.5} + sess.run([train_optimizer, cross_entropy], feed_dict=feed_dict) + + accuracy, preds = None, None + if X_test is not None and Y_test is not None: + # testing or predicting may not be wanted + accuracy, preds = sess.run([accuracy_op, predictions], feed_dict={ + x: X_test, y_: Y_test, keep_prob: 1.0}) + return accuracy, preds + + +def fct_deepfriedconvnet(X_train, Y_train, data_shape, output_dim, dataname, batch_size, num_epoch, dataset_cycling, + sigma, fastfood_stack_number, + X_test=None, Y_test=None): + with tf.Graph().as_default(): + x = tf.placeholder(tf.float32, shape=[None, *data_shape], name="x") + y_ = tf.placeholder(tf.float32, shape=[None, output_dim], name="labels") + + x_image = x + if dataname == "mnist": + convolution = convolution_mnist + classification = classification_mnist + elif dataname == "cifar": + convolution = convolution_cifar + classification = classification_cifar + else: + exit("No valid dataname provided") + # Representation layer + + h_conv = convolution(x_image) + out_fc = tf.nn.relu(fast_food(h_conv, sigma, nbr_stack=fastfood_stack_number, trainable=True)) # 84% accuracy (conv) | 59% accuracy (noconv) + # out_fc = fully_connected(h_conv) # 95% accuracy + # out_fc = tf.nn.relu(fast_food(h_conv, SIGMA, nbr_stack=1)) # 83% accuracy (conv) | 56% accuracy (noconv) + # out_fc = tf.nn.relu(fast_food(h_conv, SIGMA, nbr_stack=2)) + # out_fc = tf.nn.relu(fast_food(h_conv, SIGMA, nbr_stack=2, trainable=True)) + # out_fc = fast_food(h_conv, SIGMA, diag=True, trainable=True) # 84% accuracy (conv) | 59% accuracy (noconv) + # out_fc = random_features(h_conv, SIGMA) # 82% accuracy (conv) | 47% accuracy (noconv) + + # classification + y_conv, keep_prob = classification(out_fc, output_dim) + + # calcul de la loss + with tf.name_scope("xent"): + cross_entropy = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv, name="xentropy"), + name="xentropy_mean") + + # calcul du gradient + with tf.name_scope("train"): + global_step = tf.Variable(0, name="global_step", trainable=False) + train_optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy, global_step=global_step) + + # calcul de l'accuracy + with tf.name_scope("accuracy"): + predictions = tf.argmax(y_conv, 1) + correct_prediction = tf.equal(predictions, tf.argmax(y_, 1)) + accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + + init = tf.global_variables_initializer() + # Create a session for running Ops on the Graph. + with tf.Session() as sess: + # Initialize all Variable objects + sess.run(init) + # actual learning + + for i in range(num_epoch): + for X_batch, Y_batch in batch_generator(X_train, Y_train, batch_size, dataset_cycling): + feed_dict = {x: X_batch, y_: Y_batch, keep_prob: 0.5} + sess.run([train_optimizer, cross_entropy], feed_dict=feed_dict) + + accuracy, preds = None, None + if X_test is not None and Y_test is not None: + # testing or predicting may not be wanted + accuracy, preds = sess.run([accuracy_op, predictions], feed_dict={ + x: X_test, y_: Y_test, keep_prob: 1.0}) + return accuracy, preds + + +def fct_deepstrom(X_train, Y_train, X_nystrom, batch_size, + num_epoch, dataset_cycling, + gamma, data_shape, output_dim, dataname, output_nystrom_layer, + X_test=None, Y_test=None): + + with tf.Graph().as_default(): + + x = tf.placeholder(tf.float32, shape=[None, *data_shape], name="x") + y_ = tf.placeholder(tf.float32, shape=[None, output_dim], name="labels") + x_nystrom = tf.Variable(X_nystrom, name="nystrom_subsample", trainable=False) + + x_image = x + if dataname == "mnist": + convolution = convolution_mnist + classification = classification_mnist + elif dataname == "cifar": + convolution = convolution_cifar + classification = classification_cifar + else: + exit("No valid dataname provided") + + # reshape images + x_nystrom_image = x_nystrom + + with tf.variable_scope("convolution_mnist") as scope_conv_mnist: + h_conv = convolution(x_image) + scope_conv_mnist.reuse_variables() + h_conv_nystrom_subsample = convolution(x_nystrom_image, trainable=False) + + out_fc = nystrom_layer(h_conv, h_conv_nystrom_subsample, gamma, output_dim=output_nystrom_layer) + y_conv, keep_prob = classification(out_fc, output_dim=output_dim) + + # # calcul de la loss + with tf.name_scope("xent"): + cross_entropy = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv, name="xentropy"), + name="xentropy_mean") + + # # calcul du gradient + with tf.name_scope("train"): + global_step = tf.Variable(0, name="global_step", trainable=False) + train_optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy, global_step=global_step) + + # # calcul de l'accuracy + with tf.name_scope("accuracy"): + predictions = tf.argmax(y_conv, 1) + correct_prediction = tf.equal(predictions, tf.argmax(y_, 1)) + accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + + init = tf.global_variables_initializer() + # Create a session for running Ops on the Graph. + with tf.Session() as sess: + # Initialize all Variable objects + sess.run(init) + # actual learning + # todo l'hyper parametre du keep prob est ecrit en dur de partout + for i in range(num_epoch): + for X_batch, Y_batch in batch_generator(X_train, Y_train, batch_size, dataset_cycling): + feed_dict = {x: X_batch, y_: Y_batch, keep_prob: 0.5} + sess.run([train_optimizer, cross_entropy], feed_dict=feed_dict) + + accuracy, preds = None, None + if X_test is not None and Y_test is not None: + # testing or predicting may not be wanted + accuracy, preds = sess.run([accuracy_op, predictions], feed_dict={ + x: X_test, y_: Y_test, keep_prob: 1.0}) + return accuracy, preds + + +def display(acc, average_time, dict_options, name): + """ + Print output as csv file + :param acc: + :param average_time: + :param command_line: + :param name: + :return: + """ + list_csv_output = list() + list_csv_output.append(str(name)) + list_csv_output.append("mnist" if dict_options["--mnist"] else "cifar") + list_csv_output.append(str(acc)) + list_csv_output.append(str(average_time)) + list_csv_output.append(str(dict_options["--num-epoch"])) + list_csv_output.append(str(dict_options["--batch-size"])) + list_csv_output.append(str(dict_options["--gamma-nystrom"])) + list_csv_output.append(str(dict_options["--sigma-fastfood"])) + list_csv_output.append(str(dict_options["--subsample-size-nystrom"])) + list_csv_output.append(str(dict_options["--output-dim-nystrom"])) + print(",".join(list_csv_output)) + + +def get_head(preds, Y_test, size): + preds = preds[:size] + exp = np.argmax(Y_test[:size], axis=1) + return preds, exp + + +if __name__ == '__main__': + arguments = docopt.docopt(__doc__) + # pprint(arguments) + NUM_EPOCH = int(arguments["--num-epoch"]) + BATCH_SIZE = int(arguments["--batch-size"]) + CYCLE = arguments["--cycling"] + if arguments["--mnist"]: + data = dataset.MnistDataset() + dataname = "mnist" + elif arguments["--cifar"]: + data = dataset.Cifar10Dataset() + dataname = "cifar" + else: + exit("No dataset provided") + data.load() + data.to_image() + data.to_one_hot() + data.normalize() + data.data_astype(np.float32) + data.labels_astype(np.float32) + X_train, Y_train = data.train + X_test, Y_test = data.test + + if arguments["dense"]: + fct_args = { + "X_train": X_train, + "Y_train": Y_train, + "data_shape": (data.HEIGHT, data.WIDTH, data.DEPTH), + "output_dim": len(Y_train[0]), + "dataname": dataname, + "batch_size": BATCH_SIZE, + "num_epoch": NUM_EPOCH, + "X_test": X_test, + "Y_test": Y_test, + "dataset_cycling": CYCLE + } + NAME = "Dense" + + average_time = None + if arguments["--time"]: + average_time = time_fct(fct_dense, n_iter=1, **fct_args) + + acc, preds = None, None + if arguments["--test"]: + fct_args.update( + X_test=X_test, + Y_test=Y_test + ) + acc, preds = fct_dense(**fct_args) + display(acc, average_time, arguments, NAME) + + elif arguments["deepfriedconvnet"]: + SIGMA = float(arguments["--sigma-fastfood"]) + FASTFOOD_STACK_NUMBER = int(arguments["--fastfood-stack-number"]) + NAME = "DeepFriedConvnet" + fct_args = {"X_train": X_train, + "Y_train": Y_train, + "data_shape": (data.HEIGHT, data.WIDTH, data.DEPTH), + "output_dim": len(Y_train[0]), + "dataname": dataname, + "sigma": SIGMA, + "fastfood_stack_number": FASTFOOD_STACK_NUMBER, + "batch_size": BATCH_SIZE, + "num_epoch": NUM_EPOCH, + "dataset_cycling": CYCLE + } + + average_time = None + if arguments["--time"]: + average_time = time_fct(fct_deepfriedconvnet, n_iter=1, **fct_args) + + acc, preds = None, None + if arguments["--test"]: + fct_args.update( + X_test=X_test, + Y_test=Y_test + ) + acc, preds = fct_deepfriedconvnet(**fct_args) + display(acc, average_time, arguments, NAME) + + elif arguments["deepstorm"]: + GAMMA = float(arguments["--gamma-nystrom"]) + NAME = "Deepstrom" + NYSTROM_SAMPLE_SIZE = int(arguments["--subsample-size-nystrom"]) + X_nystrom = X_train[np.random.permutation(NYSTROM_SAMPLE_SIZE)] + output_dim_nystrom = int(arguments["--output-dim-nystrom"]) + fct_args = { + "X_train": X_train, + "Y_train": Y_train, + "data_shape": (data.HEIGHT, data.WIDTH, data.DEPTH), + "output_dim": len(Y_train[0]), + "dataname": dataname, + "X_nystrom": X_nystrom, + "gamma": GAMMA, + "batch_size": BATCH_SIZE, + "num_epoch": NUM_EPOCH, + "dataset_cycling": CYCLE, + "output_nystrom_layer": output_dim_nystrom + } + + average_time = None + if arguments["--time"]: + average_time = time_fct(fct_deepstrom, n_iter=1, **fct_args) + + acc, preds = None, None + if arguments["--test"]: + fct_args.update( + X_test=X_test, + Y_test=Y_test + ) + acc, preds = fct_deepstrom(**fct_args) + display(acc, average_time, arguments, NAME) + + else: + raise ValueError diff --git a/main/experiments/nystrom_vs_deepstrom.py b/main/experiments/nystrom_vs_deepstrom.py new file mode 100644 index 0000000000000000000000000000000000000000..e00572d743ef2fbe1bf83f9e5f2ec1379349ea7c --- /dev/null +++ b/main/experiments/nystrom_vs_deepstrom.py @@ -0,0 +1,138 @@ +""" +nystrom_vs_deepstrom: Compute accuracy efficiency of the nystrom method vs deepstrom. + +Usage: + nystrom_vs_deepstrom (--nystroem | --deepstrom) [-e numepoch -s batchsize -G gammavalue -m subsamplesize] + nystrom_vs_deepstrom -h | --help + +Options: + -h --help Show this screen. + --nystroem Run the nystroem version. + --deepstrom Run the deepstrom version. + -e numepoch --num-epoch=numepoch The number of epoch. [default: 1] + -s batchsize --batch-size=batchsize The number of example in each batch [default: 50] + -G gammavalue --gamma-nystrom=gammavalue The gamma value used in nystrom. + -m subsamplesize --subsample-size-nystrom=subsamplesize The subsample size for nystrom. +""" + +import tensorflow as tf +import numpy as np +import skluc.mldatasets as dataset +from sklearn.kernel_approximation import Nystroem +from sklearn.svm import SVC + +from nystrom.nystrom_approx import nystrom_layer +from skluc.neural_networks import batch_generator, classification_mnist + +tf.logging.set_verbosity(tf.logging.ERROR) + +import docopt + + +def deepstrom_classif(X_train, Y_train, X_nystrom, batch_size, + num_epoch, dataset_cycling, + gamma, data_shape, output_dim, output_nystrom_layer, + X_test=None, Y_test=None): + + with tf.Graph().as_default(): + + x = tf.placeholder(tf.float32, shape=[None, *data_shape], name="x") + y_ = tf.placeholder(tf.float32, shape=[None, output_dim], name="labels") + x_nystrom = tf.Variable(X_nystrom, name="nystrom_subsample", trainable=False) + + out_fc = nystrom_layer(x, x_nystrom, gamma, output_dim=output_nystrom_layer) + y_conv, keep_prob = classification_mnist(out_fc, output_dim=output_dim) + + # # calcul de la loss + with tf.name_scope("xent"): + cross_entropy = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv, name="xentropy"), + name="xentropy_mean") + + # # calcul du gradient + with tf.name_scope("train"): + global_step = tf.Variable(0, name="global_step", trainable=False) + train_optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy, + global_step=global_step) + + # # calcul de l'accuracy + with tf.name_scope("accuracy"): + predictions = tf.argmax(y_conv, 1) + correct_prediction = tf.equal(predictions, tf.argmax(y_, 1)) + accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + + init = tf.global_variables_initializer() + # Create a session for running Ops on the Graph. + with tf.Session() as sess: + # Initialize all Variable objects + sess.run(init) + # actual learning + for i in range(num_epoch): + for X_batch, Y_batch in batch_generator(X_train, Y_train, batch_size, dataset_cycling): + feed_dict = {x: X_batch, y_: Y_batch, keep_prob: 0.5} + sess.run([train_optimizer, cross_entropy], feed_dict=feed_dict) + + accuracy = None + if X_test is not None and Y_test is not None: + # testing or predicting may not be wanted + accuracy = sess.run([accuracy_op], feed_dict={ + x: X_test, y_: Y_test, keep_prob: 1.0}) + print(accuracy) + + +def nystroem_classif(X_train, Y_train, X_test, Y_test, subsample, gamma): + nys = Nystroem(kernel="rbf", gamma=gamma, n_components=len(subsample)) + nys.fit(subsample) + X_train_transformed = nys.transform(X_train) + X_test_transformed = nys.transform(X_test) + clf = SVC(kernel="linear") + clf.fit(X_train_transformed, Y_train) + print(clf.score(X_test_transformed, Y_test)) + + +if __name__ == "__main__": + arguments = docopt.docopt(__doc__) + print(arguments) + SUBSAMPLE_SIZE = int(arguments["--subsample-size-nystrom"]) + gamma = float(arguments["--gamma-nystrom"]) + nystroem = arguments["--nystroem"] + deepstrom = arguments["--deepstrom"] + num_epoch = int(arguments["--num-epoch"]) + batch_size = int(arguments["--batch-size"]) + + mnist = dataset.MnistDataset() + mnist.load() + mnist.normalize() + indexes_nystrom = np.random.permutation(60000)[:SUBSAMPLE_SIZE] + + if nystroem: + X_train, Y_train = mnist.train + X_test, Y_test = mnist.test + X_subsample = X_train[indexes_nystrom] + nystroem_classif(X_train=X_train, + Y_train=Y_train, + X_test=X_test, + Y_test=Y_test, + subsample=X_subsample, + gamma=gamma) + elif deepstrom: + mnist.to_one_hot() + mnist.data_astype(np.float32) + mnist.labels_astype(np.float32) + X_train, Y_train = mnist.train + X_test, Y_test = mnist.test + X_subsample = X_train[indexes_nystrom] + deepstrom_classif(X_train=X_train, + Y_train=Y_train, + X_test=X_test, + Y_test=Y_test, + gamma=gamma, + data_shape=X_train.shape[1:], + output_dim=Y_train.shape[1], + dataset_cycling=False, + num_epoch=num_epoch, + output_nystrom_layer=SUBSAMPLE_SIZE, + X_nystrom=X_subsample, + batch_size=batch_size) + +