-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add singular_values, svd_U, and svd_V #2286
Conversation
…and added mixed mode tests (Issue stan-dev#2101)
…svd_V} functions to return thinned matrices instead of full
…4.1 (tags/RELEASE_600/final)
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The singular values prim tests need more in there. Just use the same matrices you test w/ svd_U and svd_V. We apparently were lazy when we implemented this (https://github.com/stan-dev/math/blob/develop/test/unit/math/prim/fun/singular_values_test.cpp).
Also add a 2x3 matrix to the mix singular_values test.
There's another set of changes to make this support the new varmat matrix type (https://github.com/stan-dev/design-docs/blob/master/designs/0005-static-matrices.md).
I think all the comments I left here are pretty easy to straighten out (we'll wait for some feedback on the matrices). Ping me after you go through them, and I'll add instructions on doing the varmat stuff. It's another bunch of small changes. I've apparently left 14 comments here which is enuff stuff for one review tho :D.
@@ -17,7 +17,8 @@ namespace math { | |||
* @param m Specified matrix. | |||
* @return Singular values of the matrix. | |||
*/ | |||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr> | |||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr, | |||
require_not_vt_var<EigMat>* = nullptr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this returns matrices of size zero instead of throwing. We'll need to make this consistent across all the functions but let's wait for a response to the question in prim/fun/svd_U.hpp
first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just make the prim version throw I think. That'll be simplest for now. We can roll it back. Later.
So instead of returning a 0x0, do: check_nonzero_size("singular_values", "m", m);
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
@bbbales2 Changed as you requested. There already seems to be a 2x3 matrix test in |
…4.1 (tags/RELEASE_600/final)
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, the varmat conversion. I left directions in the comments. Lemme know what I forgot (cause I probably did and the error messages will inevitably be confusing)
@@ -17,7 +17,8 @@ namespace math { | |||
* @param m Specified matrix. | |||
* @return Singular values of the matrix. | |||
*/ | |||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr> | |||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr, | |||
require_not_vt_var<EigMat>* = nullptr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just make the prim version throw I think. That'll be simplest for now. We can roll it back. Later.
So instead of returning a 0x0, do: check_nonzero_size("singular_values", "m", m);
@@ -17,7 +17,8 @@ namespace math { | |||
* @param m Specified matrix. | |||
* @return Singular values of the matrix. | |||
*/ | |||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr> | |||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr, | |||
require_not_vt_var<EigMat>* = nullptr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright so we'll do the varmat conversion here first. Along with Eigen::Matrix<var, R, C>
, we just added a new autodiff type to Stan (this one). We don't have developer docs up so lemme walk you through how to convert this.
First change require_not_vt_var<EigMat>
to require_not_st_var<EigMat>
.
The first says "Require the value type of the template parameter to not be a var". Varmats are var_value<Eigen::Matrix<T, R, C>>
types, and the value type of a var_value<Eigen::Matrix<T, R, C>>
is Eigen::Matrix<T, R, C>
. The scalar type though is var
.
So for this to work with varmat we want to say "Require the scalar type of the template parameter to not be a var"
* @param m MxN input matrix | ||
* @return Singular values of matrix | ||
*/ | ||
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change: require_eigen_matrix_dynamic_t<EigMat>
to require_rev_matrix_t<EigMat>
That means accept Eigen::Matrix<var, R, C>
and var_value<Eigen::Matrix<double, R, C>>
types, not just the first.
Delete require_vt_var<EigMat>
. Not necessary.
template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr, | ||
require_vt_var<EigMat>* = nullptr> | ||
inline auto singular_values(const EigMat& m) { | ||
using ret_type = promote_scalar_t<var, Eigen::VectorXd>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using ret_type = return_var_matrix_t<Eigen::VectorXd, EigMat>;
This computes the autodiff return type for a non-autodiff type Eigen::VectorXd
given the input argument is EigMat
. If this is an Eigen::Matrix<var, R, C>
type, the output will similarly be an Eigen matrix of vars (call this a matvar for short). If the input is a var_value<Eigen::Matrix<double, R, C>>
, the output will be a var_value<Eigen::VectorXd>
(call this a varmat for short). Both are valid vector autodiff types in Stan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think with that singular_values
should work with varmats. Now we gotta add tests.
tols.hessian_fvar_hessian_ = 1e-2; | ||
|
||
Eigen::MatrixXd m00(0, 0); | ||
stan::test::expect_ad(f, m00); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To do varmat tests, for every stan::test::expect_ad(f, ...)
do a stan::test::expect_ad_matvar(f, ...)
.
And that's that! This should work for singular_values
, svd_U
, and svd_V
. Lemme know if you have problems.
…ances in tests (Issue stan-dev#2101)
…4.1 (tags/RELEASE_600/final)
@Dashadower I made a few edits, check em' here. If you're good with those, I'll merge this when the tests pass. |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
Summary
This implements
singular_values
,svd_U
, andsvd_V
, with its respective gradients based on Eigen's JacobiSVDTests
Side Effects
svd_U
andsvd_V
may have reversed column-wise signs from other SVD library's results.Release notes
Implement
svd_U
andsvd_V
, add gradients tosingular_values
.Checklist
Math issue SVD functions #2192
Copyright holder: Hyunji Moon, Ben Bales, Shinyoung Kim
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make test-math-dependencies
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested