tensorflow 批量读取csv文件用于做深度学习算法相关
2017-08-06 19:15
633 查看
目前用了tensorflow、deeplearning4j两个深度学习框架,dl相关算法对数据格式要求都是批量的喂进去,deepl4j在前面已经有几个例子说明,tensorflow也可以批量读取数据,不断给dl算法喂数据进去,在网上刚刚看到一个例子,http://www.cnblogs.com/hunttown/p/6844477.html ,首先数据格式如下,鸾尾花数据
做机器学习的人应该都知道:
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species
21,5.4,3.4,1.7,0.2,Iris-setosa
22,5.1,3.7,1.5,0.4,Iris-setosa
23,4.6,3.6,1.0,0.2,Iris-setosa
24,5.1,3.3,1.7,0.5,Iris-setosa
25,4.8,3.4,1.9,0.2,Iris-setosa
26,5.0,3.0,1.6,0.2,Iris-setosa
27,5.0,3.4,1.6,0.4,Iris-setosa
28,5.2,3.5,1.5,0.2,Iris-setosa
29,5.2,3.4,1.4,0.2,Iris-setosa
30,4.7,3.2,1.6,0.2,Iris-setosa
31,4.8,3.1,1.6,0.2,Iris-setosa
32,5.4,3.4,1.5,0.4,Iris-setosa
33,5.2,4.1,1.5,0.1,Iris-setosa
34,5.5,4.2,1.4,0.2,Iris-setosa
35,4.9,3.1,1.5,0.1,Iris-setosa
36,5.0,3.2,1.2,0.2,Iris-setosa
37,5.5,3.5,1.3,0.2,Iris-setosa
39,5.5,4.2,1.4,0.2,Iris-virginica
40,4.9,3.1,1.5,0.1,Iris-versicolor
38,5.0,3.2,1.2,0.2,Iris-versicolor
51,5.5,3.5,1.3,0.2,Iris-versicolor
下面是程序实现:
import tensorflow as tf
path="/Users/shuubiasahi/Desktop/业务相关文档/iris.csv"
def read_data(file_queue):
reader=tf.TextLineReader(skip_header_lines=1)
key,value=reader.read(file_queue)
defaults=[[0], [0.], [0.], [0.], [0.], ['']]
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species = tf.decode_csv(value, defaults)
preprocess_op=tf.case({
tf.equal(Species,tf.constant('Iris-setosa')):lambda :tf.constant(0),
tf.equal(Species, tf.constant('Iris-versicolor')): lambda: tf.constant(1),
tf.equal(Species, tf.constant('Iris-virginica')): lambda: tf.constant(2),
},lambda :tf.constant(-1),exclusive=True)
return tf.stack([SepalLengthCm, SepalWidthCm, PetalLengthCm, PetalWidthCm]), preprocess_op
def create_pipeline(filename,batch_size,num_epochs=None):
file_queue = tf.train.string_input_producer([filename], num_epochs=num_epochs)
example, label = read_data(file_queue)
min_after_dequeue = 1000
capacity = min_after_dequeue + batch_size
example_batch, label_batch = tf.train.shuffle_batch(
[example, label], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue
)
return example_batch, label_batch
x_train_batch, y_train_batch = create_pipeline(path, 5, num_epochs=1000)
x_test, y_test = create_pipeline(path, 60)
init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer() # local variables like epoch_num, batch_size
with tf.Session() as sess:
sess.run(init_op)
sess.run(local_init_op)
# 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():
for _ in range(6):
example, label = sess.run([x_test, y_test])
print (example)
print (label)
except tf.errors.OutOfRangeError:
print ('Done reading')
finally:
coord.request_stop()
coord.join(threads)
sess.close()
相关文章推荐
- TensorFlow学习Day3读取csv文件,动手写个logistic,softmax分类模型
- Tensorflow学习笔记之用TF读取csv文件的限制
- Java学习-016-CSV 文件读取实例源代码
- 【算法学习】图相关算法编程实现-深度优先遍历和广度优先遍历
- python:读取CSV文件的相关技巧
- Jsoup学习笔记9:Jsoup 解析saz文件,读取其中的htm文件到字符串,提取字符串中的数据写入csv文件中
- Excel开发学习笔记:读取xml文件及csv文件
- 学习笔记:csv文件的读取和tf.contrib.learn Quickstart
- 用于Unity上的CSV文件读取工具,一句代码缓存、提取
- Python 读取csv文件批量入库Mysql
- TensorFlow读取CSV数据(批量)
- PHP相关系列 - linux环境下PHP无法读取CSV文件中文字的解决方法
- 深度学习小白——Tensorflow(三) 读取数据
- matlab 批量读取execl(csv)文件
- 机器学习与深度学习相关算法的学习
- 【算法学习】图相关算法编程实现-深度优先遍历和广度优先遍历
- Deep Learning 4J 学习(四) 使用CSV文件来读取训练数据
- cocos2d-x学习笔记——Csv文件读取工具
- 深度学习-CAFFE利用CIFAR10网络模型训练自己的图像数据获得模型-3结合caffe中的CIFAR10修改相关配置文件并训练
- 20121017c++学习笔记,文件读取,字符串相关