Tensorflow - Reusing model InvalidArgumentError - python

I have issues using an exported tensorflow model. It doesn't allow me to evaluate the dataset I provided it with. If I run the evaluation in the same session as the training, there are no issues, however, that defeats the purpose of saving the model, if I have to retrain my model just to test with another dataset. The python file for generating the model is as such:
x = tf.placeholder(tf.float32, shape=[None, 1024], name = "x")
y_ = tf.placeholder(tf.float32, shape=[None, 10], name = "y_")
#===Model===
#Train
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name= "accuracy")
#Create Saver
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
for i in range(40000):
batch = shvn_data.nextbatch(100)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %f"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
#Save
saver.save(sess,'svhn_model1')
I saved input variables x and y_, to be fed through function 'accuracy', so that I can run accuracy.eval() to obtain the accuracy of prediction. I evaluated the dataset in batches of 100 images, then summed the final prediction. The python file to evaluate the model in another session is as such:
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config = config)
shvn_data = DataLoader()
saver = tf.train.import_meta_graph('svhn_model1.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))
#sess.run(tf.global_variables_initializer())
#Variables to use with model
graph = tf.get_default_graph()
x = graph.get_tensor_by_name("x:0")
y_ = graph.get_tensor_by_name("y_:0")
accuracy = graph.get_tensor_by_name("accuracy:0")
keep_prob = tf.placeholder(tf.float32)
img_whole = np.reshape(shvn_data.test_images,(-1,1024))
batch_whole = np.asarray(shvn_data.test_label.eval(), dtype = np.float32)
total_accuracy = 0
test_count = shvn_data.TEST_COUNT
batch_size = 100
steps = int(math.ceil(test_count/float(batch_size)))
for j in range(steps):
start = j*batch_size
if (j+1)*batch_size > shvn_data.TEST_COUNT:
end = test_count
else:
end = (j+1)*batch_size
img_batch = img_whole[start:end]
label_batch = batch_whole[start:end]
batch_accuracy = accuracy.eval(session = sess, feed_dict={ x: img_batch, y_: label_batch, keep_prob: 1.0}) #ISSUE LIES HERE
print("Test batch %d:%d accuracy %g"%(start,end,batch_accuracy))
total_accuracy += batch_accuracy
print ("Total Accuracy: %f" %(total_accuracy/steps))
The error is as follows.
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
raise type(e)(node_def, op, message)
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Caused by op u'Placeholder', defined at:
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/ipython/start_kernel.py", line 227, in <module>
main()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/ipython/start_kernel.py", line 223, in main
kernel.start()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-33e4fce19d34>", line 1, in <module>
runfile('/home/lwenyao/Desktop/Python/Import_Model.py', wdir='/home/lwenyao/Desktop/Python')
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/lwenyao/Desktop/Python/Import_Model.py", line 63, in <module>
saver = tf.train.import_meta_graph('svhn_model1.meta')
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1595, in import_meta_graph
**kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.py", line 499, in import_scoped_meta_graph
producer_op_list=producer_op_list)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 308, in import_graph_def
op_def=op_def)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
As previously mentioned, evaluation has no issues if I run it in the same session when training the model. The only changes that I made were i added the argument session = sess each time I called .eval() when using the imported model. Sorry for the long post!

Alright, it appears that the error was caused from attempting to create and use another keep_prob variable in the test script, after importing the model. I.e. I created keep_prob = tf.placeholder(tf.float32,) in the training file. However,accuracy.eval() in the testing file was trying to look for keep_prob specifically from the model. I created another keep_prob = tf.placeholder(tf.float32,) in testing file, thinking it would be the same, but it was not.
I modified my code in the training file by adding the label:
keep_prob = tf.placeholder(tf.float32, name="keep_prob")
and in my testing file, called for the model's variable:
#Variables to use with model
graph = tf.get_default_graph()
x = graph.get_tensor_by_name("x:0")
y_ = graph.get_tensor_by_name("y_:0")
keep_prob = graph.get_tensor_by_name("keep_prob:0")#Changed this
accuracy = graph.get_tensor_by_name("accuracy:0")
And now it works fine. My code is modified from Deep MNIST for Experts from tensorflow.

Related

Error "Kernel already used" when using trained RNN to make prediction

Thanks for looking into this question!
I am trying to train an LSTM network which predicts the next 5-day stock prices based on past 30-day stock prices. I have trained the model based on 265 samples. The variables are defined as follow:
# Variables
x = tf.placeholder("float", [265, 30])
y = tf.placeholder("float", [265, 5])
weights = {
'out': tf.Variable(tf.random_normal([n_hidden, y_size]))
}
biases = {
'out': tf.Variable(tf.random_normal([y_size]))
}
and the model looks as below:
# Define RNN architecture
def RNN(x, weights, biases):
x_size = 30
x = tf.reshape(x, [-1, x_size])
x = tf.split(x, x_size, 1)
rnn_cell = rnn.MultiRNNCell([rnn.BasicLSTMCell(n_hidden), rnn.BasicLSTMCell(n_hidden)])
outputs, states = rnn.static_rnn(rnn_cell, x, dtype = tf.float32)
return tf.matmul(outputs[-1], weights['out'] + biases['out'])
Then, I attempt to use the trained model to predict as follow:
y_pred = RNN(x_input, trained_weights, trained_biases)
in which x_input has a dimension (1x30). In gave me a list of error which I could not understand:
ValueError: Variable rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1654, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3290, in create_op
op_def=op_def)
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
Traceback (most recent call last):
File "C:\Users\teh.khoonkheng\Desktop\Others\Personal working folder\14. Projects\1. Oracle\Python\RNN_stock_01.py", line 135, in <module>
y_test = RNN(x_test, trained_weights, trained_biases)
File "C:\Users\teh.khoonkheng\Desktop\Others\Personal working folder\14. Projects\1. Oracle\Python\RNN_stock_01.py", line 81, in RNN
outputs, states = rnn.static_rnn(rnn_cell, x, dtype = tf.float32)
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 1330, in static_rnn
(output, state) = call_cell()
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 1317, in <lambda>
call_cell = lambda: cell(input_, state)
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 191, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\layers\base.py", line 714, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1242, in call
cur_inp, new_state = cell(cur_inp, cur_state)
I was wondering if I have misunderstood how static_rnn works. Have I set up the model incorrectly? And how should I use the trained RNN to make prediction?
Thanks for your help!
Like the error says, you need to mention reuse=True such that the learned states can be used later for prediction. Do this:
rnn_cell = rnn.MultiRNNCell([rnn.BasicLSTMCell(n_hidden,reuse=tf.AUTO_REUSE), rnn.BasicLSTMCell(n_hidden,reuse=tf.AUTO_REUSE)])
Also, this model looks wrong because, while you're using the trained weights and biases, but you're not using the trained LSTMcells. For new inputs, you're defining new LSTMcells.

Tensorflow FailedPreconditionError: Attempting to use uninitialized value beta1_power

I am trying to set up a simple convolutional network in Tensorflow. I'll try to keep the amount of code at a minimum level. Here's the class:
class ConvNet(object):
def __init__(self, input, labels, dataset):
self.input = input
self.true_labels = labels
#'dataset' is an instance of a class that
#I am using to read the training images
self.data = dataset
self.build()
self._optimize = None
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())
def build(self):
self.conv_layers = []
# create 3 conv layers using tf.nn.conv2d
# and append them to self.conv_layers
# create flattening layer using tf.nn.reshape
self.fc_layers = []
# create 2 fully connected layers using tf.matmul
# and append them to self.fc_layers
#property
def optimize(self):
"""Return the optimize operation."""
if not self._optimize:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits = self.fc_layers[-1],
labels = self.true_labels)
cost = tf.reduce_mean(cross_entropy)
self._optimize = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
return self._optimize
def train(self, num_epochs=1, batch_size=16):
epochs_done = 0
while not epochs_done == num_epochs:
# get next batch from training data
x_batch, y_true_batch, _, _ = self.data.train.next_batch(batch_size)
feed_dict_train = {self.input: x_batch, self.true_labels: y_true_batch}
#even if I initialize variables here, same error occurs
#self.sess.run(tf.global_variables_initializer())
self.sess.run(self.optimize, feed_dict=feed_dict_train)
if self.data.train.epochs_done > epochs_done:
#print stuff...
epochs_done += 1
self.sess.close()
I was trying to run the optimize operation to train the network on a simple dataset (e.g. images of cats vs dogs). Here is the main:
if __name__ == "__main__":
classes = ['dogs', 'cats']
num_classes = len(classes)
batch_size = 16
img_size = 128
num_channels = 3
#read training data and resize images to 128 x 128 pixels
data = dataset.read_train_sets(img_size, classes)
x = tf.placeholder(tf.float32, shape = [None, img_size, img_size, num_channels], name='x')
y_true = tf.placeholder(tf.float32, shape = [None, num_classes], name = 'y_true')
net = ConvNet(x, y_true, data)
net.train()
However I keep getting the following error (it's actually much longer than this, I can post it if necessary):
Caused by op 'beta1_power/read', defined at:
File "<string>", line 1, in <module>
File "/usr/lib/python3.5/idlelib/run.py", line 124, in main
ret = method(*args, **kwargs)
File "/usr/lib/python3.5/idlelib/run.py", line 351, in runcode
exec(code, self.locals)
File "/home/gian/face_recognition/model.py", line 366, in <module>
net.train()
File "/home/gian/face_recognition/model.py", line 309, in train
self.sess.run(self.optimize, feed_dict=feed_dict_train)
File "/home/gian/face_recognition/model.py", line 269, in optimize
self._optimize = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 409, in minimize
name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 552, in apply_gradients
self._create_slots([_get_variable_for(v) for v in var_list])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/adam.py", line 124, in _create_slots
colocate_with=first_var)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 665, in _create_non_slot_variable
v = variable_scope.variable(initial_value, name=name, trainable=False)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2157, in variable
use_resource=use_resource)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2147, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2130, in default_variable_creator
constraint=constraint)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 235, in __init__
constraint=constraint)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 391, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 142, in identity
return gen_array_ops.identity(input, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3053, in identity
"Identity", input=input, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1654, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value beta1_power
[[Node: beta1_power/read = Identity[T=DT_FLOAT, _class=["loc:#Variable"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](beta1_power)]]
Can somebody help me figure out where the problem is? Thanks a lot
This error generally occurs when you haven't initialized the optimizer.
So just add self.optimize before you initialize all the global variables.
Your code should look like this.
def __init__(self, input, labels, dataset):
self.input = input
self.true_labels = labels
#'dataset' is an instance of a class that
#I am using to read the training images
self.data = dataset
self.build()
self._optimize = None
self.sess = tf.Session()
self.optimize()
self.sess.run(tf.global_variables_initializer())

dataset.repeat() doesn't work in TensorFlow

This is the part of the code
def train(x):
prediction = cnn(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
optimizer = tf.train.AdadeltaOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in xrange(num_epochs):
epoch_loss = 0
for _ in xrange(batch_size):
_, c = sess.run([optimizer, cost])
epoch_loss += c
print('Epoch {} completed out of {} - loss {}'.format(epoch + 1, num_epochs, epoch_loss))
n_classes = 17
batch_size = 32
dropout_rate = 0.4
num_epochs = 10
train_set = read_image_dataset_tfrecordfile('train.tfrecord', resize=True)
train_set = train_set.batch(batch_size)
train_set.repeat(num_epochs)
train_iterator = train_set.make_one_shot_iterator()
x, y = train_iterator.get_next()
train(x)
When I run this it does only the first epoch and then throws OutOfRangeError, here the stack
Epoch 1 completed out of 10 - loss 5.82853866496e+11
Traceback (most recent call last):
File "/Users/user/PycharmProjects/ProveTF/main.py", line 113, in <module>
train(x)
File "/Users/user/PycharmProjects/ProveTF/main.py", line 83, in train
_, c = sess.run([optimizer, cost])
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 905, in run
run_metadata_ptr)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1137, in _run
feed_dict_tensor, options, run_metadata)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1355, in _do_run
options, run_metadata)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1374, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.OutOfRangeError: End of sequence
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,100,100,1], [?,17]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]
Caused by op u'IteratorGetNext', defined at:
File "/Users/user/PycharmProjects/ProveTF/main.py", line 110, in <module>
x, y = train_iterator.get_next()
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 330, in get_next
name=name)), self._output_types,
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/ops/gen_dataset_ops.py", line 866, in iterator_get_next
output_shapes=output_shapes, name=name)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
op_def=op_def)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
OutOfRangeError (see above for traceback): End of sequence
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,100,100,1], [?,17]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]
I tried to move the repeat() method in other places and I tried to write simply repeat() without the parameter, but it doesn't work anyway.
Any solutions or suggestions?
You need to assign train_set = train_set.repeat() just as you do with the batch method. It doesn't modify the dataset in place.

Nerual Network Building Running into Error

I am trying to build this LSTM network and I ran into this error constantly. I looked it up on Google and am still not sure what is going on. I have tried adding this:with tf.variable_scope("cell1"). Still not working. Any help is greatly appreciated.
Here is the code of the main LSTM structure
def lstm_structure(self):
train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = self.data_preprocessing(
'SSE_Composite_Index.csv')
graph=tf.Graph()
with graph.as_default():
'''Placeholders'''
X=tf.placeholder(tf.float32,shape=[None,self.num_steps,self.input_size])
y=tf.placeholder(tf.float32,shape=[None,self.num_classes])
valid=tf.constant(valid_dataset)
test=tf.constant(test_dataset)
'''Weights'''
weights={'in':tf.Variable(tf.random_normal([self.input_size,self.num_neurons])),'out':tf.Variable(tf.random_normal([self.num_neurons,self.num_classes]))}
'''Biases'''
biases={'in':tf.Variable(tf.zeros(shape=[self.num_neurons,])),'out':tf.Variable(tf.zeros(shape=[self.num_classes,]))}
def lstm(X, weights, biases, reuse=True
):
'''from input to cell'''
with tf.variable_scope("foo") as f:
if reuse:
f.reuse_variables()
X = tf.reshape(X, shape=[-1, self.input_size])
X_in = tf.matmul(X, weights['in']) + biases['in']
X_in = tf.reshape(X_in, shape=[-1, self.num_steps, self.num_neurons])
'''cell'''
cell_ = tf.contrib.rnn.BasicLSTMCell(self.num_neurons, forget_bias=1, state_is_tuple=True)
_init_state = cell_.zero_state(self.batch_size, dtype=tf.float32)
outputs, states = tf.nn.dynamic_rnn(cell_, X_in, initial_state=_init_state, time_major=False, dtype=tf.float32)
'''from cell to output'''
results = tf.matmul(states[1], weights['out']) + biases['out']
return results
logits=lstm(X,weights,biases,False)
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss)
train_prediction=tf.nn.softmax(logits=logits)
valid_prediction=tf.nn.softmax(lstm(valid,weights,biases,True))
test_prediction=tf.nn.softmax(lstm(test,weights,biases,True))
with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
print('Initialised')
loss_list = []
for step in range(self.num_epochs):
if step==0:
i = 0
while i < len(train_dataset):
start = i
end = i + self.batch_size
batch_data = train_dataset[start:end, :]
batch_label = train_labels[start:end, :]
i += self.batch_size
a, b, prediction = session.run([optimizer, loss, train_prediction],
feed_dict={X: batch_data, y: batch_label})
loss_list.append(b)
else:
i = 0
while i < len(train_dataset):
start = i
end = i + self.batch_size
batch_data = train_dataset[start:end, :]
batch_label = train_labels[start:end, :]
i += self.batch_size
a, b, prediction = session.run([optimizer, loss, train_prediction],
feed_dict={X: batch_data, y: batch_label})
loss_list.append(b)
if step % 10 == 0:
print('Step', step, 'Loss', b)
print('Training Accuracy', self.accuracy(prediction, batch_label), '%')
print('Validation Accuracy',
self.accuracy(valid_prediction.eval(), valid_labels), '%')
print('test Accuracy', self.accuracy(test_prediction.eval(), test_labels), '%')
print('Finished')
The error message that I am getting is:
Traceback (most recent call last):
File "C:/Users/LiXin/PycharmProjects/PythonProjects/LSTM_CLASSIFIER.py", line 114, in <module>
lstm.LSTM_structure()
File "C:/Users/LiXin/PycharmProjects/PythonProjects/LSTM_CLASSIFIER.py", line 87, in LSTM_structure
valid_prediction=tf.nn.softmax(LSTM(valid,weights,biases))
File "C:/Users/LiXin/PycharmProjects/PythonProjects/LSTM_CLASSIFIER.py", line 75, in LSTM
outputs,states=tf.nn.dynamic_rnn(cell_,X_in,initial_state=_init_state,time_major=False,dtype=tf.float32)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 614, in dynamic_rnn
dtype=dtype)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 777, in _dynamic_rnn_loop
swap_memory=swap_memory)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2816, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2640, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2590, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 762, in _time_step
(output, new_state) = call_cell()
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 748, in <lambda>
call_cell = lambda: cell(input_t, state)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\layers\base.py", line 575, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 438, in call
self._linear = _Linear([inputs, h], 4 * self._num_units, True)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1171, in __init__
initializer=kernel_initializer)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1203, in get_variable
constraint=constraint)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1092, in get_variable
constraint=constraint)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 417, in get_variable
return custom_getter(**custom_getter_kwargs)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 186, in _rnn_get_variable
variable = getter(*args, **kwargs)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 394, in _true_getter
use_resource=use_resource, constraint=constraint)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 742, in _get_single_variable
name, "".join(traceback.format_list(tb))))
ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
The following code works, however.
def RNN_neural_network_model(x):
layer={'weights':tf.Variable(tf.random_normal([rnn_size,n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes]))}
x=tf.transpose(x,[1,0,2])
x=tf.reshape(x,[-1,chunk_size])
x=tf.split(x,n_chunks,0)
lstm_cell=tf.contrib.rnn.BasicLSTMCell(rnn_size,state_is_tuple=True)
# stacked_lstm = tf.contrib.rnn.MultiRNNCell(
# [lstmcell(rnn_size) for _ in range(num_layers)])
outputs, states=rnn.static_rnn(lstm_cell,x,dtype=tf.float32)
output = tf.matmul(outputs[-1], layer['weights'])+ layer['biases']
return output
You should wrap your LSTM definition inside a variable scope and then reuse it for validation and testing. Try the following
def LSTM(X,weights,biases, reuse=reuse):
'''from input to cell'''
with tf.variable_scope("foo") as f:
if reuse:
f.reuse_variables()
X=tf.reshape(X,shape=[-1,self.input_size])
X_in=tf.matmul(X,weights['in'])+biases['in']
X_in=tf.reshape(X_in,shape=[-1,self.num_steps,self.num_neurons])
'''cell'''
cell_=tf.contrib.rnn.BasicLSTMCell(self.num_neurons,forget_bias=1,state_is_tuple=True)
_init_state=cell_.zero_state(self.batch_size,dtype=tf.float32)
outputs,states=tf.nn.dynamic_rnn(cell_,X_in,initial_state=_init_state,time_major=False,dtype=tf.float32)
'''from cell to output'''
results=tf.matmul(states[1],weights['out'])+biases['out']
return results
Change your training, testing code as below
logits=LSTM(X,weights,biases,False)
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss)
train_prediction=tf.nn.softmax(logits=logits)
valid_prediction=tf.nn.softmax(LSTM(valid,weights,biases, True))
test_prediction=tf.nn.softmax(LSTM(test,weights,biases, True))

Tensorflow: I get something wrong in accuracy

I just run a simple code and want to get accuracy after training. I load the model that I saved, but when I want to get accuracy, I get something wrong. Why?
# coding=utf-8
from color_1 import read_and_decode, get_batch, get_test_batch
import AlexNet
import cv2
import os
import time
import numpy as np
import tensorflow as tf
import AlexNet_train
import math
batch_size=128
num_examples = 1000
crop_size=56
def evaluate(test_x, test_y):
image_holder = tf.placeholder(tf.float32, [batch_size, 56, 56, 3], name='x-input')
label_holder = tf.placeholder(tf.int32, [batch_size], name='y-input')
y = AlexNet.inference(image_holder,evaluate,None)
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(label_holder,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
coord = tf.train.Coordinator()
sess.run(init_op)
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
ckpt=tf.train.get_checkpoint_state(AlexNet_train.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, os.path.join(AlexNet_train.MODEL_SAVE_PATH, ckpt_name))
print('Loading success, global_step is %s' % global_step)
step=0
image_batch, label_batch = sess.run([test_x, test_y])
accuracy_score=sess.run(accuracy,feed_dict={image_holder: image_batch,
label_holder: label_batch})
print("After %s training step(s),validation "
"precision=%g" % (global_step, accuracy_score))
coord.request_stop()
coord.join(threads)
def main(argv=None):
test_image, test_label = read_and_decode('val.tfrecords')
test_images, test_labels = get_test_batch(test_image, test_label, batch_size, crop_size)
evaluate(test_images, test_labels)
if __name__=='__main__':
tf.app.run()
And here is error,it said that this line in my code is wrong:" correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(label_holder,1))"
Traceback (most recent call last):
File "/home/vrview/tensorflow/example/char/tfrecords/AlexNet/Alex_save/AlexNet_test.py", line 80, in <module>
tf.app.run()
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/home/vrview/tensorflow/example/char/tfrecords/AlexNet/Alex_save/AlexNet_test.py", line 76, in main
evaluate(test_images, test_labels)
File "/home/vrview/tensorflow/example/char/tfrecords/AlexNet/Alex_save/AlexNet_test.py", line 45, in evaluate
label_holder: label_batch})
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected dimension in the range [-1, 1), but got 1
[[Node: ArgMax_1 = ArgMax[T=DT_INT32, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_y-input_0, ArgMax_1/dimension)]]
Caused by op u'ArgMax_1', defined at:
File "/home/vrview/tensorflow/example/char/tfrecords/AlexNet/Alex_save/AlexNet_test.py", line 80, in <module>
tf.app.run()
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/home/vrview/tensorflow/example/char/tfrecords/AlexNet/Alex_save/AlexNet_test.py", line 76, in main
evaluate(test_images, test_labels)
File "/home/vrview/tensorflow/example/char/tfrecords/AlexNet/Alex_save/AlexNet_test.py", line 22, in evaluate
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(label_holder,1))
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 263, in argmax
return gen_math_ops.arg_max(input, axis, name)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 168, in arg_max
name=name)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
op_def=op_def)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2395, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1264, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): Expected dimension in the range [-1, 1), but got 1
[[Node: ArgMax_1 = ArgMax[T=DT_INT32, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_y-input_0, ArgMax_1/dimension)]]
How to solve it?
Taking the part of this answer related to the problem here:
tf.argmax's definition states:
axis: A Tensor. Must be one of the following types: int32, int64.
int32, 0 <= axis < rank(input). Describes which axis of the input
Tensor to reduce across.
It seems, then, that the only way to run argmax on the last axis of the tensor is by giving it axis=-1, because of the "strictly less than" sign in the definition of the function.

Categories