Mercurial > hg > camir-aes2014
comparison core/tools/machine_learning/cvpartition_trunctrain_incsubsets.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 % class cvpartition_trunctrain | |
3 % NOTE: this is a fake cvpartition double for | |
4 % using cvpartitions in truncated-training size experiments | |
5 % | |
6 % differently from cvpartition_trunctrain, we take all the training sizes | |
7 % at once and generate training partitions where the smaller ones are subsets | |
8 % of the bigger ones | |
9 % --- | |
10 classdef cvpartition_trunctrain_incsubsets | |
11 | |
12 properties (Hidden) | |
13 | |
14 mtest; | |
15 mtraining; | |
16 end | |
17 properties | |
18 N; | |
19 NumTestSets; | |
20 TrainSize; | |
21 TestSize; | |
22 end | |
23 | |
24 | |
25 methods | |
26 | |
27 % --- | |
28 % constuctor: directly calculates the truncated testset | |
29 % --- | |
30 function P = cvpartition_trunctrain_incsubsets(Pin, perctrain) | |
31 | |
32 % --- | |
33 % NOTE: we use a different permutation for each cv-Buun (testset), | |
34 % as otherwise the very small training sets will have about the same | |
35 % data | |
36 % --- | |
37 if ~cvpartition_trunctrain_incsubsets.exists_permutation(Pin) | |
38 cvpartition_trunctrain_incsubsets.renew_permutation(Pin); | |
39 end | |
40 | |
41 P.N = Pin.N; | |
42 P.NumTestSets = Pin.NumTestSets; | |
43 | |
44 for i = 1:Pin.NumTestSets | |
45 | |
46 % copy testing data | |
47 P.TestSize(i) = Pin.TestSize(i); | |
48 P.mtest{i} = Pin.test(i); | |
49 | |
50 % calculate new training size | |
51 P.TrainSize(i) = ceil(perctrain * Pin.TrainSize(i)); | |
52 | |
53 % get actual training indices | |
54 idx = find(Pin.training(i)); | |
55 | |
56 % --- | |
57 % NOTE: the Test-Set-Specific permutation is applied | |
58 % we only extract as many indices as fit in Pin | |
59 % --- | |
60 permu = cvpartition_trunctrain_incsubsets.get_permutation(i,Pin.TrainSize(i)); | |
61 | |
62 % truncate the indices | |
63 idx = idx(permu(1:P.TrainSize(i))); | |
64 | |
65 % build truncated training set | |
66 P.mtraining{i} = false(P.N, 1); | |
67 P.mtraining{i}(idx) = true; | |
68 end | |
69 end | |
70 function out = test(P, i) | |
71 | |
72 out = P.mtest{i}; | |
73 end | |
74 | |
75 function out = training(P, i) | |
76 | |
77 out = P.mtraining{i}; | |
78 end | |
79 end | |
80 | |
81 methods (Static) | |
82 | |
83 % --- | |
84 % TODO: save the permutation in a global variable, | |
85 % tomake the same smaller set available | |
86 % for all further experiments. | |
87 % moreover, it would be great if the smaller training sets | |
88 % are subsets of the bigger ones | |
89 % --- | |
90 function renew_permutation(P) | |
91 global globalvars; | |
92 | |
93 if isfield(globalvars.camir, ... | |
94 'cvpartition_trunctrain_incsubsets'); | |
95 warning 'renwewing permutations for train sets'; | |
96 end | |
97 | |
98 for i = 1:P.NumTestSets | |
99 globalvars.camir.cvpartition_trunctrain_incsubsets.permutation(i).data = ... | |
100 randperm(P.N); | |
101 end | |
102 end | |
103 | |
104 function idx = get_permutation(testId, trainSize) | |
105 % returns the permutation for specific test set | |
106 global globalvars; | |
107 | |
108 idx = globalvars.camir.cvpartition_trunctrain_incsubsets.permutation(testId).data; | |
109 | |
110 % cut the permutation to contain no exxcess numbers | |
111 idx = idx(idx <= trainSize); | |
112 end | |
113 | |
114 function out = exists_permutation(P) | |
115 global globalvars; | |
116 if isfield(globalvars.camir, ... | |
117 'cvpartition_trunctrain_incsubsets'); | |
118 | |
119 out = (numel(globalvars.camir.cvpartition_trunctrain_incsubsets.permutation) == P.NumTestSets) ... | |
120 && (numel(globalvars.camir.cvpartition_trunctrain_incsubsets.permutation(1).data) == P.N); | |
121 | |
122 | |
123 else out = false; | |
124 end | |
125 end | |
126 | |
127 end | |
128 end |