Close Navigation
Learn more about IBKR accounts
Python: Fixing the Random Seed for Reproducibility

Python: Fixing the Random Seed for Reproducibility

Posted February 7, 2023
Sang-Heon Lee
SHLee AI Financial Model

This post shows how to fix the random seed to get reproducible results with Keras.

Setting random seeds fixed

For an illustration, a simple DNN model is implemented in the Jupyter Notebook using a function for fixing the random seed. The function seed_everything() is borrowed from https://dacon.io/codeshare/2363.

Every time we run this code as a whole, the result is always 96.562645 and is not changed.

# Import library and define functions
 
# In[1]:
_________________________________________________________________
from keras.models import Sequential
from keras.layers import Dense
 
import tensorflow as tf
import numpy as np
import random
import os
 
def seed_everything(seed: int = 42):
    random.seed(seed)
    np.random.seed(seed)
    os.environ["PYTHONHASHSEED"] = str(seed)
    tf.random.set_seed(seed)
_________________________________________________________________
 
# Simple DNN model with a fixed random seed 
# In[2]:
_________________________________________________________________
x = np.array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = np.array([40, 50, 60, 70])
 
seed_everything(1) # fix the random seed
 
model = Sequential()
model.add(Dense(50, activation='relu', input_dim=3))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(x, y, epochs=1000, verbose=0)
 
x_input = np.array([60, 70, 80])
x_input = x_input.reshape((1,3))
pred = model.predict(x_input, verbose=0)
print(pred)
_________________________________________________________________
 
[[96.562645]]

Robustness checks

However, when we reuse and rerun some parts of the above code in the same file, we can encounter some unexpected results as follows.

# rerun model fit
# In[3]:
_________________________________________________________________
model.fit(x, y, epochs=1000, verbose=0)
x_input = np.array([60, 70, 80])
x_input = x_input.reshape((1,3))
pred = model.predict(x_input, verbose=0)
print(pred)
_________________________________________________________________
[[90.000145]]
 
# rerun model.fit with a fixed random seed
# In[4]:
_________________________________________________________________
seed_everything(1)
 
model.fit(x, y, epochs=1000, verbose=0)
x_input = np.array([60, 70, 80])
x_input = x_input.reshape((1,3))
pred = model.predict(x_input, verbose=0)
print(pred)
_________________________________________________________________
[[90.00001]]
 
 
# rerun model.compile & model.fit with a fixed random seed
# In[5]:
_________________________________________________________________
seed_everything(1)
 
model.compile(optimizer='adam', loss='mse')
model.fit(x, y, epochs=1000, verbose=0)
x_input = np.array([60, 70, 80])
x_input = x_input.reshape((1,3))
pred = model.predict(x_input, verbose=0)
print(pred)
_________________________________________________________________
[[89.99999]]
 
# call seed_everything(1) model = Sequential()
# In[6]:
_________________________________________________________________
seed_everything(1)
 
model = Sequential()
model.add(Dense(50, activation='relu', input_dim=3))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(x, y, epochs=1000, verbose=0)
x_input = np.array([60, 70, 80])
x_input = x_input.reshape((1,3))
pred = model.predict(x_input, verbose=0)
print(pred)
_________________________________________________________________
[[96.562645]]

The bottom line is that to reproduce always the same result of the same model several times in one file, seed_everything() function needs to be located before model = Sequential().

Originally posted on SH Fintech Modeling Blog.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.