我正在尝试将csv文件(所有列都是浮点数)写入tfrecords文件,然后将其读回。我看到的所有示例都打包了csv列,然后直接将它提供给sess.run(),但我无法弄清楚如何将特征列和标签列写入tfrecord。我怎么能这样做?
答案 0 :(得分:28)
您需要一个单独的脚本来将csv文件转换为TFRecords。
想象一下,你有一个带有以下标题的CSV:
feature_1, feature_2, ..., feature_n, label
您需要使用pandas
读取CSV,手动构建tf.train.Example
,然后使用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())
答案 1 :(得分:1)
上述解决方案不适用于我的情况。另一种读取csv文件并创建tfRecord的方法如下所示:
功能集列名称为:编号,时间,高度,宽度,平均值,标准,方差,非均匀性,PixelCount,contourCount,Class。
我们从dataset.csv获得的示例功能:
功能= [5,'D',268,497,13.706,863.4939,29.385,0.0427,39675,10]
标签:中等
import pandas as pd
import tensorflow as tf
def create_tf_example(features, label):
tf_example = tf.train.Example(features=tf.train.Features(feature={
'Time': tf.train.Feature(bytes_list=tf.train.BytesList(value=[features[1].encode('utf-8')])),
'Height':tf.train.Feature(int64_list=tf.train.Int64List(value=[features[2]])),
'Width':tf.train.Feature(int64_list=tf.train.Int64List(value=[features[3]])),
'Mean':tf.train.Feature(float_list=tf.train.FloatList(value=[features[4]])),
'Std':tf.train.Feature(float_list=tf.train.FloatList(value=[features[5]])),
'Variance':tf.train.Feature(float_list=tf.train.FloatList(value=[features[6]])),
'Non-homogeneity':tf.train.Feature(float_list=tf.train.FloatList(value=[features[7]])),
'PixelCount':tf.train.Feature(int64_list=tf.train.Int64List(value=[features[8]])),
'contourCount':tf.train.Feature(int64_list=tf.train.Int64List(value=[features[9]])),
'Class':tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.encode('utf-8')])),
}))
return tf_example
csv = pd.read_csv("dataset.csv").values
with tf.python_io.TFRecordWriter("dataset.tfrecords") as writer:
for row in csv:
features, label = row[:-1], row[-1]
print features, label
example = create_tf_example(features, label)
writer.write(example.SerializeToString())
writer.close()
有关更多详细信息click here。这对我有用,希望它有用。
答案 2 :(得分:0)
def convert_to():
filename = os.path.join(wdir, 'ml-100k' + '.tfrecords')
print('Writing', filename)
with tf.python_io.TFRecordWriter(filename) as writer:
with open("/Users/shishir/Documents/botconnect_Playground/tfRecords/ml-100k.train.rating", "r") as f:
line = f.readline()
while line != None and line != "":
arr = line.split("\t")
u, i, l = int(arr[0]), int(arr[1]), int(arr[2])
u_arr = np.reshape(u,[1]).astype('int64')
i_arr = np.reshape(i,[1]).astype('int64')
l_arr = np.reshape(l,[1]).astype('int64')
example = tf.train.Example()
example.features.feature["user"].int64_list.value.extend(u_arr)
example.features.feature["item"].int64_list.value.extend(i_arr)
example.features.feature["label"].int64_list.value.append(int(l_arr))
writer.write(example.SerializeToString())
line = f.readline()
这就是我的解决方案,它的确有效!希望这有帮助
干杯。