python - 将具有张量特征的 tf.train.Dataset 序列化为 tfrecord 文件?
问题描述
我的数据集如下所示:
dataset1 = tf.data.Dataset.from_tensor_slices((
tf.random.uniform([4, 100], maxval=100, dtype=tf.int32),
tf.random.uniform([4])))
for record in dataset1.take(2):
print(record)
print(type(record))
(<tf.Tensor: shape=(100,), dtype=int32, numpy=
array([28, 96, 6, 22, 36, 33, 34, 29, 20, 77, 40, 82, 45, 81, 62, 59, 30,
86, 44, 17, 43, 32, 19, 32, 96, 24, 14, 65, 43, 59, 0, 96, 20, 17,
54, 31, 88, 72, 88, 55, 57, 63, 92, 50, 95, 76, 99, 63, 95, 82, 22,
36, 87, 56, 44, 29, 12, 45, 82, 27, 56, 32, 44, 66, 77, 99, 97, 58,
52, 81, 42, 54, 78, 3, 29, 86, 59, 98, 67, 39, 25, 27, 16, 46, 68,
81, 72, 30, 53, 95, 33, 71, 93, 82, 95, 55, 13, 53, 30, 21],
dtype=int32)>, <tf.Tensor: shape=(), dtype=float32, numpy=0.42071342>)
(<tf.Tensor: shape=(100,), dtype=int32, numpy=
array([71, 52, 9, 25, 94, 45, 64, 56, 99, 92, 62, 96, 13, 97, 39, 10, 27,
41, 81, 37, 38, 20, 77, 11, 26, 28, 55, 99, 50, 7, 89, 2, 66, 64,
11, 97, 4, 30, 34, 20, 81, 86, 68, 84, 75, 4, 22, 35, 87, 44, 57,
94, 27, 19, 60, 37, 38, 83, 39, 75, 65, 80, 97, 72, 20, 69, 35, 20,
37, 5, 60, 11, 84, 46, 25, 30, 13, 74, 5, 82, 34, 1, 79, 91, 41,
83, 94, 80, 79, 6, 3, 26, 84, 20, 53, 78, 93, 36, 54, 44],
dtype=int32)>, <tf.Tensor: shape=(), dtype=float32, numpy=0.73927164>)
<class 'tuple'>
所以每条记录都是两个张量的元组,一个是输入,另一个是模型的输出。我正在尝试将此数据集转换为一个.tfrecord
文件,该文件需要我Example
从每条记录中提取出来。这是我的尝试:
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
if isinstance(value, type(tf.constant(0))):
value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
"""Returns a float_list from a float / double."""
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def serialize_example(feature1, feature2):
feature = {
'feature1': _bytes_feature(tf.io.serialize_tensor(feature1)),
'feature2': _float_feature(feature2),
}
example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
return example_proto.SerializeToString()
当我这样做时dataset1.map(serialize_example)
,我希望我的代码能在做之前工作
writer = tf.data.experimental.TFRecordWriter(some_path)
writer.write(dataset1)
但是,当我尝试时出现以下错误dataset1.map(serialize_example)
:
...
value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
AttributeError: 'Tensor' object has no attribute 'numpy'
我应该如何将此数据集转换为.tfrecord
文件?
解决方案
我尝试按照文档进行操作,这就是我能想到的(您可以在 colab 中立即对其进行测试):
import tensorflow as tf
dataset1 = tf.data.Dataset.from_tensor_slices((
tf.random.uniform([4, 100], maxval=100, dtype=tf.int32),
tf.random.uniform([4])))
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
if isinstance(value, type(tf.constant(0))):
value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
"""Returns a float_list from a float / double."""
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def serialize_example(feature1, feature2):
feature = {
'feature1': _bytes_feature(tf.io.serialize_tensor(feature1)),
'feature2': _float_feature(feature2),
}
example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
return example_proto.SerializeToString()
def tf_serialize_example(f0,f1):
tf_string = tf.py_function(
serialize_example,
(f0, f1), # Pass these args to the above function.
tf.string) # The return type is `tf.string`.
return tf.reshape(tf_string, ()) # The result is a scalar.
dataset1 = dataset1.map(tf_serialize_example)
writer = tf.data.experimental.TFRecordWriter('test.tfrecord')
writer.write(dataset1)
基本上主要部分是写一个tf.py_function
. 这是因为serialize_example
它是一个非张量类函数:您不能.numpy()
在图形模式下使用。这就是AttributeError: 'Tensor' object has no attribute 'numpy'
(尽管笨拙地)试图告诉你的。不同的是,一个EagerTensor
将有一个.numpy()
方法。
另外一件事:如果您不需要tf.int32
作为输入的数据类型,则可以tf.int64
使用以下函数:
def _int64_feature(value):
"""Returns an int64_list from a bool / enum / int / uint."""
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
我认为这个函数是类似张量的,所以你不需要tf.py_function
,但我没有尝试过。当然,您也可以转换为float32
,否则float64
存储起来会更重。
推荐阅读
- javascript - 拆分字符串包含 html 标签
- rust - 铸造钢筋混凝土
到 Rc - python-3.x - 带有 CLI 实用程序的私有仓库中的 Python 包 - 如何部署、使用和构建
- javascript - 输入中的特殊字符以 %nn 形式出现
- javascript - 无法理解 Redux-Saga 中的非阻塞调用
- algorithm - 合并排序算法合并功能不起作用
- vue.js - Axios PUT 对 DRF Api 的请求通过,但未更新
- aws-cdk - 如何在 aws cdk 中使用自定义资源将文件上传到 s3 存储桶
- highcharts - Highcharts 8 - 在窗口调整大小时更改图表类型
- flutter - 如何确定 Flutter Web 应用是在手机还是 PC 上运行?