Structuring a Keras project to achieve reproducible results in GPU

后端 未结 2 1595
一个人的身影
一个人的身影 2020-12-20 04:13

I am writing a tensorflow.Keras wrapper to perform ML experiments.

I need my framework to be able to perform an experiment as specified in a configuration yaml file

2条回答
  •  囚心锁ツ
    2020-12-20 04:39

    Try adding seed parameters to weights/biases initializers. Just to add more specifics to Alexander Ejbekov's comment.

    Tensorflow has two random seeds graph level and op level. If you're using more than one graph, you need to specify seed in every one. You can override graph level seed with op level, by setting seed parameter within function. And you can make two functions even from different graphs output same value if same seed is set. Consider this example:

    g1 = tf.Graph()
    with g1.as_default():
        tf.set_random_seed(1)
        a = tf.get_variable('a', shape=(1,), initializer=tf.keras.initializers.glorot_normal())
        b = tf.get_variable('b', shape=(1,), initializer=tf.keras.initializers.glorot_normal(seed=2))
    with tf.Session(graph=g1) as sess:
        sess.run(tf.global_variables_initializer())
        print(sess.run(a)) 
        print(sess.run(b))
    g2 = tf.Graph()
    with g2.as_default():
        a1 = tf.get_variable('a1', shape=(1,), initializer=tf.keras.initializers.glorot_normal(seed=1))
    
    with tf.Session(graph=g2) as sess:
        sess.run(tf.global_variables_initializer())
        print(sess.run(a1))
    

    In this example, output of a is the same as a1, but b is different.

提交回复
热议问题