use of hex.rng.MersenneTwisterRNG in project h2o-2 by h2oai.
the class NeuralNetMnist method csv.
private static void csv(String dest, String images, String labels) throws Exception {
DataInputStream imagesBuf = new DataInputStream(new GZIPInputStream(new FileInputStream(new File(images))));
DataInputStream labelsBuf = new DataInputStream(new GZIPInputStream(new FileInputStream(new File(labels))));
// Magic
imagesBuf.readInt();
int count = imagesBuf.readInt();
// Magic
labelsBuf.readInt();
assert count == labelsBuf.readInt();
// Rows
imagesBuf.readInt();
// Cols
imagesBuf.readInt();
System.out.println("Count=" + count);
byte[][] rawI = new byte[count][PIXELS];
byte[] rawL = new byte[count];
for (int n = 0; n < count; n++) {
imagesBuf.readFully(rawI[n]);
rawL[n] = labelsBuf.readByte();
}
MersenneTwisterRNG rand = new MersenneTwisterRNG(MersenneTwisterRNG.SEEDS);
for (int n = count - 1; n >= 0; n--) {
int shuffle = rand.nextInt(n + 1);
byte[] image = rawI[shuffle];
rawI[shuffle] = rawI[n];
rawI[n] = image;
byte label = rawL[shuffle];
rawL[shuffle] = rawL[n];
rawL[n] = label;
}
Vec[] vecs = new Vec[PIXELS + 1];
NewChunk[] chunks = new NewChunk[vecs.length];
for (int v = 0; v < vecs.length; v++) {
vecs[v] = new AppendableVec(Key.make(UUID.randomUUID().toString()));
chunks[v] = new NewChunk(vecs[v], 0);
}
for (int n = 0; n < count; n++) {
for (int v = 0; v < vecs.length - 1; v++) chunks[v].addNum(rawI[n][v] & 0xff, 0);
chunks[chunks.length - 1].addNum(rawL[n], 0);
}
for (int v = 0; v < vecs.length; v++) {
chunks[v].close(0, null);
vecs[v] = ((AppendableVec) vecs[v]).close(null);
}
Frame frame = new Frame(null, vecs);
Utils.writeFileAndClose(new File(dest), frame.toCSV(false));
imagesBuf.close();
labelsBuf.close();
}
Aggregations