Git Product home page Git Product logo

lstm-reproducible-results's Introduction

LSTM-Reproducible-results

How to get reproducible result when running Keras with Tensorflow backend


When you are running your code with CPU, the 'Failed 2' case will solve your reproducible problem.
But the real problem is running with GPU.
Because of the randomness in GPU proceeding, we must handle multi threads with one thread and limit the CUDNN using.
I've tried so many codes to make my LSTM model can produce same results.
And Finally I got the answer! So I want to share the way with a clear documet!

Failed 1

Using theano backend I've changed backend with theano.

.keras/keras.jason ->

{
  "epsilon": 1e-07,
  "floatx": "float32",
  "backend": "tensorflow",    <---- change to "theano"
  "image_data_format": "channels_last"
}

and implement python with below code. ( in my case, I used mnist_cnn.py )

THEANO_FLAGS="dnn.conv.algo_bwd_filter=deterministic,dnn.conv.algo_bwd_data=deterministic" python mnist_cnn.py

but the results are all different every trying.


Failed 2

set numpy random seed

random_seed=2017 
from numpy.random import seed 
seed(random_seed)

Failed 3

set tensorflow random seed

from tensorflow import set_random_seed
set_random_seed(random_seed)

Failed 4

set build-in random seed

import random
random.seed(random_seed)

Failed 5

set PYTHONHASHSEED

import os
os.environ['PYTHONHASHSEED'] = '0'

Solve the randomness of LSTM results.

from __future__ import print_function
from numpy.random import seed
import random
import tensorflow as tf

import os
os.environ['PYTHONHASHSEED'] = '0'
seed(42)
random.seed(12345)
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)

from keras import backend as K
tf.set_random_seed(1234)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)

and write your codes which you just wrote. in my case~

model = Sequential()
model.add(LSTM(rnn_width, stateful=False, return_sequences=True,
                        input_shape=(X.shape[1], X.shape[2])))
model.add(Dropout(rnn_dropout))
model.add(AttentionDecoder(rnn_width, n_lotto_numbers))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])

Cautions : You must follow the order of above code. We must set numpy.random.seed before import keras and the order of the other codes are same! My writings are not exactly right, because I learned above informations in stackflows, git questions... I hope this code will help your reproducible problem!

lstm-reproducible-results's People

Contributors

hanseyeong avatar

Watchers

James Cloos avatar

Forkers

newhotter

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.