Search in sources :

Example 21 with Tensor

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

the class Join method generalSubspaceJoin.

private Tensor generalSubspaceJoin(Tensor subspace, Tensor superspace, TensorType joinedType, boolean reversedArgumentOrder) {
    int[] subspaceIndexes = subspaceIndexes(superspace.type(), subspace.type());
    Tensor.Builder builder = Tensor.Builder.of(joinedType);
    for (Iterator<Tensor.Cell> i = superspace.cellIterator(); i.hasNext(); ) {
        Map.Entry<TensorAddress, Double> supercell = i.next();
        TensorAddress subaddress = mapAddressToSubspace(supercell.getKey(), subspaceIndexes);
        double subspaceValue = subspace.get(subaddress);
        if (!Double.isNaN(subspaceValue))
            builder.cell(supercell.getKey(), reversedArgumentOrder ? combinator.applyAsDouble(supercell.getValue(), subspaceValue) : combinator.applyAsDouble(subspaceValue, supercell.getValue()));
    }
    return builder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with Tensor

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

the class Concat method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor a = argumentA.evaluate(context);
    Tensor b = argumentB.evaluate(context);
    a = ensureIndexedDimension(dimension, a);
    b = ensureIndexedDimension(dimension, b);
    // If you get an exception here you have implemented a mixed tensor
    IndexedTensor aIndexed = (IndexedTensor) a;
    IndexedTensor bIndexed = (IndexedTensor) b;
    TensorType concatType = type(a.type(), b.type());
    DimensionSizes concatSize = concatSize(concatType, aIndexed, bIndexed, dimension);
    Tensor.Builder builder = Tensor.Builder.of(concatType, concatSize);
    long aDimensionLength = aIndexed.type().indexOfDimension(dimension).map(d -> aIndexed.dimensionSizes().size(d)).orElseThrow(RuntimeException::new);
    int[] aToIndexes = mapIndexes(a.type(), concatType);
    int[] bToIndexes = mapIndexes(b.type(), concatType);
    concatenateTo(aIndexed, bIndexed, aDimensionLength, concatType, aToIndexes, bToIndexes, builder);
    concatenateTo(bIndexed, aIndexed, 0, concatType, bToIndexes, aToIndexes, builder);
    return builder.build();
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) Set(java.util.Set) TensorType(com.yahoo.tensor.TensorType) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TensorAddress(com.yahoo.tensor.TensorAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) EvaluationContext(com.yahoo.tensor.evaluation.EvaluationContext) DimensionSizes(com.yahoo.tensor.DimensionSizes) TypeContext(com.yahoo.tensor.evaluation.TypeContext) Optional(java.util.Optional) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes) TensorType(com.yahoo.tensor.TensorType) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 23 with Tensor

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

the class SlimeSummaryTestCase method testDecoding.

@Test
public void testDecoding() {
    Tensor tensor1 = Tensor.from("tensor(x{},y{}):{{x:foo,y:bar}:0.1}");
    Tensor tensor2 = Tensor.from("tensor(x[],y[1]):{{x:0,y:0}:-0.3}");
    String summary_cf = "file:src/test/java/com/yahoo/prelude/fastsearch/summary.cfg";
    DocsumDefinitionSet set = createDocsumDefinitionSet(summary_cf);
    byte[] docsum = makeDocsum(tensor1, tensor2);
    FastHit hit = new FastHit();
    assertNull(set.lazyDecode("default", docsum, hit));
    assertThat(hit.getField("integer_field"), equalTo(4));
    assertThat(hit.getField("short_field"), equalTo((short) 2));
    assertThat(hit.getField("byte_field"), equalTo((byte) 1));
    assertThat(hit.getField("float_field"), equalTo(4.5f));
    assertThat(hit.getField("double_field"), equalTo(8.75));
    assertThat(hit.getField("int64_field"), equalTo(8L));
    assertThat(hit.getField("string_field"), equalTo("string_value"));
    assertThat(hit.getField("data_field"), instanceOf(RawData.class));
    assertThat(hit.getField("data_field").toString(), equalTo("data_value"));
    assertThat(hit.getField("longstring_field"), equalTo((Object) "longstring_value"));
    assertThat(hit.getField("longdata_field"), instanceOf(RawData.class));
    assertThat(hit.getField("longdata_field").toString(), equalTo("longdata_value"));
    assertThat(hit.getField("xmlstring_field"), instanceOf(XMLString.class));
    assertThat(hit.getField("xmlstring_field").toString(), equalTo("<tag>xmlstring_value</tag>"));
    if (hit.getField("jsonstring_field") instanceof JSONString) {
        JSONString jstr = (JSONString) hit.getField("jsonstring_field");
        assertThat(jstr.getContent(), equalTo("{\"foo\":1,\"bar\":2}"));
        assertThat(jstr.getParsedJSON(), notNullValue());
        com.yahoo.data.access.Inspectable obj = jstr;
        com.yahoo.data.access.Inspector value = obj.inspect();
        assertThat(value.field("foo").asLong(), equalTo(1L));
        assertThat(value.field("bar").asLong(), equalTo(2L));
    } else {
        StructuredData sdata = (StructuredData) hit.getField("jsonstring_field");
        assertThat(sdata.toJson(), equalTo("{\"foo\":1,\"bar\":2}"));
        com.yahoo.data.access.Inspectable obj = sdata;
        com.yahoo.data.access.Inspector value = obj.inspect();
        assertThat(value.field("foo").asLong(), equalTo(1L));
        assertThat(value.field("bar").asLong(), equalTo(2L));
    }
    assertEquals(tensor1, hit.getField("tensor_field1"));
    assertEquals(tensor2, hit.getField("tensor_field2"));
}
Also used : RawData(com.yahoo.prelude.hitfield.RawData) Tensor(com.yahoo.tensor.Tensor) StructuredData(com.yahoo.search.result.StructuredData) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 24 with Tensor

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

the class JoinTestCase method testJoinIndexedSubspace.

/**
 * Test the indexed subspace join optimization
 */
@Test
public void testJoinIndexedSubspace() {
    Tensor t1, t2;
    t1 = Tensor.from("tensor(x[]):{{x:0}:1.0,{x:1}:2.0}");
    t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
    assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:20.0,{x:1,y:1,z:0}:0.0}"), t1.multiply(t2));
    t1 = Tensor.from("tensor(x[]):{{x:0}:1.0,{x:1}:2.0}");
    t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
    assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:5.0,{x:1,y:1,z:0}:0.0}"), t2.divide(t1));
    t1 = Tensor.from("tensor(y[]):{{y:0}:1.0,{y:1}:2.0}");
    t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
    assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10.0,{x:1,y:1,z:0}:0.0}"), t1.multiply(t2));
    t1 = Tensor.from("tensor(y[]):{{y:0}:1.0,{y:1}:2.0}");
    t2 = Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10,{x:1,y:1,z:0}:0.0}");
    assertEquals(Tensor.from("tensor(x[],y[],z[]):{{x:0,y:0,z:0}:6,{x:0,y:1,z:0}:0.0,{x:1,y:0,z:0}:10.0,{x:1,y:1,z:0}:0.0}"), t2.divide(t1));
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 25 with Tensor

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

the class JsonFormatTestCase method testJsonEncodingOfDenseTensor.

@Test
public void testJsonEncodingOfDenseTensor() {
    Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x{},y{})"));
    builder.cell().label("x", 0).label("y", 0).value(2.0);
    builder.cell().label("x", 0).label("y", 1).value(3.0);
    builder.cell().label("x", 1).label("y", 0).value(5.0);
    builder.cell().label("x", 1).label("y", 1).value(7.0);
    Tensor tensor = builder.build();
    byte[] json = JsonFormat.encode(tensor);
    assertEquals("{\"cells\":[" + "{\"address\":{\"x\":\"0\",\"y\":\"0\"},\"value\":2.0}," + "{\"address\":{\"x\":\"0\",\"y\":\"1\"},\"value\":3.0}," + "{\"address\":{\"x\":\"1\",\"y\":\"0\"},\"value\":5.0}," + "{\"address\":{\"x\":\"1\",\"y\":\"1\"},\"value\":7.0}" + "]}", new String(json, StandardCharsets.UTF_8));
}
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