Last active
June 14, 2017 08:47
-
-
Save shaybensasson/c7597a5cb4b1c88db68ea894c73d6f49 to your computer and use it in GitHub Desktop.
trying keras to be random seeded (with theano and/or tf)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
trying keras to be random seeded | |
Switch backends by changin ~/.keras/keras.json: | |
Theano: https://drive.google.com/open?id=0B-FcStylmYuVU1E5S25QX2JXcGs | |
TensorFlow: https://drive.google.com/open?id=0B-FcStylmYuVNnpfZERoeUo0QnM | |
Keras Backend: | |
*Theano (1 epoch, GPU): | |
Test loss: 0.111780489241 | |
Test accuracy: 0.9667 | |
Time elpased: 0:00:07.026813 | |
*Tensorflow (1 epoch, CPU): | |
Test loss: 0.797505338073 | |
Test accuracy: 0.7895 | |
Time elpased: 0:00:17.213114 | |
*Tensorflow (1 epoch, CPU): | |
Test loss: 0.797505338073 | |
Test accuracy: 0.7895 | |
Time elpased: 0:00:17.213114 | |
*Tensorflow (1 epoch, GPU): | |
DOES NOT SEEM TO BE DETERMINISTIC | |
Test loss: 0.797505338073 | |
Test accuracy: 0.7895 | |
Time elpased: 0:00:17.213114 | |
Original: | |
Trains a simple deep NN on the MNIST dataset. | |
Gets to 98.40% test accuracy after 20 epochs | |
(there is *a lot* of margin for parameter tuning). | |
2 seconds per epoch on a K520 GPU. | |
''' | |
from __future__ import print_function | |
import time | |
from timeit import default_timer as timer | |
from datetime import timedelta | |
t_start = timer() | |
seed = 7 | |
#import os | |
#os.environ['PYTHONHASHSEED'] = '0' #python 3 | |
import numpy as np | |
import random as rn | |
np.random.seed(seed) | |
rn.seed(seed) | |
from keras import backend as K | |
if (K.backend() == 'tensorflow'): | |
import tensorflow as tf | |
#single thread restriction :( | |
session_conf = tf.ConfigProto( | |
intra_op_parallelism_threads=1, | |
inter_op_parallelism_threads=1) | |
from keras import backend as K | |
tf.set_random_seed(seed) | |
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf) | |
#sess = tf.Session(graph=tf.get_default_graph()) | |
K.set_session(sess) | |
#a = tf.random_uniform([1]) | |
#print("Session 1") | |
#with tf.Session() as sess1: | |
# print(sess1.run(a)) # generates 'A1' | |
# print(sess1.run(a)) # generates 'A2' | |
#tf.reset_default_graph() #essential when executing inside a notebook | |
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout | |
from keras.optimizers import RMSprop | |
batch_size = 128 | |
num_classes = 10 | |
epochs = 1 | |
# the data, shuffled and split between train and test sets | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
x_train = x_train.reshape(60000, 784) | |
x_test = x_test.reshape(10000, 784) | |
x_train = x_train.astype('float32') | |
x_test = x_test.astype('float32') | |
x_train /= 255 | |
x_test /= 255 | |
print(x_train.shape[0], 'train samples') | |
print(x_test.shape[0], 'test samples') | |
# convert class vectors to binary class matrices | |
y_train = keras.utils.to_categorical(y_train, num_classes) | |
y_test = keras.utils.to_categorical(y_test, num_classes) | |
model = Sequential() | |
model.add(Dense(512, activation='relu', input_shape=(784,))) | |
model.add(Dropout(0.2)) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(10, activation='softmax')) | |
model.summary() | |
model.compile(loss='categorical_crossentropy', | |
optimizer=RMSprop(), | |
metrics=['accuracy']) | |
history = model.fit(x_train, y_train, | |
batch_size=batch_size, | |
epochs=epochs, | |
shuffle=True, | |
verbose=0, | |
validation_data=(x_test, y_test)) | |
score = model.evaluate(x_test, y_test, verbose=0) | |
print(); | |
print('Test loss:', score[0]) | |
print('Test accuracy:', score[1]) | |
print('Time elpased: {}'.format(timedelta(seconds=timer()-t_start))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment