comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/Misc/mixexp_graddesc.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1
2 %%%%%%%%%%
3
4 function [theta, eta] = mixture_of_experts(q, data, num_iter, theta, eta)
5 % MIXTURE_OF_EXPERTS Fit a piecewise linear regression model using stochastic gradient descent.
6 % [theta, eta] = mixture_of_experts(q, data, num_iter)
7 %
8 % Inputs:
9 % q = number of pieces (experts)
10 % data(l,:) = input example l
11 %
12 % Outputs:
13 % theta(i,:) = regression vector for expert i
14 % eta(i,:) = softmax (gating) params for expert i
15
16 [num_cases dim] = size(data);
17 data = [ones(num_cases,1) data]; % prepend with offset
18 mu = 0.5; % step size
19 sigma = 1; % variance of noise
20
21 if nargin < 4
22 theta = 0.1*rand(q, dim);
23 eta = 0.1*rand(q, dim);
24 end
25
26 for t=1:num_iter
27 for iter=1:num_cases
28 x = data(iter, 1:dim);
29 ystar = data(iter, dim+1); % target
30 % yhat(i) = E[y | Q=i, x] = prediction of i'th expert
31 yhat = theta * x';
32 % gate_prior(i,:) = Pr(Q=i | x)
33 gate_prior = exp(eta * x');
34 gate_prior = gate_prior / sum(gate_prior);
35 % lik(i) = Pr(y | Q=i, x)
36 lik = (1/(sqrt(2*pi)*sigma)) * exp(-(0.5/sigma^2) * ((ystar - yhat) .* (ystar - yhat)));
37 % gate_posterior(i,:) = Pr(Q=i | x, y)
38 gate_posterior = gate_prior .* lik;
39 gate_posterior = gate_posterior / sum(gate_posterior);
40 % Update
41 eta = eta + mu*(gate_posterior - gate_prior)*x;
42 theta = theta + mu*(gate_posterior .* (ystar - yhat))*x;
43 end
44
45 if mod(t,100)==0
46 fprintf(1, 'iter %d\n', t);
47 end
48
49 end
50 fprintf(1, '\n');
51