elelias
elelias

Reputation: 4779

tensorflow run takes forever on small data

I'm trying a simple example of a book where I have a train data sample with 892 lines, the usual titanic survival textbook example. I define:

def read_csv(batch_size, file_path, record_defaults):

    filename_queue = tf.train.string_input_producer([file_path])
    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():
    passenger_id, survived, pclass, name, sex, age, sibsp, parch, ticket, fare, cabin, embarked = \
        read_csv(BATCH_SIZE, file_path, record_defaults)

    # convert categorical data
    is_first_class = tf.to_float(tf.equal(pclass, [1]))
    is_second_class = tf.to_float(tf.equal(pclass, [2]))
    is_third_class = tf.to_float(tf.equal(pclass, [3]))

    gender = tf.to_float(tf.equal(sex, ["female"]))

    # Finally we pack all the features 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([is_first_class, is_second_class, is_third_class, gender, age]))


    print 'shape of features', features.get_shape()
    return features, survived

And now I try to do:

graph = tf.Graph()

with tf.Session(graph=graph) as sess:

    W = tf.Variable(tf.zeros([5, 1]), name="weights")
    b = tf.Variable(0., name="bias")
    tf.global_variables_initializer().run()
    print 'tf was run'
    X,Y = inputs()
    print 'inputs!'
    sess.run(Y)

and I see

'tf was run!'
'inputs!'

but the run part hangs forever (or at least a very long tiem). I'm running this on Jupyter with a 2.7 kernel and tf version 0.12

What am I missing?

Upvotes: 2

Views: 1274

Answers (1)

nessuno
nessuno

Reputation: 27060

At the line

return tf.train.shuffle_batch(decoded,
                              batch_size=batch_size,
                              capacity=batch_size * 50,
                              min_after_dequeue=batch_size)

You're defining the operation of extraction of values from the queue and the creation of batches.

If you look at the complete signature of the method you'll notice that there's a parameter that refers to a number of threads.

tf.train.shuffle_batch(
tensors,
batch_size,
capacity,
min_after_dequeue,
num_threads=1,
seed=None, enqueue_many=False, shapes=None,
allow_smaller_final_batch=False, shared_name=None, name=None)

I'm pointing this out because the operations you defined are executed from some threads. The thread must be started and stopped, this function does not do this for you. The only thing that this function does, about the thread handling, is to add num_thread to a queue.

In practice, to start and stop the threads you need to define within the session an operation that wake up the threads in the queue:

graph = tf.Graph()

with tf.Session(graph=graph) as sess:

    W = tf.Variable(tf.zeros([5, 1]), name="weights")
    b = tf.Variable(0., name="bias")
    tf.global_variables_initializer().run()
    print 'tf was run'
    X,Y = inputs()
    # define a coordinator to start and stop the threads
    coord = tf.train.Coordinator()
    # wake up the threads
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    print 'inputs!'
    sess.run(Y) #execute operation
    # When done, ask the threads to stop.
    coord.request_stop()
    # Wait for threads to finish.
    coord.join(threads)

Upvotes: 4

Related Questions