Save TensorFlow state (weights) while running

Viewed 52

I've got a python program that is running fitting over a model. I didn't implements a saver so i'm wondering if there's a method to recover the weights directly from memory (maybe from a temp file?) while the fitting is running

1 Answers

I think that should be possible using the command, tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES).

Code for extracting/recovering weights of a model without Saving the Model is shown below.

import tensorflow as tf
import numpy as np

X_ = tf.placeholder(tf.float64, [None, 5], name="Input")
Y_ = tf.placeholder(tf.float64, [None, 1], name="Output")

X = np.random.randint(1,10,[10,5])
Y = np.random.randint(0,2,[10,1])

with tf.variable_scope("LogReg"):
    pred = tf.layers.dense(X_, 1, activation=tf.nn.sigmoid, name = 'fc1')
    loss = tf.losses.mean_squared_error(labels=Y_, predictions=pred)
    training_ops = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

with tf.Session() as sess:

    all_vars= tf.global_variables()
    def get_var(name):
        for i in range(len(all_vars)):
            if all_vars[i].name.startswith(name):
                return all_vars[i]
        return None

    sess.run(tf.global_variables_initializer())    
    for i in range(200):
        sess.run([training_ops], feed_dict={X_: X,Y_: Y})
        Weight_Vars = sess.run([tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)], feed_dict={X_: X,Y_: Y})
        print(Weight_Vars)
Related