TensorFlow tf.svd() can compute the singular value decompositions of tensors. However, we find that this funtion run very slowly. In this tutorial, we will give a tip to help you to fix this problem.
An Example
In one of our deep learning models, we have used tf.svd(). However, it is running very slowly. For example, we run a batch size of dataset, it will take about 2 minutes.
It is not a good way to compute the singular value decompositions by tensorflow tf.svd(). Is there any way to make tf.svd() run faster?
To make tf.svd() run faster
If you use tf.svd() to compute the singular value decompositions, you may encouter two problems:
1.You may get some NaN value
To fix this problem, you can refer to this tutorial.
2.tf.svd() run slowly
To make tf.svd() run faster, we will use numpy.linalg.svd() to replace it. We use function replace_tf_svd_with_np_svd() to implement this solution. You can find this function in above tutorial.
We have tested the effect.
If we use tensorflow tf.svd(), it will take about 2 minutes to run a batch size. After replacing it with replace_tf_svd_with_np_svd() function, it will take only about 6 senconds.
Here is an example: