【发布时间】:2021-12-11 01:00:08
【问题描述】:
我的问题是:在预处理期间,我想使用tf.data.Dataset 和tf.function API 从一组函数中随机选择一个函数应用于数据集示例。
具体来说,我的数据是 3D 体积,我希望从一组 24 个预定义的旋转函数中应用旋转。我想在tf.function 中编写这段代码,这样就限制了numpy 和列表索引等包的使用。
例如,我想做这样的事情:
import tensorflow as tf
@tf.function
def func1(tensor):
# Apply some rotation here
...
@tf.function
def func2(tensor):
...
...
@tf.function
def func24(tensor):
...
@tf.function
def apply(tensor):
list_of_funcs = [func1, func2, ..., func24]
# Randomly sample from 0-23
a = tf.random.uniform([1], minval=0, maxval=23, dtype=tf.int32)
return list_of_funcs[a](tensor)
但是我无法将list_of_funcs 索引为TypeError: list indices must be integers or slices, not Tensor。此外,我无法将这些函数 (AFAIK) 收集到 tf.Tensor 并使用 tf.gather。
所以我的问题是:我怎样才能合理而整洁地从tf.function 中的这些函数中采样?
【问题讨论】:
-
我宁愿考虑是否有比定义 24 个单独的旋转函数更好的方法......它们真的如此不同以至于你不能拥有一个具有不同参数化的函数吗?
-
这很好,您当然可以为所有 24 个函数定义一个函数,但是我担心在这种情况下该函数最终会变成一堆 if 语句。
标签: python tensorflow tensorflow2.0 tensorflow-datasets