TensorFlow

来源:互联网 发布:日式风格的房间知乎 编辑:程序博客网 时间:2024/05/22 22:27

TensorFlow - 读取CSV格式文件

flyfish

以TensorFlow提供的例子说明

filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])reader = tf.TextLineReader()key, value = reader.read(filename_queue)# Default values, in case of empty columns. Also specifies the type of the# decoded result.record_defaults = [[1], [1], [1], [1], [1]]col1, col2, col3, col4, col5 = tf.decode_csv(    value, record_defaults=record_defaults)features = tf.stack([col1, col2, col3, col4])with tf.Session() as sess:  # Start populating the filename queue.  coord = tf.train.Coordinator()  threads = tf.train.start_queue_runners(coord=coord)  for i in range(1200):    # Retrieve a single instance:    example, label = sess.run([features, col5])  coord.request_stop()  coord.join(threads)

API说明concat
代码示例

import tensorflow as tft1 = [[1, 2, 3],[4, 5, 6]]t2 = [[7, 8, 9],[10, 11, 12]]result =(tf.concat( [t1,t2],0))sess = tf.Session() print(sess.run(result)) 

tf.concat( [t1,t2],0)的结果是

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

tf.concat( [t1,t2],1)的结果是

[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]

API说明stack
代码示例

import tensorflow as tfx =[1, 2]y= [3, 4]z= [5, 6]result =tf.stack([x, y, z],0)sess = tf.Session() print(sess.run(result)) 

tf.stack([x, y, z],0)的结果是

[[1 2]
[3 4]
[5 6]]

tf.stack([x, y, z],1)的结果是
[[1 3 5]
[2 4 6]]

To read text files in comma-separated value (CSV) format, use a
tf.TextLineReader with the tf.decode_csv operation.

读取CSV格式的文本文件, 需要使用TextLineReader和decode_csv 操作

Each execution of read reads a single line from the file. The
decode_csv op then parses the result into a list of tensors. The
record_defaults argument determines the type of the resulting >tensors and sets thedefault value to use if a value is missing in the >input string.

每次执行read都会从文件中读取一行内容, decode_csv 操作会将内容解析到张量列表。record_default参数决定了结果的张量的类型,并设置了如果在输入字符串中缺失值时的默认值。

You must call tf.train.start_queue_runners to populate the queue
before you call run or eval to execute the read. Otherwise read will
block while it waits for filenames from the queue.

你必须在调用run或者eval去执行read之前, 调用tf.train.start_queue_runners来填充队列。否则read操作会阻塞。

原创粉丝点击