| 1 |
package ml; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.Collections; |
| 7 |
import java.util.HashMap; |
| 8 |
import java.util.Random; |
| 9 |
import java.util.concurrent.ArrayBlockingQueue; |
| 10 |
import java.util.concurrent.locks.ReadWriteLock; |
| 11 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 12 |
|
| 13 |
import fileIO.ByteFile; |
| 14 |
import fileIO.ByteStreamWriter; |
| 15 |
import fileIO.FileFormat; |
| 16 |
import fileIO.ReadWrite; |
| 17 |
import shared.Parse; |
| 18 |
import shared.Parser; |
| 19 |
import shared.PreParser; |
| 20 |
import shared.Shared; |
| 21 |
import shared.Timer; |
| 22 |
import shared.Tools; |
| 23 |
import structures.ByteBuilder; |
| 24 |
import structures.LongList; |
| 25 |
import template.Accumulator; |
| 26 |
import template.ThreadWaiter; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class Trainer implements Accumulator<WorkerThread> { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static void main(String[] args){ |
| 46 |
|
| 47 |
Timer t=new Timer(); |
| 48 |
|
| 49 |
|
| 50 |
Trainer x=new Trainer(args); |
| 51 |
|
| 52 |
|
| 53 |
x.process(t); |
| 54 |
|
| 55 |
|
| 56 |
Shared.closeStream(x.outstream); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public Trainer(String[] args){ |
| 64 |
|
| 65 |
{ |
| 66 |
PreParser.printExecuting=false; |
| 67 |
Parser.printSetThreads=false; |
| 68 |
PreParser pp=new PreParser(args, getClass(), false); |
| 69 |
args=pp.args; |
| 70 |
outstream=pp.outstream; |
| 71 |
|
| 72 |
commandLine="#CL "+Shared.fullCommandline(false, true); |
| 73 |
commands=new ArrayList<String>(1); |
| 74 |
commands.add(commandLine); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 79 |
ReadWrite.setZipThreads(Shared.threads()); |
| 80 |
|
| 81 |
{ |
| 82 |
final Parser parser=parse(args); |
| 83 |
overwrite=parser.overwrite; |
| 84 |
append=parser.append; |
| 85 |
|
| 86 |
if(netIn==null) {netIn=parser.in1;} |
| 87 |
if(dataIn==null) {dataIn=parser.in2;} |
| 88 |
|
| 89 |
} |
| 90 |
if(netOutBest==null && netOutFinal!=null) { |
| 91 |
netOutBest=netOutFinal; |
| 92 |
} |
| 93 |
|
| 94 |
validateParams(); |
| 95 |
fixExtensions(); |
| 96 |
checkFileExistence(); |
| 97 |
checkStatics(); |
| 98 |
|
| 99 |
ffDataOut=FileFormat.testOutput(dataOut, FileFormat.TXT, null, true, overwrite, false, false); |
| 100 |
ffNetOutFinal=FileFormat.testOutput(netOutFinal, FileFormat.TXT, null, true, true, false, false); |
| 101 |
ffNetOutBest=FileFormat.testOutput(netOutBest, FileFormat.TXT, null, true, true, false, false); |
| 102 |
if(ffNetOutBest!=null && ffNetOutBest.exists() && !overwrite) { |
| 103 |
throw new RuntimeException(ffNetOutBest.name()+" exists and overwrite = "+overwrite); |
| 104 |
} |
| 105 |
|
| 106 |
ffNetIn=FileFormat.testInput(netIn, FileFormat.TXT, null, true, true); |
| 107 |
ffDataIn=FileFormat.testInput(dataIn, FileFormat.TXT, null, true, true); |
| 108 |
ffValidateIn=FileFormat.testInput(validateIn, FileFormat.TXT, null, true, true); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
alphaIncrease=alphaZero*(alphaMult-1.0)/(peakAlphaEpoch); |
| 114 |
alphaEpochs=(Tools.min(maxEpochs, 800000)-peakAlphaEpoch); |
| 115 |
alphaDropoff=1.0/Math.pow(alphaMult2*alphaMult, 1.0/alphaEpochs); |
| 116 |
|
| 117 |
randyNetSeed=(netSeed0<0 ? new Random() : new Random(netSeed0)); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
maxAnnealEpoch=(maxAnnealEpoch<Integer.MAX_VALUE ? maxAnnealEpoch : (int)(maxAnnealEpochMult*maxEpochs)); |
| 122 |
fpeStart=Tools.min(maxEpochs, fpeStart); |
| 123 |
|
| 124 |
if(targetFPR>=0) {CellNet.compareCode=CellNet.compareFNR;} |
| 125 |
else if(targetFNR>=0) {CellNet.compareCode=CellNet.compareFPR;} |
| 126 |
else if(crossoverFpMult>0) {CellNet.compareCode=CellNet.compareFPR;} |
| 127 |
else{CellNet.compareCode=CellNet.compareWER;} |
| 128 |
|
| 129 |
printFPR=targetFPR<0 || forcePrintFPR; |
| 130 |
printFNR=targetFNR<0 || forcePrintFNR; |
| 131 |
|
| 132 |
int[] dims=(dims0==null ? minDims : dims0); |
| 133 |
if(dims!=null) { |
| 134 |
numLayers=dims.length; |
| 135 |
numInputs=dims[0]; |
| 136 |
numOutputs=dims[numLayers-1]; |
| 137 |
} |
| 138 |
if(dumpEpoch<=0 && dumpRate>0){ |
| 139 |
dumpEpoch=(int)(dumpEpochMult*maxEpochs); |
| 140 |
} |
| 141 |
|
| 142 |
if(maxLines<0){maxLines=Shared.MAX_ARRAY_LEN;} |
| 143 |
if(maxLinesV<0){maxLinesV=maxLines;} |
| 144 |
|
| 145 |
if(startTriage<0 && startTriageMult>=0){ |
| 146 |
startTriage=(int)(startTriageMult*maxEpochs); |
| 147 |
} |
| 148 |
|
| 149 |
if(minWeightEpoch<0 && minWeightEpochMult>=0){ |
| 150 |
minWeightEpoch=(int)(minWeightEpochMult*maxEpochs); |
| 151 |
} |
| 152 |
|
| 153 |
Function.normalizeTypeRates(); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private Parser parse(String[] args){ |
| 162 |
|
| 163 |
|
| 164 |
Parser parser=new Parser(); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
for(int i=0; i<args.length; i++){ |
| 172 |
String arg=args[i]; |
| 173 |
|
| 174 |
|
| 175 |
String[] split=arg.split("="); |
| 176 |
String a=split[0].toLowerCase(); |
| 177 |
String b=split.length>1 ? split[1] : null; |
| 178 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if(a.equals("verbose")){ |
| 183 |
verbose=Parse.parseBoolean(b); |
| 184 |
}else if(a.equals("train") || a.equals("training")){ |
| 185 |
training=Parse.parseBoolean(b); |
| 186 |
}else if(a.equals("evaluate") || a.equals("test")){ |
| 187 |
training=!Parse.parseBoolean(b); |
| 188 |
}else if(a.equals("numnetworks") || a.equals("numnets") || a.equals("nets") || a.equals("networks")){ |
| 189 |
networksPerCycle=Parse.parseIntKMG(b); |
| 190 |
}else if(a.equals("cycles")){ |
| 191 |
cycles=Parse.parseIntKMG(b); |
| 192 |
}else if(a.equals("scan") || a.equals("seedstoscan")){ |
| 193 |
seedsToScan=Parse.parseIntKMG(b); |
| 194 |
}else if(a.equals("scanepochs") || a.equals("scanbatches")){ |
| 195 |
scanEpochs=Parse.parseIntKMG(b); |
| 196 |
}else if(a.equals("scansamples")){ |
| 197 |
scanSamples=Parse.parseIntKMG(b); |
| 198 |
}else if(a.equals("scanmult")){ |
| 199 |
seedsToScanMult=Float.parseFloat(b); |
| 200 |
|
| 201 |
|
| 202 |
}else if(a.equals("balance") || a.equals("balanced")){ |
| 203 |
if(b==null || Tools.startsWithLetter(b)) { |
| 204 |
balance=Parse.parseBoolean(b) ? 1.0f : 0; |
| 205 |
}else{ |
| 206 |
balance=Float.parseFloat(b); |
| 207 |
} |
| 208 |
}else if(a.equals("normalizebias")){ |
| 209 |
CellNet.NORMALIZE_BIAS=Parse.parseBoolean(b); |
| 210 |
}else if(a.equals("normalizeweights")){ |
| 211 |
CellNet.NORMALIZE_WEIGHTS=Parse.parseBoolean(b); |
| 212 |
}else if(a.equals("normalize")){ |
| 213 |
CellNet.NORMALIZE_BIAS=CellNet.NORMALIZE_WEIGHTS=Parse.parseBoolean(b); |
| 214 |
}else if(a.equals("concise")){ |
| 215 |
CellNet.CONCISE=Parse.parseBoolean(b); |
| 216 |
}else if(a.equals("dense")){ |
| 217 |
CellNet.DENSE=Parse.parseBoolean(b); |
| 218 |
}else if(a.equals("sparse")){ |
| 219 |
CellNet.DENSE=!Parse.parseBoolean(b); |
| 220 |
}else if(a.equals("outsparse")){ |
| 221 |
CellNet.OUT_SPARSE=Parse.parseBoolean(b); |
| 222 |
if(CellNet.OUT_SPARSE) {CellNet.OUT_DENSE=false;} |
| 223 |
}else if(a.equals("outdense")){ |
| 224 |
CellNet.OUT_DENSE=Parse.parseBoolean(b); |
| 225 |
if(CellNet.OUT_DENSE) {CellNet.OUT_SPARSE=false;} |
| 226 |
}else if(a.equals("outhex")){ |
| 227 |
CellNet.OUT_HEX=Parse.parseBoolean(b); |
| 228 |
}else if(a.equals("print") || a.equals("printstatus")){ |
| 229 |
printStatus=Parse.parseBoolean(b); |
| 230 |
}else if(a.equals("machineout")){ |
| 231 |
machineOut=Parse.parseBoolean(b); |
| 232 |
}else if(a.equals("printall")){ |
| 233 |
printMode=(Parse.parseBoolean(b) ? printAll : printBest); |
| 234 |
}else if(a.equals("printbest")){ |
| 235 |
printMode=(Parse.parseBoolean(b) ? printBest : printBest); |
| 236 |
}else if(a.equals("printfirst")){ |
| 237 |
printMode=(Parse.parseBoolean(b) ? printFirst : printBest); |
| 238 |
}else if(a.equals("printaverage") || a.equals("printavg")){ |
| 239 |
printMode=(Parse.parseBoolean(b) ? printAverage : printBest); |
| 240 |
}else if(a.equals("printinterval") || a.equals("printerval")){ |
| 241 |
printInterval=Parse.parseIntKMG(b); |
| 242 |
}else if(a.equals("ignorebadlines")){ |
| 243 |
DataLoader.IGNORE_BAD_LINES=Parse.parseBoolean(b); |
| 244 |
}else if(a.equals("lines") || a.equals("maxlines") || a.equals("samples") || a.equals("maxsamples")){ |
| 245 |
maxLines=Parse.parseIntKMG(b); |
| 246 |
if(maxLines<0){maxLines=Integer.MAX_VALUE;} |
| 247 |
|
| 248 |
}else if(a.equals("vlines") || a.equals("vsamples")){ |
| 249 |
maxLinesV=Parse.parseIntKMG(b); |
| 250 |
if(maxLinesV<0){maxLinesV=Integer.MAX_VALUE;} |
| 251 |
|
| 252 |
}else if(a.equals("shuffleraw")){ |
| 253 |
shuffleRaw=Parse.parseBoolean(b); |
| 254 |
|
| 255 |
}else if(a.equals("copynet")){ |
| 256 |
copyNetInWorkerThread=Parse.parseBoolean(b); |
| 257 |
}else if(a.equals("setnet")){ |
| 258 |
setNetInWorkerThread=Parse.parseBoolean(b); |
| 259 |
|
| 260 |
}else if(a.equals("dump") || a.equals("dumprate")){ |
| 261 |
if(b!=null && b.length()>0 && Character.isLetter(b.charAt(0))){ |
| 262 |
boolean x=Parse.parseBoolean(b); |
| 263 |
dumpRate=x ? 0.5f : 0f; |
| 264 |
}else { |
| 265 |
dumpRate=Float.parseFloat(b); |
| 266 |
} |
| 267 |
}else if(a.equals("dumpepoch") || a.equals("dumpbatch")){ |
| 268 |
if(b.indexOf('.')<0){ |
| 269 |
dumpEpoch=Parse.parseIntKMG(b); |
| 270 |
}else{ |
| 271 |
dumpEpochMult=Float.parseFloat(b); |
| 272 |
} |
| 273 |
}else if(a.equals("dumpepochmult") || a.equals("dumpbatchmult")){ |
| 274 |
dumpEpochMult=Float.parseFloat(b); |
| 275 |
}else if(a.equals("partialdump")){ |
| 276 |
if(b==null || Character.isLetter(b.charAt(0))) { |
| 277 |
partialDumpFraction=(Parse.parseBoolean(b) ? partialDumpFraction : 1.0f); |
| 278 |
}else { |
| 279 |
partialDumpFraction=Float.parseFloat(b); |
| 280 |
} |
| 281 |
}else if(a.equals("halfdump")){ |
| 282 |
if(b==null || Character.isLetter(b.charAt(0))) { |
| 283 |
partialDumpFraction=(Parse.parseBoolean(b) ? 0.5f : 1.0f); |
| 284 |
}else { |
| 285 |
partialDumpFraction=Float.parseFloat(b); |
| 286 |
} |
| 287 |
|
| 288 |
}else if(a.equals("exclusive")){ |
| 289 |
exclusive=Parse.parseBoolean(b); |
| 290 |
}else if(a.equals("inclusive")){ |
| 291 |
exclusive=!Parse.parseBoolean(b); |
| 292 |
}else if(a.equals("validatefraction") || a.equals("vfraction")) { |
| 293 |
validateFraction=Float.parseFloat(b); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
}else if(a.equals("samplesperepoch") || a.equals("spe") || a.equals("samplesperbatch") || a.equals("spb") || a.equals("batchsize")){ |
| 299 |
|
| 300 |
|
| 301 |
assert(false) : "lpe disabled"; |
| 302 |
}else if(a.equals("fractionperepoch") || a.equals("fpe") || a.equals("fractionperbatch") || a.equals("fpb")){ |
| 303 |
fractionPerEpoch0=Float.parseFloat(b); |
| 304 |
}else if(a.equals("fractionperepoch2") || a.equals("fpe2") || a.equals("fractionperbatch2") || a.equals("fpb2")){ |
| 305 |
fractionPerEpoch2=Float.parseFloat(b); |
| 306 |
}else if(a.equals("fpestart") || a.equals("fractionalstart")){ |
| 307 |
fpeStart=Parse.parseIntKMG(b); |
| 308 |
}else if(a.equals("fptriage") || a.equals("ptriage")){ |
| 309 |
positiveTriage=Float.parseFloat(b); |
| 310 |
}else if(a.equals("fntriage") || a.equals("ntriage")){ |
| 311 |
negativeTriage=Float.parseFloat(b); |
| 312 |
}else if(a.equals("triage")){ |
| 313 |
positiveTriage=negativeTriage=Float.parseFloat(b); |
| 314 |
}else if(a.equals("starttriage") || a.equals("triagestart")){ |
| 315 |
startTriage=Parse.parseIntKMG(b); |
| 316 |
}else if(a.equals("starttriagemult") || a.equals("triagestartmult")){ |
| 317 |
startTriageMult=Float.parseFloat(b); |
| 318 |
|
| 319 |
}else if(a.equals("minweightepoch") || a.equals("minweightbatch") || a.equals("weightstart")){ |
| 320 |
minWeightEpoch=Parse.parseIntKMG(b); |
| 321 |
}else if(a.equals("minweightepochmult") || a.equals("minweightbatchmult") || a.equals("weightstartmult")){ |
| 322 |
minWeightEpochMult=Float.parseFloat(b); |
| 323 |
|
| 324 |
}else if(a.equals("lowweightannealcutoff") || a.equals("lwac")){ |
| 325 |
Cell.setLowWeightAnnealCutoff(Float.parseFloat(b)); |
| 326 |
}else if(a.equals("startanneal") || a.equals("annealstart") || a.equals("minannealepoch")){ |
| 327 |
minAnnealEpoch=Parse.parseIntKMG(b); |
| 328 |
}else if(a.equals("stopanneal") || a.equals("annealstop") || a.equals("maxannealepoch")) { |
| 329 |
if(b.indexOf('.')>=0){ |
| 330 |
maxAnnealEpochMult=Float.parseFloat(b); |
| 331 |
maxAnnealEpoch=Integer.MAX_VALUE; |
| 332 |
}else{ |
| 333 |
maxAnnealEpoch=Parse.parseIntKMG(b); |
| 334 |
} |
| 335 |
}else if(a.equals("netseed")){ |
| 336 |
netSeed0=Long.parseLong(b); |
| 337 |
setNetSeed=true; |
| 338 |
}else if(a.equals("annealseed")){ |
| 339 |
assert(false) : "annealseed is no longer supported."; |
| 340 |
|
| 341 |
|
| 342 |
}else if(a.equals("shuffleseed")){ |
| 343 |
SampleSet.shuffleSeed=Long.parseLong(b); |
| 344 |
}else if(a.equals("seed")){ |
| 345 |
netSeed0=Long.parseLong(b); |
| 346 |
setNetSeed=true; |
| 347 |
|
| 348 |
|
| 349 |
}else if(a.equals("density") || a.equals("maxdensity")){ |
| 350 |
maxDensity=Float.parseFloat(b); |
| 351 |
assert(maxDensity>0 && maxDensity<=1) : "Density must be between 0 and 1: "+maxDensity; |
| 352 |
|
| 353 |
}else if(a.equals("density1")){ |
| 354 |
maxDensity1=Float.parseFloat(b); |
| 355 |
}else if(a.equals("densityarray")){ |
| 356 |
densityArray=Parse.parseFloatArray(b, ",", "x"); |
| 357 |
}else if(a.equals("edges") || a.equals("maxedges")){ |
| 358 |
maxEdges=Parse.parseKMG(b); |
| 359 |
|
| 360 |
}else if(a.equals("edgeblocksize") || a.equals("blocksize") || a.equals("quant") || a.equals("ebs")){ |
| 361 |
edgeBlockSize=Parse.parseIntKMG(b); |
| 362 |
} |
| 363 |
|
| 364 |
else if(a.equals("dims") || a.equals("dimensions")){ |
| 365 |
dims0=Parse.parseIntArray(b, ",", "x"); |
| 366 |
}else if(a.equals("mindims") || a.equals("mindimensions")){ |
| 367 |
minDims=Parse.parseIntArray(b, ",", "x"); |
| 368 |
}else if(a.equals("maxdims") || a.equals("maxdimensions")){ |
| 369 |
maxDims=Parse.parseIntArray(b, ",", "x"); |
| 370 |
}else if(a.equals("in") || a.equals("data") || a.equals("din")){ |
| 371 |
dataIn=b; |
| 372 |
}else if(a.equals("validateset") || a.equals("validate") || a.equals("vin") || a.equals("vset")){ |
| 373 |
validateIn=b; |
| 374 |
}else if(a.equals("net") || a.equals("network") || a.equals("netin") || a.equals("networkin")){ |
| 375 |
netIn=b; |
| 376 |
}else if(a.equals("out") || a.equals("netout") || a.equals("networkout") || a.equals("outfinal") || a.equals("outf")){ |
| 377 |
netOutFinal=b; |
| 378 |
}else if(a.equals("outb") || a.equals("outbest") || a.equals("netoutbest")){ |
| 379 |
netOutBest=b; |
| 380 |
}else if(a.equals("epochs") || a.equals("maxepochs") || a.equals("batches")){ |
| 381 |
maxEpochs=Parse.parseIntKMG(b); |
| 382 |
}else if(a.equals("subsets") || a.equals("sets")){ |
| 383 |
subsets=Parse.parseIntKMG(b); |
| 384 |
if(subsets>0) {setsize=-1;} |
| 385 |
}else if(a.equals("setsize")){ |
| 386 |
setsize=Parse.parseIntKMG(b); |
| 387 |
if(setsize>0) {subsets=-1;} |
| 388 |
}else if(a.equals("crossover") || a.equals("xvr")){ |
| 389 |
if(b==null || Tools.startsWithLetter(b)) { |
| 390 |
boolean x=Parse.parseBoolean(b); |
| 391 |
if(x) { |
| 392 |
if(crossoverFpMult<=0) {crossoverFpMult=1;} |
| 393 |
}else { |
| 394 |
if(crossoverFpMult>0) {crossoverFpMult=-1;} |
| 395 |
} |
| 396 |
}else { |
| 397 |
crossoverFpMult=Float.parseFloat(b); |
| 398 |
} |
| 399 |
if(crossoverFpMult>0) {targetFPR=targetFNR=-1;} |
| 400 |
}else if(a.equals("fpmult")){ |
| 401 |
crossoverFpMult=Float.parseFloat(b); |
| 402 |
if(crossoverFpMult>0) {targetFPR=targetFNR=-1;} |
| 403 |
}else if(a.equals("error") || a.equals("targeterror")){ |
| 404 |
targetError=Float.parseFloat(b); |
| 405 |
}else if(a.equals("targetfpr") || a.equals("fpr")){ |
| 406 |
targetFPR=Float.parseFloat(b); |
| 407 |
if(targetFPR>0) {crossoverFpMult=targetFNR=-1;} |
| 408 |
}else if(a.equals("targetfnr") || a.equals("fnr")){ |
| 409 |
targetFNR=Float.parseFloat(b); |
| 410 |
if(targetFNR>0) {targetFPR=crossoverFpMult=-1;} |
| 411 |
}else if(a.equals("printtpr")){boolean printAlpha=true, printAnneal=true; |
| 412 |
printTPR=Parse.parseBoolean(b); |
| 413 |
}else if(a.equals("printtnr")){ |
| 414 |
printTNR=Parse.parseBoolean(b); |
| 415 |
}else if(a.equals("printalpha") || a.equals("printalp")){ |
| 416 |
printAlpha=Parse.parseBoolean(b); |
| 417 |
}else if(a.equals("printanneal") || a.equals("printann")){ |
| 418 |
printAnneal=Parse.parseBoolean(b); |
| 419 |
}else if(a.equals("alpha")){ |
| 420 |
alphaZero=Float.parseFloat(b); |
| 421 |
}else if(a.equals("alphamult")){ |
| 422 |
alphaMult=Float.parseFloat(b); |
| 423 |
}else if(a.equals("alphamult2")){ |
| 424 |
alphaMult2=Float.parseFloat(b); |
| 425 |
}else if(a.equals("anneal") || a.equals("annealstrength")){ |
| 426 |
if(b==null || b.length()<1 || Tools.startsWithLetter(b)) { |
| 427 |
annealStrength0=Parse.parseBoolean(b) ? 0.04f : 0; |
| 428 |
}else{ |
| 429 |
annealStrength0=Float.parseFloat(b); |
| 430 |
} |
| 431 |
}else if(a.equals("annealprob") || a.equals("annealrate")) { |
| 432 |
annealProb=Float.parseFloat(b); |
| 433 |
}else if(a.equals("annealmult2")) { |
| 434 |
annealMult2=Float.parseFloat(b); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
}else if(a.equals("biasalphamult")){ |
| 439 |
Cell.biasAlphaMult=Float.parseFloat(b); |
| 440 |
}else if(a.equals("biasannealmult")){ |
| 441 |
Cell.biasAnnealMult=Float.parseFloat(b); |
| 442 |
}else if(a.equals("annealbias")){ |
| 443 |
Cell.annealBias=Parse.parseBoolean(b); |
| 444 |
}else if(a.equals("sortall")){ |
| 445 |
sortAll=Parse.parseBoolean(b); |
| 446 |
}else if(a.equals("sort")){ |
| 447 |
sort=Parse.parseBoolean(b); |
| 448 |
}else if(a.equals("sortmt") || a.equals("mtsort")){ |
| 449 |
allowMTSort=Parse.parseBoolean(b); |
| 450 |
}else if(a.equals("sortinthread")){ |
| 451 |
sortInThread=Parse.parseBoolean(b); |
| 452 |
}else if(a.equals("pivotsort")){ |
| 453 |
PIVOT_SORT=Parse.parseBoolean(b); |
| 454 |
}else if(a.equals("launchinthread")){ |
| 455 |
launchInThread=Parse.parseBoolean(b); |
| 456 |
}else if(a.equals("setlock") || a.equals("usesetlock")){ |
| 457 |
useSetLock=Parse.parseBoolean(b); |
| 458 |
}else if(a.equals("shuffle")){ |
| 459 |
SampleSet.shuffle=Parse.parseBoolean(b); |
| 460 |
}else if(a.equals("shuffle2") || a.equals("shufflesubset")){ |
| 461 |
shuffleSubset=Parse.parseBoolean(b); |
| 462 |
}else if(a.equals("shufflemod")){ |
| 463 |
SHUFFLEMOD=Integer.parseInt(b); |
| 464 |
}else if(a.equals("ordered")){ |
| 465 |
|
| 466 |
orderedJobs=Parse.parseBoolean(b); |
| 467 |
}else if(a.equals("peakalphaepoch") || a.equals("peakalpha") || a.equals("pae") || a.equals("peakalphabatch") || a.equals("peakalpha") || a.equals("pab")){ |
| 468 |
peakAlphaEpoch=Parse.parseIntKMG(b); |
| 469 |
}else if(a.equals("alphadropoff")){ |
| 470 |
|
| 471 |
}else if(a.equals("alphamult")){ |
| 472 |
alphaMult=Float.parseFloat(b); |
| 473 |
} |
| 474 |
|
| 475 |
else if(a.equals("booleancutoff") || a.equals("cutoff") || a.equals("thresh")){ |
| 476 |
cutoffForEvaluation=Cell.cutoffForTraining=Float.parseFloat(b); |
| 477 |
setCutoffForEvaluation=Cell.setCutoffForTraining=true; |
| 478 |
}else if(a.equals("usemidpoint")){ |
| 479 |
Cell.useMidpoint=Parse.parseBoolean(b); |
| 480 |
}else if(a.equals("booleancutoffbackprop") || a.equals("cutoffbackprop") || a.equals("cutofferror") |
| 481 |
|| a.equals("threshbackprop") || a.equals("trainingcutoff") || a.equals("cutofftraining")){ |
| 482 |
Cell.cutoffForTraining=Float.parseFloat(b); |
| 483 |
Cell.setCutoffForTraining=true; |
| 484 |
}else if(a.equals("cutoffeval") || a.equals("evalcutoff")){ |
| 485 |
cutoffForEvaluation=Float.parseFloat(b); |
| 486 |
setCutoffForEvaluation=true; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
else if(a.equals("positiveerrormult") || a.equals("pem")){ |
| 494 |
Cell.positiveErrorMult=Float.parseFloat(b); |
| 495 |
}else if(a.equals("falsepositiveerrormult") || a.equals("fpem")){ |
| 496 |
Cell.falsePositiveErrorMult=Float.parseFloat(b); |
| 497 |
}else if(a.equals("excesspositiveerrormult") || a.equals("epem")){ |
| 498 |
Cell.excessPositiveErrorMult=Float.parseFloat(b); |
| 499 |
}else if(a.equals("negativeerrormult") || a.equals("nem")){ |
| 500 |
Cell.negativeErrorMult=Float.parseFloat(b); |
| 501 |
}else if(a.equals("falsenegativeerrormult") || a.equals("fnem")){ |
| 502 |
Cell.falseNegativeErrorMult=Float.parseFloat(b); |
| 503 |
}else if(a.equals("excessnegativeerrormult") || a.equals("enem")){ |
| 504 |
Cell.excessNegativeErrorMult=Float.parseFloat(b); |
| 505 |
}else if(a.equals("falsepositeverrorincr") || a.equals("fpei")){ |
| 506 |
Cell.fpErrorIncr=Float.parseFloat(b); |
| 507 |
}else if(a.equals("falsenegativeerrorincr") || a.equals("fnei")){ |
| 508 |
Cell.fnErrorIncr=Float.parseFloat(b); |
| 509 |
}else if(a.equals("spread")){ |
| 510 |
Cell.spread=Float.parseFloat(b); |
| 511 |
}else if(a.equals("excesspivotmult") || a.equals("pivotmult") || a.equals("epm")){ |
| 512 |
Sample.excessPivotMult=Float.parseFloat(b); |
| 513 |
}else if(a.equalsIgnoreCase("edgeAmplitudeIncreaseMult") || a.equals("eaim")){ |
| 514 |
Cell.edgeAmplitudeIncreaseMult=Float.parseFloat(b); |
| 515 |
}else if(a.equalsIgnoreCase("edgeAmplitudeIncreaseThresh") || a.equals("eait")){ |
| 516 |
Cell.edgeAmplitudeIncreaseThresh=Float.parseFloat(b); |
| 517 |
} |
| 518 |
|
| 519 |
else if(a.equalsIgnoreCase("probFlat") || a.equals("pflat")){ |
| 520 |
CellNet.PROB_FLAT=Float.parseFloat(b); |
| 521 |
}else if(a.equalsIgnoreCase("probExp") || a.equals("pexp")){ |
| 522 |
CellNet.PROB_EXP=Float.parseFloat(b); |
| 523 |
}else if(a.equalsIgnoreCase("explamda") || a.equals("lamda") || a.equals("lambda")){ |
| 524 |
CellNet.EXP_LAMDA=Float.parseFloat(b); |
| 525 |
}else if(a.equalsIgnoreCase("randweightcap") || a.equalsIgnoreCase("weightcap") || a.equals("expcap")){ |
| 526 |
CellNet.RAND_WEIGHT_CAP=Float.parseFloat(b); |
| 527 |
} |
| 528 |
|
| 529 |
else if(a.equals("convertto01") || a.equals("converttoboolean")){ |
| 530 |
Matrix.convertTo01=Parse.parseBoolean(b); |
| 531 |
}else if(a.equals("outputrangemin") || a.equals("rangemin") || a.equals("minoutput")){ |
| 532 |
Matrix.targetOutputRangeMin=Float.parseFloat(b); |
| 533 |
Matrix.setTargetOutputRangeMin=true; |
| 534 |
}else if(a.equals("outputrangemax") || a.equals("rangemax") || a.equals("maxoutput")){ |
| 535 |
Matrix.targetOutputRangeMax=Float.parseFloat(b); |
| 536 |
Matrix.setTargetOutputRangeMax=true; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
else if(a.equals("profile")){ |
| 541 |
Profiler.PROFILING=Parse.parseBoolean(b); |
| 542 |
} |
| 543 |
|
| 544 |
else if(a.equals("final") || a.equals("finallayer") || a.equals("finaltype")){ |
| 545 |
Cell.finalLayerType=Function.toType(b); |
| 546 |
}else if(a.equals("type") || a.equals("defaulttype")){ |
| 547 |
Cell.defaultActivationType=Function.toType(b); |
| 548 |
}else if(Function.toType(a, false)>=0){ |
| 549 |
int type=Function.toType(a, true); |
| 550 |
if(b==null){ |
| 551 |
Cell.defaultActivationType=type; |
| 552 |
}else{ |
| 553 |
Function.TYPE_RATES[type]=Float.parseFloat(b); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
else if(a.equals("maxtype")){ |
| 559 |
Cell.MAX_TYPE=Function.toType(b); |
| 560 |
outstream.println(Cell.MAX_TYPE); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
else if(a.equals("randomtype") || a.equals("randomtypes") || a.equals("randomtyperate")){ |
| 595 |
if(b==null || Character.isLetter(b.charAt(0))) { |
| 596 |
Cell.randomTypeRate=(Parse.parseBoolean(b) ? 0.5f : 0f); |
| 597 |
}else{ |
| 598 |
Cell.randomTypeRate=Float.parseFloat(b); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
else if(parser.parse(arg, a, b)){ |
| 603 |
|
| 604 |
}else{ |
| 605 |
outstream.println("Unknown parameter "+args[i]); |
| 606 |
assert(false) : "Unknown parameter "+args[i]; |
| 607 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 608 |
} |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
if(!training){ |
| 613 |
subsets=1; |
| 614 |
alphaZero=0; |
| 615 |
annealStrength0=0; |
| 616 |
maxEpochs=1; |
| 617 |
netOutFinal=netOutBest=null; |
| 618 |
quiet=true; |
| 619 |
|
| 620 |
} |
| 621 |
if(Parser.silent) {quiet=true;} |
| 622 |
return parser; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
private void fixExtensions(){ |
| 627 |
netIn=Tools.fixExtension(netIn); |
| 628 |
dataIn=Tools.fixExtension(dataIn); |
| 629 |
validateIn=Tools.fixExtension(validateIn); |
| 630 |
if(netIn==null && dims0==null && (minDims==null || maxDims==null)){ |
| 631 |
throw new RuntimeException("Error - a net file or dims is required."); |
| 632 |
} |
| 633 |
if(dataIn==null && (validateIn==null || training)){throw new RuntimeException("Error - a data file is required.");} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
private void checkFileExistence(){ |
| 638 |
|
| 639 |
if(!Tools.testOutputFiles(overwrite, append, true, netOutFinal, netOutBest, dataOut)){ |
| 640 |
outstream.println((netOutFinal==null)+", "+netOutFinal); |
| 641 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+ |
| 642 |
netOutFinal+", "+netOutFinal+", "+dataOut+"\n"); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
if(!Tools.testInputFiles(false, false, dataIn, netIn)){ |
| 647 |
|
| 648 |
} |
| 649 |
if(netOutFinal==null && netOutBest==null && maxEpochs>1) { |
| 650 |
if(training) {outstream.println("Warning - no output file specified.");} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
if(!Tools.testForDuplicateFiles(true, dataIn, netIn, netOutFinal, dataOut)){ |
| 655 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
if(!Tools.testForDuplicateFiles(true, dataIn, netIn, netOutBest, dataOut)){ |
| 660 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
private static void checkStatics(){ |
| 666 |
|
| 667 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 668 |
ByteFile.FORCE_MODE_BF2=true; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
private boolean validateParams(){ |
| 679 |
|
| 680 |
|
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
void process(Timer t){ |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
linesProcessed=linesOut=0; |
| 696 |
bytesProcessed=bytesOut=0; |
| 697 |
|
| 698 |
int dataLines=training ? maxLines : Tools.min(maxLines, maxLinesV); |
| 699 |
|
| 700 |
if(validateIn!=null) {validateFraction=0;} |
| 701 |
SampleSet[] ssa=(dataIn==null ? null : |
| 702 |
loadData(dataIn, training, dataLines, shuffleRaw, validateFraction, maxLinesV)); |
| 703 |
|
| 704 |
data=(ssa==null ? null : ssa[0]); |
| 705 |
validateSet=(validateIn==null ? ssa[1] : |
| 706 |
loadData(validateIn, false, maxLinesV, shuffleRaw && maxLinesV<Shared.MAX_ARRAY_LEN, 0, maxLinesV)[0]); |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
if(!setCutoffForEvaluation && data!=null && Cell.useMidpoint){cutoffForEvaluation=data.outputMidpoint();} |
| 714 |
if(!Cell.setCutoffForTraining && data!=null && Cell.useMidpoint){Cell.cutoffForTraining=data.outputMidpoint();} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
spawnThreads(); |
| 720 |
|
| 721 |
writeNetwork(finalNet, ffNetOutFinal); |
| 722 |
writeData(ffDataOut); |
| 723 |
|
| 724 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
t.stop(); |
| 729 |
|
| 730 |
if(!quiet) { |
| 731 |
long bytesProcessed=linesProcessed*4*(numInputs+numOutputs); |
| 732 |
|
| 733 |
outstream.println(Tools.timeLinesProcessed(t, linesProcessed, 8)); |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
outstream.println(Tools.thingsProcessed(t.elapsed, linesProcessed*(edgesIn/networksIn), 8, "Edges")); |
| 738 |
|
| 739 |
outstream.println(); |
| 740 |
outstream.println("Lines In: \t"+(DataLoader.lastValidLines+DataLoader.lastInvalidLines)); |
| 741 |
|
| 742 |
if(DataLoader.lastInvalidLines>0) { |
| 743 |
outstream.println("Invalid Lines In: \t"+DataLoader.lastInvalidLines); |
| 744 |
} |
| 745 |
|
| 746 |
if(linesOut>0) { |
| 747 |
outstream.println("Lines Out: \t"+linesOut); |
| 748 |
outstream.println("Bytes Out: \t"+bytesOut); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
if(machineOut && bestNet!=null && bestNet.dims!=null){ |
| 753 |
String sh=toMachineHeader(bestNet.dims); |
| 754 |
String ss=toMachineOut(t); |
| 755 |
outstream.println(sh); |
| 756 |
outstream.println(ss); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
if(errorState){ |
| 761 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
private String toMachineHeader(int[] dims) { |
| 766 |
final boolean printAvg=(true || networksTested>1); |
| 767 |
final boolean printAvgDims=(true || (maxDims!=null && printAvg)); |
| 768 |
ByteBuilder bb=new ByteBuilder(); |
| 769 |
bb.append("machineheader:"); |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
bb.append("\ttime"); |
| 775 |
|
| 776 |
bb.append("\tbERR").append("\tbWER").append("\tbFPR").append("\tbFNR"); |
| 777 |
bb.append("\tbTPR").append("\tbTNR").append("\tbCTF"); |
| 778 |
|
| 779 |
if(printAvg) { |
| 780 |
bb.append("\taERR").append("\taWER").append("\taFPR").append("\taFNR"); |
| 781 |
bb.append("\taTPR").append("\taTNR").append("\taCTF"); |
| 782 |
} |
| 783 |
|
| 784 |
for(int i=1; i<dims.length-1; i++) { |
| 785 |
bb.tab().append("bD").append(i); |
| 786 |
} |
| 787 |
|
| 788 |
if(printAvgDims) { |
| 789 |
for(int i=1; i<dims.length-1; i++) { |
| 790 |
bb.tab().append("aD").append(i); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
bb.tab().append("CL"); |
| 795 |
return bb.toString(); |
| 796 |
} |
| 797 |
|
| 798 |
private String toMachineOut(Timer t) { |
| 799 |
final boolean printAvg=(true || networksTested>1); |
| 800 |
final boolean printAvgDims=(true || (maxDims!=null && printAvg)); |
| 801 |
StringBuilder sb=new StringBuilder(); |
| 802 |
sb.append("machineout:"); |
| 803 |
sb.append('\t').append(t.timeInSeconds()); |
| 804 |
|
| 805 |
String bdims=this.genDimsString(bestNet.dims, true); |
| 806 |
String adims=""; |
| 807 |
if(printAvgDims) { |
| 808 |
adims=(finalNets!=null && finalNets.size()>1 ? this.genAvgDimsString(finalNets, true) : bdims); |
| 809 |
} |
| 810 |
String bstats=genPrintStringMachine(bestNet); |
| 811 |
|
| 812 |
CellNet avg=finalNet; |
| 813 |
if(finalNets!=null && finalNets.size()>1){avg=makeAvg(finalNets);} |
| 814 |
String astats=genPrintStringMachine(avg); |
| 815 |
|
| 816 |
sb.append('\t').append(bstats); |
| 817 |
if(printAvg) {sb.append('\t').append(astats);} |
| 818 |
sb.append('\t').append(bdims); |
| 819 |
if(printAvgDims) {sb.append('\t').append(adims);} |
| 820 |
sb.append('\t').append(commandLine); |
| 821 |
|
| 822 |
return sb.toString(); |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
private void spawnThreads(){ |
| 831 |
|
| 832 |
|
| 833 |
annealStrength=annealStrength0; |
| 834 |
int annealEpochs=Tools.min(maxEpochs, maxAnnealEpoch)-minAnnealEpoch; |
| 835 |
annealDropoff=1.0/Math.pow(annealMult2, 1.0/annealEpochs); |
| 836 |
annealDropoff0=annealDropoff; |
| 837 |
|
| 838 |
|
| 839 |
final int samples=(data!=null ? data.samples.length : validateSet.samples.length); |
| 840 |
threads=Tools.mid(1, Shared.threads(), Tools.max(samples, validateSet.samples.length)); |
| 841 |
jobsPerEpoch=Tools.mid(1, threads, (int)(4*(threads+(networksPerCycle-1))/(1.6*networksPerCycle))); |
| 842 |
int seeds=calcSeedsToScan(); |
| 843 |
int nets=networksPerCycle*cycles; |
| 844 |
String ss=(seeds>nets ? seeds+" seeds, " : ""); |
| 845 |
if(!quiet) { |
| 846 |
outstream.println("Using "+plural("worker thread", threads)+", " |
| 847 |
+plural("trainer thread", networksPerCycle)+", "+ss+plural("job", jobsPerEpoch)+"/trainer/batch" |
| 848 |
+", "+pluralES("batch", maxEpochs)+", "+plural("cycle", cycles) |
| 849 |
+" to train "+plural("network", networksPerCycle*cycles)+" total."); |
| 850 |
} |
| 851 |
|
| 852 |
networkQueue=new ArrayBlockingQueue<CellNet>(networksPerCycle); |
| 853 |
workerQueue=new ArrayBlockingQueue<JobData>(jobsPerEpoch*networksPerCycle); |
| 854 |
|
| 855 |
|
| 856 |
alwt=new ArrayList<WorkerThread>(threads); |
| 857 |
for(int i=0; i<threads; i++){ |
| 858 |
alwt.add(new WorkerThread(i, workerQueue, cutoffForEvaluation)); |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
ThreadWaiter.startThreads(alwt); |
| 863 |
|
| 864 |
|
| 865 |
finalNet=trainNetworks(); |
| 866 |
|
| 867 |
|
| 868 |
for(int i=0; i<threads; i++) { |
| 869 |
workerQueue.add(JobData.POISON); |
| 870 |
} |
| 871 |
|
| 872 |
boolean success=ThreadWaiter.waitForThreadsToFinish(alwt, this); |
| 873 |
mprof.printTimes(); |
| 874 |
wprof.printTimes(); |
| 875 |
errorState&=!success; |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
} |
| 881 |
|
| 882 |
private int calcSeedsToScan() { |
| 883 |
if(seedsToScan>=0) {return seedsToScan;} |
| 884 |
if(netIn!=null) {return 0;} |
| 885 |
final int totalNets=networksPerCycle*cycles; |
| 886 |
long x=Tools.max(seedsToScan, (int)(Tools.min(Integer.MAX_VALUE, totalNets*seedsToScanMult))); |
| 887 |
return (int)Tools.min(x, Shared.MAX_ARRAY_LEN); |
| 888 |
} |
| 889 |
|
| 890 |
private CellNet trainNetworks() { |
| 891 |
bestNet=null; |
| 892 |
finalNet=null; |
| 893 |
|
| 894 |
final int totalNets=networksPerCycle*cycles; |
| 895 |
final int seedsToScan=calcSeedsToScan(); |
| 896 |
if(seedsToScan>totalNets) { |
| 897 |
outstream.println("Scanning "+seedsToScan+" seeds to find the best "+totalNets+"."); |
| 898 |
Timer t=new Timer(); |
| 899 |
ArrayList<Seed> seedList0=scanForSeeds(seedsToScan, totalNets); |
| 900 |
seedList=new LongList(seedList0.size()); |
| 901 |
for(Seed s : seedList0) {seedList.add(s.netSeed);} |
| 902 |
t.stopAndPrint(); |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
for(int i=0; i<cycles; i++) { |
| 907 |
if(cycles>1) {outstream.println("Cycle "+i);} |
| 908 |
clearCycle(); |
| 909 |
CellNet s=runCycle(); |
| 910 |
if(finalNet==null || s.compareTo(finalNet)>0) { |
| 911 |
finalNet=s; |
| 912 |
} |
| 913 |
} |
| 914 |
if(!quiet) {outstream.println();} |
| 915 |
if(networksTested>1) { |
| 916 |
outstream.println("Best:\t\t"+genPrintString(bestNet, -1, false, |
| 917 |
printFPR, printFNR, printTPR, printTNR, printCutoff, false, false)); |
| 918 |
outstream.println("Final:\t\t"+genPrintString(finalNet, -1, false, |
| 919 |
printFPR, printFNR, printTPR, printTNR, printCutoff, false, false)); |
| 920 |
if(printMode==printAverage) { |
| 921 |
outstream.println("Average: \t"+genPrintString(makeAvg(finalNets), -1, false, |
| 922 |
printFPR, printFNR, printTPR, printTNR, printCutoff, false, false)); |
| 923 |
} |
| 924 |
|
| 925 |
if(dims0==null) { |
| 926 |
outstream.println("\nBest Dims:\t"+Arrays.toString(bestNet.dims)); |
| 927 |
outstream.println("Final Dims:\t"+Arrays.toString(finalNet.dims)); |
| 928 |
if(printMode==printAverage) { |
| 929 |
outstream.println("Average Dims: \t"+genAvgDimsString(finalNets, false)); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
outstream.println("\nBest Seed:\t"+bestNet.seed); |
| 934 |
}else{ |
| 935 |
if(bestNet!=null) { |
| 936 |
outstream.println("Best:\t\t"+genPrintString(bestNet, -1, false, |
| 937 |
printFPR, printFNR, printTPR, printTNR, printCutoff, false, false)); |
| 938 |
} |
| 939 |
outstream.println("Final:\t\t"+genPrintString(finalNet, -1, false, |
| 940 |
printFPR, printFNR, printTPR, printTNR, printCutoff, false, false)); |
| 941 |
if(!quiet) {outstream.println();} |
| 942 |
} |
| 943 |
return finalNet; |
| 944 |
} |
| 945 |
|
| 946 |
private CellNet runCycle() { |
| 947 |
final CellNet[] nets=fetchNetworks(netIn, networksPerCycle); |
| 948 |
assert(networksPerCycle==nets.length); |
| 949 |
ArrayList<TrainerThread> altt=new ArrayList<TrainerThread>(networksPerCycle); |
| 950 |
for(int i=0; i<nets.length; i++) { |
| 951 |
networksIn++; |
| 952 |
edgesIn+=nets[i].countEdges(); |
| 953 |
altt.add(new TrainerThread(this, nets[i])); |
| 954 |
} |
| 955 |
for(TrainerThread tt : altt) { |
| 956 |
tt.start(); |
| 957 |
} |
| 958 |
CellNet best=null; |
| 959 |
for(int i=0; i<networksPerCycle; i++) { |
| 960 |
CellNet net=fetchFromQueue(networkQueue); |
| 961 |
if(printMode==printAverage) { |
| 962 |
finalNets.add(net); |
| 963 |
} |
| 964 |
best=compare(net, best); |
| 965 |
} |
| 966 |
for(TrainerThread tt : altt) { |
| 967 |
accumulate(tt); |
| 968 |
} |
| 969 |
return best; |
| 970 |
} |
| 971 |
|
| 972 |
<X> X fetchFromQueue(ArrayBlockingQueue<X> queue) { |
| 973 |
X x=null; |
| 974 |
while(x==null) { |
| 975 |
try { |
| 976 |
x=queue.take(); |
| 977 |
} catch (InterruptedException e) { |
| 978 |
e.printStackTrace(); |
| 979 |
} |
| 980 |
} |
| 981 |
return x; |
| 982 |
} |
| 983 |
|
| 984 |
synchronized CellNet compareWithBest(CellNet net) { |
| 985 |
CellNet better=compare(net, bestNet); |
| 986 |
|
| 987 |
if(printStatus && !quiet) { |
| 988 |
printStatus(net); |
| 989 |
} |
| 990 |
if(better!=bestNet) { |
| 991 |
bestNet=better; |
| 992 |
writeNetwork(bestNet, ffNetOutBest); |
| 993 |
} |
| 994 |
return bestNet; |
| 995 |
} |
| 996 |
|
| 997 |
synchronized CellNet compare(CellNet a, CellNet b) { |
| 998 |
assert(a!=null); |
| 999 |
int x=a.compareTo(b); |
| 1000 |
return x>0 ? a : b; |
| 1001 |
} |
| 1002 |
|
| 1003 |
synchronized Status compare(Status a, Status b) { |
| 1004 |
assert(a!=null); |
| 1005 |
if(b==null) {return a;} |
| 1006 |
int x=a.compareTo(b); |
| 1007 |
return x>0 ? a : b; |
| 1008 |
} |
| 1009 |
|
| 1010 |
private final void accumulate(TrainerThread tt){ |
| 1011 |
synchronized(tt) { |
| 1012 |
mprof.accumulate(tt.mprof); |
| 1013 |
networksTested++; |
| 1014 |
|
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
private final void accumulate(ScannerThread tt){ |
| 1019 |
synchronized(tt) { |
| 1020 |
mprof.accumulate(tt.mprof); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
@Override |
| 1025 |
public final void accumulate(WorkerThread wt){ |
| 1026 |
synchronized(wt) { |
| 1027 |
wprof.accumulate(wt.tprof); |
| 1028 |
linesProcessed+=wt.linesProcessedT; |
| 1029 |
linesOut+=wt.linesOutT; |
| 1030 |
errorState|=(!wt.success); |
| 1031 |
errorState|=(wt.errorStateT); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
@Override |
| 1036 |
public final boolean success(){return !errorState;} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
private ArrayList<Seed> scanForSeeds(final int seedsToScan0, final int seedsToReturn0) { |
| 1080 |
int seedsToScan=seedsToScan0, seedsToReturn=seedsToReturn0; |
| 1081 |
final int stsPerThread=(seedsToScan+networksPerCycle-1)/networksPerCycle; |
| 1082 |
final int strPerThread=(seedsToReturn+networksPerCycle-1)/networksPerCycle; |
| 1083 |
|
| 1084 |
seedQueue=new ArrayBlockingQueue<ArrayList<Seed>>(networksPerCycle); |
| 1085 |
ArrayList<ScannerThread> alst=new ArrayList<ScannerThread>(networksPerCycle); |
| 1086 |
for(int i=0; i<networksPerCycle; i++) { |
| 1087 |
final long seed=(i>0 || netSeed0<0 ? randyNetSeed.nextLong()&Long.MAX_VALUE : netSeed0); |
| 1088 |
final int stsThisThread=Tools.min(seedsToScan, stsPerThread); |
| 1089 |
final int strThisThread=Tools.min(seedsToReturn, strPerThread); |
| 1090 |
seedsToScan-=stsThisThread; |
| 1091 |
seedsToReturn-=strThisThread; |
| 1092 |
final ScannerThread st=new ScannerThread(this, dims0, minDims, maxDims, stsThisThread, |
| 1093 |
strThisThread, scanEpochs, scanSamples, seed, seedQueue); |
| 1094 |
alst.add(st); |
| 1095 |
} |
| 1096 |
for(ScannerThread st : alst) { |
| 1097 |
st.start(); |
| 1098 |
} |
| 1099 |
|
| 1100 |
ArrayList<Seed> seeds=new ArrayList<Seed>(seedsToReturn0); |
| 1101 |
for(int i=0; i<networksPerCycle; i++) { |
| 1102 |
ArrayList<Seed> seedsFromQueue=fetchFromQueue(seedQueue); |
| 1103 |
seeds.addAll(seedsFromQueue); |
| 1104 |
} |
| 1105 |
assert(seeds.size()==seedsToReturn0) : seeds.size(); |
| 1106 |
Collections.sort(seeds); |
| 1107 |
|
| 1108 |
for(ScannerThread st : alst) { |
| 1109 |
accumulate(st); |
| 1110 |
} |
| 1111 |
return seeds; |
| 1112 |
} |
| 1113 |
|
| 1114 |
private CellNet[] fetchNetworks(String path, int count) { |
| 1115 |
return path==null ? createNetworks(count) : loadNetworks(path); |
| 1116 |
} |
| 1117 |
|
| 1118 |
private CellNet[] createNetworks(int count){ |
| 1119 |
CellNet[] nets=new CellNet[count]; |
| 1120 |
for(int i=0; i<count; i++) { |
| 1121 |
nets[i]=randomNetwork(); |
| 1122 |
} |
| 1123 |
return nets; |
| 1124 |
} |
| 1125 |
|
| 1126 |
private CellNet randomNetwork() { |
| 1127 |
final long seed=(seedList!=null && netsMade<seedList.size() ? seedList.get(netsMade) : |
| 1128 |
(netsMade>0 || netSeed0<0 ? randyNetSeed.nextLong()&Long.MAX_VALUE : netSeed0)); |
| 1129 |
|
| 1130 |
|
| 1131 |
netsMade++; |
| 1132 |
return randomNetwork(seed); |
| 1133 |
} |
| 1134 |
|
| 1135 |
public final CellNet randomNetwork(long seed) { |
| 1136 |
final int[] dims=(dims0!=null ? dims0 : makeDims(minDims, maxDims, seed)); |
| 1137 |
return randomNetwork(dims, seed, maxDensity, maxDensity1, maxEdges, edgeBlockSize, commands); |
| 1138 |
} |
| 1139 |
|
| 1140 |
public static final CellNet randomNetwork(int[] dims, long seed, float maxDensity, |
| 1141 |
float maxDensity1, long maxEdges, int edgeBlockSize, ArrayList<String> commands) { |
| 1142 |
final long fullyConnectedEdges=fullyConnectedEdges(dims); |
| 1143 |
float density=maxEdges<1 ? maxDensity : |
| 1144 |
Tools.min(maxDensity, (float)(maxEdges/(double)fullyConnectedEdges)); |
| 1145 |
float density1=(maxDensity1<=0 ? density : maxDensity1); |
| 1146 |
assert(density>0f && density<=1f) : density+", "+maxEdges+", "+fullyConnectedEdges; |
| 1147 |
final CellNet net=new CellNet(dims, seed, density, density1, edgeBlockSize, commands); |
| 1148 |
|
| 1149 |
net.randomize(); |
| 1150 |
return net; |
| 1151 |
} |
| 1152 |
|
| 1153 |
public static long fullyConnectedEdges(int[] dims) { |
| 1154 |
long sum=0; |
| 1155 |
for(int i=1; i<dims.length; i++) { |
| 1156 |
long edges=dims[i-1]*dims[i]; |
| 1157 |
sum+=edges; |
| 1158 |
} |
| 1159 |
return sum; |
| 1160 |
} |
| 1161 |
|
| 1162 |
public static final int[] makeDims(int[] minDims, int[] maxDims, long seed) { |
| 1163 |
Random randy=Shared.threadLocalRandom(seed); |
| 1164 |
assert(minDims.length==maxDims.length); |
| 1165 |
assert(minDims[0]==maxDims[0]); |
| 1166 |
assert(minDims[minDims.length-1]==maxDims[maxDims.length-1]); |
| 1167 |
int[] dims=minDims.clone(); |
| 1168 |
for(int i=1; i<dims.length-1; i++) { |
| 1169 |
assert(minDims[i]<=maxDims[i]); |
| 1170 |
dims[i]=minDims[i]+randy.nextInt(maxDims[i]-minDims[i]+1); |
| 1171 |
} |
| 1172 |
return dims; |
| 1173 |
} |
| 1174 |
|
| 1175 |
private CellNet[] loadNetworks(final String path){ |
| 1176 |
assert(path!=null); |
| 1177 |
String[] paths=path.split(","); |
| 1178 |
networksPerCycle=(paths==null ? networksPerCycle : paths.length); |
| 1179 |
CellNet[] nets=new CellNet[networksPerCycle]; |
| 1180 |
for(int i=0; i<paths.length; i++) { |
| 1181 |
nets[i]=loadNetwork(paths[i]); |
| 1182 |
} |
| 1183 |
return nets; |
| 1184 |
} |
| 1185 |
|
| 1186 |
private CellNet loadNetwork(String path){ |
| 1187 |
final CellNet net=CellNetParser.load(path); |
| 1188 |
if(setCutoffForEvaluation) {net.setCutoff(cutoffForEvaluation);} |
| 1189 |
|
| 1190 |
assert(numInputs<0 || net.numInputs()==numInputs); |
| 1191 |
assert(numOutputs<0 || net.numOutputs()==numOutputs); |
| 1192 |
numLayers=net.numLayers(); |
| 1193 |
numInputs=net.numInputs(); |
| 1194 |
numOutputs=net.numOutputs(); |
| 1195 |
net.commands.add(commandLine); |
| 1196 |
|
| 1197 |
return net; |
| 1198 |
} |
| 1199 |
|
| 1200 |
private synchronized void writeNetwork(CellNet net, FileFormat ff) { |
| 1201 |
if(ff==null) {return;} |
| 1202 |
if(net.lastStats==null) { |
| 1203 |
net.lastStats=genPrintStringFull(net, -1); |
| 1204 |
} |
| 1205 |
ByteStreamWriter bsw=makeBSW(ff); |
| 1206 |
ByteBuilder bb=net.toBytes(); |
| 1207 |
linesOut+=net.lastLinesWritten; |
| 1208 |
bytesOut+=bb.length(); |
| 1209 |
bsw.println(bb); |
| 1210 |
bsw.poisonAndWait(); |
| 1211 |
} |
| 1212 |
|
| 1213 |
private SampleSet[] loadData(String path, boolean makeSubsets, int maxLines0, |
| 1214 |
boolean shuffleRaw, float splitFraction, int maxLines1){ |
| 1215 |
if(!quiet) {outstream.println("Loading "+path);} |
| 1216 |
SampleSet[] ssa=DataLoader.load(path, maxLines0, shuffleRaw, splitFraction, maxLines1, exclusive, balance); |
| 1217 |
for(SampleSet ss : ssa) { |
| 1218 |
ss.makeSamples(); |
| 1219 |
} |
| 1220 |
if(makeSubsets) { |
| 1221 |
if(setsize>0) { |
| 1222 |
assert(setsize>=100) : "Setsize should be at least 100"; |
| 1223 |
subsets=Tools.max(1, ssa[0].samples.length/setsize); |
| 1224 |
outstream.println("Data was organized into "+subsets+(subsets==1 ? " set." : " sets.")); |
| 1225 |
} |
| 1226 |
subsets=Tools.mid(1, subsets, ssa[0].samples.length); |
| 1227 |
ssa[0].makeSubsets(subsets); |
| 1228 |
} |
| 1229 |
return ssa; |
| 1230 |
} |
| 1231 |
|
| 1232 |
private void writeData(FileFormat ff){ |
| 1233 |
if(ff==null) {return;} |
| 1234 |
ByteStreamWriter bsw=makeBSW(ff); |
| 1235 |
ByteBuilder bb=new ByteBuilder(); |
| 1236 |
bb.append("dims").tab().append(data.matrix.numInputs).tab().append(data.matrix.numOutputs).nl(); |
| 1237 |
if(data.matrix.columns!=null) { |
| 1238 |
for(String s : data.matrix.columns) { |
| 1239 |
bb.append(s).tab(); |
| 1240 |
} |
| 1241 |
if(bb.endsWith('\t')) { |
| 1242 |
bb.trimLast(1).nl(); |
| 1243 |
} |
| 1244 |
} |
| 1245 |
for(Sample s : data.samples) { |
| 1246 |
s.toBytes(bb); |
| 1247 |
bsw.print(bb); |
| 1248 |
linesOut++; |
| 1249 |
bytesOut+=bb.length(); |
| 1250 |
bb.clear(); |
| 1251 |
} |
| 1252 |
bsw.poisonAndWait(); |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
void clearCycle() { |
| 1258 |
lastPrintEpoch=-1; |
| 1259 |
lastPrintTime=System.currentTimeMillis(); |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
private static String plural(String s, int count){return count+" "+(count==1 ? s : s+"s");} |
| 1267 |
private static String pluralES(String s, int count){return count+" "+(count==1 ? s : s+"es");} |
| 1268 |
|
| 1269 |
synchronized void printStatus(CellNet net) { |
| 1270 |
if(networksPerCycle<=1 || printMode==printAll || (printMode==printFirst && net.epoch>lastPrintEpoch)) { |
| 1271 |
printStatusInner(net); |
| 1272 |
}else if(printMode==printBest){ |
| 1273 |
final CellNet old=networkMap.get(net); |
| 1274 |
CellNet best=old; |
| 1275 |
if(old==null) { |
| 1276 |
networkMap.put(net, net); |
| 1277 |
best=net; |
| 1278 |
}else{ |
| 1279 |
int x=net.compareTo(old); |
| 1280 |
if(x>0) { |
| 1281 |
net.count+=old.count; |
| 1282 |
networkMap.put(net, net); |
| 1283 |
best=net; |
| 1284 |
}else{ |
| 1285 |
old.count+=net.count; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
if(best.count>=networksPerCycle){ |
| 1289 |
printStatusInner(best); |
| 1290 |
networkMap.remove(best); |
| 1291 |
} |
| 1292 |
}else if(printMode==printAverage){ |
| 1293 |
ArrayList<CellNet> list=networkMap2.get(net); |
| 1294 |
if(list==null) { |
| 1295 |
list=new ArrayList<CellNet>(networksPerCycle); |
| 1296 |
networkMap2.put(net, list); |
| 1297 |
} |
| 1298 |
list.add(net); |
| 1299 |
if(list.size()>=networksPerCycle){ |
| 1300 |
CellNet avg=makeAvg(list); |
| 1301 |
printStatusInner(avg); |
| 1302 |
|
| 1303 |
|
| 1304 |
list.clear(); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
private ArrayList<CellNet> concentrateBest(ArrayList<CellNet> list){ |
| 1310 |
Collections.sort(list); |
| 1311 |
Collections.reverse(list); |
| 1312 |
final int count=(int)Math.ceil(Math.pow(list.size(), 0.6)); |
| 1313 |
final int start=(count>4 ? 1 : 0); |
| 1314 |
final ArrayList<CellNet> concentrate=new ArrayList<CellNet>(count); |
| 1315 |
int x=0; |
| 1316 |
for(int i=start; i<count+start; i++) { |
| 1317 |
x++; |
| 1318 |
CellNet net=list.get(i); |
| 1319 |
concentrate.add(net); |
| 1320 |
} |
| 1321 |
assert(x==count); |
| 1322 |
return concentrate; |
| 1323 |
} |
| 1324 |
|
| 1325 |
private CellNet makeAvg(ArrayList<CellNet> list0) { |
| 1326 |
ArrayList<CellNet> list=concentrateBest(list0); |
| 1327 |
final CellNet zero=list.get(0); |
| 1328 |
CellNet base=new CellNet(new int[] {1,1}, 1, 1.0f, 0f, 1, null); |
| 1329 |
base.epoch=zero.epoch; |
| 1330 |
base.alpha=zero.alpha; |
| 1331 |
base.annealStrength=zero.annealStrength; |
| 1332 |
base.errorRate=0; |
| 1333 |
base.weightedErrorRate=0; |
| 1334 |
base.fpRate=0; |
| 1335 |
base.fnRate=0; |
| 1336 |
base.tpRate=0; |
| 1337 |
base.tnRate=0; |
| 1338 |
float cutoff=0; |
| 1339 |
for(CellNet net : list) { |
| 1340 |
base.errorRate+=net.errorRate; |
| 1341 |
base.weightedErrorRate+=net.weightedErrorRate; |
| 1342 |
base.fpRate+=net.fpRate; |
| 1343 |
base.fnRate+=net.fnRate; |
| 1344 |
base.tpRate+=net.tpRate; |
| 1345 |
base.tnRate+=net.tnRate; |
| 1346 |
cutoff+=net.cutoff; |
| 1347 |
} |
| 1348 |
final float mult=1.0f/list.size(); |
| 1349 |
base.errorRate*=mult; |
| 1350 |
base.weightedErrorRate*=mult; |
| 1351 |
base.fpRate*=mult; |
| 1352 |
base.fnRate*=mult; |
| 1353 |
base.tpRate*=mult; |
| 1354 |
base.tnRate*=mult; |
| 1355 |
base.setCutoff(cutoff*mult); |
| 1356 |
return base; |
| 1357 |
} |
| 1358 |
|
| 1359 |
private float[] genAvgDims(ArrayList<CellNet> list0) { |
| 1360 |
ArrayList<CellNet> list=concentrateBest(list0); |
| 1361 |
final CellNet zero=list.get(0); |
| 1362 |
float[] dims=new float[zero.dims.length]; |
| 1363 |
for(CellNet net : list) { |
| 1364 |
for(int i=0; i<dims.length; i++) { |
| 1365 |
dims[i]+=net.dims[i]; |
| 1366 |
} |
| 1367 |
} |
| 1368 |
for(int i=0; i<dims.length; i++) { |
| 1369 |
dims[i]/=list.size(); |
| 1370 |
} |
| 1371 |
return dims; |
| 1372 |
} |
| 1373 |
|
| 1374 |
private String genAvgDimsString(ArrayList<CellNet> list0, boolean machine) { |
| 1375 |
float[] dims=genAvgDims(list0); |
| 1376 |
int[] dims2=new int[dims.length]; |
| 1377 |
for(int i=0; i<dims.length; i++) { |
| 1378 |
dims2[i]=(int)Math.round(dims[i]); |
| 1379 |
} |
| 1380 |
return genDimsString(dims2, machine); |
| 1381 |
} |
| 1382 |
|
| 1383 |
private String genDimsString(int[] dims, boolean machine) { |
| 1384 |
ByteBuilder bb=new ByteBuilder(); |
| 1385 |
if(machine) { |
| 1386 |
for(int i=1; i<dims.length-1; i++) { |
| 1387 |
|
| 1388 |
bb.append(dims[i]).tab(); |
| 1389 |
} |
| 1390 |
bb.setLength(bb.length-1); |
| 1391 |
}else { |
| 1392 |
bb.append('['); |
| 1393 |
for(int f : dims) { |
| 1394 |
bb.append(f).comma().space(); |
| 1395 |
} |
| 1396 |
bb.setLength(bb.length-2); |
| 1397 |
bb.append(']'); |
| 1398 |
} |
| 1399 |
return bb.toString(); |
| 1400 |
} |
| 1401 |
|
| 1402 |
private void printStatusInner(CellNet net) { |
| 1403 |
final long t=System.currentTimeMillis(); |
| 1404 |
final long elapsed=t-lastPrintTime; |
| 1405 |
lastPrintTime=t; |
| 1406 |
|
| 1407 |
net.lastStats=genPrintStringFull(net, elapsed); |
| 1408 |
|
| 1409 |
String s=genPrintStringDefault(net, elapsed); |
| 1410 |
outstream.println(s); |
| 1411 |
lastPrintEpoch=Tools.max(net.epoch, lastPrintEpoch); |
| 1412 |
} |
| 1413 |
|
| 1414 |
synchronized void printStatusOld(CellNet net, String status, int epoch) { |
| 1415 |
assert(status!=null); |
| 1416 |
if(epoch>lastPrintEpoch || printMode==printAll || printMode==printBest){ |
| 1417 |
outstream.println("seed="+net.seed+"\t"+status); |
| 1418 |
lastPrintEpoch=Tools.max(epoch, lastPrintEpoch); |
| 1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
String genPrintStringFull(CellNet net, long millis) { |
| 1423 |
String s=genPrintString(net, millis, true, true, true, true, true, true, true, true); |
| 1424 |
return s; |
| 1425 |
} |
| 1426 |
String genPrintStringDefault(CellNet stat, long millis) { |
| 1427 |
String s=genPrintString(stat, millis, true, printFPR, printFNR, printTPR, printTNR, printCutoff, printAlpha, printAnneal); |
| 1428 |
return s; |
| 1429 |
} |
| 1430 |
|
| 1431 |
private String genPrintString(CellNet net, long millis, boolean epo, |
| 1432 |
boolean fpr, boolean fnr, boolean tpr, boolean tnr, boolean ctf, boolean alp, boolean ann) { |
| 1433 |
String s="ERR= %.6f\tWER= %.6f"; |
| 1434 |
s=String.format(s, net.errorRate, net.weightedErrorRate); |
| 1435 |
if(epo) { |
| 1436 |
|
| 1437 |
int e=net.epoch; |
| 1438 |
s="Batch "+(e<10000 ? e : ((e/1000)+"k"))+":\t"+s; |
| 1439 |
} |
| 1440 |
if(fpr) { |
| 1441 |
s+=String.format("\tFPR= %.6f", net.fpRate); |
| 1442 |
} |
| 1443 |
if(fnr) { |
| 1444 |
s+=String.format("\tFNR= %.6f", net.fnRate); |
| 1445 |
} |
| 1446 |
if(tpr) { |
| 1447 |
s+=String.format("\tTPR= %.6f", net.tpRate); |
| 1448 |
} |
| 1449 |
if(tnr) { |
| 1450 |
s+=String.format("\tTNR= %.6f", net.tnRate); |
| 1451 |
} |
| 1452 |
if(ctf) { |
| 1453 |
s+=String.format("\tCTF= %.6f", net.cutoff); |
| 1454 |
} |
| 1455 |
if(alp) { |
| 1456 |
s+=String.format("\tALP= %.5f", net.alpha); |
| 1457 |
} |
| 1458 |
if(ann) { |
| 1459 |
s+=String.format("\tANN= %.6f", (net.epoch>=minAnnealEpoch ? net.annealStrength : 0)); |
| 1460 |
} |
| 1461 |
if(millis>=0) { |
| 1462 |
if(millis>=100000) { |
| 1463 |
s+="\t"+(millis/1000)+"s"; |
| 1464 |
}else if(millis>10000){ |
| 1465 |
s+=String.format("\t%.1fs", (millis/1000f)); |
| 1466 |
}else{ |
| 1467 |
s+=String.format("\t%.2fs", (millis/1000f)); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
return s; |
| 1471 |
} |
| 1472 |
|
| 1473 |
private String genPrintStringMachine(CellNet net) { |
| 1474 |
ByteBuilder bb=new ByteBuilder(); |
| 1475 |
|
| 1476 |
bb.append(net.errorRate, 6); |
| 1477 |
bb.tab().append(net.weightedErrorRate, 6); |
| 1478 |
bb.tab().append(net.fpRate, 6); |
| 1479 |
bb.tab().append(net.fnRate, 6); |
| 1480 |
bb.tab().append(net.tpRate, 6); |
| 1481 |
bb.tab().append(net.tnRate, 6); |
| 1482 |
bb.tab().append(net.cutoff, 6); |
| 1483 |
return bb.toString(); |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 1491 |
if(ff==null){return null;} |
| 1492 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 1493 |
bsw.start(); |
| 1494 |
return bsw; |
| 1495 |
} |
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
private final String commandLine; |
| 1502 |
final ArrayList<String> commands; |
| 1503 |
|
| 1504 |
|
| 1505 |
private String netIn=null; |
| 1506 |
|
| 1507 |
|
| 1508 |
private String dataIn=null; |
| 1509 |
|
| 1510 |
|
| 1511 |
private String validateIn=null; |
| 1512 |
|
| 1513 |
|
| 1514 |
private String dataOut=null; |
| 1515 |
|
| 1516 |
|
| 1517 |
private String netOutFinal=null; |
| 1518 |
|
| 1519 |
|
| 1520 |
private String netOutBest=null; |
| 1521 |
|
| 1522 |
private int[] dims0; |
| 1523 |
private int[] minDims; |
| 1524 |
private int[] maxDims; |
| 1525 |
private int numInputs=-1, numOutputs=-1, numLayers=-1; |
| 1526 |
|
| 1527 |
SampleSet data; |
| 1528 |
SampleSet validateSet; |
| 1529 |
|
| 1530 |
float validateFraction=0.1f; |
| 1531 |
boolean exclusive=true; |
| 1532 |
|
| 1533 |
private int netsMade=0; |
| 1534 |
private float maxDensity=1.0f; |
| 1535 |
|
| 1536 |
private float maxDensity1=0f; |
| 1537 |
private int edgeBlockSize=1; |
| 1538 |
private long maxEdges=-1; |
| 1539 |
private float[] densityArray=null; |
| 1540 |
private long netSeed0=-1; |
| 1541 |
|
| 1542 |
private boolean setNetSeed=false; |
| 1543 |
|
| 1544 |
|
| 1545 |
int threads=1; |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
boolean orderedJobs=true; |
| 1552 |
|
| 1553 |
ArrayBlockingQueue<CellNet> networkQueue; |
| 1554 |
ArrayBlockingQueue<JobData> workerQueue; |
| 1555 |
|
| 1556 |
ArrayBlockingQueue<ArrayList<Seed>> seedQueue; |
| 1557 |
private final Profiler mprof=new Profiler("M", 13); |
| 1558 |
private final Profiler wprof=new Profiler("W", 7); |
| 1559 |
|
| 1560 |
ArrayList<WorkerThread> alwt; |
| 1561 |
|
| 1562 |
boolean training=true; |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
int maxEpochs=400000; |
| 1567 |
float targetError=-1; |
| 1568 |
float targetFPR=-1; |
| 1569 |
float targetFNR=-1; |
| 1570 |
float crossoverFpMult=1; |
| 1571 |
|
| 1572 |
boolean sortAll=false; |
| 1573 |
boolean sort=true; |
| 1574 |
boolean sortInThread=false; |
| 1575 |
boolean shuffleSubset=true; |
| 1576 |
boolean launchInThread=false; |
| 1577 |
boolean allowMTSort=true; |
| 1578 |
|
| 1579 |
static boolean PIVOT_SORT=false; |
| 1580 |
static int SHUFFLEMOD=4; |
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
boolean useSetLock=true; |
| 1588 |
static boolean setNetInWorkerThread=true; |
| 1589 |
static boolean copyNetInWorkerThread=false; |
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
double alphaZero=0.08f; |
| 1598 |
double alphaMult=5.0f; |
| 1599 |
double alphaMult2=2.8f; |
| 1600 |
int peakAlphaEpoch=640; |
| 1601 |
|
| 1602 |
float annealStrength0=.003f; |
| 1603 |
float annealProb=.225f; |
| 1604 |
float annealMult2=800; |
| 1605 |
double annealDropoff0=0.999975f; |
| 1606 |
|
| 1607 |
float maxAnnealEpochMult=0.8f; |
| 1608 |
int minAnnealEpoch=400; |
| 1609 |
int maxAnnealEpoch=Integer.MAX_VALUE; |
| 1610 |
|
| 1611 |
|
| 1612 |
static float cutoffForEvaluation=0.5f; |
| 1613 |
static boolean setCutoffForEvaluation=false; |
| 1614 |
|
| 1615 |
int subsets=-1; |
| 1616 |
int setsize=60000; |
| 1617 |
|
| 1618 |
float fractionPerEpoch0=0.08f; |
| 1619 |
float fractionPerEpoch2=0.08f; |
| 1620 |
int fpeStart=192; |
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
float dumpRate=0.5f; |
| 1625 |
private float dumpEpochMult=0.25f; |
| 1626 |
int dumpEpoch=-1; |
| 1627 |
float partialDumpFraction=0.9f; |
| 1628 |
final boolean shrinkSubsets=true; |
| 1629 |
|
| 1630 |
|
| 1631 |
float positiveTriage=0.0001f; |
| 1632 |
float negativeTriage=0.0005f; |
| 1633 |
int startTriage=-1; |
| 1634 |
float startTriageMult=0.2f; |
| 1635 |
|
| 1636 |
int minWeightEpoch=-1; |
| 1637 |
float minWeightEpochMult=0.03f; |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
boolean forcePrintFPR=false, forcePrintFNR=false; |
| 1642 |
final boolean printFPR, printFNR; |
| 1643 |
boolean printTPR=false, printTNR=false, printCutoff=true; |
| 1644 |
boolean printAlpha=false, printAnneal=false; |
| 1645 |
|
| 1646 |
double annealStrength; |
| 1647 |
double annealDropoff; |
| 1648 |
|
| 1649 |
|
| 1650 |
final double alphaIncrease; |
| 1651 |
final int alphaEpochs; |
| 1652 |
final double alphaDropoff; |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
int jobsPerEpoch; |
| 1657 |
int networksPerCycle=1; |
| 1658 |
int cycles=1; |
| 1659 |
long networksTested=0; |
| 1660 |
|
| 1661 |
private LongList seedList; |
| 1662 |
private int seedsToScan=-1; |
| 1663 |
private float seedsToScanMult=0; |
| 1664 |
private int scanEpochs=1024; |
| 1665 |
private int scanSamples=80000; |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
int lastPrintEpoch=-1; |
| 1671 |
|
| 1672 |
private CellNet finalNet=null; |
| 1673 |
private CellNet bestNet=null; |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
int printInterval=10000; |
| 1678 |
boolean quiet=false; |
| 1679 |
boolean printStatus=true; |
| 1680 |
boolean machineOut=false; |
| 1681 |
int printMode=printAverage; |
| 1682 |
long lastPrintTime=-1; |
| 1683 |
private static final int printFirst=0, printAll=1, printBest=2, printAverage=3; |
| 1684 |
|
| 1685 |
HashMap<CellNet, CellNet> networkMap=new HashMap<CellNet, CellNet>(); |
| 1686 |
HashMap<CellNet, ArrayList<CellNet>> networkMap2=new HashMap<CellNet, ArrayList<CellNet>>(); |
| 1687 |
ArrayList<CellNet> finalNets=new ArrayList<CellNet>(); |
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
private long linesProcessed=0; |
| 1692 |
private long linesOut=0; |
| 1693 |
private long bytesProcessed=0; |
| 1694 |
private long bytesOut=0; |
| 1695 |
|
| 1696 |
private long edgesIn=0; |
| 1697 |
private long networksIn=0; |
| 1698 |
|
| 1699 |
private int maxLines=Shared.MAX_ARRAY_LEN; |
| 1700 |
int maxLinesV=-1; |
| 1701 |
private boolean shuffleRaw=false; |
| 1702 |
private float balance=0.4f; |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
private final FileFormat ffNetIn; |
| 1710 |
|
| 1711 |
|
| 1712 |
private final FileFormat ffDataIn; |
| 1713 |
|
| 1714 |
private final FileFormat ffValidateIn; |
| 1715 |
|
| 1716 |
private final FileFormat ffDataOut; |
| 1717 |
|
| 1718 |
private final FileFormat ffNetOutFinal; |
| 1719 |
|
| 1720 |
private final FileFormat ffNetOutBest; |
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
final Random randyNetSeed; |
| 1726 |
|
| 1727 |
@Override |
| 1728 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 1729 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
private PrintStream outstream=System.err; |
| 1737 |
|
| 1738 |
public static boolean verbose=false; |
| 1739 |
|
| 1740 |
public boolean errorState=false; |
| 1741 |
|
| 1742 |
private boolean overwrite=true; |
| 1743 |
|
| 1744 |
private boolean append=false; |
| 1745 |
|
| 1746 |
} |