Why am I getting different outputs for the same dataset?

This code is to try to predict the future price of a cryptocurrency. When I feed it data, it outputs different things every time. Why is this?

Link to full code: https://pastebin.com/cEfDCL8H

This code outputs what seems random, and I can't figure out why.

x,y = preprocess_df(test_df)

model = tf.keras.models.load_model('models/RNN_Final-15-0.972.model')

prediction = model.predict(x)

print("15 Min Prediction(0): " + str(CATEGORIES[np.argmax(prediction[0])]))

Upvotes: 0

Views: 282

Answers (1)

programandoconro
programandoconro

Reputation: 2709

While Neural networks initialization, random weights are assigned. This produces differences in the final output. To avoid it, you can use a random seed so every time the same random weights are applied.

For example: You need to set the seed in all your needed variables, as explained here:

# Set a seed value
seed_value= 12321 
import os

# 1. Set `PYTHONHASHSEED` environment variable at a fixed value
os.environ['PYTHONHASHSEED']=str(seed_value)

# 2. Set `python` built-in pseudo-random generator at a fixed value
import random
random.seed(seed_value)

# 3. Set `numpy` pseudo-random generator at a fixed value
import numpy as np
np.random.seed(seed_value)

# 4. Set `tensorflow` pseudo-random generator at a fixed value
import tensorflow as tf
tf.set_random_seed(seed_value)

# 5. For layers that introduce randomness like dropout, make sure to set seed values 

model.add(Dropout(0.25, seed=seed_value))

Upvotes: 1

Related Questions