annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/softmax1.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 % Check that softmax works with a simple classification demo.
Daniel@0 2 % Based on netlab's demglm2
Daniel@0 3 % X -> Q where X is an input node, and Q is a softmax
Daniel@0 4
Daniel@0 5 rand('state', 0);
Daniel@0 6 randn('state', 0);
Daniel@0 7
Daniel@0 8 % Check inference
Daniel@0 9
Daniel@0 10 input_dim = 2;
Daniel@0 11 num_classes = 3;
Daniel@0 12 IRLS_iter = 3;
Daniel@0 13
Daniel@0 14 net = glm(input_dim, num_classes, 'softmax');
Daniel@0 15
Daniel@0 16 dag = zeros(2);
Daniel@0 17 dag(1,2) = 1;
Daniel@0 18 discrete_nodes = [2];
Daniel@0 19 bnet = mk_bnet(dag, [input_dim num_classes], 'discrete', discrete_nodes, 'observed', 1);
Daniel@0 20 bnet.CPD{1} = root_CPD(bnet, 1);
Daniel@0 21 clamped = 0;
Daniel@0 22 bnet.CPD{2} = softmax_CPD(bnet, 2, net.w1, net.b1, clamped, IRLS_iter);
Daniel@0 23
Daniel@0 24 engine = jtree_inf_engine(bnet);
Daniel@0 25
Daniel@0 26 x = rand(1, input_dim);
Daniel@0 27 q = glmfwd(net, x);
Daniel@0 28
Daniel@0 29 [engine, ll] = enter_evidence(engine, {x, []});
Daniel@0 30 m = marginal_nodes(engine, 2);
Daniel@0 31 assert(approxeq(m.T(:), q(:)));
Daniel@0 32
Daniel@0 33
Daniel@0 34 % Check learning
Daniel@0 35 % We use EM, but in fact there is no hidden data.
Daniel@0 36 % The M step will call IRLS on the softmax node.
Daniel@0 37
Daniel@0 38 % Generate data from three classes in 2d
Daniel@0 39 input_dim = 2;
Daniel@0 40 num_classes = 3;
Daniel@0 41
Daniel@0 42 % Fix seeds for reproducible results
Daniel@0 43 randn('state', 42);
Daniel@0 44 rand('state', 42);
Daniel@0 45
Daniel@0 46 ndata = 10;
Daniel@0 47 % Generate mixture of three Gaussians in two dimensional space
Daniel@0 48 data = randn(ndata, input_dim);
Daniel@0 49 targets = zeros(ndata, 3);
Daniel@0 50
Daniel@0 51 % Priors for the clusters
Daniel@0 52 prior(1) = 0.4;
Daniel@0 53 prior(2) = 0.3;
Daniel@0 54 prior(3) = 0.3;
Daniel@0 55
Daniel@0 56 % Cluster centres
Daniel@0 57 c = [2.0, 2.0; 0.0, 0.0; 1, -1];
Daniel@0 58
Daniel@0 59 ndata1 = prior(1)*ndata;
Daniel@0 60 ndata2 = (prior(1) + prior(2))*ndata;
Daniel@0 61 % Put first cluster at (2, 2)
Daniel@0 62 data(1:ndata1, 1) = data(1:ndata1, 1) * 0.5 + c(1,1);
Daniel@0 63 data(1:ndata1, 2) = data(1:ndata1, 2) * 0.5 + c(1,2);
Daniel@0 64 targets(1:ndata1, 1) = 1;
Daniel@0 65
Daniel@0 66 % Leave second cluster at (0,0)
Daniel@0 67 data((ndata1 + 1):ndata2, :) = ...
Daniel@0 68 data((ndata1 + 1):ndata2, :);
Daniel@0 69 targets((ndata1+1):ndata2, 2) = 1;
Daniel@0 70
Daniel@0 71 data((ndata2+1):ndata, 1) = data((ndata2+1):ndata,1) *0.6 + c(3, 1);
Daniel@0 72 data((ndata2+1):ndata, 2) = data((ndata2+1):ndata,2) *0.6 + c(3, 2);
Daniel@0 73 targets((ndata2+1):ndata, 3) = 1;
Daniel@0 74
Daniel@0 75
Daniel@0 76 if 0
Daniel@0 77 ndata = 1;
Daniel@0 78 data = x;
Daniel@0 79 targets = [1 0 0];
Daniel@0 80 end
Daniel@0 81
Daniel@0 82 options = foptions;
Daniel@0 83 options(1) = -1; % verbose
Daniel@0 84 options(14) = IRLS_iter;
Daniel@0 85 [net2, options2] = glmtrain(net, options, data, targets);
Daniel@0 86 net2.ll = options2(8); % type 'help foptions' for details
Daniel@0 87
Daniel@0 88 cases = cell(2, ndata);
Daniel@0 89 for l=1:ndata
Daniel@0 90 q = find(targets(l,:)==1);
Daniel@0 91 x = data(l,:);
Daniel@0 92 cases{1,l} = x(:);
Daniel@0 93 cases{2,l} = q;
Daniel@0 94 end
Daniel@0 95
Daniel@0 96 max_iter = 2; % we have complete observability, so 1 iter is enough
Daniel@0 97 [bnet2, ll2] = learn_params_em(engine, cases, max_iter);
Daniel@0 98
Daniel@0 99 w = get_field(bnet2.CPD{2},'weights');
Daniel@0 100 b = get_field(bnet2.CPD{2},'offset')';
Daniel@0 101
Daniel@0 102 w
Daniel@0 103 net2.w1
Daniel@0 104
Daniel@0 105 b
Daniel@0 106 net2.b1
Daniel@0 107
Daniel@0 108 % assert(approxeq(net2.ll, ll2)); % glmtrain returns ll after final M step, learn_params before
Daniel@0 109