What is the time-complexity of the pseudo-inverse in pytorch (i.e. torch.pinverse)?

Viewed 2270

Let's say I have a matrix X with n, m == X.shape in PyTorch. What is the time complexity of calculating the pseudo-inverse with torch.pinverse?

In other words, what is the time complexity of

X_p = torch.pinverse(X)

?

Here is the documentation

2 Answers

As the privious answer correctly mentioned, it is O(n m^2), but n is the larger dimension, not m (here is where the previous answer goes wrong).

TLDR: it is linear in the larger dimension and quadratic in the smaller dimension.

More details:

A simple code like this shows that the complexity is linear with respect to the larger dimension:

import time
import torch

n = 200
results = []
for m in trange(300, 50000, 500):
    a = torch.rand(n,m)
    start_time = time.time()
    b = torch.pinverse(a)
    duration = time.time() - start_time
    results.append((m, duration))

res = list(zip(*results))
plt.plot(res[0], res[1])

enter image description here

While for values smaller than n we have:

n = 1100
results = []
for m in trange(50, n, 20):
    a = torch.rand(n,m)
    start_time = time.time()
    b = torch.pinverse(a)
    duration = time.time() - start_time
    results.append((m, duration))

enter image description here

Related