diff --git a/main/timeit.py b/main/timeit.py index 383368066351cde295c312750f037b793bc898c0..f06a7720ae3300162f63bee76b9785eacd3f121a 100644 --- a/main/timeit.py +++ b/main/timeit.py @@ -3,18 +3,17 @@ import numpy as np from sklearn.metrics.pairwise import rbf_kernel import skluc.mldatasets as dataset -from skluc.neural_networks import fully_connected, get_next_batch, tf_op +from skluc.neural_networks import fully_connected, get_next_batch, tf_op, conv_relu_pool from skluc.utils import time_fct -from nystrom.nystrom_approx import tf_rbf_kernel, convolution_mnist -import sklearn as sk +from skluc.nystrom.nystrom_approx import tf_rbf_kernel tf.logging.set_verbosity(tf.logging.ERROR) # Preparing the dataset ######################### mnist = dataset.MnistDataset() -mnist = mnist.load() -X_train, _ = mnist["train"] +mnist.load() +X_train, _ = mnist.train X_train = np.array(X_train / 255) X_train = X_train.astype(np.float32) @@ -24,8 +23,8 @@ X_train = X_train.astype(np.float32) if __name__ == '__main__': input_dim = X_train.shape[1] output_dim_fc = 4096*2 - batch_size = 10 - subsample_size = 100 + batch_size = 500 + subsample_size = 500 X_batch = get_next_batch(X_train, 0, batch_size) X_subsample = get_next_batch(X_train, 0, subsample_size) @@ -40,12 +39,13 @@ if __name__ == '__main__': x_subsample_image = tf.reshape(x_subsample, [subsample_size, side_size, side_size, 1]) # fully connected ops - out_fc_x = fully_connected(x, output_dim_fc, act=tf.nn.relu) - out_fc_subsample = fully_connected(x_subsample, output_dim_fc, act=tf.nn.relu) + out_fc_x = fully_connected(x, output_dim_fc, act=tf.nn.relu, variable_scope="fc_x") + out_fc_subsample = fully_connected(x_subsample, output_dim_fc, act=tf.nn.relu, variable_scope="fc_subsample") # convolution ops - out_conv_x = convolution_mnist(x_image) - out_conv_subsample = convolution_mnist(x_subsample_image) + out_conv_x = conv_relu_pool(x_image, [5, 5, 1, 20], [20], pool_size=3, variable_scope="conv_x") + out_conv_subsample = conv_relu_pool(x_subsample_image, [5, 5, 1, 20], [20], pool_size=3, + variable_scope="conv_subsample") init_dim = np.prod([s.value for s in out_conv_x.shape[1:] if s.value is not None]) x_conv_flat = tf.reshape(out_conv_x, [-1, init_dim]) @@ -66,23 +66,21 @@ if __name__ == '__main__': x, y = sess.run([x_conv_flat, subsample_conv_flat], feed_dict=feed_dict) rbf_kernel(x, y, gamma=0.001) - - # todo regarder le temps de la retro propagation - # todo kernel tensorflow on cpu - # todo kernel tensorflow on gpu - # todo kernel sklearn on cpu - d_time_results = { - "fc_x": time_fct(lambda: tf_op(feed_dict, [out_fc_x])), - "fc_subsample": time_fct(lambda: tf_op(feed_dict, [out_fc_subsample])), - "reshape_x": time_fct(lambda: tf_op(feed_dict, [x_image])), - "reshape_subsample": time_fct(lambda: tf_op(feed_dict, [x_subsample_image])), - "reshape_x + conv_x": time_fct(lambda: tf_op(feed_dict, [out_conv_x])), - "reshape_subsample + conv_subsample": time_fct(lambda: tf_op(feed_dict, [out_fc_subsample])), - "reshape_x + conv_x + reshape_subsample + conv_subsample + kernel_cpu": time_fct(lambda: tf_op(feed_dict, [kernel_cpu])), - "reshape_x + conv_x + reshape_subsample + conv_subsample + kernel_gpu": time_fct(lambda: tf_op(feed_dict, [kernel_gpu])), - "reshape_x + conv_x + reshape_subsample + conv_subsample + kernel_sklearn": time_fct(kernel_sklearn) + "fc_x": lambda: time_fct(lambda: tf_op(feed_dict, [out_fc_x]), n_iter=10), + "fc_subsample": lambda: time_fct(lambda: tf_op(feed_dict, [out_fc_subsample]), n_iter=10), + "reshape_x": lambda: time_fct(lambda: tf_op(feed_dict, [x_image]), n_iter=10), + "reshape_subsample": lambda: time_fct(lambda: tf_op(feed_dict, [x_subsample_image]), n_iter=10), + "reshape_x + conv_x": lambda: time_fct(lambda: tf_op(feed_dict, [out_conv_x]), n_iter=10), + "reshape_subsample + conv_subsample": lambda: time_fct(lambda: tf_op(feed_dict, [out_conv_subsample]), n_iter=10), + "reshape_x + conv_x + reshape_subsample + conv_subsample": lambda: time_fct(lambda: tf_op(feed_dict, [out_conv_x, out_conv_subsample]), n_iter=10), + "reshape_x + conv_x + reshape_subsample + conv_subsample + kernel_cpu": lambda: time_fct(lambda: tf_op(feed_dict, [kernel_cpu]), n_iter=10), + "reshape_x + conv_x + reshape_subsample + conv_subsample + kernel_gpu": lambda: time_fct(lambda: tf_op(feed_dict, [kernel_gpu]), n_iter=10), + "reshape_x + conv_x + reshape_subsample + conv_subsample + kernel_sklearn": lambda: time_fct(kernel_sklearn, n_iter=10) } for key, value in d_time_results.items(): - print("{}:\t{:.4f}".format(key, value)) + print("{}:\t{:.4f}s".format(key, value())) + tf.reset_default_graph() + + # todo renome ce fichier