Karl
Karl

Reputation: 121

Confused about saving/restoring trained weights and biases in Tensorflow

I am training a convolutional neural network in Tensorflow. My code runs to completion without error. That said, I am having trouble understanding exactly how I can save the weights and biases the NN learns (this is important as I'm training on a server and would like to do easier visualization stuff locally).

I initialize my weights and biases thusly:

weights = {
'wConv1':  tf.Variable(tf.random_normal([5, 5, 1,   3],0,0.25),    name='wC1'),
'wConv2':  tf.Variable(tf.random_normal([5, 5, 3,  32],0,0.25),  name='wC2'),
'wConv3':  tf.Variable(tf.random_normal([5, 5, 32, 64],0,0.25),  name='wC3'),
'wConv4':  tf.Variable(tf.random_normal([5, 5, 64, 128],0,0.25), name='wC4'),
'wConv5':  tf.Variable(tf.random_normal([5, 5, 128, 64],0,0.25), name='wC5'),
'wConv6':  tf.Variable(tf.random_normal([5, 5, 64, 32],0,0.25),  name='wC6'),
'wConv7':  tf.Variable(tf.random_normal([5, 5, 32, 16],0,0.25),  name='wC7'),
'wOUT'  :  tf.Variable(tf.random_normal([5, 5, 16, 1],0,0.25),          name='wCOUT')
}

biases = {
'bConv1': tf.Variable(tf.random_normal([3]),   name='bC1'),
'bConv2': tf.Variable(tf.random_normal([32]),  name='bC2'),
'bConv3': tf.Variable(tf.random_normal([64]),  name='bC3'),
'bConv4': tf.Variable(tf.random_normal([128]), name='bC4'),
'bConv5': tf.Variable(tf.random_normal([64]),  name='bC5'),
'bConv6': tf.Variable(tf.random_normal([32]),  name='bC6'),
'bConv7': tf.Variable(tf.random_normal([16]),  name='bC7'),
'bOUT': tf.Variable(tf.random_normal([1]),     name='bCOUT')
 }

Then, once however-many epochs I run are complete, I save everything using the following:

 saver = tf.train.Saver({"weights": weights, "biases": biases})
 save_path = saver.save(sess, "./output/trained.ckpt")     

Now, on my own machine I have an evaluation script, wherein I try to load the weights:

with sess.as_default():
          saver = tf.train.import_meta_graph('output.ckpt.meta')
          saver.restore(sess,tf.train.latest_checkpoint('./'))
          a= tf.all_variables()
          sess.run(tf.global_variables_initializer())
          b=sess.run(pred,feed_dict={x: input[:,:,:,30,:]})

Now, the issue is, when I load in "a" I get a mess, with what appears to be many copies of my bias and weight variables:

<tf.Variable 'wC1:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC2:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC3:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC4:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC5:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC6:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC7:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wCOUT:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'bC1:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC2:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC3:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC4:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC5:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC6:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC7:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'bCOUT:0' shape=(1,) dtype=float32_ref>,
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>,
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>,
<tf.Variable 'wC1/Adam:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC1/Adam_1:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC2/Adam:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC2/Adam_1:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC3/Adam:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC3/Adam_1:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC4/Adam:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC4/Adam_1:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC5/Adam:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC5/Adam_1:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC6/Adam:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC6/Adam_1:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC7/Adam:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wC7/Adam_1:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wCOUT/Adam:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'wCOUT/Adam_1:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'bC1/Adam:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC1/Adam_1:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC2/Adam:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC2/Adam_1:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC3/Adam:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC3/Adam_1:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC4/Adam:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC4/Adam_1:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC5/Adam:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC5/Adam_1:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC6/Adam:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC6/Adam_1:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC7/Adam:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'bC7/Adam_1:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'bCOUT/Adam:0' shape=(1,) dtype=float32_ref>,
<tf.Variable 'bCOUT/Adam_1:0' shape=(1,) dtype=float32_ref>,
<tf.Variable 'wC1:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC2:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC3:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC4:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC5:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC6:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC7:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wCOUT:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'bC1:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC2:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC3:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC4:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC5:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC6:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC7:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'bCOUT:0' shape=(1,) dtype=float32_ref>,
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>,
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>,
<tf.Variable 'wC1/Adam:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC1/Adam_1:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC2/Adam:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC2/Adam_1:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC3/Adam:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC3/Adam_1:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC4/Adam:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC4/Adam_1:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC5/Adam:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC5/Adam_1:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC6/Adam:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC6/Adam_1:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC7/Adam:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wC7/Adam_1:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wCOUT/Adam:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'wCOUT/Adam_1:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'bC1/Adam:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC1/Adam_1:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC2/Adam:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC2/Adam_1:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC3/Adam:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC3/Adam_1:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC4/Adam:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC4/Adam_1:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC5/Adam:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC5/Adam_1:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC6/Adam:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC6/Adam_1:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC7/Adam:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'bC7/Adam_1:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'bCOUT/Adam:0' shape=(1,) dtype=float32_ref>,
<tf.Variable 'bCOUT/Adam_1:0' shape=(1,) dtype=float32_ref>,
<tf.Variable 'wC1:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
<tf.Variable 'wC2:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
<tf.Variable 'wC3:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
<tf.Variable 'wC4:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
<tf.Variable 'wC5:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
<tf.Variable 'wC6:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
<tf.Variable 'wC7:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
<tf.Variable 'wCOUT:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
<tf.Variable 'bC1:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'bC2:0' shape=(32,) dtype=float32_ref>,
<tf.Variable 'bC3:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC4:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'bC5:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'bC6:0' shape=(32,) dtype=float32_ref>,


<tf.Variable 'bC7:0' shape=(16,) dtype=float32_ref>,
 <tf.Variable 'bCOUT:0' shape=(1,) dtype=float32_ref>,
 <tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>,
 <tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>,
 <tf.Variable 'wC1/Adam:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
 <tf.Variable 'wC1/Adam_1:0' shape=(5, 5, 1, 3) dtype=float32_ref>,
 <tf.Variable 'wC2/Adam:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
 <tf.Variable 'wC2/Adam_1:0' shape=(5, 5, 3, 32) dtype=float32_ref>,
 <tf.Variable 'wC3/Adam:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
 <tf.Variable 'wC3/Adam_1:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
 <tf.Variable 'wC4/Adam:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
 <tf.Variable 'wC4/Adam_1:0' shape=(5, 5, 64, 128) dtype=float32_ref>,
 <tf.Variable 'wC5/Adam:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
 <tf.Variable 'wC5/Adam_1:0' shape=(5, 5, 128, 64) dtype=float32_ref>,
 <tf.Variable 'wC6/Adam:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
 <tf.Variable 'wC6/Adam_1:0' shape=(5, 5, 64, 32) dtype=float32_ref>,
 <tf.Variable 'wC7/Adam:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
 <tf.Variable 'wC7/Adam_1:0' shape=(5, 5, 32, 16) dtype=float32_ref>,
 <tf.Variable 'wCOUT/Adam:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
 <tf.Variable 'wCOUT/Adam_1:0' shape=(5, 5, 16, 1) dtype=float32_ref>,
 <tf.Variable 'bC1/Adam:0' shape=(3,) dtype=float32_ref>,
 <tf.Variable 'bC1/Adam_1:0' shape=(3,) dtype=float32_ref>,
 <tf.Variable 'bC2/Adam:0' shape=(32,) dtype=float32_ref>,
 <tf.Variable 'bC2/Adam_1:0' shape=(32,) dtype=float32_ref>,
 <tf.Variable 'bC3/Adam:0' shape=(64,) dtype=float32_ref>,
 <tf.Variable 'bC3/Adam_1:0' shape=(64,) dtype=float32_ref>,
 <tf.Variable 'bC4/Adam:0' shape=(128,) dtype=float32_ref>,
 <tf.Variable 'bC4/Adam_1:0' shape=(128,) dtype=float32_ref>,
 <tf.Variable 'bC5/Adam:0' shape=(64,) dtype=float32_ref>,
 <tf.Variable 'bC5/Adam_1:0' shape=(64,) dtype=float32_ref>,
 <tf.Variable 'bC6/Adam:0' shape=(32,) dtype=float32_ref>,
 <tf.Variable 'bC6/Adam_1:0' shape=(32,) dtype=float32_ref>,
 <tf.Variable 'bC7/Adam:0' shape=(16,) dtype=float32_ref>,
 <tf.Variable 'bC7/Adam_1:0' shape=(16,) dtype=float32_ref>,
 <tf.Variable 'bCOUT/Adam:0' shape=(1,) dtype=float32_ref>,
 <tf.Variable 'bCOUT/Adam_1:0' shape=(1,) dtype=float32_ref>]

My question is, how can I save ONLY the trained weights and biases in Tensorflow and then load them later on for testing purposes?

Upvotes: 1

Views: 1306

Answers (1)

amirbar
amirbar

Reputation: 839

Before answering the exact question, let me first address your concern:

the issue is, when I load in "a" I get a mess, with what appears to be many copies of my bias and weight variables

In your evaluation script you load your training metagraph:

saver = tf.train.import_meta_graph('output.ckpt.meta')

Inside that graph, during training, other than the explicit weight and biases variables you defined, there are variables related to the optimization process (i.e variables with suffix adam or beta1_power). Executing the line specified above, they are defined again in your evaluation script, although may not necessarily required for inference.

An alternative, would be to define the exact graph you want for inference, which may be a bit different from training. In your case - just not defining an optimizer.

Now to address your question:

My question is, how can I save ONLY the trained weights and biases in Tensorflow and then load them later on for testing purposes?

From your code, it seems like your'e essentially doing this. The other variables you see stems from the described above.

One thing to point out - make sure that you don't initialize the variables after restoring them. If you stay with the current code, first initialize and then restore. If you plan to change your inference graph and not include the optimizer you will not need to initialize any variable.

Upvotes: 1

Related Questions