【发布时间】:2020-09-04 18:34:31
【问题描述】:
我在 Tensorflow 中使用特征分解,发现它非常慢。这是显示 TensorFlow 速度与 numpy 和 scipy 的代码:
import numpy as np
import scipy as sp
import tensorflow as tf
from time import time
A = np.random.randn(400, 400)
A_tf = tf.constant(A)
cur = time()
d, v = sp.linalg.eig(A)
print(f'sp: {time() - cur:4.2f} s')
cur = time()
d, v = np.linalg.eig(A)
print(f'np: {time() - cur:4.2f} s')
cur = time()
d, v = tf.linalg.eig(A_tf)
print(f'tf: {time() - cur:4.2f} s')
这给出了以下输出:
sp: 0.09 s
np: 0.08 s
tf: 5.04 s
有什么想法吗?
【问题讨论】:
-
为了获得更好的性能,请尝试将
tf.linalg.eig包装在@tf.function中。更多详情请参考this。谢谢!
标签: python tensorflow eigenvalue eigenvector