如何使用tf.TextLineReader()和tf.decode_csv解码具有长行的csv
文件(例如,每行有很多项,这样逐个列出输出是不现实的)?
典型的用法是:
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [1,1,1,1,1]
a,b,c,d,e = tf.decode_csv(records=value,record_defaults=record_defaults, field_delim=" ")
当我们在一行中有数千个项目时,不可能像上面的(a,b,c,d,e)那样逐个分配它们,所有的项目都可以解码为一个列表或类似的东西吗?
发布于 2017-09-14 17:48:03
假设您有1800列数据。您可以将其用作记录默认值:
record_defaults=[[1]]*1800
然后使用
all_columns = tf.decode_csv(value, record_defaults=record_defaults)
来阅读它们。
发布于 2016-12-13 17:58:32
嗯,tf.decode_csv
返回一个列表,所以您可以简单地这样做:
record_defaults = [[1], [1], [1], [1], [1]]
all_columns = tf.decode_csv(value, record_defaults=record_defaults)
all_columns
Out: [<tf.Tensor 'DecodeCSV:0' shape=() dtype=int32>,
<tf.Tensor 'DecodeCSV:1' shape=() dtype=int32>,
<tf.Tensor 'DecodeCSV:2' shape=() dtype=int32>,
<tf.Tensor 'DecodeCSV:3' shape=() dtype=int32>,
<tf.Tensor 'DecodeCSV:4' shape=() dtype=int32>
]
然后,您可以像往常一样评估它:
sess = tf.Session()
sess.run(all_columns)
Out: [1, 1, 1, 1, 1]
请注意,您需要传递一个秩为1的record_defaults
。如果您有一些挂起队列的问题。
发布于 2020-07-27 11:12:13
下面是我在record_defaults中混合不同数据类型的方法:
record_defaults = [tf.constant(.1, dtype=tf.float32) for count in range(100)] # 5 fp32 features
record_defaults.extend([tf.constant(1, dtype=tf.int32) for count in range(2)]) # 2 int32 features
https://stackoverflow.com/questions/41115931
复制相似问题