Extracting weights values from a tensorflow model

2020-04-10 04:46发布

问题:

I am training a model in tensorflow and I am doing checkpoints for my model. I the Checkpoints directory, I have four files namely,

  • checkpoint
  • model.cpkt-0.data-00000-of-00001
  • model.cpkt-0.index
  • model.cpkt-0.meta

Now I want to extract the weights values for each layer in my graph, how can I do that?

I tried this:

import tensorflow as tf
sess = tf.InteractiveSession()

saver = tf.train.import_meta_graph('model.cpkt-0.meta')
w = saver.restore(sess, 'model.cpkt-0.data-00000-of-00001')

But I am getting the following error:

Unable to open table file ./model.cpkt-0.data-00000-of-00001: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?

回答1:

You are restoring in a wrong way

saver.restore(sess, 'model.cpkt-0')
# get the graph
g = tf.get_default_graph()
w1 = g.get_tensor_by_name('some_variable_name as per your definition in the model')