tofar
tofar

Reputation: 11

How to deal the tensor from tf.decode_csv() liking a array.

import tensorflow as tf
def read_data(file_queue):
    reader = tf.TextLineReader(skip_header_lines=1)
    key, value = reader.read(file_queue)
    defaults = [[0], [0]]
    value1, value2 = tf.decode_csv(value, defaults)
    return tf.stack(value1, value2)

But I would like to deal with some of the data, such as "3 wins 2 fails" converted to [3, 2]

Upvotes: 1

Views: 255

Answers (1)

nessuno
nessuno

Reputation: 27042

Using tensorflow you're describing a computation. value1,value2 (and any other operation resulting from a tf.* call) are symbolic variables, that points to nodes in a graph.

That's why if you print "the data" you get Tensor("DecodeCSV:0", shape=(), dtype=int32), that's the python representation of "the data".

The actual data, instead, is present only once the graph is built and placed into a Session.

In short, if you want to extract the "real data" you have to exit from the tensorflow graph and fetch the values (thus forcing the execution of the operations described in the graph).

You have to do something like:

sess = tf.Session()
v1,v2 = sess.run([value1, value2])
return v2,v2

However, this is not the correct way of using tensorflow.

Instead, you have to describe as much as possible the computation, and then execute everything into the graph when needed. (creating a session, allocate the memory, place the graph into it, execute the operations, data transfer, ... are heavy operations that should be done not so frequently)

Therefore, I suggest you of having a look at the control flow operation that tensorflow offers: https://www.tensorflow.org/api_guides/python/control_flow_ops#Comparison_Operators

You can control the flow of the values into the graph using them, avoiding to force useless exchanges of data between tensorflow and python

Upvotes: 1

Related Questions