Skip to content
Snippets Groups Projects
Commit 516c99fe authored by Luc Giffon's avatar Luc Giffon
Browse files

remove glbal_speed and nystrom_vs_deepstrom because they didn't seem usefull anymore

parent 6166bb9f
No related branches found
No related tags found
No related merge requests found
"""
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 skluc.main.tensorflow_.kernel import tf_rbf_kernel
from skluc.main.tensorflow_.kernel_approximation import nystrom_layer
from skluc.main.tensorflow_.utils import inference_mnist, batch_generator, convolution_mnist, classification_mnist, \
inference_cifar10, convolution_cifar, classification_cifar
import skluc.main.data.mldatasets as dataset
from skluc.main.tensorflow_.kernel_approximation import fastfood_layer
import docopt
from skluc.main.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 = fastfood_layer(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, kernel=tf_rbf_kernel, output_dim=output_nystrom_layer, gamma=gamma)
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.
summary_writer = tf.summary.FileWriter("tb_deepstrom_end_to_end_global_speed")
with tf.Session() as sess:
merged_summary = tf.summary.merge_all()
summary_writer.add_graph(sess.graph)
# 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)
summary_str = sess.run(merged_summary, feed_dict=feed_dict)
summary_writer.add_summary(summary_str, j)
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__':
raise NotImplementedError("Not updated code.")
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
"""
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.
-G gammavalue --gamma-nystrom=gammavalue The gamma value used in nystrom.
-m subsamplesize --subsample-size-nystrom=subsamplesize The subsample size for nystrom.
-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]
"""
import tensorflow as tf
import numpy as np
import skluc.main.data.mldatasets as dataset
from sklearn.kernel_approximation import Nystroem
from sklearn.svm import SVC
from skluc.main.tensorflow_.kernel_approximation import nystrom_layer
from skluc.main.tensorflow_.utils 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})
lst_output = [str(accuracy[0]), str(x_nystrom.shape[0]), str(gamma), str(batch_size), str(num_epoch)]
print(",".join(lst_output))
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)
score = clf.score(X_test_transformed, Y_test)
lst_output = [str(score), str(len(subsample)), str(gamma)]
print(",".join(lst_output))
if __name__ == "__main__":
raise NotImplementedError("Not updated code.")
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(float(arguments["--num-epoch"]))
batch_size = int(arguments["--batch-size"])
mnist = dataset.MnistDataset()
mnist.load()
mnist.normalize()
np.random.seed(0)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment