gpt4 book ai didi

python - TensorFlow:数据集应用方法的简单自定义 transformation_func 的示例实现

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:01 25 4
gpt4 key购买 nike

我正在尝试为 apply 实现一个简单的自定义 transformation_func数据集 API 中的方法,但没有发现文档特别有用。

具体来说,我的数据集包含视频帧和相应的标签:{[frame_0, label_0], [frame_1, label_1], [frame_2, label_2],...}.

我想对其进行转换,使其另外包含每个标签的前一帧:{[frame_0, frame_1, label_1], [frame_1, frame_2, label_2], [frame_2, frame_3, label_3], ...

这可能可以通过执行诸如 tf.data.Dataset.zip(dataset, dataset.skip(1)) 之类的操作来实现,但那样我就会有重复的标签。

我没能找到transformation_func 的引用实现。有人能让我开始做这件事吗?

最佳答案

apply只是为了方便使用现有的转换函数,ds.apply(func)func(ds) 几乎相同,只是以一种更“可链接”的方式。这是一种可能的方法来做你想做的事:

import tensorflow as tf

frames = tf.constant([ 1, 2, 3, 4, 5, 6], dtype=tf.int32)
labels = tf.constant(['a', 'b', 'c', 'd', 'e', 'f'], dtype=tf.string)
# Create dataset
ds = tf.data.Dataset.from_tensor_slices((frames, labels))
# Zip it with itself but skipping the first one
ds = tf.data.Dataset.zip((ds, ds.skip(1)))
# Make desired output structure
ds = ds.map(lambda fl1, fl2: (fl1[0], fl2[0], fl2[1]))
# Iterate
it = ds.make_one_shot_iterator()
elem = it.get_next()
# Test
with tf.Session() as sess:
while True:
try: print(sess.run(elem))
except tf.errors.OutOfRangeError: break

输出:

(1, 2, b'b')
(2, 3, b'c')
(3, 4, b'd')
(4, 5, b'e')
(5, 6, b'f')

关于python - TensorFlow:数据集应用方法的简单自定义 transformation_func 的示例实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54037100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com