为了将输入数据序列化为TFRecord格式,我尝试遵循TFRecord格式,但是在读取它时,我一直碰到这个错误:
InvalidArgumentError:键: my_key。无法解析序列化的示例。
我不知道我哪里出了问题。这是我无法解决的问题的一个极小范围的复制。
序列化了一些示例数据:
with tf.python_io.TFRecordWriter('train.tfrecords') as writer:
for idx in range(10):
example = tf.train.Example(
features=tf.train.Features(
feature={
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[1,2,3])),
'test': tf.train.Feature(float_list=tf.train.FloatList(value=[0.1,0.2,0.3]))
}
)
)
writer.write(example.SerializeToString())
writer.close()解析函数&反序列化:
def parse(tfrecord):
features = {
'label': tf.FixedLenFeature([], tf.int64, default_value=0),
'test': tf.FixedLenFeature([], tf.float32, default_value=0.0),
}
return tf.parse_single_example(tfrecord, features)
dataset = tf.data.TFRecordDataset('train.tfrecords').map(parse)
getnext = dataset.make_one_shot_iterator().get_next()当尝试运行此程序时:
with tf.Session() as sess:
v = sess.run(getnext)
print (v)我触发上述错误消息。
是否有可能通过这个错误并反序列化我的数据?
发布于 2018-11-27 17:15:06
tf.FixedLenFeature()用于读取固定大小的数据数组。数据的形状应事先确定。将解析函数更新为
def parse(tfrecord):
return tf.parse_single_example(tfrecord, features={
'label': tf.FixedLenFeature([3], tf.int64, default_value=[0,0,0]),
'test': tf.FixedLenFeature([3], tf.float32, default_value=[0.0, 0.0, 0.0]),
})应该做好这份工作。
发布于 2019-09-18 10:18:09
作为另一种选择,如果输入功能的长度不是固定的,而且大小是任意的,那么您也可以使用tf.io.FixedLenSequenceFeature()和参数allow_missing = True和default_value=0 (在int和0.0类型的情况下浮点),这不要求输入特性的大小与tf.io.FixedLenFeature()不同。您可以找到更多信息,这里。
https://stackoverflow.com/questions/53499409
复制相似问题