Search in sources :

Example 6 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class BaggieTest method testSampling.

@Test
public void testSampling() {
    int cap = 8;
    Baggie<String> s = new Baggie(cap);
    for (int i = 0; i < cap; i++) {
        s.put("x" + i, i / ((float) cap));
    }
    String t = Joiner.on(",").join(s.toList());
    assertEquals("x7=0.87498856,x6=0.7500076,x5=0.6249962,x4=0.50001526,x3=0.3750038,x2=0.24999237,x1=0.12501144,x0=0.0", t);
    Random rng = new XorShift128PlusRandom(1);
    Frequency f = new Frequency();
    final int[] num = { cap * 100 };
    s.sample(rng, (x) -> {
        f.addValue(x.get());
        return num[0]-- > 0;
    });
    System.out.println(f);
    assertTrue(f.getCount("x" + (cap - 1)) > f.getCount("x0"));
    {
        // change all to a new value (0.5)
        final int[] resetNum = { cap * 100 };
        // TODO use s.commit()
        s.sample(rng, (x) -> {
            x.set(0.5f);
            return resetNum[0]-- > 0;
        });
        s.forEach((x, p) -> {
            assertEquals(0.5f, p, 0.001f);
            return true;
        });
        // effectively cleared
        assertEquals(cap, s.size());
        assertEquals(0.5f, s.priMax(), 0.001f);
        assertEquals(0.5f, s.priMin(), 0.001f);
    }
    {
        // remove everything
        final int[] remNum = { 0 };
        s.sample(rng, (x) -> {
            // delete it
            x.set(Float.NaN);
            remNum[0]++;
            return true;
        });
        // all removed during sampling
        assertEquals(cap, remNum[0]);
        // effectively cleared
        assertEquals(0, s.size());
        assertEquals(Float.NaN, s.priMax(), 0.001f);
        assertEquals(Float.NaN, s.priMin(), 0.001f);
    }
}
Also used : Test(org.junit.jupiter.api.Test) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Assertions(org.junit.jupiter.api.Assertions) Frequency(org.apache.commons.math3.stat.Frequency) Random(java.util.Random) Joiner(com.google.common.base.Joiner) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Frequency(org.apache.commons.math3.stat.Frequency) Test(org.junit.jupiter.api.Test)

Example 7 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class TestLSTM1 method testLSTM1.

@Test
public void testLSTM1() {
    // System.out.println("Test of SimpleLSTM\n");
    Random r = new XorShift128PlusRandom(1234);
    DistractedSequenceRecall task = new DistractedSequenceRecall(r, 12, 3, 22, 1000);
    int cell_blocks = 4;
    // double learningRate = 0.05;
    SimpleLSTM slstm = task.lstm(cell_blocks);
    int epochs = 150;
    double error = 0;
    for (int epoch = 0; epoch < epochs; epoch++) {
        double fit = task.scoreSupervised(slstm, 0.1f);
        error = 1 - fit;
        if (epoch % 10 == 0)
            System.out.println("[" + epoch + "] error = " + error);
    }
    // System.out.println("done.");
    assertTrue(error < 0.01f);
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) SimpleLSTM(jcog.learn.lstm.SimpleLSTM) DistractedSequenceRecall(jcog.learn.lstm.DistractedSequenceRecall) Test(org.junit.jupiter.api.Test)

Example 8 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class BagLab method update.

private synchronized void update() {
    int inputRate = 20;
    for (int j = 0; j < inputRate; j++) {
        int n = inputSliders.size();
        for (int i = 0; i < n; i++) {
            if (Math.random() < inputSliders.get(i).value()) {
                float p = (i + (float) Math.random()) / (n - 1);
                // float q = (float)Math.random(); //random quality
                bag.put(new PLink<>((int) Math.floor(Math.random() * uniques), p));
            }
        }
    }
    bag.commit();
    int bins = selectionHistogram.length;
    float sampleBatches = 1;
    int batchSize = 32;
    if (iteration++ % histogramResetPeriod == 0)
        Arrays.fill(selectionHistogram, 0);
    // System.out.println(bag.size());
    XorShift128PlusRandom rng = new XorShift128PlusRandom(1);
    List<PriReference<Integer>> sampled = $.newArrayList(1024);
    for (int i = 0; i < (int) sampleBatches; i++) {
        sampled.clear();
        // System.out.println(h + " " + v);
        bag.sample(rng, batchSize, (Consumer<PriReference<Integer>>) sampled::add);
        // BLink<Integer> sample = bag.sample();
        for (PriReference<Integer> sample : sampled) {
            if (sample != null) {
                float p = sample.priElseZero();
                selectionHistogram[Util.bin(p, bins - 1)]++;
            } else {
                break;
            }
        }
    }
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) PriReference(jcog.pri.PriReference)

Example 9 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class BaggieTest method testSustainedAdd.

@Test
public void testSustainedAdd() {
    int cap = 256;
    int uniq = cap * 2;
    Baggie<String> s = new Baggie<>(cap);
    Random rng = new XorShift128PlusRandom(1);
    for (int i = 0; i < cap * 256; i++) {
        s.put("x" + i, rng.nextFloat());
    }
    assertEquals(cap, s.size());
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Test(org.junit.jupiter.api.Test)

Example 10 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class PermutationsTest method testShuffleReset.

public void testShuffleReset(int size, int selected) {
    int expected = factorial(size);
    // just to be safe
    int attempts = (1 + expected / selected) * (1 + expected / selected);
    Set<String> sequences = new HashSet();
    Set<String> arrays = new TreeSet();
    Random rng = new XorShift128PlusRandom(2);
    int[] n = new int[size];
    ShuffledPermutations perm = new ShuffledPermutations();
    int attempt;
    for (attempt = 1; attempt < attempts; attempt++) {
        perm.restart(size, rng);
        StringBuilder sb = new StringBuilder();
        int x;
        for (x = 0; perm.hasNext() && x < selected; x++) {
            String aa = Arrays.toString(perm.nextPermute(n));
            arrays.add(aa);
            sb.append(aa).append(' ');
        }
        /*System.out.println(//perm.shuffle +
                    " " + sb.toString());*/
        sequences.add(sb.toString());
        if (arrays.size() == expected)
            break;
    }
    System.out.println(size + " exhausted all " + expected + " permutations after " + attempt + " attempts when sets of " + selected + " are selected");
    // by this point there should be at least > 1
    assertTrue(sequences.size() >= 1);
    // arrays.forEach(a -> System.out.println(a));
    assertTrue(expected >= arrays.size());
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom)

Aggregations

XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)12 Random (java.util.Random)6 Test (org.junit.jupiter.api.Test)6 Unify (nars.term.subst.Unify)3 Frequency (org.apache.commons.math3.stat.Frequency)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DistractedSequenceRecall (jcog.learn.lstm.DistractedSequenceRecall)2 SimpleLSTM (jcog.learn.lstm.SimpleLSTM)2 Compound (nars.term.Compound)2 Joiner (com.google.common.base.Joiner)1 BagTest (jcog.bag.BagTest)1 DefaultHijackBag (jcog.bag.impl.hijack.DefaultHijackBag)1 PriReference (jcog.pri.PriReference)1 PatternIndex (nars.index.term.PatternIndex)1 Term (nars.term.Term)1 Assertions (org.junit.jupiter.api.Assertions)1