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

remove useless sigma initialisation in fc_nn + add random features to...

remove useless sigma initialisation in fc_nn + add random features to neural_networks (to be splitted and renamed as tensorflow utils) + add comment to the time_fct
parent 541df1da
No related branches found
No related tags found
No related merge requests found
...@@ -44,9 +44,6 @@ Y_test = Y_test.astype(np.float32) ...@@ -44,9 +44,6 @@ Y_test = Y_test.astype(np.float32)
def main(): def main():
SIGMA = 5.0
print("Sigma = {}".format(SIGMA))
with tf.Graph().as_default(): with tf.Graph().as_default():
input_dim, output_dim = X_train.shape[1], Y_train.shape[1] input_dim, output_dim = X_train.shape[1], Y_train.shape[1]
......
...@@ -5,6 +5,7 @@ import numpy as np ...@@ -5,6 +5,7 @@ import numpy as np
# todo peutetre renommer ce module en "tensorflow utils" # todo peutetre renommer ce module en "tensorflow utils"
def weight_variable(shape, trainable=True): def weight_variable(shape, trainable=True):
# initial = tf.truncated_normal(shape, stddev=0.1) # initial = tf.truncated_normal(shape, stddev=0.1)
# return tf.Variable(initial, name="weights", trainable=trainable) # return tf.Variable(initial, name="weights", trainable=trainable)
...@@ -160,3 +161,31 @@ def inference_mnist(x_image, output_dim): ...@@ -160,3 +161,31 @@ def inference_mnist(x_image, output_dim):
out_fc = fc_mnist(h_conv) out_fc = fc_mnist(h_conv)
y_out, keep_prob = classification_mnist(out_fc, output_dim) y_out, keep_prob = classification_mnist(out_fc, output_dim)
return y_out, keep_prob return y_out, keep_prob
# todo verifier que ces random_variable/random_biases sont utilisables
# --- Random Fourier Features --- #
def random_variable(shape, sigma):
W = np.random.normal(size=shape, scale=sigma).astype(np.float32)
return tf.Variable(W, name="random_Weights", trainable=False)
def random_biases(shape):
b = np.random.uniform(0, 2 * np.pi, size=shape).astype(np.float32)
return tf.Variable(b, name="random_biase", trainable=False)
# --- Representation Layer --- #
def random_features(conv_out, sigma):
with tf.name_scope("random_features"):
init_dim = np.prod([s.value for s in conv_out.shape if s.value is not None])
conv_out2 = tf.reshape(conv_out, [-1, init_dim])
W = random_variable((init_dim, init_dim), sigma)
b = random_biases(init_dim)
h1 = tf.matmul(conv_out2, W, name="Wx") + b
h1_cos = tf.cos(h1)
h1_final = tf.scalar_mul(np.sqrt(2.0 / init_dim).astype(np.float32), h1_cos)
return h1_final
\ No newline at end of file
...@@ -2,6 +2,15 @@ import time as t ...@@ -2,6 +2,15 @@ import time as t
def time_fct(fct, *args, n_iter=100, **kwargs): def time_fct(fct, *args, n_iter=100, **kwargs):
"""
Return the average time spent by the function.
:param fct: the actual function to time
:param args: the positional arguments of the function
:param n_iter: number of runs of the function
:param kwargs: the keyword arguments of the function
:return: the average time of execution of the function
"""
time_sum = 0 time_sum = 0
for _ in range(n_iter): for _ in range(n_iter):
start = t.time() start = t.time()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment