Home > manopt > autodiff > basic_examples_AD > realtest_AD1.m

realtest_AD1

PURPOSE ^

Test AD for a real optimization problem on a product manifold (struct)

SYNOPSIS ^

function realtest_AD1()

DESCRIPTION ^

 Test AD for a real optimization problem on a product manifold (struct)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function realtest_AD1()
0002 % Test AD for a real optimization problem on a product manifold (struct)
0003 
0004     % Verify that Manopt was indeed added to the Matlab path.
0005     if isempty(which('spherefactory'))
0006         error(['You should first add Manopt to the Matlab path.\n' ...
0007                'Please run importmanopt.']);
0008     end
0009     
0010     % Verify that the deep learning tool box was installed
0011     assert(exist('dlarray', 'file') == 2, ['Deep learning tool box is '... 
0012     'needed for automatic differentiation.\n Please install the'...
0013     'latest version of the deep learning tool box and \nupgrade to Matlab'...
0014     ' R2021b if possible.'])
0015     
0016     % Generate the problem data.
0017     n = 100;
0018     A = randn(n);
0019     A = .5*(A+A');
0020     
0021     % Create the product manifold
0022     S = spherefactory(n);
0023     manifold.x = S;
0024     manifold.y = S;
0025     problem.M = productmanifold(manifold);
0026     
0027     % Define the problem cost function
0028     problem.cost  = @(X) -X.x'*(A*X.y);
0029     
0030     % Define the gradient and the hessian via automatic differentiation
0031     problem = manoptAD(problem);
0032 
0033     % Numerically check gradient and Hessian consistency.
0034     figure;
0035     checkgradient(problem);
0036     figure;
0037     checkhessian(problem);
0038     
0039     % Solve.
0040     [x, xcost, info] = trustregions(problem);          %#ok<ASGLU>
0041     
0042     % Test
0043     ground_truth = svd(A);
0044     distance = abs(ground_truth(1) - (-problem.cost(x)));
0045     fprintf('The distance between the ground truth and the solution is %e \n',distance);
0046 
0047     
0048 end

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005