Search in sources :

Example 1 with MersenneTwisterRNG

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();
}
Also used : Frame(water.fvec.Frame) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) NewChunk(water.fvec.NewChunk) GZIPInputStream(java.util.zip.GZIPInputStream) Vec(water.fvec.Vec) AppendableVec(water.fvec.AppendableVec) AppendableVec(water.fvec.AppendableVec) File(java.io.File) MersenneTwisterRNG(hex.rng.MersenneTwisterRNG)

Aggregations

MersenneTwisterRNG (hex.rng.MersenneTwisterRNG)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 AppendableVec (water.fvec.AppendableVec)1 Frame (water.fvec.Frame)1 NewChunk (water.fvec.NewChunk)1 Vec (water.fvec.Vec)1