Tensorflow 从 csv 创建一个 tfrecords 文件 [英] Tensorflow create a tfrecords file from csv
问题描述
我正在尝试将 csv 文件(所有列都是浮点数)写入 tfrecords 文件,然后将它们读回.我看到的所有示例都打包了 csv 列,然后将其直接提供给 sess.run() 但我不知道如何将特征列和标签列写入 tfrecord.我怎么能这样做?
I am trying to write a csv file (all columns are floats) to a tfrecords file then read them back out. All the examples I have seen pack the csv columns then feed it to sess.run() directly but I can't figure out how to write the feature columns and label column to a tfrecord instead. How could I do this?
推荐答案
您需要一个单独的脚本来将您的 csv 文件转换为 TFRecords.
You will need a separate script to convert your csv file to TFRecords.
假设您有一个带有以下标题的 CSV:
Imagine you have a CSV with the following header:
feature_1, feature_2, ..., feature_n, label
您需要使用pandas
之类的内容读取您的CSV,手动构建tf.train.Example
,然后使用TFRecordWriter
将其写入文件
You need to read your CSV with something like pandas
, construct tf.train.Example
manually and then write it to file with TFRecordWriter
csv = pandas.read_csv("your.csv").values
with tf.python_io.TFRecordWriter("csv.tfrecords") as writer:
for row in csv:
features, label = row[:-1], row[-1]
example = tf.train.Example()
example.features.feature["features"].float_list.value.extend(features)
example.features.feature["label"].int64_list.value.append(label)
writer.write(example.SerializeToString())
这篇关于Tensorflow 从 csv 创建一个 tfrecords 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!