tensorflow - 在 tf.function 中使用复杂的控制流可以吗?
问题描述
我有以下 Python 函数,我想将它包装成@tf.function
(最初输入参数是 numpy 数组,但为了在 GPU 上执行,将它们转换为 TF 张量不是问题)。
def reproject(before_frame, motion_vecs):
reprojected_image = np.zeros((before_frame.shape[0], before_frame.shape[1], before_frame.shape[2]))
for row_idx in range(before_frame.shape[0]):
for col_idx in range(before_frame.shape[1]):
for c_idx in range(before_frame.shape[2]):
diff_u = int(round(
(before_frame.shape[1] * motion_vecs[row_idx][col_idx][0])
))
diff_v = int(round(
(before_frame.shape[0] * motion_vecs[row_idx][col_idx][1])
))
before_pixel_position = (
row_idx + diff_v,
col_idx + diff_u
)
if before_pixel_position[0] < before_frame.shape[0] and before_pixel_position[1] < before_frame.shape[1] \
and before_pixel_position[0] > 0 and before_pixel_position[1] > 0:
reprojected_image[row_idx][col_idx][c_idx] = before_frame[
before_pixel_position[0]
][
before_pixel_position[1]
][c_idx]
return reprojected_image
我可以看到在 Tensorflow 教程中人们使用vectorized_map
ormap_fn
代替循环,而tf.cond
不是if
运算符。那么使用这些函数是控制流的唯一选择吗?如果是,背后的原因是什么?
解决方案
推荐阅读
- java - 无法在 jboss 应用服务器 7 中部署战争文件
- css - 我想在工具提示悬停时更改标题背景颜色
- python - 需要帮助在循环中使用 Python 中的 Scrapy 抓取多个网页并从那里到下一页
- java - ORA-02289: 序列不存在 - 选择键或将结果设置到参数对象时出错
- jquery - 选择多个带有图标的select2占位符
- pyspark - 如何根据来自其他 pyspark 数据帧的日期值过滤第二个 pyspark 数据帧?
- excel - 按日期预测的数据透视表
- java - 如何使用 SPNEGO/Kerberos auth 配置 ReactorNettyWebSocketClient
- python - 如何在图表内的海龟图形中显示饼图的百分比数?
- python - tkinter 中 while 循环的替代方案