我正在努力学习编写tensorflow tfrecord文件的基础知识。我正在用python编写一个带有ndarray的简单示例,但是由于某种原因,当我读取它时,它的长度必须是可变的,并且作为SparseTensor读取
举个例子
def serialize_tf_record(features, targets):
record = {
'shape': tf.train.Int64List(value=features.shape),
'features': tf.train.FloatList(value=features.flatten()),
'targets': tf.train.Int64List(value=targets),
}
return build_tf_example(record)
def deserialize_tf_record(record):
tfrecord_format = {
'shape': tf.io.VarLenFeature(tf.int64),
'features': tf.io.VarLenFeature(tf.float32),
'targets': tf.io.VarLenFeature(tf.int64),
}
features_tensor = tf.io.parse_single_example(record, tfrecord_format)
return features_tensor
有人能解释为什么这会写可变长度的记录吗?它在代码中是固定的,但我似乎无法用tensorflow知道其固定的方式来编写它。tensorflow文档在这里非常可怕。有人能帮我澄清一下API吗
您应该提供更多的上下文代码,比如
build_tf_example
函数以及特性和目标的示例下面是一个返回密集张量的示例:
record
转换为一个功能字典(以便轻松地序列化)FixedLenSequenceFeature
输入特征来解析它,从而构建一个密集张量而不是稀疏张量李>相关问题 更多 >
编程相关推荐