2

I've built a simple 4 layer Neural Network (2 hidden layers) in TensorFlow. I am not using the built in NN provided by TensorFlow but implementing a basic version of my own. Now, to keep W (weights) and B(biases) Tensor's at one place I built a dictionary of these variables like this:

weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

After I learn these parameters, I want to save them using the Saver object. I tried this:

saver = tf.train.Saver([weights,biases])
save_path = saver.save(sess,"./data/model.ckpt")

But I'm unsuccessful in doing so. The error seen is:

TypeError: unhashable type: 'dict'

Now, one solution is to separate all the variables : h1,h2,out,b1,b2,bias_out (out of biases dictionary) into individual variables and save them but this seems a naive approach. If I later have more variables that need to be clubbed together I'd like to keep it that way, it's more clean and manageable. How can I save the grouped variables together ?

arshellium
  • 215
  • 1
  • 6
  • 17

1 Answers1

2

Tensorflow Saver don't accept list of dict. Maybe you should try to merge your dictionaries first :

parameters = weights.copy()
parameters.update(bias)

Or (with Python 3.5)

parameters = {**weights,**bias}

And after that :

saver = tf.train.Saver(parameters)
save_path = saver.save(sess,"./data/model.ckpt")

An other solution :

saver = tf.train.Saver({name:variable for name,variable in weights.items()+bias.items()})
save_path = saver.save(sess,"./data/model.ckpt")

The last solution may have some problem, like "out" as key in both weights and bias, so it seems only one ["out":variable] will be saved.

Community
  • 1
  • 1
Corentin
  • 201
  • 2
  • 5
  • This is how I verified it: use 'python inspect_checkpoint.py --file_name="path/to/model/file" ' . The output is as follows: (type_of_var1) [shape] Eg. h1 (DT_FLOAT) [784,256] – arshellium Sep 28 '16 at 09:54
  • Finally I used this syntax which was albeit the correct one. saver = tf.train.Saver({**weights,**biases}) Thanks @corentin – arshellium Sep 28 '16 at 09:57