| 1 |
package ml; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import shared.LineParser2; |
| 9 |
import shared.Parse; |
| 10 |
import shared.Tools; |
| 11 |
import structures.FloatList; |
| 12 |
import structures.IntList; |
| 13 |
|
| 14 |
public class CellNetParser { |
| 15 |
|
| 16 |
static CellNet parse(String fname, boolean nullOnFailure) { |
| 17 |
if(nullOnFailure && !new File(fname).exists()) {return null;} |
| 18 |
CellNetParser cnp=new CellNetParser(fname); |
| 19 |
return cnp.net; |
| 20 |
} |
| 21 |
|
| 22 |
public static CellNet load(String fname) {return load(fname, true);} |
| 23 |
public static CellNet load(String fname, boolean nullOnFailure) { |
| 24 |
return parse(fname, nullOnFailure); |
| 25 |
} |
| 26 |
|
| 27 |
private CellNetParser(String fname_){ |
| 28 |
this(ByteFile.toLines(fname_)); |
| 29 |
fname=fname_; |
| 30 |
} |
| 31 |
|
| 32 |
private CellNetParser(ArrayList<byte[]> lines_){ |
| 33 |
lines=lines_; |
| 34 |
|
| 35 |
parseHeader(); |
| 36 |
CellNet.DENSE=dense; |
| 37 |
|
| 38 |
net=new CellNet(dims, seed, density, density1, edgeBlockSize, commands); |
| 39 |
net.epochsTrained=epochs; |
| 40 |
net.samplesTrained=samples; |
| 41 |
|
| 42 |
net.setCutoff(cutoff); |
| 43 |
assert(layers==net.layers); |
| 44 |
posFirstEdge=pos; |
| 45 |
if(dense) { |
| 46 |
parseEdgesDense(); |
| 47 |
}else { |
| 48 |
parseEdgesSparse(); |
| 49 |
} |
| 50 |
net.makeWeightMatrices(); |
| 51 |
} |
| 52 |
|
| 53 |
public void parseHeader() { |
| 54 |
|
| 55 |
|
| 56 |
while(pos<lines.size()) { |
| 57 |
byte[] line=lines.get(pos); |
| 58 |
|
| 59 |
if(line.length<1){ |
| 60 |
|
| 61 |
}else if(Tools.startsWith(line, "#")){ |
| 62 |
if(Tools.startsWith(line, "##ctf") || Tools.startsWith(line, "#ctf")){ |
| 63 |
cutoff=parseFloat(line); |
| 64 |
}else if(Tools.startsWith(line, "##")){ |
| 65 |
|
| 66 |
}else if(Tools.startsWith(line, "#version")){ |
| 67 |
version=parseInt(line); |
| 68 |
}else if(Tools.startsWith(line, "#layers")){ |
| 69 |
layers=parseInt(line); |
| 70 |
}else if(Tools.startsWith(line, "#seed")){ |
| 71 |
seed=parseLong(line); |
| 72 |
}else if(Tools.startsWith(line, "#annealseed")){ |
| 73 |
|
| 74 |
}else if(Tools.startsWith(line, "#density1")){ |
| 75 |
density1=parseFloat(line); |
| 76 |
}else if(Tools.startsWith(line, "#density")){ |
| 77 |
density=parseFloat(line); |
| 78 |
}else if(Tools.startsWith(line, "#blocksize")){ |
| 79 |
edgeBlockSize=parseInt(line); |
| 80 |
|
| 81 |
|
| 82 |
}else if(Tools.startsWith(line, "#epochs")){ |
| 83 |
epochs=parseLong(line); |
| 84 |
}else if(Tools.startsWith(line, "#samples")){ |
| 85 |
samples=parseLong(line); |
| 86 |
}else if(Tools.startsWith(line, "#concise")){ |
| 87 |
concise=true; |
| 88 |
}else if(Tools.startsWith(line, "#dense")){ |
| 89 |
dense=true; |
| 90 |
}else if(Tools.startsWith(line, "#sparse")){ |
| 91 |
dense=false; |
| 92 |
}else if(Tools.startsWith(line, "#dims")){ |
| 93 |
dims=parseIntArray(line, delimiter, true); |
| 94 |
assert(layers==dims.length) : layers+", "+Arrays.toString(dims); |
| 95 |
}else if(Tools.startsWith(line, "#CL")){ |
| 96 |
commands.add(new String(line)); |
| 97 |
}else if(Tools.startsWith(line, "#edges")){ |
| 98 |
if(line.length>7) {edges=parseInt(line);} |
| 99 |
}else if(Tools.startsWith(line, "#")){ |
| 100 |
assert(false) : "\nUnexpected header line: '"+new String(line)+"'" |
| 101 |
+ "\nComments should start with ##\n"; |
| 102 |
} |
| 103 |
}else{ |
| 104 |
break; |
| 105 |
} |
| 106 |
pos++; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
private void parseEdgesDense() { |
| 112 |
assert(concise); |
| 113 |
pos=posFirstEdge; |
| 114 |
long numEdges=0; |
| 115 |
|
| 116 |
int numCells=(int) shared.Vector.sum(dims); |
| 117 |
LineParser2 lp=new LineParser2(delimiter); |
| 118 |
FloatList weights=new FloatList(); |
| 119 |
|
| 120 |
while(pos<lines.size()) { |
| 121 |
byte[] line=lines.get(pos); |
| 122 |
|
| 123 |
if(line.length<1){ |
| 124 |
|
| 125 |
}else if(Tools.startsWith(line, "##")){ |
| 126 |
|
| 127 |
}else if(Tools.startsWith(line, 'C') || Tools.startsWith(line, 'W')){ |
| 128 |
|
| 129 |
lp.set(line); |
| 130 |
lp.setBounds(0, 0); |
| 131 |
int cid=lp.parseInt(); |
| 132 |
|
| 133 |
String s=lp.parseString(); |
| 134 |
int type=Tools.find(s, Function.TYPES); |
| 135 |
assert(type>=0) : type+", "+s+"\n'"+new String(line)+"'"; |
| 136 |
Cell c=net.list.get(cid); |
| 137 |
c.function=Function.getFunction(type); |
| 138 |
assert(c.function.type()==type); |
| 139 |
|
| 140 |
c.setBias(lp.parseFloat(), true); |
| 141 |
weights.clear(); |
| 142 |
while(lp.hasMore()) { |
| 143 |
weights.add(lp.parseFloat()); |
| 144 |
} |
| 145 |
c.weights=weights.toArray(); |
| 146 |
c.deltas=new float[c.weights.length]; |
| 147 |
assert(c.weights.length==c.id()-c.lpos-c.prevLayerStart) : new String(line)+"\n"+ |
| 148 |
c.weights.length+", "+c.layer+", "+c.id()+", "+c.lpos+", "+c.prevLayerStart+", "+ |
| 149 |
(c.id()-c.lpos-c.prevLayerStart); |
| 150 |
}else { |
| 151 |
assert(false) : new String(line); |
| 152 |
} |
| 153 |
pos++; |
| 154 |
} |
| 155 |
assert(CellNet.DENSE || net.check()); |
| 156 |
} |
| 157 |
|
| 158 |
private void parseEdgesSparse() { |
| 159 |
assert(concise); |
| 160 |
pos=posFirstEdge; |
| 161 |
long numEdges=0; |
| 162 |
|
| 163 |
int numCells=(int) shared.Vector.sum(dims); |
| 164 |
LineParser2 lp=new LineParser2(delimiter); |
| 165 |
FloatList weights=new FloatList(); |
| 166 |
IntList inputs=new IntList(); |
| 167 |
|
| 168 |
while(pos<lines.size()) { |
| 169 |
byte[] line=lines.get(pos); |
| 170 |
|
| 171 |
if(line.length<1){ |
| 172 |
|
| 173 |
}else if(Tools.startsWith(line, "##")){ |
| 174 |
|
| 175 |
}else if(Tools.startsWith(line, 'C') || Tools.startsWith(line, 'W')){ |
| 176 |
|
| 177 |
lp.set(line); |
| 178 |
lp.setBounds(0, 0); |
| 179 |
int cid=lp.parseInt(); |
| 180 |
|
| 181 |
String s=lp.parseString(); |
| 182 |
int type=Tools.find(s, Function.TYPES); |
| 183 |
assert(type>=0) : type+", "+s+"\n'"+new String(line)+"'"; |
| 184 |
Cell c=net.list.get(cid); |
| 185 |
assert(c.weights==null); |
| 186 |
c.function=Function.getFunction(type); |
| 187 |
assert(c.function.type()==type); |
| 188 |
|
| 189 |
c.setBias(lp.parseFloat(), true); |
| 190 |
weights.clear(); |
| 191 |
while(lp.hasMore()) { |
| 192 |
weights.add(lp.parseFloat()); |
| 193 |
} |
| 194 |
c.weights=weights.toArray(); |
| 195 |
c.deltas=new float[c.weights.length]; |
| 196 |
assert(c.inputs==null || c.inputs.length==c.weights.length) : |
| 197 |
c.layer+", "+c.lpos+", "+c.inputs.length+", "+c.weights.length+"\n"+Arrays.toString(c.inputs); |
| 198 |
}else if(Tools.startsWith(line, 'I')){ |
| 199 |
|
| 200 |
lp.set(line); |
| 201 |
lp.setBounds(0, 0); |
| 202 |
int cid=lp.parseInt(); |
| 203 |
Cell c=net.list.get(cid); |
| 204 |
assert(c.inputs==null); |
| 205 |
inputs.clear(); |
| 206 |
while(lp.hasMore()) { |
| 207 |
inputs.add(lp.parseInt()); |
| 208 |
} |
| 209 |
c.inputs=inputs.toArray(); |
| 210 |
|
| 211 |
assert(c.weights==null || c.inputs.length==c.weights.length); |
| 212 |
}else if(Tools.startsWith(line, 'H')){ |
| 213 |
|
| 214 |
lp.set(line); |
| 215 |
lp.setBounds(0, 0); |
| 216 |
int cid=lp.parseInt(); |
| 217 |
Cell c=net.list.get(cid); |
| 218 |
assert(c.inputs==null); |
| 219 |
lp.advance(); |
| 220 |
c.inputs=CellNet.fromHex(line, lp.a()); |
| 221 |
assert(c.weights==null || c.inputs.length==c.weights.length); |
| 222 |
}else { |
| 223 |
assert(false) : new String(line); |
| 224 |
} |
| 225 |
pos++; |
| 226 |
} |
| 227 |
CellNet.makeOutputSets(net.net); |
| 228 |
assert(net.check()); |
| 229 |
} |
| 230 |
|
| 231 |
boolean hasMore(){ |
| 232 |
return pos<lines.size(); |
| 233 |
} |
| 234 |
|
| 235 |
byte[] nextLine(){ |
| 236 |
if(pos>=lines.size()){return null;} |
| 237 |
byte[] line=lines.get(pos); |
| 238 |
pos++; |
| 239 |
return line; |
| 240 |
} |
| 241 |
|
| 242 |
String fname; |
| 243 |
final ArrayList<byte[]> lines; |
| 244 |
private final CellNet net; |
| 245 |
long seed; |
| 246 |
|
| 247 |
float density=1f; |
| 248 |
float density1=0f; |
| 249 |
int edgeBlockSize=1; |
| 250 |
int edges=0; |
| 251 |
long epochs=0; |
| 252 |
long samples=0; |
| 253 |
int version; |
| 254 |
int layers=-1; |
| 255 |
boolean concise=false; |
| 256 |
boolean dense=true; |
| 257 |
int[] dims; |
| 258 |
int pos=0; |
| 259 |
float cutoff=0.5f; |
| 260 |
final int posFirstEdge; |
| 261 |
ArrayList<String> commands=new ArrayList<String>(); |
| 262 |
|
| 263 |
public static final byte delimiter=' '; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private static String parseString(byte[] line){ |
| 270 |
int idx=Tools.indexOf(line, delimiter); |
| 271 |
String s=new String(line, idx+1, line.length-idx-1); |
| 272 |
return s; |
| 273 |
} |
| 274 |
|
| 275 |
private static float parseFloat(byte[] line){ |
| 276 |
int idx=Tools.indexOf(line, delimiter); |
| 277 |
return Parse.parseFloat(line, idx+1, line.length); |
| 278 |
} |
| 279 |
|
| 280 |
private static int parseInt(byte[] line){ |
| 281 |
int idx=Tools.indexOf(line, delimiter); |
| 282 |
return Parse.parseInt(line, idx+1, line.length); |
| 283 |
} |
| 284 |
|
| 285 |
private static long parseLong(byte[] line){ |
| 286 |
int idx=Tools.indexOf(line, delimiter); |
| 287 |
return Parse.parseLong(line, idx+1, line.length); |
| 288 |
} |
| 289 |
|
| 290 |
public static int[] parseIntArray(final byte[] line, final byte delimiter, boolean parseTitle){ |
| 291 |
int a=0, b=0; |
| 292 |
IntList list=new IntList(3); |
| 293 |
|
| 294 |
if(parseTitle) { |
| 295 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 296 |
assert(b>a) : "Missing Title: "+new String(line); |
| 297 |
b++; |
| 298 |
a=b; |
| 299 |
} |
| 300 |
|
| 301 |
while(a<line.length) { |
| 302 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 303 |
assert(b>a) : "Missing element "+list.size+": '"+new String(line)+"'"; |
| 304 |
int x=Parse.parseInt(line, a, b); |
| 305 |
|
| 306 |
list.add(x); |
| 307 |
b++; |
| 308 |
a=b; |
| 309 |
} |
| 310 |
return list.toArray(); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
} |