|
| 1 | +using OptimizationBase, LinearAlgebra, ForwardDiff, Zygote, FiniteDiff, |
| 2 | + DifferentiationInterface, SparseConnectivityTracer |
| 3 | +using Test, ReverseDiff |
| 4 | + |
| 5 | +@testset "Matrix Valued" begin |
| 6 | + for adtype in [AutoForwardDiff(), AutoZygote(), AutoFiniteDiff(), |
| 7 | + AutoSparse(AutoForwardDiff(), sparsity_detector = TracerLocalSparsityDetector()), |
| 8 | + AutoSparse(AutoZygote(), sparsity_detector = TracerLocalSparsityDetector()), |
| 9 | + AutoSparse(AutoFiniteDiff(), sparsity_detector = TracerLocalSparsityDetector())] |
| 10 | + # 1. Matrix Factorization |
| 11 | + function matrix_factorization_objective(X, A) |
| 12 | + U, V = @view(X[1:size(A, 1), 1:Int(size(A, 2) / 2)]), |
| 13 | + @view(X[1:size(A, 1), (Int(size(A, 2) / 2) + 1):size(A, 2)]) |
| 14 | + return norm(A - U * V') |
| 15 | + end |
| 16 | + function non_negative_constraint(X, A) |
| 17 | + U, V = X |
| 18 | + return [all(U .>= 0) && all(V .>= 0)] |
| 19 | + end |
| 20 | + A_mf = rand(4, 4) # Original matrix |
| 21 | + U_mf = rand(4, 2) # Factor matrix U |
| 22 | + V_mf = rand(4, 2) # Factor matrix V |
| 23 | + |
| 24 | + optf = OptimizationFunction{false}( |
| 25 | + matrix_factorization_objective, adtype, cons = non_negative_constraint) |
| 26 | + optf = OptimizationBase.instantiate_function( |
| 27 | + optf, hcat(U_mf, V_mf), adtype, A_mf, g = true, h = true, |
| 28 | + cons_j = true, cons_h = true) |
| 29 | + optf.grad(hcat(U_mf, V_mf)) |
| 30 | + optf.hess(hcat(U_mf, V_mf)) |
| 31 | + if adtype != AutoSparse( |
| 32 | + AutoForwardDiff(), sparsity_detector = TracerLocalSparsityDetector()) && |
| 33 | + adtype != |
| 34 | + AutoSparse(AutoZygote(), sparsity_detector = TracerLocalSparsityDetector()) && |
| 35 | + adtype != |
| 36 | + AutoSparse(AutoFiniteDiff(), sparsity_detector = TracerLocalSparsityDetector()) |
| 37 | + optf.cons_j(hcat(U_mf, V_mf)) |
| 38 | + optf.cons_h(hcat(U_mf, V_mf)) |
| 39 | + end |
| 40 | + |
| 41 | + # 2. Principal Component Analysis (PCA) |
| 42 | + function pca_objective(X, A) |
| 43 | + return -tr(X' * A * X) # Minimize the negative of the trace for maximization |
| 44 | + end |
| 45 | + function orthogonality_constraint(X, A) |
| 46 | + return [norm(X' * X - I) < 1e-6] |
| 47 | + end |
| 48 | + A_pca = rand(4, 4) # Covariance matrix (can be symmetric positive definite) |
| 49 | + X_pca = rand(4, 2) # Matrix to hold principal components |
| 50 | + |
| 51 | + optf = OptimizationFunction{false}( |
| 52 | + pca_objective, adtype, cons = orthogonality_constraint) |
| 53 | + optf = OptimizationBase.instantiate_function( |
| 54 | + optf, X_pca, adtype, A_pca, g = true, h = true, |
| 55 | + cons_j = true, cons_h = true) |
| 56 | + optf.grad(X_pca) |
| 57 | + optf.hess(X_pca) |
| 58 | + if adtype != AutoSparse( |
| 59 | + AutoForwardDiff(), sparsity_detector = TracerLocalSparsityDetector()) && |
| 60 | + adtype != |
| 61 | + AutoSparse(AutoZygote(), sparsity_detector = TracerLocalSparsityDetector()) && |
| 62 | + adtype != |
| 63 | + AutoSparse(AutoFiniteDiff(), sparsity_detector = TracerLocalSparsityDetector()) |
| 64 | + optf.cons_j(X_pca) |
| 65 | + optf.cons_h(X_pca) |
| 66 | + end |
| 67 | + |
| 68 | + # 3. Matrix Completion |
| 69 | + function matrix_completion_objective(X, P) |
| 70 | + A, Omega = P |
| 71 | + return norm(Omega .* (A - X)) |
| 72 | + end |
| 73 | + # r = 2 # Rank of the matrix to be completed |
| 74 | + # function rank_constraint(X, P) |
| 75 | + # return [rank(X) <= r] |
| 76 | + # end |
| 77 | + A_mc = rand(4, 4) # Original matrix with missing entries |
| 78 | + Omega_mc = rand(4, 4) .> 0.5 # Mask for observed entries (boolean matrix) |
| 79 | + X_mc = rand(4, 4) # Matrix to be completed |
| 80 | + optf = OptimizationFunction{false}( |
| 81 | + matrix_completion_objective, adtype, cons = rank_constraint) |
| 82 | + optf = OptimizationBase.instantiate_function( |
| 83 | + optf, X_mc, adtype, (A_mc, Omega_mc), g = true, h = true) |
| 84 | + optf.grad(X_mc) |
| 85 | + optf.hess(X_mc) |
| 86 | + end |
| 87 | +end |
0 commit comments