Structuring a Keras project to achieve reproducible results in GPU

后端 未结 2 1608
一个人的身影
一个人的身影 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:47

    Keras + Tensorflow.

    Step 1, disable GPU.

    import os
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = ""
    

    Step 2, seed those libraries which are included in your code, say "tensorflow, numpy, random".

    import tensorflow as tf
    import numpy as np
    import random as rn
    
    sd = 1 # Here sd means seed.
    np.random.seed(sd)
    rn.seed(sd)
    os.environ['PYTHONHASHSEED']=str(sd)
    
    from keras import backend as K
    config = tf.ConfigProto(intra_op_parallelism_threads=1,inter_op_parallelism_threads=1)
    tf.set_random_seed(sd)
    sess = tf.Session(graph=tf.get_default_graph(), config=config)
    K.set_session(sess)
    

    Make sure these two pieces of code are included at the start of your code, then the result will be reproducible.

提交回复
热议问题