Search code examples
pythonmachine-learningneural-networktensorflow

Export weights of neural network using tensorflow


I wrote neural-network using tensorflow tools. everything working and now I want to export the final weights of my neural network to make a single prediction method. How can I do this?


Solution

  • You will need to save your model at the end of training by using the tf.train.Saver class.

    While initializing the Saver object, you will need to pass a list of all the variables you wish to save. The best part is that you can use these saved variables in a different computation graph!

    Create a Saver object by using,

    # Assume you want to save 2 variables `v1` and `v2`
    saver = tf.train.Saver([v1, v2])
    

    Save your variables by using the tf.Session object,

    saver.save(sess, 'filename');
    

    Of course, you can add additional details like global_step.

    You can restore the variables in the future by using the restore() function. The restored variables will be initialized to these values automatically.