| 1 |
package ml; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.Random; |
| 6 |
|
| 7 |
import shared.Shared; |
| 8 |
import shared.Tools; |
| 9 |
import structures.FloatList; |
| 10 |
|
| 11 |
public class Subset { |
| 12 |
|
| 13 |
public Subset(ArrayList<Sample> sampleList) { |
| 14 |
final Sample[] x=new Sample[0]; |
| 15 |
final ArrayList<Sample> pos=new ArrayList<Sample>(); |
| 16 |
final ArrayList<Sample> neg=new ArrayList<Sample>(); |
| 17 |
samples=sampleList.toArray(x); |
| 18 |
for(Sample s : samples) { |
| 19 |
|
| 20 |
if(s.goal[0]>=0.5f) { |
| 21 |
pos.add(s); |
| 22 |
}else{ |
| 23 |
neg.add(s); |
| 24 |
} |
| 25 |
} |
| 26 |
positive=pos.toArray(x); |
| 27 |
negative=neg.toArray(x); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
void sortSamples(float fraction, boolean allowMultithreadedSort) { |
| 32 |
fraction=(Tools.min(fraction, 1f)); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
if(allowMultithreadedSort) { |
| 38 |
Shared.sort(positive, 0, (int)Math.ceil(fraction*positive.length)); |
| 39 |
Shared.sort(negative, 0, (int)Math.ceil(fraction*negative.length)); |
| 40 |
}else { |
| 41 |
Arrays.sort(positive, 0, (int)Math.ceil(fraction*positive.length)); |
| 42 |
Arrays.sort(negative, 0, (int)Math.ceil(fraction*negative.length)); |
| 43 |
} |
| 44 |
assert(positive.length<2 || positive[0].pivot>=positive[1].pivot) : positive[0].pivot+", "+positive[1].pivot; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
int apos=0, ppos=0, npos=0; |
| 56 |
while(apos<samples.length) { |
| 57 |
if(npos<negative.length) { |
| 58 |
samples[apos]=negative[npos]; |
| 59 |
apos++; |
| 60 |
npos++; |
| 61 |
} |
| 62 |
if(ppos<positive.length) { |
| 63 |
samples[apos]=positive[ppos]; |
| 64 |
apos++; |
| 65 |
ppos++; |
| 66 |
} |
| 67 |
} |
| 68 |
assert(apos==samples.length); |
| 69 |
assert(ppos==positive.length); |
| 70 |
assert(npos==negative.length); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
void sortSamples2(float sortFraction, int useSamples, boolean allowMultithreadedSort, FloatList pivots) { |
| 75 |
sortFraction=(Tools.min(sortFraction, 1f)); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
final int pSortLimit=(int)Math.ceil(sortFraction*positive.length); |
| 83 |
final int nSortLimit=(int)Math.ceil(sortFraction*negative.length); |
| 84 |
final int pSwapLimit=Tools.min((useSamples+1)/2, positive.length); |
| 85 |
final int nSwapLimit=Tools.min((useSamples+1)/2, negative.length); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if(Trainer.PIVOT_SORT) { |
| 100 |
pivotSort(positive, pivots, pSortLimit, pSwapLimit, allowMultithreadedSort); |
| 101 |
pivotSort(negative, pivots, nSortLimit, nSwapLimit, allowMultithreadedSort); |
| 102 |
}else { |
| 103 |
sort(positive, pivots, pSortLimit, allowMultithreadedSort); |
| 104 |
sort(negative, pivots, nSortLimit, allowMultithreadedSort); |
| 105 |
} |
| 106 |
|
| 107 |
int apos=0, ppos=0, npos=0; |
| 108 |
while(apos<samples.length) { |
| 109 |
if(npos<negative.length) { |
| 110 |
samples[apos]=negative[npos]; |
| 111 |
apos++; |
| 112 |
npos++; |
| 113 |
} |
| 114 |
if(ppos<positive.length) { |
| 115 |
samples[apos]=positive[ppos]; |
| 116 |
apos++; |
| 117 |
ppos++; |
| 118 |
} |
| 119 |
} |
| 120 |
assert(apos==samples.length); |
| 121 |
assert(ppos==positive.length); |
| 122 |
assert(npos==negative.length); |
| 123 |
} |
| 124 |
|
| 125 |
private static final void sort(final Sample[] samples, final FloatList pivots, |
| 126 |
final int lim, final boolean mt) { |
| 127 |
if(mt) { |
| 128 |
Shared.sort(samples, 0, lim); |
| 129 |
}else { |
| 130 |
Arrays.sort(samples, 0, lim); |
| 131 |
} |
| 132 |
assert(samples.length<2 || samples[0].pivot>=samples[1].pivot) : samples[0].pivot+", "+samples[1].pivot; |
| 133 |
} |
| 134 |
|
| 135 |
private static final void pivotSort(final Sample[] samples, final FloatList pivots, |
| 136 |
int sortLim, final int swapLim, final boolean mt) { |
| 137 |
assert(sortLim>0 && sortLim<=samples.length); |
| 138 |
assert(swapLim>0 && swapLim<=samples.length); |
| 139 |
sortLim=Tools.mid(sortLim, swapLim*2, samples.length); |
| 140 |
|
| 141 |
pivots.clear(); |
| 142 |
for(int i=0; i<sortLim; i++) { |
| 143 |
|
| 144 |
|
| 145 |
pivots.add(samples[i].pivot); |
| 146 |
} |
| 147 |
if(mt) { |
| 148 |
Shared.sort(pivots.array, 0, sortLim); |
| 149 |
}else { |
| 150 |
Arrays.sort(pivots.array, 0, sortLim); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
final int cutoffLoc=Tools.max(sortLim-swapLim, 0); |
| 156 |
final float cutoff=pivots.get(cutoffLoc); |
| 157 |
for(int src=0, dst=0; src<samples.length && dst<swapLim; src++) { |
| 158 |
|
| 159 |
final float p=samples[src].pivot; |
| 160 |
if(p>=cutoff) { |
| 161 |
Sample temp=samples[dst]; |
| 162 |
samples[dst]=samples[src]; |
| 163 |
samples[src]=temp; |
| 164 |
dst++; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
void triage(long currentEpoch, long startTriage, float positiveTriage, float negativeTriage) { |
| 172 |
assert(currentEpoch>=startTriage); |
| 173 |
if(positiveTriage<=0 && negativeTriage<=0) {return;} |
| 174 |
final int distance=500; |
| 175 |
{ |
| 176 |
final int max=Math.round(positiveTriage*positive.length); |
| 177 |
if(max>0){ |
| 178 |
for(Sample s : positive) {s.setPivot();;} |
| 179 |
Shared.sort(positive); |
| 180 |
|
| 181 |
for(int i=0; i<max; i++) {positive[i].setEpoch(currentEpoch+distance);} |
| 182 |
for(int i=Tools.max(max, positive.length-max); i<positive.length; i++) { |
| 183 |
|
| 184 |
positive[i].setEpoch(currentEpoch); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
{ |
| 189 |
final int max=Math.round(negativeTriage*negative.length); |
| 190 |
if(max>0){ |
| 191 |
for(Sample s : negative) {s.setPivot();} |
| 192 |
Shared.sort(negative); |
| 193 |
|
| 194 |
for(int i=0; i<max; i++) {negative[i].setEpoch(currentEpoch+distance);} |
| 195 |
for(int i=Tools.max(max, negative.length-max); i<negative.length; i++) { |
| 196 |
|
| 197 |
negative[i].setEpoch(currentEpoch); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void shuffle() { |
| 204 |
|
| 205 |
for(int i=0; i<samples.length; i++) { |
| 206 |
int idx=randy.nextInt(samples.length); |
| 207 |
Sample s=samples[idx]; |
| 208 |
samples[idx]=samples[i]; |
| 209 |
samples[i]=s; |
| 210 |
} |
| 211 |
numShuffles++; |
| 212 |
} |
| 213 |
public void reset() { |
| 214 |
nextFullPassEpoch=-1; |
| 215 |
numShuffles=0; |
| 216 |
randy.setSeed(0); |
| 217 |
} |
| 218 |
|
| 219 |
final Sample[] samples; |
| 220 |
final Sample[] positive; |
| 221 |
final Sample[] negative; |
| 222 |
private final Random randy=new Random(0); |
| 223 |
|
| 224 |
int nextFullPassEpoch=-1; |
| 225 |
int numShuffles=0; |
| 226 |
|
| 227 |
} |