yamini goel
yamini goel

Reputation: 539

Reproduce same results on each run - Keras, Google Colab

I run the following code in Google Colab(with GPU):

import random
random.seed(1)
import numpy as np
from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)
import pandas as pd
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Flatten, Dense, Lambda, SimpleRNN
from keras.optimizers import *
from keras.utils import np_utils
from keras.initializers import *
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, roc_auc_score, auc, precision_recall_curve
from sklearn.metrics import confusion_matrix
from keras.callbacks import EarlyStopping
from keras import backend as K
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)


##Loading dataset train and validation files, the files are same for every run

es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)

print("***********************************************************************************************")

def make_model():
    model = Sequential()        
    model.add(Conv2D(10,(5,5), kernel_initializer=glorot_uniform(seed=1), input_shape = (22,10,1), use_bias = True, activation = "relu", strides = 1, padding = "valid"))
    model.add(MaxPooling2D(pool_size=(2,2)))    
    model.add(Flatten())
    model.add(Dense(20, kernel_initializer=glorot_uniform(seed=1), activation = "relu"))
    model.add(Lambda(lambda x: tf.expand_dims(x, axis=1)))
    model.add(SimpleRNN(20, kernel_initializer=glorot_uniform(seed=1), activation="relu",return_sequences=False))
    model.add(Dense(1, kernel_initializer=glorot_uniform(seed=1), activation="sigmoid"))    
    opti = SGD(lr = 0.01)
    model.compile(loss = "binary_crossentropy", optimizer = opti, metrics = ["accuracy"])

    return model

model = make_model()
model.fit(x_train, y_train, validation_data = (x_validation,y_validation), epochs = 50, batch_size = 20, verbose = 2, callbacks=[es])

Despite setting all seed values, my prediction results of the model are different on subsequent runs. The training and testing of the model happens in the same Colab cell.

Upvotes: 0

Views: 790

Answers (1)

Tarik
Tarik

Reputation: 11209

You are dealing with floating point numbers that are multiplied and added on different threads and can therefore happen in different order. Floating point additions and multiplications are not commutative. See What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Upvotes: 1

Related Questions