From 7b308be4e2de373cb4117cdc924e9e6ee19f1d6a Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 7 May 2018 22:24:47 +0200 Subject: [PATCH] Different implementations of different speed --- tensorflow_impl_tests/eager_main.py | 41 +++++++++++++++++++++++++ tensorflow_impl_tests/normal_main.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tensorflow_impl_tests/eager_main.py create mode 100644 tensorflow_impl_tests/normal_main.py diff --git a/tensorflow_impl_tests/eager_main.py b/tensorflow_impl_tests/eager_main.py new file mode 100644 index 0000000..1b58abc --- /dev/null +++ b/tensorflow_impl_tests/eager_main.py @@ -0,0 +1,41 @@ +import time +import numpy as np +import tensorflow as tf + +tf.enable_eager_execution() + + + +output_size = 1 +hidden_size = 40 +input_size = 30 + + + +model = tf.keras.Sequential([ + tf.keras.layers.Dense(40, activation="sigmoid", input_shape=(1,30)), + tf.keras.layers.Dense(1, activation="sigmoid") +]) + +input = [0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0, 0, 0, 1, 0] + +all_input = np.array([input for _ in range(8500)]) + +single_in = np.array(input).reshape(1,-1) + + +start = time.time() + +all_predictions = model.predict_on_batch(all_input) + +print(all_predictions) +print(time.time() - start) + + + +start = time.time() +all_predictions = [model(single_in) for _ in range(8500)] + +print(all_predictions[:10]) +print(time.time() - start) + diff --git a/tensorflow_impl_tests/normal_main.py b/tensorflow_impl_tests/normal_main.py new file mode 100644 index 0000000..acfc044 --- /dev/null +++ b/tensorflow_impl_tests/normal_main.py @@ -0,0 +1,46 @@ +import tensorflow as tf +import numpy as np +import time + +class Everything: + + def __init__(self): + + self.output_size = 1 + self.hidden_size = 40 + self.input_size = 30 + + self.input = tf.placeholder('float', [1, self.input_size]) + + xavier_init = tf.contrib.layers.xavier_initializer() + + + W_1 = tf.get_variable("w_1", (self.input_size, self.hidden_size), + initializer=xavier_init) + W_2 = tf.get_variable("w_2", (self.hidden_size, self.output_size), + initializer=xavier_init) + + b_1 = tf.get_variable("b_1", (self.hidden_size,), + initializer=tf.zeros_initializer) + b_2 = tf.get_variable("b_2", (self.output_size,), + initializer=tf.zeros_initializer) + + value_after_input = tf.sigmoid(tf.matmul(self.input, W_1) + b_1, name='hidden_layer') + + self.value = tf.sigmoid(tf.matmul(value_after_input, W_2) + b_2, name='output_layer') + + def eval(self): + input = np.array([0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0, 0, 0, 1, 0]) + start = time.time() + sess = tf.Session() + sess.run(tf.global_variables_initializer()) + for i in range(8500): + val = sess.run(self.value, feed_dict={self.input: input.reshape(1,-1)}) + print(time.time() - start) + print(val) + + +everything = Everything() +everything.eval() + +