Search in sources :

Example 46 with Tensor

use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.

the class ConcatTestCase method testUnequalSizesSameDimension.

@Test
public void testUnequalSizesSameDimension() {
    Tensor a = Tensor.from("tensor(x[2]):{ {x:0}:1, {x:1}:2 }");
    Tensor b = Tensor.from("tensor(x[3]):{ {x:0}:4, {x:1}:5, {x:2}:6 }");
    assertConcat("tensor(x[5]):{ {x:0}:1, {x:1}:2, {x:2}:4, {x:3}:5, {x:4}:6 }", a, b, "x");
    assertConcat("tensor(x[2],y[2]):{ {x:0,y:0}:1, {x:1,y:0}:2, {x:0,y:1}:4, {x:1,y:1}:5 }", a, b, "y");
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 47 with Tensor

use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.

the class ConcatTestCase method testConcatNumberAndVectorUnbound.

@Test
public void testConcatNumberAndVectorUnbound() {
    Tensor a = Tensor.from("{1}");
    Tensor b = Tensor.from("tensor(x[]):{ {x:0}:2, {x:1}:3, {x:2}:4 }");
    assertConcat("tensor(x[])", "tensor(x[4]):{ {x:0}:1, {x:1}:2, {x:2}:3, {x:3}:4 }", a, b, "x");
    assertConcat("tensor(x[],y[2])", "tensor(x[3],y[2]):{ {x:0,y:0}:1, {x:1,y:0}:1, {x:2,y:0}:1, " + "{x:0,y:1}:2, {x:1,y:1}:3, {x:2,y:1}:4  }", a, b, "y");
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 48 with Tensor

use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.

the class ConcatTestCase method testDimensionsubset.

@Test
public void testDimensionsubset() {
    Tensor a = Tensor.from("tensor(x[],y[]):{ {x:0,y:0}:1, {x:1,y:0}:2, {x:0,y:1}:3, {x:1,y:1}:4 }");
    Tensor b = Tensor.from("tensor(y[2]):{ {y:0}:5, {y:1}:6 }");
    assertConcat("tensor(x[],y[])", "tensor(x[3],y[2]):{{x:0,y:0}:1.0,{x:0,y:1}:3.0,{x:1,y:0}:2.0,{x:1,y:1}:4.0,{x:2,y:0}:5.0,{x:2,y:1}:6.0}", a, b, "x");
    assertConcat("tensor(x[],y[])", "tensor(x[2],y[4]):{{x:0,y:0}:1.0,{x:0,y:1}:3.0,{x:0,y:2}:5.0,{x:0,y:3}:6.0,{x:1,y:0}:2.0,{x:1,y:1}:4.0,{x:1,y:2}:5.0,{x:1,y:3}:6.0}", a, b, "y");
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 49 with Tensor

use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.

the class MatmulTestCase method testMatmul2d.

@Test
public void testMatmul2d() {
    // d0 is the 'outermost' dimension, etc.
    Tensor.Builder ab = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[3])"));
    ab.cell(1, 0, 0);
    ab.cell(2, 0, 1);
    ab.cell(3, 0, 2);
    ab.cell(4, 1, 0);
    ab.cell(5, 1, 1);
    ab.cell(6, 1, 2);
    Tensor a = ab.build();
    Tensor.Builder bb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[3],d1[2])"));
    bb.cell(7, 0, 0);
    bb.cell(8, 0, 1);
    bb.cell(9, 1, 0);
    bb.cell(10, 1, 1);
    bb.cell(11, 2, 0);
    bb.cell(12, 2, 1);
    Tensor b = bb.build();
    Tensor.Builder rb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[2])"));
    rb.cell(58, 0, 0);
    rb.cell(64, 0, 1);
    rb.cell(139, 1, 0);
    rb.cell(154, 1, 1);
    Tensor r = rb.build();
    Tensor result = a.matmul(b.rename(ImmutableList.of("d0", "d1"), ImmutableList.of("d1", "d2")), "d1").rename("d2", "d1");
    assertEquals(r, result);
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 50 with Tensor

use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.

the class MatmulTestCase method testMatmul3d.

@Test
public void testMatmul3d() {
    // Convention: a is the 'outermost' dimension, etc.
    Tensor.Builder ab = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[2],d2[3])"));
    ab.cell(1, 0, 0, 0);
    ab.cell(2, 0, 0, 1);
    ab.cell(3, 0, 0, 2);
    ab.cell(4, 0, 1, 0);
    ab.cell(5, 0, 1, 1);
    ab.cell(6, 0, 1, 2);
    ab.cell(7, 1, 0, 0);
    ab.cell(8, 1, 0, 1);
    ab.cell(9, 1, 0, 2);
    ab.cell(10, 1, 1, 0);
    ab.cell(11, 1, 1, 1);
    ab.cell(12, 1, 1, 2);
    Tensor a = ab.build();
    Tensor.Builder bb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[3],d2[2])"));
    bb.cell(13, 0, 0, 0);
    bb.cell(14, 0, 0, 1);
    bb.cell(15, 0, 1, 0);
    bb.cell(16, 0, 1, 1);
    bb.cell(17, 0, 2, 0);
    bb.cell(18, 0, 2, 1);
    bb.cell(19, 1, 0, 0);
    bb.cell(20, 1, 0, 1);
    bb.cell(21, 1, 1, 0);
    bb.cell(22, 1, 1, 1);
    bb.cell(23, 1, 2, 0);
    bb.cell(24, 1, 2, 1);
    Tensor b = bb.build();
    Tensor.Builder rb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[2],d2[2])"));
    rb.cell(94, 0, 0, 0);
    rb.cell(100, 0, 0, 1);
    rb.cell(229, 0, 1, 0);
    rb.cell(244, 0, 1, 1);
    rb.cell(508, 1, 0, 0);
    rb.cell(532, 1, 0, 1);
    rb.cell(697, 1, 1, 0);
    rb.cell(730, 1, 1, 1);
    Tensor r = rb.build();
    Tensor result = a.matmul(b.rename(ImmutableList.of("d1", "d2"), ImmutableList.of("d2", "d3")), "d2").rename("d3", "d2");
    assertEquals(r, result);
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Aggregations

Tensor (com.yahoo.tensor.Tensor)58 Test (org.junit.Test)26 TensorType (com.yahoo.tensor.TensorType)17 IndexedTensor (com.yahoo.tensor.IndexedTensor)10 TensorAddress (com.yahoo.tensor.TensorAddress)7 MixedTensor (com.yahoo.tensor.MixedTensor)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 MapContext (com.yahoo.searchlib.rankingexpression.evaluation.MapContext)3 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)3 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)3 DimensionSizes (com.yahoo.tensor.DimensionSizes)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableList (com.google.common.collect.ImmutableList)2 MappedTensor (com.yahoo.tensor.MappedTensor)2 IOException (java.io.IOException)2 List (java.util.List)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 Path (com.yahoo.path.Path)1