为什么tensorflow map_fn在循环中比python慢

zfycwa2u  于 7个月前  发布在  Python
关注(0)|答案(1)|浏览(122)

我想对形状为[batch, n_pts, 3]的Tensor中的每批点应用一个独立的旋转。我用两种不同的方法实现了它。第一种是将Tensor转换为numpy数组和基本的python for循环。第二种是使用tensorflow tf.map_fn()来消除for循环。然而,当我运行这个时,tensorflow map_fn()慢了大约100倍。
我的问题是我在这里使用了错误的tf.map_fn()函数。你希望什么时候使用tf.map_fn()比标准numpy/python有性能上的提升?
如果我正确使用它,那么我想知道为什么tensorflow tf.map_fn()慢得多。
我复制实验的代码是:

import time
import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K

def rotate_tf(pc):

    theta = tf.random.uniform((1,)) * 2.0 * np.pi
    cosval = tf.math.cos(theta)[0]
    sinval = tf.math.sin(theta)[0]

    R = tf.Variable([
        [cosval, -sinval, 0.0],
        [sinval, cosval, 0.0],
        [0.0, 0.0, 1.0]
    ])

    def dot(p):
        return K.dot(R, tf.expand_dims(p, axis=-1))

    return tf.squeeze(tf.map_fn(dot, pc))

def rotate_np(pc):

    theta = np.random.uniform() * 2.0 * np.pi
    cosval = np.cos(theta)
    sinval = np.sin(theta)

    R = np.array([
        [cosval, -sinval, 0.0],
        [sinval, cosval, 0.0],
        [0.0, 0.0, 1.0]
    ])

    for idx, p in enumerate(pc):
        pc[idx] = np.dot(R, p)

    return pc

pts = tf.random.uniform((8, 100, 3))
n = 10

# Start tensorflow map_fn() method
start = time.time()

for i in range(n):
    pts = tf.map_fn(rotate_tf, pts)

print('processed tf in: {:.4f} s'.format(time.time()-start))

# Start numpy method
start = time.time()

for i in range(n):

    pts = pts.numpy()
    for i, p in enumerate(pts):
        pts[i] = rotate_np(p)
    pts = tf.Variable(pts)

print('processed np in: {:.4f} s'.format(time.time()-start))

字符串
其输出为:

processed tf in: 3.8427 s
processed np in: 0.0314 s

hgqdbh6s

hgqdbh6s1#

你使用的计算机资源是什么?我的猜测是TF正在操作更复杂的对象(Tensor)。
但是你没有满负荷使用TF!TF有一个graph mode。这个功能会加快你的计算速度。
当使用小数组时,numpy优于TF(代码如下)。对于更大的数据,我发现相反的是True。我做了一些小的修改:

  • @tf.函数装饰器
  • 删除rotate_tf中的tf.变量
  • 删除了for循环中的numpy/tf转换,以获取numpy for循环的执行时间
  • 更改n和 pts 的批量大小
import time
import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K

@tf.function(jit_compile=True, reduce_retracing=True)
def rotate_tf(pc):

    theta = tf.random.uniform((1,)) * 2.0 * np.pi
    cosval = tf.math.cos(theta)[0]
    sinval = tf.math.sin(theta)[0]

    R = tf.convert_to_tensor([
        [cosval, -sinval, 0.0],
        [sinval, cosval, 0.0],
        [0.0, 0.0, 1.0]
    ])

    def dot(p):
        return K.dot(R, tf.expand_dims(p, axis=-1))

    return tf.squeeze(tf.map_fn(dot, pc))

def rotate_np(pc):

    theta = np.random.uniform() * 2.0 * np.pi
    cosval = np.cos(theta)
    sinval = np.sin(theta)

    R = np.array([
        [cosval, -sinval, 0.0],
        [sinval, cosval, 0.0],
        [0.0, 0.0, 1.0]
    ])

    for idx, p in enumerate(pc):
        pc[idx] = np.dot(R, p)

    return pc

pts = tf.random.uniform((8, 100, 3))
n = 100

# Start tensorflow map_fn() method
start = time.time()

for i in range(n):
    pts = tf.map_fn(rotate_tf, pts)

print('processed tf in: {:.4f} s'.format(time.time()-start))

# Start numpy method
start = time.time()

pts = pts.numpy()
for i in range(n):

    for i, p in enumerate(pts):
        pts[i] = rotate_np(p)
    
print('processed np in: {:.4f} s'.format(time.time()-start))

字符串

相关问题