計算SVD(Singular Value Decomposition)的方式及解決記憶體不足的問題

問題: 想要計算某紋理資料(large matrices)的SVD,要如何做呢?!


環境

1.OS: Ubuntu 20.04.3 LTS

2.RAM: 16GB


步驟

1.首先,試著找 Python 的眾多套件來計算,陸續找了 SciPy(https://scipy.org/), scikit-learn(https://scikit-learn.org/stable/), NumPy(https://numpy.org/), PRIMME(PReconditioned Iterative MultiMethod Eigensolver, https://github.com/primme/primme) 等來計算 SVD,但執行程式約半小時後會跳出 Killed

2.接著,試著找 C++ 的 library 來計算,陸續找了 Eigen(https://eigen.tuxfamily.org/index.php?title=Main_Page),並使用它號稱For larger ones, BDCSVD is highly recommended and can several order of magnitude faster.的module來寫程式(https://gist.github.com/dvsseed/9b7d77b79079e5e9b361ed4faf1ca274),果然執行沒多久即顯示如下:

terminate called after throwing an instance of 'std::bad_alloc'

  what():  std::bad_alloc

Aborted (core dumped)

3.之後,也找了 GSL -- GNU Scientific Library(https://www.gnu.org/software/gsl/) 及 PyTorch 來寫程式,一樣是宣告 程式記憶體區段錯誤 (核心已傾印)

4.最後,正當想要放棄搜尋自己刻的時候,竟然奇蹟出現了,讓我看到 ALGLIB(https://www.alglib.net/) 這個好物,立即撰寫程式(https://gist.github.com/dvsseed/c3645890d51cfe337ab1addb51cdfba0)並測試結果,可以成功匯出Eigen value到.csv檔案😭😭😭


心得雖然上述的 C++ 程式可以執行但效能不佳,執行耗時約 5,492.87 secs,相較於 MATLAB 計算費時 646.13 secs,仍有進步(利用GPU或parallel computing)及討論的空間...


留言