| 1 |
package ml; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.Collections; |
| 7 |
import java.util.Random; |
| 8 |
|
| 9 |
import fileIO.ByteFile; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Shared; |
| 13 |
import shared.Tools; |
| 14 |
import structures.IntList; |
| 15 |
|
| 16 |
public class DataLoader { |
| 17 |
|
| 18 |
private DataLoader(String fname_){ |
| 19 |
fnames=new File(fname_).exists() ? |
| 20 |
new String[] {fname_} : Tools.commaPattern.split(fname_); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
public static Matrix[] split(Matrix m, float fraction, int maxLines1, boolean exclusive) { |
| 25 |
if(!exclusive) {fraction=1;} |
| 26 |
|
| 27 |
Matrix[] array=new Matrix[] {new Matrix(), new Matrix()}; |
| 28 |
@SuppressWarnings("unchecked") |
| 29 |
ArrayList<float[]>[] inputs=new ArrayList[] {new ArrayList<float[]>(), new ArrayList<float[]>()}; |
| 30 |
@SuppressWarnings("unchecked") |
| 31 |
ArrayList<float[]>[] outputs=new ArrayList[] {new ArrayList<float[]>(), new ArrayList<float[]>()}; |
| 32 |
|
| 33 |
for(Matrix n : array) { |
| 34 |
n.dims=m.dims; |
| 35 |
n.numInputs=m.numInputs; |
| 36 |
n.numOutputs=m.numOutputs; |
| 37 |
n.columns=m.columns; |
| 38 |
} |
| 39 |
|
| 40 |
Random randy=new Random(0); |
| 41 |
for(int i=0; i<m.inputs.length; i++) { |
| 42 |
float[] in=m.inputs[i], out=m.outputs[i]; |
| 43 |
int positive=(out[0]>=0.5f ? 1 : 0); |
| 44 |
int negative=positive^1; |
| 45 |
int pick=(randy.nextFloat()>=fraction || inputs[1].size()>=maxLines1) ? 0 : 1; |
| 46 |
inputs[pick].add(in); |
| 47 |
outputs[pick].add(out); |
| 48 |
array[pick].numNegative+=negative; |
| 49 |
array[pick].numPositive+=positive; |
| 50 |
array[pick].validLines++; |
| 51 |
} |
| 52 |
|
| 53 |
for(int set=0; set<array.length; set++){ |
| 54 |
Matrix n=array[set]; |
| 55 |
n.inputs=new float[inputs[set].size()][]; |
| 56 |
n.outputs=new float[outputs[set].size()][]; |
| 57 |
for(int i=0; i<n.inputs.length; i++) { |
| 58 |
n.inputs[i]=inputs[set].get(i); |
| 59 |
n.outputs[i]=outputs[set].get(i); |
| 60 |
} |
| 61 |
n.data=new float[][][] {n.inputs, n.outputs}; |
| 62 |
if(exclusive || set>0) {n.detectRange();} |
| 63 |
} |
| 64 |
|
| 65 |
if(!exclusive) {array[0]=m;} |
| 66 |
|
| 67 |
return array; |
| 68 |
} |
| 69 |
|
| 70 |
public static SampleSet[] load(String fname, int maxLines0, boolean shuffleRaw, |
| 71 |
float splitFraction, int maxLines1, boolean exclusive, float balance) { |
| 72 |
DataLoader dl=new DataLoader(fname); |
| 73 |
dl.load(maxLines0, shuffleRaw || splitFraction>0, balance); |
| 74 |
|
| 75 |
lastValidLines=dl.validLines; |
| 76 |
lastInvalidLines=dl.invalidLines; |
| 77 |
if(splitFraction<=0) { |
| 78 |
return new SampleSet[] {new SampleSet(dl.matrix)}; |
| 79 |
}else { |
| 80 |
Matrix[] array=split(dl.matrix, splitFraction, maxLines1, exclusive); |
| 81 |
return new SampleSet[] {new SampleSet(array[0]), new SampleSet(array[1])}; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private void load(final int maxLines, final boolean shuffleRaw, final float balance) { |
| 86 |
|
| 87 |
matrix=new Matrix(); |
| 88 |
ArrayList<float[]> inputList=new ArrayList<float[]>(); |
| 89 |
ArrayList<float[]> outputList=new ArrayList<float[]>(); |
| 90 |
byte[] s=null; |
| 91 |
final int max=(shuffleRaw ? Shared.MAX_ARRAY_LEN : maxLines); |
| 92 |
|
| 93 |
for(String f : fnames) { |
| 94 |
FileFormat ff=FileFormat.testInput(f, FileFormat.TEXT, null, true, false); |
| 95 |
ByteFile bf=ByteFile.makeByteFile(ff); |
| 96 |
for(s=bf.nextLine(); s!=null && validLines<max; s=bf.nextLine()){ |
| 97 |
if(s.length>0) { |
| 98 |
if(s[0]=='#') { |
| 99 |
if(Tools.startsWith(s, "#dims")) { |
| 100 |
matrix.dims=parseIntArray(s, delimiter, true); |
| 101 |
matrix.numInputs=matrix.dims[0]; |
| 102 |
matrix.numOutputs=matrix.dims[matrix.dims.length-1]; |
| 103 |
assert(matrix.dims.length>1) : matrix.dims.length+", "+Arrays.toString(matrix.dims)+", '"+new String(s)+"'"; |
| 104 |
}else if(Tools.startsWith(s, "#inputs")) { |
| 105 |
matrix.numInputs=parseInt(s); |
| 106 |
}else if(Tools.startsWith(s, "#outputs")) { |
| 107 |
matrix.numOutputs=parseInt(s); |
| 108 |
}else if(Tools.startsWith(s, "##")) { |
| 109 |
matrix.columns=new ArrayList<String>(Arrays.asList(new String(s).split("\t"))); |
| 110 |
matrix.columns.set(0, matrix.columns.get(0).substring(2)); |
| 111 |
}else { |
| 112 |
|
| 113 |
} |
| 114 |
}else { |
| 115 |
if(matrix.numInputs==0) { |
| 116 |
int terms=Tools.split(s, 0, (byte)'\t').size(); |
| 117 |
matrix.numInputs=terms-1; |
| 118 |
matrix.numOutputs=1; |
| 119 |
System.err.println("Inferring "+matrix.numInputs+" inputs, "+matrix.numOutputs+" output."); |
| 120 |
} |
| 121 |
assert(matrix.numInputs>0 & matrix.numOutputs>0) : |
| 122 |
"Number of inputs and outputs must be in data file header, e.g. '#inputs 5'"; |
| 123 |
float[] inputs=new float[matrix.numInputs]; |
| 124 |
float[] outputs=new float[matrix.numOutputs]; |
| 125 |
boolean valid=parseDataLine(s, inputs, outputs); |
| 126 |
if(valid) { |
| 127 |
inputList.add(inputs); |
| 128 |
outputList.add(outputs); |
| 129 |
validLines++; |
| 130 |
}else { |
| 131 |
invalidLines++; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
bf.close(); |
| 137 |
if(validLines>=max) {break;} |
| 138 |
} |
| 139 |
if(shuffleRaw) {shuffle(inputList, outputList, maxLines);} |
| 140 |
if(balance>0) {balance(inputList, outputList, balance);} |
| 141 |
matrix.inputs=new float[inputList.size()][]; |
| 142 |
matrix.outputs=new float[outputList.size()][]; |
| 143 |
for(int i=0; i<matrix.inputs.length; i++) { |
| 144 |
matrix.inputs[i]=inputList.get(i); |
| 145 |
matrix.outputs[i]=outputList.get(i); |
| 146 |
} |
| 147 |
matrix.data=new float[][][] {matrix.inputs, matrix.outputs}; |
| 148 |
matrix.initializeRange(); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
private static void shuffle(ArrayList<float[]> inputList, ArrayList<float[]> outputList, int maxLines) { |
| 155 |
ArrayList<Pair> list=new ArrayList<Pair>(inputList.size()); |
| 156 |
for(int i=0; i<inputList.size(); i++){ |
| 157 |
Pair p=new Pair(inputList.get(i), outputList.get(i)); |
| 158 |
list.add(p); |
| 159 |
} |
| 160 |
Random randy=new Random(SampleSet.shuffleSeed); |
| 161 |
Collections.shuffle(list, randy); |
| 162 |
|
| 163 |
inputList.clear(); |
| 164 |
outputList.clear(); |
| 165 |
for(int i=0, lim=Tools.min(maxLines, list.size()); i<lim; i++) { |
| 166 |
Pair p=list.get(i); |
| 167 |
inputList.add(p.in); |
| 168 |
outputList.add(p.out); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
private static void balance(ArrayList<float[]> inputList, ArrayList<float[]> outputList, float mult) { |
| 173 |
int pos=0, neg=0; |
| 174 |
assert(mult>0 && mult<=1); |
| 175 |
for(float[] out : outputList) { |
| 176 |
if(out[0]>=0.5f) {pos++;} else {neg++;} |
| 177 |
} |
| 178 |
final int target=(int)(Tools.max(pos, neg)*mult); |
| 179 |
if(pos>=target && neg>=target) {return;} |
| 180 |
assert(pos>=target || neg>=target); |
| 181 |
if(pos<1 || neg<1) { |
| 182 |
throw new RuntimeException("Can't balance with zero examples: pos="+pos+", neg="+neg); |
| 183 |
} |
| 184 |
for(int i=0; i<inputList.size() && (pos<target || neg<target); i++) { |
| 185 |
float[] in=inputList.get(i), out=outputList.get(i); |
| 186 |
if(out[0]>=0.5 && pos<target) { |
| 187 |
pos++; |
| 188 |
inputList.add(in); |
| 189 |
outputList.add(out); |
| 190 |
}else if(out[0]<0.5f && neg<target) { |
| 191 |
pos++; |
| 192 |
inputList.add(in); |
| 193 |
outputList.add(out); |
| 194 |
} |
| 195 |
} |
| 196 |
shuffle(inputList, outputList, inputList.size()); |
| 197 |
} |
| 198 |
|
| 199 |
boolean parseDataLine(byte[] line, float[] inputs, float[] outputs) { |
| 200 |
int a=0, b=0; |
| 201 |
for(int i=0; i<matrix.numInputs; i++) { |
| 202 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 203 |
if(b<=a && IGNORE_BAD_LINES) {return false;} |
| 204 |
assert(b>a) : "Missing input "+i+" : '"+new String(line)+"'"; |
| 205 |
float value=Parse.parseFloat(line, a, b); |
| 206 |
b++; |
| 207 |
a=b; |
| 208 |
inputs[i]=value; |
| 209 |
} |
| 210 |
for(int i=0; i<matrix.numOutputs; i++) { |
| 211 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 212 |
if(b<=a && IGNORE_BAD_LINES) {return false;} |
| 213 |
assert(b>a) : "Missing output "+i+" : '"+new String(line)+"'"; |
| 214 |
float value=Parse.parseFloat(line, a, b); |
| 215 |
b++; |
| 216 |
a=b; |
| 217 |
outputs[i]=value; |
| 218 |
} |
| 219 |
|
| 220 |
if(b<=line.length && IGNORE_BAD_LINES) {return false;} |
| 221 |
assert(b>line.length) : "\nExtra characters for line '"+new String(line)+ |
| 222 |
"'; numInputs="+matrix.numInputs+", numOutputs="+matrix.numOutputs+ |
| 223 |
", "+b+", "+line.length+"\n"+Arrays.toString(inputs)+"\n"+Arrays.toString(outputs)+"\n"; |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private static int parseInt(byte[] line){ |
| 232 |
int idx=Tools.indexOf(line, delimiter); |
| 233 |
return Parse.parseInt(line, idx+1, line.length); |
| 234 |
} |
| 235 |
|
| 236 |
public static int[] parseIntArray(final byte[] line, final byte delimiter, boolean parseTitle){ |
| 237 |
int a=0, b=0; |
| 238 |
IntList list=new IntList(3); |
| 239 |
|
| 240 |
if(parseTitle) { |
| 241 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 242 |
assert(b>a) : "Missing Title: "+new String(line); |
| 243 |
b++; |
| 244 |
a=b; |
| 245 |
} |
| 246 |
|
| 247 |
while(a<line.length) { |
| 248 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 249 |
assert(b>a) : "Missing element "+list.size+": '"+new String(line)+"'"; |
| 250 |
int x=Parse.parseInt(line, a, b); |
| 251 |
|
| 252 |
list.add(x); |
| 253 |
b++; |
| 254 |
a=b; |
| 255 |
} |
| 256 |
return list.toArray(); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
private static class Pair{ |
| 262 |
Pair(float[] in_, float[] out_){ |
| 263 |
in=in_; |
| 264 |
out=out_; |
| 265 |
} |
| 266 |
final float[] in, out; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
String fnames[]; |
| 275 |
Matrix matrix; |
| 276 |
|
| 277 |
int pos=0; |
| 278 |
long validLines=0; |
| 279 |
long invalidLines=0; |
| 280 |
|
| 281 |
static long lastValidLines=0; |
| 282 |
static long lastInvalidLines=0; |
| 283 |
|
| 284 |
public static final byte delimiter='\t'; |
| 285 |
public static boolean IGNORE_BAD_LINES=false; |
| 286 |
|
| 287 |
|
| 288 |
} |