- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 41198 个
- 通用积分
- 2.6173
- 学术水平
- 7 点
- 热心指数
- 5 点
- 信用等级
- 5 点
- 经验
- 2201 点
- 帖子
- 198
- 精华
- 1
- 在线时间
- 36 小时
- 注册时间
- 2015-6-1
- 最后登录
- 2024-3-3
|
- % CCA examples
- % This script demonstrates how to use the CCA function.
- %
- % Copyright(c) Liang Sun (sun.liang@asu.edu), Shuiwang Ji (shuiwang.ji@asu.edu), and Jieping Ye (jieping.ye@asu.edu), Arizona State Univerisity
- %
- clear all; clc;
- addpath('../Functions');
- % % We generate 200 samples, with dimensionality 10 and 100
- % d1 = 100;
- % d2 = 10;
- % n = 200;
- % X = rand(d1, n);
- % Y = rand(d2, n);
- % load the synthetic data
- % The dimensionality of X and Y are 1000 and 20, respectively.
- % and the sample size is 400.
- data_path = '../Data/Synthetic_1.mat';
- S = load(data_path);
- X = S.X;
- Y = S.Y;
- % ********** Examples of using CCA *********
- %=============================================================
- % Part 1. We show different usages of CCA when alg='eig'.
- opts = [];
- opts.alg = 'eig';
- opts.PrjX = 1;
- opts.PrjY = 1;
- opts.regX = 0.1;
- opts.regY = 1;
- [W_x, W_y, corr_list] = CCA(X, Y, opts);
- % We verify the correlation coefficient in the projected spaces
- X_p = W_x' * colCenter(X);
- Y_p = W_y' * colCenter(Y);
- c_list = zeros(size(W_x, 2), 1);
- if isfield(opts, 'regX')
- regX = opts.regX;
- else
- regX = 0;
- end
- if isfield(opts, 'regY')
- regY = opts.regY;
- else
- regY = 0;
- end
- for i = 1:size(X_p, 1)
- x_p = X_p(i, :)';
- y_p = Y_p(i, :)';
- % We consider regularization for both X and Y.
- denom1 = x_p' * x_p + regX * W_x(:, i)' * W_x(:, i);
- denom2 = y_p' * y_p + regY * W_y(:, i)' * W_y(:, i);
- c = x_p' * y_p / sqrt(denom1) / sqrt(denom2);
- c_list(i) = c;
- end
- if norm(c_list-corr_list)<1e-10
- display('verfirication is successful for PrjX=1 and PrjY=1');
- else
- display('verification failed for PrjX=1 and PrjY=1');
- end
- % only projection for X is computed
- opts = [];
- opts.alg = 'eig';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.regX = 0.1;
- opts.regY = 1;
- [W_x1, W_y1, corr_list1] = CCA(X, Y, opts);
- % compute the difference between this setting and the last setting.
- d_W_x1 = norm(W_x - W_x1);
- d_c1 = norm(corr_list - corr_list1);
- if d_W_x1<1e-10 && d_c1<1e-10
- display('equivalence is verified for (PrjX, PrjY) is (1,0) and (1,1)');
- else
- display('equivalence is not verified for (PrjX, PrjY) is (1,0) and (1,1)');
- end
- % only projection for Y is computed
- opts = [];
- opts.alg = 'eig';
- opts.PrjX = 0;
- opts.PrjY = 1;
- opts.regX = 0.1;
- opts.regY = 1;
- [W_x2, W_y2, corr_list2] = CCA(X, Y, opts);
- % compute the difference between this setting and the last setting.
- % note that the sign may be different
- sign_change = sign(W_y2(1,:) ./ W_y(1,:));
- d_W_y2 = norm(W_y - W_y2 * diag(sign_change));
- d_c2 = norm(corr_list - corr_list2);
- if d_W_y2<1e-10 && d_c2<1e-10
- display('equivalence is verfied for (PrjX, PrjY) is (0,1) and (1,1)');
- else
- display('equivalence is not verfied for (PrjX, PrjY) is (0,1) and (1,1)');
- end
- %=============================================================
- % Part 2. Use ls and ls-lsqr implementation and verify the equivalence relationship in the un-regularized setting.
- % Compare ls and eig implementations
- % Generate the data
- opts = [];
- opts.alg = 'ls';
- opts.PrjX = 1;
- opts.PrjY = 0;
- W_x_ls = CCA(X, Y, opts);
- % Compute W_x_eig for the corresponding eig implementations.
- opts = [];
- opts.alg = 'eig';
- opts.PrjX = 1;
- opts.PrjY = 1;
- [W_x_eig_c1, W_y_eig_c1, corr_list_c1] = CCA(X, Y,opts);
- % compute the difference
- d_ls_eig_eq = norm(W_x_ls*W_x_ls' - W_x_eig_c1*W_x_eig_c1');
- if d_ls_eig_eq<1e-10
- display('ls implementation is equivalent to eig implementation');
- else
- display('ls implementation is not equivalent to eig implementation');
- end
- % test ls-lsqr implementation
- opts = [];
- opts.alg = 'ls-lsqr';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.lsqr_tol = 1e-6;
- W_x_ls_lsqr = CCA(X, Y, opts);
- % compute the difference
- d_ls_lsqr_eq = norm(W_x_ls*W_x_ls' - W_x_ls_lsqr*W_x_ls_lsqr');
- if d_ls_lsqr_eq<1e-5
- display('ls implementation is equivalent to ls-lsqr implementation');
- else
- display('ls implementation is not equivalent to ls-lsqr implementation');
- end
- % test 2-norm regularized ls
- opts = [];
- opts.alg = 'ls';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.reg_2norm = 1;
- W_x_ls_2reg = CCA(X, Y, opts);
- % display('check 1-norm regularization for ls implementation');
- % test 1-norm regularized ls
- opts = [];
- opts.alg = 'ls';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.reg_1norm = 0.2;
- W_x_ls_1reg = CCA(X, Y, opts);
- % test 1-norm and 2-norm regularization together for ls
- opts = [];
- opts.alg = 'ls';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.reg_1norm = 0.2;
- opts.reg_2norm = 1;
- W_x_ls_12reg = CCA(X, Y, opts);
- % test 2-norm regularization for ls-lsqr
- opts = [];
- opts.alg = 'ls-lsqr';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.reg_2norm = 1;
- opts.lsqr_tol = 1e-6;
- W_x_ls_lsqr_2reg = CCA(X, Y, opts);
- % %=============================================================
- % Part 3. Use 2s and 2s-lsqr implementation
- % Comparison in the un-regularized setting.
- opts = [];
- opts.alg = '2s';
- opts.PrjX = 1;
- opts.PrjY = 0;
- W_x_2s = CCA(X, Y, opts);
- d_2s_eig_eq1 = norm(W_x_2s*W_x_2s' - W_x_eig_c1*W_x_eig_c1');
- if d_2s_eig_eq1<1e-10
- display('2s implementation is equivalent to eig implementation in un-regularized setting');
- else
- display('2s implementation is not equivalent to eig implementation in un-regularized setting');
- end
- % Compute projection for 2s-lsqr in the un-regularized setting.
- opts = [];
- opts.alg = '2s-lsqr';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.lsqr_tol = 1e-6;
- W_x_2s_lsqr = CCA(X, Y, opts);
- % compute the difference
- d_2s_lsqr_eq = norm(W_x_2s*W_x_2s' - W_x_2s_lsqr*W_x_2s_lsqr');
- if d_2s_lsqr_eq<1e-5
- display('2s implementation is equivalent to 2s-lsqr implementation (without regularization)');
- else
- display('ls implementation is not equivalent to ls-lsqr implementation (without regularization)');
- end
- % Comparison in the regularization setting
- opts = [];
- opts.alg = '2s';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.reg_2norm = 1;
- W_x_2s_reg = CCA(X, Y, opts);
- % Compute projection for eig implementation
- opts = [];
- opts.alg = 'eig';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.regX = 1;
- [W_x_eig_c2, W_y_eig_c2, corr_list_c2] = CCA(X, Y, opts);
- d_2s_eig_eq2 = norm(W_x_2s_reg*W_x_2s_reg' - W_x_eig_c2*W_x_eig_c2');
- if d_2s_eig_eq2<1e-10
- display('2s implementation is equivalent to eig implementation in regularization setting');
- else
- display('2s implementation is not equivalent to eig implementation in regularization setting');
- end
- % Compute projection for 2s-lsqr with regularization
- opts = [];
- opts.alg = '2s-lsqr';
- opts.PrjX = 1;
- opts.PrjY = 0;
- opts.lsqr_tol = 1e-6;
- opts.reg_2norm = 1;
- W_x_2s_lsqr_reg = CCA(X, Y, opts);
- % compute the difference
- d_2s_lsqr_reg_eq = norm(W_x_2s_reg*W_x_2s_reg' - W_x_2s_lsqr_reg*W_x_2s_lsqr_reg');
- if d_2s_lsqr_reg_eq<1e-5
- display('2s implementation is equivalent to 2s-lsqr implementation (with regularization)');
- else
- display('ls implementation is not equivalent to ls-lsqr implementation (with regularization)');
- end
复制代码
|
|