Search in sources :

Example 11 with ArrayTensor

use of jcog.math.tensor.ArrayTensor in project narchy by automenta.

the class BagTest method samplingPriDist.

public static Tensor samplingPriDist(@NotNull Bag<PLink<String>, PLink<String>> b, int batches, int batchSize, int bins) {
    assert (bins > 1);
    Set<String> hit = new TreeSet();
    Frequency hits = new Frequency();
    ArrayTensor f = new ArrayTensor(bins);
    assertFalse(b.isEmpty());
    Random rng = new XoRoShiRo128PlusRandom(1);
    for (int i = 0; i < batches; i++) {
        b.sample(rng, batchSize, x -> {
            f.data[Util.bin(b.pri(x), bins)]++;
            String s = x.get();
            hits.addValue(s);
            hit.add(s);
        });
    }
    int total = batches * batchSize;
    assertEquals(total, Util.sum(f.data), 0.001f);
    if (hits.getUniqueCount() != b.size()) {
        System.out.println(hits.getUniqueCount() + " != " + b.size());
        Set<String> items = b.stream().map(PLink::get).collect(Collectors.toSet());
        items.removeAll(hit);
        System.out.println("not hit: " + items);
        System.out.println(hits);
        fail("all elements must have been sampled at least once");
    }
    return f.scale(1f / total);
}
Also used : XoRoShiRo128PlusRandom(jcog.math.random.XoRoShiRo128PlusRandom) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) XoRoShiRo128PlusRandom(jcog.math.random.XoRoShiRo128PlusRandom) Frequency(org.apache.commons.math3.stat.Frequency) ArrayTensor(jcog.math.tensor.ArrayTensor)

Example 12 with ArrayTensor

use of jcog.math.tensor.ArrayTensor in project narchy by automenta.

the class TensorTest method test1DTensorChain.

@Test
public void test1DTensorChain() {
    ArrayTensor a = new ArrayTensor(4);
    a.set(1, 2);
    ArrayTensor b = new ArrayTensor(2);
    b.set(2, 0);
    Tensor ab = TensorChain.get(a, b);
    assertEquals(1, ab.shape().length);
    assertEquals(6, ab.shape()[0]);
    final String[] s = { "" };
    ab.forEach((i, v) -> s[0] += v + " ");
    assertEquals("[0.0 0.0 1.0 0.0 2.0 0.0 ]", Arrays.toString(s));
}
Also used : ArrayTensor(jcog.math.tensor.ArrayTensor) Tensor(jcog.math.tensor.Tensor) ArrayTensor(jcog.math.tensor.ArrayTensor) Test(org.junit.jupiter.api.Test)

Example 13 with ArrayTensor

use of jcog.math.tensor.ArrayTensor in project narchy by automenta.

the class SineWave method get.

/* (non-Javadoc)
     * @see net.beadsproject.beads.data.BufferFactory#generateBuffer(int)
     */
@Override
public ArrayTensor get(int bufferSize) {
    int size = bufferSize;
    ArrayTensor b = new ArrayTensor(size);
    float[] bd = b.data;
    for (int i = 0; i < bufferSize; i++) {
        bd[i] = (float) Math.sin(2.0 * Math.PI * i / bufferSize);
    }
    return b;
}
Also used : ArrayTensor(jcog.math.tensor.ArrayTensor)

Example 14 with ArrayTensor

use of jcog.math.tensor.ArrayTensor in project narchy by automenta.

the class TriangleWave method get.

/* (non-Javadoc)
     * @see net.beadsproject.beads.data.BufferFactory#generateBuffer(int)
     */
@Override
public ArrayTensor get(int bufferSize) {
    int size = bufferSize;
    ArrayTensor b = new ArrayTensor(size);
    for (int i = 0; i < bufferSize; i++) {
        b.data[i] = i < bufferSize / 2f ? i / (bufferSize / 2f) * 2.0f - 1.0f : (1f - ((i - (bufferSize / 2f)) / (bufferSize / 2f))) * 2.0f - 1.0f;
    }
    return b;
}
Also used : ArrayTensor(jcog.math.tensor.ArrayTensor)

Example 15 with ArrayTensor

use of jcog.math.tensor.ArrayTensor in project narchy by automenta.

the class CurveWave method get.

/* (non-Javadoc)
     * @see net.beadsproject.beads.data.BufferFactory#generateBuffer(int)
     */
@Override
public ArrayTensor get(int bufferSize) {
    int size = bufferSize;
    ArrayTensor b = new ArrayTensor(size);
    double exponent = Math.exp(-curviness);
    for (int i = 0; i < bufferSize; i++) {
        b.data[i] = (float) Math.pow(((float) i) / bufferSize, exponent);
    }
    return b;
}
Also used : ArrayTensor(jcog.math.tensor.ArrayTensor)

Aggregations

ArrayTensor (jcog.math.tensor.ArrayTensor)18 Test (org.junit.jupiter.api.Test)3 XoRoShiRo128PlusRandom (jcog.math.random.XoRoShiRo128PlusRandom)1 XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)1 Tensor (jcog.math.tensor.Tensor)1 Frequency (org.apache.commons.math3.stat.Frequency)1