python - 什么时候应该使用 tf.train.BytesList、tf.train.FloatList 和 tf.train.Int64List 将数据存储在 tf.train.Feature 中?

标签 python tensorflow dataformat

TensorFlow 提供了 3 种不同的数据存储格式 tf.train.Feature .它们是:

tf.train.BytesList
tf.train.FloatList
tf.train.Int64List

我经常在 tf.train.Int64List 之间做出选择/tf.train.FloatListtf.train.BytesList .

我在网上看到一些示例,它们将整数/ float 转换为字节,然后将它们存储在 tf.train.BytesList 中。 .这比使用其他格式之一更可取吗?如果是这样,为什么 TensorFlow 甚至提供 tf.train.Int64Listtf.train.FloatList作为可选格式,您可以将它们转换为字节并使用 tf.train.BytesList

谢谢。

最佳答案

因为字节列表需要更多的内存。它旨在存储字符串数据,或者例如转换为单个字节串的 numpy 数组。考虑示例:

def int64_feature(value):
    if type(value) != list:
        value = [value]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

def float_feature(value):
    if type(value) != list:
        value = [value]
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))

def bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

writer = tf.python_io.TFRecordWriter('file.tfrecords')
bytes = np.array(1.1).tostring() 
int = 1
float = 1.1
example = tf.train.Example(features=tf.train.Features(feature={'1': float_feature(float)}))
writer.write(example.SerializeToString())
writer.close()

for str_rec in tf.python_io.tf_record_iterator('file.tfrecords'):
    example = tf.train.Example()
    example.ParseFromString(str_rec)
    str = (example.features.feature['1'].float_list.value[0])
    print(getsizeof(str))

对于 dtype float 它将输出 24 个字节,这是最小值。但是,您不能将 int 传递给 tf.train.FloatList。在这种情况下,int dtype 将占用 28 个字节,而未解码的字节数为 41 个(在应用 np.fromstring 之前),之后甚至更多。

关于python - 什么时候应该使用 tf.train.BytesList、tf.train.FloatList 和 tf.train.Int64List 将数据存储在 tf.train.Feature 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55201347/

相关文章:

python - 如何使用 Popen.communicate(在 Linux 上)传递击键(ALT+TAB)?

python - 当值为0时如何在轴上显示条形类别

windows - Windows 上的 TF r2.3 错误地址构建问题

将 Keras 模型转换为 C

Excel 正在从 CSV 文件中去除前导 0

java - 使用 SimpleDateFormat 将日期时间值转换为预期值

Python - 连接或堆叠两个以上不同形状的数组

python - 比较 2 个 DataFrame 的半匹配行

tensorflow - 如何正确结合TensorFlow的Dataset API和Keras?

带有嵌套列表创建的 Python CSV 导入