tensorflow读取数据之CSV格式
2018-03-05 17:30
1041 查看
tensorflow要想用起来,首先自己得搞定数据输入。官方文档中介绍了几种,1.一次性从内存中读取数据到矩阵中,直接输入;2.从文件中边读边输入,而且已经给设计好了多线程读写模型;3.把网络或者内存中的数据转化为tensorflow的专用格式tfRecord,存文件后再读取。
其中,从文件中边读边输入,官方文档举例是用的CSV格式文件。我在网上找了一份代码,修改了一下,因为他的比较简略,我就补充一下遇到的问题先贴代码 #coding=utf-8import tensorflow as tfimport numpy as npdefreadMyFileFormat(fileNameQueue): reader = tf.TextLineReader() key, value = reader.read(fileNameQueue) record_defaults = [[1], [1], [1]] col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults) features = tf.pack([col1, col2]) label = col3 return features, labeldefinputPipeLine(fileNames = ["1.csv","2.csv"], batchSize =4, numEpochs = None): fileNameQueue = tf.train.string_input_producer(fileNames, num_epochs = numEpochs) example, label = readMyFileFormat(fileNameQueue) min_after_dequeue =8 capacity = min_after_dequeue +3 * batchSize exampleBatch, labelBatch = tf.train.shuffle_batch([example, label], batch_size = batchSize, num_threads = 3, capacity = cap acity, min_after_dequeue = min_after_dequeue) return exampleBatch, labelBatchfeatureBatch, labelBatch = inputPipeLine(["1.csv","2.csv"], batchSize = 4)with tf.Session() as sess: # Start populating the filename queue.coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) # Retrieve a single instance:try:#while not coord.should_stop():whileTrue: example, label = sess.run([featureBatch, labelBatch])print example except tf.errors.OutOfRangeError: print'Done reading' finally: coord.request_stop()coord.join(threads) sess.close()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~其中,record_defaults = [[1], [1], [1]] ,是用于指定矩阵格式以及数据类型的,CSV文件中的矩阵,是NXM的,则此处为1XM,[1]中的1 用于指定数据类型,比如矩阵中如果有小数,则为float,[1]应该变为[1.0]。
col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults) , 矩阵中有几列,这里就要写几个参数,比如5列,就要写到col5,不管你到底用多少。否则报错。
tf.pack([col1, col2]) ,好像要求col1与col2是同一数据类型,否则报错。
我的测试数据
-0.76 | 15.67 | -0.12 | 15.67 |
-0.48 | 12.52 | -0.06 | 12.51 |
1.33 | 9.11 | 0.12 | 9.1 |
-0.88 | 20.35 | -0.18 | 20.36 |
-0.25 | 3.99 | -0.01 | 3.99 |
-0.87 | 26.25 | -0.23 | 26.25 |
-1.03 | 2.87 | -0.03 | 2.87 |
-0.51 | 7.81 | -0.04 | 7.81 |
-1.57 | 14.46 | -0.23 | 14.46 |
-0.1 | 10.02 | -0.01 | 10.02 |
-0.56 | 8.92 | -0.05 | 8.92 |
-1.2 | 4.1 | -0.05 | 4.1 |
-0.77 | 5.15 | -0.04 | 5.15 |
-0.88 | 4.48 | -0.04 | 4.48 |
-2.7 | 10.82 | -0.3 | 10.82 |
-1.23 | 2.4 | -0.03 | 2.4 |
-0.77 | 5.16 | -0.04 | 5.15 |
-0.81 | 6.15 | -0.05 | 6.15 |
-0.6 | 5.01 | -0.03 | 5 |
-1.25 | 4.75 | -0.06 | 4.75 |
-2.53 | 7.31 | -0.19 | 7.3 |
-1.15 | 16.39 | -0.19 | 16.39 |
-1.7 | 5.19 | -0.09 | 5.18 |
-0.62 | 3.23 | -0.02 | 3.22 |
-0.74 | 17.43 | -0.13 | 17.41 |
-0.77 | 15.41 | -0.12 | 15.41 |
0 | 47 | 0 | 47.01 |
0.25 | 3.98 | 0.01 | 3.98 |
-1.1 | 9.01 | -0.1 | 9.01 |
-1.02 | 3.87 | -0.04 | 3.87 |
相关文章推荐
- tensorflow读取数据(csv格式)
- tensorflow读取数据之CSV格式
- TensorFlow读取CSV数据(批量)
- pandas读取完的csv数据格式
- TensorFlow - 读取CSV格式文件
- sql读取csv和xls格式的文件,数据源
- 读取csv格式的数据
- c#读取csv文件成DataTable,将DataTable数据存储为csv格式文件
- C#根据指定url下载文件并读取.csv数据转为json格式
- tensorflow读取数据-tfrecord格式
- TensorFlow 读取CSV数据代码实现
- python通过串口读取GPS NMEA格式的数据,并保存为csv文件
- 总结02-26用C#从sql数据库导出csv格式的数据,从csv格式读取数据导入到sql表中
- Tensorflow学习教程------tfrecords数据格式生成与读取
- 机器学习: TensorFlow 的数据读取与TFRecords 格式
- tensorflow读取数据-tfrecord格式
- 读取csv文件转换成你自己想要格式的数据
- [置顶] python读取外部数据之读取csv格式
- TensorFlow读取CSV数据