backstopmedia / tensorflowbook

459 stars 297 forks source link

Ch4_softmax #8

Closed Ed4Piksel closed 6 years ago

Ed4Piksel commented 8 years ago

File reading broken, batch size broken on fix file read as per attached

Softmax example in TF using the classical Iris dataset

Download iris.data from https://archive.ics.uci.edu/ml/datasets/Iris

import tensorflow as tf import os

this time weights form a matrix, not a column vector, one "weight vector" per class.

W = tf.Variable(tf.zeros([4, 3]), name="weights")

so do the biases, one per class.

b = tf.Variable(tf.zeros([3], name="bias"))

def combine_inputs(X): return tf.matmul(X, W) + b

def inference(X): return tf.nn.softmax(combine_inputs(X))

def loss(X, Y): return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(combine_inputs(X), Y))

def read_csv(batch_size, file_name, record_defaults): filename_queue = tf.train.string_input_producer([file_name])

reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename_queue)

# decode_csv will convert a Tensor from type string (the text line) in
# a tuple of tensor columns with the specified defaults, which also
# sets the data type for each column
decoded = tf.decode_csv(value, record_defaults=record_defaults)

# batch actually reads the file and loads "batch_size" rows in a single tensor
return tf.train.shuffle_batch(decoded,
                              batch_size=batch_size,
                              capacity=batch_size * 50,
                              min_after_dequeue=batch_size)

def inputs():

sepal_length, sepal_width, petal_length, petal_width, label =\
    read_csv(100, "./iris.data", [[0.0], [0.0], [0.0], [0.0], [""]])

# convert class names to a 0 based class index.
label_number = tf.to_int32(tf.argmax(tf.to_int32(tf.pack([
    tf.equal(label, ["Iris-setosa"]),
    tf.equal(label, ["Iris-versicolor"]),
    tf.equal(label, ["Iris-virginica"])
])), 0))

# Pack all the features that we care about in a single matrix;
# We then transpose to have a matrix with one example per row and one feature per column.
features = tf.transpose(tf.pack([sepal_length, sepal_width, petal_length, petal_width]))

return features, label_number

def train(total_loss): learning_rate = 0.01 return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)

def evaluate(sess, X, Y):

predicted = tf.cast(tf.arg_max(inference(X), 1), tf.int32)

print sess.run(tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32)))

Launch the graph in a session, setup boilerplate

with tf.Session() as sess:

tf.initialize_all_variables().run()

X, Y = inputs()

total_loss = loss(X, Y)
train_op = train(total_loss)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

# actual training loop
training_steps = 1000
for step in range(training_steps):
    sess.run([train_op])
    # for debugging and learning purposes, see how the loss gets decremented thru training steps
    if step % 10 == 0:
        print "loss: ", sess.run([total_loss])

evaluate(sess, X, Y)

coord.request_stop()
coord.join(threads)
sess.close()

loss:

OutOfRangeError Traceback (most recent call last)

in () 90 # for debugging and learning purposes, see how the loss gets decremented thru training steps 91 if step % 10 == 0: ---> 92 print "loss: ", sess.run([total_loss]) 93 94 evaluate(sess, X, Y) /usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata) 370 try: 371 result = self._run(None, fetches, feed_dict, options_ptr, --> 372 run_metadata_ptr) 373 if run_metadata: 374 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) /usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata) 634 try: 635 results = self._do_run(handle, target_list, unique_fetches, --> 636 feed_dict_string, options, run_metadata) 637 finally: 638 # The movers are no longer used. Delete them. /usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 706 if handle is None: 707 return self._do_call(_run_fn, self._session, feed_dict, fetch_list, --> 708 target_list, options, run_metadata) 709 else: 710 return self._do_call(_prun_fn, self._session, handle, feed_dict, /usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args) 726 except KeyError: 727 pass --> 728 raise type(e)(node_def, op, message) 729 730 def _extend_graph(self): OutOfRangeError: RandomShuffleQueue '_7_shuffle_batch_1/random_shuffle_queue' is closed and has insufficient elements (requested 100, current size 49) [[Node: shuffle_batch_1 = QueueDequeueMany[_class=["loc:@shuffle_batch_1/random_shuffle_queue"], component_types=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_STRING], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch_1/random_shuffle_queue, shuffle_batch_1/n)]] Caused by op u'shuffle_batch_1', defined at: File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/usr/local/lib/python2.7/site-packages/ipykernel/__main__.py", line 3, in app.launch_new_instance() File "/usr/local/lib/python2.7/site-packages/traitlets/config/application.py", line 589, in launch_instance app.start() File "/usr/local/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 442, in start ioloop.IOLoop.instance().start() File "/usr/local/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 162, in start super(ZMQIOLoop, self).start() File "/usr/local/lib/python2.7/site-packages/tornado/ioloop.py", line 883, in start handler_func(fd_obj, events) File "/usr/local/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper return fn(_args, *_kwargs) File "/usr/local/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events self._handle_recv() File "/usr/local/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv self._run_callback(callback, msg) File "/usr/local/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback callback(_args, *_kwargs) File "/usr/local/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper return fn(_args, *_kwargs) File "/usr/local/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher return self.dispatch_shell(stream, msg) File "/usr/local/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell handler(stream, idents, msg) File "/usr/local/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 391, in execute_request user_expressions, allow_stdin) File "/usr/local/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 199, in do_execute shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2723, in run_cell interactivity=interactivity, compiler=compiler, result=result) File "/usr/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2825, in run_ast_nodes if self.run_code(code, result): File "/usr/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 78, in X, Y = inputs() File "", line 45, in inputs sepal_length, sepal_width, petal_length, petal_width, label = read_csv(100, "iris.data", [[0.0], [0.0], [0.0], [0.0], [""]]) File "", line 40, in read_csv min_after_dequeue=batch_size) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 779, in shuffle_batch dequeued = queue.dequeue_many(batch_size, name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py", line 400, in dequeue_many self._queue_ref, n=n, component_types=self._dtypes, name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 465, in _queue_dequeue_many timeout_ms=timeout_ms, name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2260, in create_op original_op=self._default_original_op, op_def=op_def) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1230, in __init__ self._traceback = _extract_stack()
Stefan20162016 commented 8 years ago

just change the "skip_header_lines=1" to 0 on line "reader = tf.TextLineReader(skip_header_lines=1)" because there are/is no header line(s). Before that I had to change the filename_queue to "filename_queue= tf.train.string_input_producer([os.path.join(os.getcwd() , file_name)])".

greets

kochraph commented 7 years ago

Hello, Thanks for this great ebook, I am also having this problem : tensorflow.python.framework.errors.OutOfRangeError: RandomShuffleQueue And cannot find a way to resolve it, any idea ?

OK I think I just found out that the data file downloaded had 2 extra blank line that kill the read_csv def !! Hope that this can help others...

LujunWeng commented 7 years ago

@kochraph Thank you. That really helps. I got 1 extra blank line at the end of the file and I couldn't find the solution for an hour. I don't know why it cannot process this situation properly. All my error information starts from Expect 5 fields but have 0 in record 0, which I didn't know what it meant at all. Anyway, deleting the blank line made it work.

arielscarpinelli commented 6 years ago

Thank you for your comments. I've added a comment in the example file requesting to remove the empty line of the bottom when you download the dataset file.