load multiple models in Tensorflow

后端 未结 5 1918
北荒
北荒 2021-02-04 09:08

I have written the following convolutional neural network (CNN) class in Tensorflow [I have tried to omit some lines of code for clarity.]

class CNN:
de         


        
5条回答
  •  天命终不由人
    2021-02-04 09:51

    Yes there is. Use separate graphs.

    g1 = tf.Graph()
    g2 = tf.Graph()
    
    with g1.as_default():
        cnn1 = CNN(..., restore_file='snapshot-model1-10000',..........) 
    with g2.as_default():
        cnn2 = CNN(..., restore_file='snapshot-model2-10000',..........)
    

    EDIT:

    If you want them into same graph. You'll have to rename some variables. One idea is have each CNN in separate scope and let saver handle variables in that scope e.g.:

    saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES), scope='model1')
    

    and in cnn wrap all your construction in scope:

    with tf.variable_scope('model1'):
        ...
    

    EDIT2:

    Other idea is renaming variables which saver manages (since I assume you want to use your saved checkpoints without retraining everything. Saving allows different variable names in graph and in checkpoint, have a look at documentation for initialization.

提交回复
热议问题