作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将训练和验证数据存储在两个单独的 TFRecord 文件中,其中存储了 4 个值:信号 A(float32 形状(150,)),信号 B(float32 形状(150,)),标签(标量 int64), id(字符串)。我的阅读解析功能是:
def _parse_data_function(sample_proto):
raw_signal_description = {
'label': tf.io.FixedLenFeature([], tf.int64),
'id': tf.io.FixedLenFeature([], tf.string),
}
for key, item in SIGNALS.items():
raw_signal_description[key] = tf.io.FixedLenFeature(item, tf.float32)
# Parse the input tf.Example proto using the dictionary above.
return tf.io.parse_single_example(sample_proto, raw_signal_description)
SIGNALS
是一个字典映射信号名称->信号形状。然后,我阅读了原始数据集:
training_raw = tf.data.TFRecordDataset(<path to training>), compression_type='GZIP')
val_raw = tf.data.TFRecordDataset(<path to validation>), compression_type='GZIP')
training_data = training_raw.map(_parse_data_function)
val_data = val_raw.map(_parse_data_function)
training_data
的标题或
val_data
,我得到:
<MapDataset shapes: {Signal A: (150,), Signal B: (150,), id: (), label: ()}, types: {Signal A: tf.float32, Signal B: tf.float32, id: tf.string, label: tf.int64}>
def data_generator(mapdataset):
for sample in mapdataset:
yield (sample['Signal A'], sample['label'])
validation_data
使用相同的方法。
model.fit
的参数.理想情况下,我只会在 map 表示和数据集表示之间进行转换,它在信号 A 张量和标签对上进行迭代。
<TensorSliceDataset shapes: ((150,), ()), types: (tf.float32, tf.int64)>
但不一定
TensorSliceDataset
最佳答案
您可以简单地在 parse 函数中执行此操作。例如:
def _parse_data_function(sample_proto):
raw_signal_description = {
'label': tf.io.FixedLenFeature([], tf.int64),
'id': tf.io.FixedLenFeature([], tf.string),
}
for key, item in SIGNALS.items():
raw_signal_description[key] = tf.io.FixedLenFeature(item, tf.float32)
# Parse the input tf.Example proto using the dictionary above.
parsed = tf.io.parse_single_example(sample_proto, raw_signal_description)
return parsed['Signal A'], parsed['label']
map
这个函数超过了
TFRecordDataset
,您将拥有一个元组数据集
(signal_a, label)
而不是字典数据集。你应该可以把它放入
model.fit
直接地。
关于python - Tensorflow 2.0 : how to transform from MapDataset (after reading from TFRecord) to some structure that can be input to model. 适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60601412/
我将训练和验证数据存储在两个单独的 TFRecord 文件中,其中存储了 4 个值:信号 A(float32 形状(150,)),信号 B(float32 形状(150,)),标签(标量 int64)
我是一名优秀的程序员,十分优秀!