Tensorflow加载数据

1.reader = tf.TextLineReader(),每次读取一行

阅读器的read方法会输出一个key来表征输入的文件和其中的纪录(对于调试非常有用),同时得到一个字符串标量, 这个字符串标量可以被一个或多个解析器,或者转换操作将其解码为张量并且构造成为样本。

file1.csv内容

1001011120
1011011120
1021011120
1031011120
1041011120
1051011120
1061011120

file0.csv内容

11011120
2

10

11120
31011120
41011120
51011120
61011120
71011120
import tensorflow as tf
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.concat(0, [col1, col2, col3, col4])
features = [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])
    print(example,label)

  coord.request_stop()
  coord.join(threads)

输出结果

[1, 10, 11, 12] 0
[2, 10, 11, 12] 0
[3, 10, 11, 12] 0
[4, 10, 11, 12] 0
[5, 10, 11, 12] 0
[6, 10, 11, 12] 0
[7, 10, 11, 12] 0
[100, 10, 11, 12] 0
[101, 10, 11, 12] 0
[102, 10, 11, 12] 0
[103, 10, 11, 12] 0
[104, 10, 11, 12] 0
[105, 10, 11, 12] 0
[106, 10, 11, 12] 0
[100, 10, 11, 12] 0
[101, 10, 11, 12] 0
[102, 10, 11, 12] 0
[103, 10, 11, 12] 0
[104, 10, 11, 12] 0
[105, 10, 11, 12] 0
[106, 10, 11, 12] 0
[1, 10, 11, 12] 0
[2, 10, 11, 12] 0
可以看出一个文件的内容是按顺序读的,没有打乱。

2.固定长度记录

从二进制文件中读取固定长度纪录, 可以使用tf.FixedLengthRecordReadertf.decode_raw操作。decode_raw操作可以讲一个字符串转换为一个uint8的张量。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值