Reputation: 33
i was working on the following code in tensorflow, and its working fine, but i desided to save my session and restore it in order to predict any test variables, i am not getting any errors but the second code where i restore the session the output is always zero, that mean hidden_1_layer, hidden_2_layer, hidden_3_layer and output_layer are zero and there values are note restored,
i am not able to figure out what i have to change in order to save/restore my session properly
the following are the codes i wrote:
the training code for the nn:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data", one_hot=True)
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 100
x = tf.placeholder('float',[None, 784])
y = tf.placeholder('float')
v1 = tf.Variable(3, name='v1')
v2 = tf.Variable(3, name='v2')
saver = tf.train.Saver()
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes]))}
def neural_network_model(data):
l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']) , hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']) , hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']) , hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples/batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out of', hm_epochs,' loss:',epoch_loss)
saver.save(sess, "C:/Users/jack/Desktop/test/model.ckpt")
correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('Accuracy', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))
train_neural_network(x)
the code i wsed to restore the session and predict a value using the nn i made:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
mnist = input_data.read_data_sets("/tmp/data", one_hot=True)
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 100
x = tf.placeholder('float',[None, 784])
y = tf.placeholder('float')
v1 = tf.Variable(3, name='v1')
saver = tf.train.Saver()
hidden_1_layer = {'weights':tf.Variable(tf.zeros([784, n_nodes_hl1])),
'biases':tf.Variable(tf.zeros([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.zeros([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.zeros([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.zeros([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.zeros([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.zeros([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.zeros([n_classes]))}
def neural_network_model(data):
l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']) , hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']) , hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']) , hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(data):
prediction = neural_network_model(x)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver.restore(sess,"C:/Users/jack/Desktop/test/model.ckpt")
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:data}),1)))
print(result)
train_neural_network([mnist.train.images[2]])
print([mnist.train.labels[2]])
thank you for your help
Upvotes: 1
Views: 324
Reputation: 3570
When tf.train.Saver()
is called it creates a saver for all variables available in the graph so far. Therefore it should be called after the network is defined:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data", one_hot=True)
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 100
x = tf.placeholder('float',[None, 784])
y = tf.placeholder('float')
v1 = tf.Variable(3, name='v1')
v2 = tf.Variable(3, name='v2')
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes]))}
saver = tf.train.Saver()
def neural_network_model(data):
(The same for the restoring)
You can check the variables that will be saved by printing print(saver._var_list)
Upvotes: 2