Guest User

Untitled

a guest
Aug 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.39 KB | None | 0 0
  1. %%%% BUILD DATASET %%%%
  2. nb_samples = 1000; input_dim = 1; output_dim = 2; nb_topics = 3; nb_iter = 25;
  3.  
  4. x = -5 + 10*rand(nb_samples, input_dim+1);
  5. x(:,end) = 1;
  6.  
  7. %T = rand(nb_topics, input_dim+1);
  8. T = 1*[-2, 0, 2;-1, 3, 1]';
  9. t = softmax(T*x')';
  10.  
  11. experts = -1+2*rand(input_dim + 1, output_dim, nb_topics);
  12. experts(:, 1, :) = repmat([1,0]', 1, nb_topics);
  13. experts(:,2,1) = [-1, -4];
  14. experts(:,2,2) = [1, 0];
  15. experts(:,2,3) = [-1, 2];
  16. y = mnrnd(1,t);
  17.  
  18. data = zeros(nb_samples, output_dim);
  19. for i=1:nb_samples
  20.     data(i,:) = x(i,:)*experts(:,:,find(y(i,:)==1)) + mvnrnd(zeros(1,output_dim), 0.05*eye(output_dim));
  21. end
  22.  
  23. real_T = T;
  24. real_expert = experts;
  25.  
  26. %%%% TRAIN WITH EM %%%%
  27.  
  28. %Initialization
  29. % T = rand(nb_topics, input_dim+1);
  30. % experts = -1+2*rand(input_dim + 1, output_dim, nb_topics);
  31. ll = zeros(1, nb_iter);
  32.  
  33. %Training
  34. for iter=1:nb_iter
  35.     %%%% EXPECTATION %%%%
  36.     gating_values = softmax(T*x')';
  37.     prediction_experts = zeros(nb_samples, nb_topics);
  38.    
  39.     for j=1:nb_topics
  40.         prediction_experts(:,j) = mvnpdf(data, x*experts(:,:,j),  0.05*eye(output_dim)) + 10^(-3);
  41.     end
  42.    
  43.     expectation = gating_values.*prediction_experts;
  44.     expectation = expectation./repmat(sum(expectation, 2),1,nb_topics);
  45.    
  46.     %%%% MAXIMIZATION %%%%
  47. %     for i=1:nb_topics
  48. %         experts(:,:,i) = lscov(x, data, expectation(:,i));
  49. %     end
  50.    
  51. %     T = lscov(x, log(expectation) + log(repmat(sum(exp(T*x'))', 1, nb_topics)))';
  52. %     T = lscov(x, log(expectation))';
  53.    
  54.     %%% LOG-LIKELIHOOD %%%%
  55.     ll(1, iter) = sum(log(sum(gating_values.*prediction_experts,2)));
  56.    
  57. end
  58.  
  59. %%%% PLOTTING %%%%
  60. clf();
  61. s1 = subplot(2,2,1);
  62. d1 = x*experts(:,:,1);
  63. d2 = x*experts(:,:,2);
  64. d3 = x*experts(:,:,3);
  65. plot(data(:,1),data(:,2),'kx', d1(:,1), d1(:,2), 'bx', d2(:,1), d2(:,2), 'rx', d3(:,1), d3(:,2), 'gx')
  66. subplot(2,2,2);
  67. tt = softmax(T*x')';
  68. plot(x(:,1), t(:,1), 'bx', x(:,1), t(:,2), 'rx', x(:,1), t(:,3), 'gx', x(:,1), tt(:,1), 'bo', x(:,1), tt(:,2), 'ro', x(:,1), tt(:,3), 'go')
  69. subplot(2,2,3);
  70. plot(x(:,1), prediction_experts(:,1), 'bx', x(:,1), prediction_experts(:,2), 'rx', x(:,1), prediction_experts(:,3), 'gx', x(:,1), gating_values(:,1), 'bo', x(:,1), gating_values(:,2), 'ro', x(:,1), gating_values(:,3), 'go')
  71. subplot(2,2,4);
  72. plot(x(:,1), expectation(:,1), 'bx', x(:,1), expectation(:,2), 'rx', x(:,1), expectation(:,3), 'gx')
  73. figure(2)
  74. plot(1:iter, ll)
Add Comment
Please, Sign In to add comment