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);
}
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));
}
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;
}
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;
}
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;
}
Aggregations