Numo::Linalg.randsvd
Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular value decomposition with randomized algorithm. This gem re-implements RandSVD using Numo::NArray and Numo::Linalg instead of NMatrix.
References:
-
P.-G. Martinsson, A. Szlam, M. Tygert, “Normalized power iterations for the computation of SVD,” Proc. of NIPS Workshop on Low-Rank Methods for Large-Scale Machine Learning, 2011.
-
P.-G. Martinsson, V. Rokhlin, M. Tygert, “A randomized algorithm for the approximation of matrices,” Tech. Rep., 1361, Yale University Department of Computer Science, 2006.
Installation
This gem requires Numo::Linalg, so install numo-linalg or numo-tiny_linalg:
$ gem install numo-linalg
Or:
$ gem install numo-tiny_linalg
Add this line to your application’s Gemfile:
gem 'numo-linalg-randsvd'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install numo-linalg-randsvd
Usage
require 'numo/linalg/autoloader'
# # Or
# require 'numo/tiny_linalg'
# Numo::Linalg = Numo::TinyLinalg
require 'numo/linalg/randsvd'
# An example of matrix decomposition is as follows:
x = Numo::DFloat.new(100, 20).rand
y = Numo::DFloat.new(20, 50).rand
z = x.dot(y)
p z
# Numo::DFloat#shape=[100,50]
# ...
# Performing the randomized singular value decomposition with specified the number of singular values.
n_singular_values = 20
s, u, vt = Numo::Linalg.randsvd(z, n_singular_values)
p s
# Numo::DFloat#shape=[20]
# ...
p u
# Numo::DFloat#shape=[100,20]
# ...
p vt
# Numo::DFloat#shape=[20,50]
# ...
# Reconstructing the matrix with the singular values and singular vectors.
zz = u.dot(s.diag).dot(vt)
p zz
# Numo::DFloat#shape=[100,50]
# ...
p (z - zz).abs.max
# 5.5067062021407764e-14
Contributing
Bug reports and pull requests are welcome on GitHub at github.com/yoshoku/numo-linalg-randsvd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the BSD-3-Clause License