Nasiba
Nasiba

Reputation: 331

How to save weights of training data from MNIST testing on tensorflow for future use?

I am facing problems with saving the training weight (W) for MNIST tensorflow example as described here. MNIST tensorflow. If my understanding is correct, we need the training weight in future for other testing cases (not the MNIST testing case). I tried print W.eval() to get the weight; but it happens to provide me with a zero matrix of 784x10. How can I get the weights in an array form or in .csv format (not .cpkt)?

Upvotes: 5

Views: 1310

Answers (4)

denis
denis

Reputation: 21947

Has anyone used gradientzoo ?

save, version, and share your neural net model weights

(haven't used it myself)

Upvotes: 0

Burak
Burak

Reputation: 61

You can use tf.train.Saver().save to save your model after it has been created. to use it simply run:

   tf.train.Saver().save( SESSION-NAME-HERE , "SAVE-LOCATION-HERE")

At some point after both tf.Session() as SESSION-NAME-HERE: and SESSION-NAME-HERE.run(tf.global_variables_initializer()) have been used (But before you close the session).

To Restore you can use tf.train.Saver().restore() with similar syntax to before:

   tf.train.Saver().restore(SESSION-NAME-HERE , "SAVE-LOCATION-HERE")

Run this After tf.Session() as SESSION-NAME-HERE:. This code restores previous variables used.

For futher information see: https://www.tensorflow.org/how_tos/variables/

Upvotes: 0

gb7688
gb7688

Reputation: 91

I ran into a similar problem. Recording the answer here in case it's your issue as well / for posterity. I was printing W to the console and it looked like it was all zeros:

[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]

But actually, the weights displayed in each column correspond to the first 3 and last 3 pixels in each image (upper left and lower right, I think) ... and there is no color in any MNIST image in those locations. So the weights stay zero.

If you follow Daniel Slater's advice you can see the non-zero weights in the output.

Upvotes: 0

Daniel Slater
Daniel Slater

Reputation: 4143

I've done something similar like so:

weight_values = session.run(W)
numpy.savetxt("myfilename.csv", weight_values, delimiter=",")

Upvotes: 3

Related Questions