当前位置:   article > 正文

【域适应】基于散度成分分析(SCA)的四分类任务典型方法实现

【域适应】基于散度成分分析(SCA)的四分类任务典型方法实现

关于

SCA(scatter component analysis)是基于一种简单的几何测量,即分散,它在再现内核希尔伯特空间上进行操作。 SCA找到一种在最大化类的可分离性、最小化域之间的不匹配和最大化数据的可分离性之间进行权衡的表示;每一个都通过分散进行量化。 

参考论文:Shibboleth Authentication Request

工具

MATLAB

方法实现

SCA变换实现
  1. function [test_accuracy, predicted_labels, Zs, Zt] = SCA(X_s_cell, Y_s_cell, X_t, Y_t, params)
  2. INPUT(params is optional):
  3. X_s_cell - cell of (n_s*d) matrix, each matrix corresponds to the instance features of a source domain
  4. Y_s_cell - cell of (n_s*1) matrix, each matrix corresponds to the instance labels of a source domain
  5. X_t - (n_t*d) matrix, rows correspond to instances and columns correspond to features
  6. Y_t - (n_t*1) matrix, each row is the class label of corresponding instances in X_t
  7. [params] - params.beta: vector of validated values of beta
  8. params.delta: vector of validated values of delta
  9. params.k_list: vector of validated dimension of the transformed space
  10. params.X_v: (n_v*d) matrix of instance features of validation set (use the source instances if not provided)
  11. params.Y_v: (n_v*1) matrix of instance labels of validation set (use the source instances if not provided)
  12. params.verbose: if true, show the validation accuracy of each parameter setting
  13. OUTPUT:
  14. test_accuracy - test accuracy on target instances
  15. predicted_labels - predicted labels of target instances
  16. Zs - projected source domain instances
  17. Zt - projected target domain instances
  18. Shoubo Hu (shoubo.sub [at] gmail.com)
  19. 2019-06-02
  20. Reference
  21. [1] Ghifary, M., Balduzzi, D., Kleijn, W. B., & Zhang, M. (2017).
  22. Scatter component analysis: A unified framework for domain
  23. adaptation and domain generalization. IEEE transactions on pattern
  24. analysis and machine intelligence, 39(7), 1414-1430.
  25. %}
  26. if nargin < 4
  27. error('Error. \nOnly %d input arguments! At least 4 required', nargin);
  28. elseif nargin == 4
  29. % default params values
  30. beta = [0.1 0.3 0.5 0.7 0.9];
  31. delta = [1e-3 1e-2 1e-1 1 1e1 1e2 1e3 1e4 1e5 1e6];
  32. k_list = [2];
  33. X_v = cat(1, X_s_cell{:});
  34. Y_v = cat(1, Y_s_cell{:});
  35. verbose = false;
  36. elseif nargin == 5
  37. if ~isfield(params, 'beta')
  38. beta = [0.1 0.3 0.5 0.7 0.9];
  39. else
  40. beta = params.beta;
  41. end
  42. if ~isfield(params, 'delta')
  43. delta = [1e-3 1e-2 1e-1 1 1e1 1e2 1e3 1e4 1e5 1e6];
  44. else
  45. delta = params.delta;
  46. end
  47. if ~isfield(params, 'k_list')
  48. k_list = [2];
  49. else
  50. k_list = params.k_list;
  51. end
  52. if ~isfield(params, 'verbose')
  53. verbose = false;
  54. else
  55. verbose = params.verbose;
  56. end
  57. if ~isfield(params, 'X_v')
  58. X_v = cat(1, X_s_cell{:});
  59. Y_v = cat(1, Y_s_cell{:});
  60. else
  61. if ~isfield(params, 'Y_v')
  62. error('Error. Labels of validation set needed!');
  63. end
  64. X_v = params.X_v;
  65. Y_v = params.Y_v;
  66. end
  67. end
  68. % ----- training phase
  69. % ----- ----- source domains
  70. X_s = cat(1, X_s_cell{:});
  71. Y_s = cat(1, Y_s_cell{:});
  72. fprintf('Number of source domains: %d, Number of classes: %d.\n', length(X_s_cell), length(unique(Y_s)) );
  73. fprintf('Validating hyper-parameters ...\n');
  74. dist_s_s = pdist2(X_s, X_s);
  75. dist_s_s = dist_s_s.^2;
  76. sgm_s = compute_width(dist_s_s);
  77. % ----- ----- validation set
  78. dist_s_v = pdist2(X_s, X_v);
  79. dist_s_v = dist_s_v.^2;
  80. sgm_v = compute_width(dist_s_s);
  81. n_s = size(X_s, 1);
  82. n_v = size(X_v, 1);
  83. H_s = eye(n_s) - ones(n_s)./n_s;
  84. H_v = eye(n_v) - ones(n_v)./n_v;
  85. K_s_s = exp(-dist_s_s./(2 * sgm_s * sgm_s));
  86. K_s_v = exp(-dist_s_v./(2 * sgm_v * sgm_v));
  87. K_s_v_bar = H_s * K_s_v * H_v;
  88. [P, T, D, Q, K_s_s_bar] = SCA_terms(K_s_s, X_s_cell, Y_s_cell);
  89. acc_mat = zeros(length(k_list), length(beta), length(delta));
  90. for i = 1:length(beta)
  91. cur_beta = beta(i);
  92. for j = 1:length(delta)
  93. cur_delta = delta(j);
  94. [B, A] = SCA_trans(P, T, D, Q, K_s_s_bar, cur_beta, cur_delta, 1e-5);
  95. for k = 1:length(k_list)
  96. [acc, ~, ~, ~] = SCA_test(B, A, K_s_s_bar, K_s_v_bar, Y_s, Y_v, k_list( k ) );
  97. acc_mat(k, i, j) = acc;
  98. if verbose
  99. fprintf('beta: %f, delta: %f, acc: %f\n', cur_beta, cur_delta, acc);
  100. end
  101. end
  102. end
  103. end
  104. fprintf('Validation done! Classifying the target domain instances ...\n');
  105. % ----- test phase
  106. % ----- ----- get optimal parameters
  107. acc_tr_best = max( acc_mat(:) );
  108. ind = find( acc_mat == acc_tr_best );
  109. [k, i, j] = size( acc_mat );
  110. [best_k, best_i, best_j] = ind2sub([k, i, j], ind(1));
  111. best_beta = beta(best_i);
  112. best_delta = delta(best_j);
  113. best_k = k_list(best_k);
  114. % ----- ----- test on the target domain
  115. dist_s_t = pdist2(X_s, X_t);
  116. dist_s_t = dist_s_t.^2;
  117. sgm = compute_width(dist_s_t);
  118. K_s_t = exp(-dist_s_t./(2 * sgm * sgm));
  119. n_s = size(X_s, 1);
  120. H_s = eye(n_s) - ones(n_s)./n_s;
  121. n_t = size(X_t, 1);
  122. H_t = eye(n_t) - ones(n_t)./n_t;
  123. K_s_t_bar = H_s * K_s_t * H_t;
  124. [B, A] = SCA_trans(P, T, D, Q, K_s_s_bar, best_beta, best_delta, 1e-5);
  125. [test_accuracy, predicted_labels, Zs, Zt] = SCA_test(B, A, K_s_s_bar, K_s_t_bar, Y_s, Y_t, best_k );
  126. fprintf('Test accuracy: %f\n', test_accuracy);
  127. end
基于SCA的域迁移分类实现
  1. clear all
  2. clc
  3. addpath('./modules');
  4. load('./syn_data/data.mat');
  5. % ----- parameters
  6. % target / all / source domains
  7. tgt_dm = [5];
  8. val_dm = [3 4];
  9. src_dm = [1 2];
  10. data_cell = XY_cell;
  11. X_t = data_cell{tgt_dm(1)}(:, 1:2);
  12. Y_t = data_cell{tgt_dm(1)}(:, 3);
  13. % ----- training data
  14. X_s_cell = cell(1,length(src_dm));
  15. Y_s_cell = cell(1,length(src_dm));
  16. for idx = 1:length(src_dm)
  17. cu_dm = src_dm(1, idx);
  18. X_s_cell{idx} = data_cell{cu_dm}(:, 1:2);
  19. Y_s_cell{idx} = data_cell{cu_dm}(:, 3);
  20. end
  21. % ----- validation data
  22. X_v = [];
  23. Y_v = [];
  24. for idx = 1:length(val_dm)
  25. cu_dm = val_dm(1, idx);
  26. X_v = [X_v; data_cell{cu_dm}(:, 1:2)];
  27. Y_v = [Y_v; data_cell{cu_dm}(:, 3)];
  28. end
  29. params.X_v = X_v;
  30. params.Y_v = Y_v;
  31. params.verbose = true;
  32. [test_accuracy, predicted_labels, Zs, Zt] = SCA(X_s_cell, Y_s_cell, X_t, Y_t, params);

代码获取

相关问题和代码开发,可后台私信沟通交流。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/447902
推荐阅读
相关标签
  

闽ICP备14008679号