Search in sources :

Example 1 with TensorAddress

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

the class Map method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor argument = argument().evaluate(context);
    Tensor.Builder builder = Tensor.Builder.of(argument.type());
    for (Iterator<Tensor.Cell> i = argument.cellIterator(); i.hasNext(); ) {
        java.util.Map.Entry<TensorAddress, Double> cell = i.next();
        builder.cell(cell.getKey(), mapper.applyAsDouble(cell.getValue()));
    }
    return builder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) Tensor(com.yahoo.tensor.Tensor)

Example 2 with TensorAddress

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

the class Join method joinTo.

private void joinTo(IndexedTensor a, IndexedTensor b, TensorType joinedType, DimensionSizes joinedSize, int[] aToIndexes, int[] bToIndexes, boolean reversedOrder, Tensor.Builder builder) {
    Set<String> sharedDimensions = Sets.intersection(a.type().dimensionNames(), b.type().dimensionNames());
    Set<String> dimensionsOnlyInA = Sets.difference(a.type().dimensionNames(), b.type().dimensionNames());
    DimensionSizes aIterateSize = joinedSizeOf(a.type(), joinedType, joinedSize);
    DimensionSizes bIterateSize = joinedSizeOf(b.type(), joinedType, joinedSize);
    // for each combination of dimensions only in a
    for (Iterator<IndexedTensor.SubspaceIterator> ia = a.subspaceIterator(dimensionsOnlyInA, aIterateSize); ia.hasNext(); ) {
        IndexedTensor.SubspaceIterator aSubspace = ia.next();
        // for each combination of dimensions in a which is also in b
        while (aSubspace.hasNext()) {
            Tensor.Cell aCell = aSubspace.next();
            PartialAddress matchingBCells = partialAddress(a.type(), aSubspace.address(), sharedDimensions);
            // for each matching combination of dimensions ony in b
            for (IndexedTensor.SubspaceIterator bSubspace = b.cellIterator(matchingBCells, bIterateSize); bSubspace.hasNext(); ) {
                Tensor.Cell bCell = bSubspace.next();
                TensorAddress joinedAddress = joinAddresses(aCell.getKey(), aToIndexes, bCell.getKey(), bToIndexes, joinedType);
                double joinedValue = reversedOrder ? combinator.applyAsDouble(bCell.getValue(), aCell.getValue()) : combinator.applyAsDouble(aCell.getValue(), bCell.getValue());
                builder.cell(joinedAddress, joinedValue);
            }
        }
    }
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes) PartialAddress(com.yahoo.tensor.PartialAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 3 with TensorAddress

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

the class Join method mappedGeneralJoin.

private Tensor mappedGeneralJoin(Tensor a, Tensor b, TensorType joinedType) {
    int[] aToIndexes = mapIndexes(a.type(), joinedType);
    int[] bToIndexes = mapIndexes(b.type(), joinedType);
    Tensor.Builder builder = Tensor.Builder.of(joinedType);
    for (Iterator<Tensor.Cell> aIterator = a.cellIterator(); aIterator.hasNext(); ) {
        Map.Entry<TensorAddress, Double> aCell = aIterator.next();
        for (Iterator<Tensor.Cell> bIterator = b.cellIterator(); bIterator.hasNext(); ) {
            Map.Entry<TensorAddress, Double> bCell = bIterator.next();
            TensorAddress combinedAddress = joinAddresses(aCell.getKey(), aToIndexes, bCell.getKey(), bToIndexes, joinedType);
            // not combinable
            if (combinedAddress == null)
                continue;
            builder.cell(combinedAddress, combinator.applyAsDouble(aCell.getValue(), bCell.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 4 with TensorAddress

use of com.yahoo.tensor.TensorAddress 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 5 with TensorAddress

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

the class Concat method concatenateTo.

private void concatenateTo(IndexedTensor a, IndexedTensor b, long offset, TensorType concatType, int[] aToIndexes, int[] bToIndexes, Tensor.Builder builder) {
    Set<String> otherADimensions = a.type().dimensionNames().stream().filter(d -> !d.equals(dimension)).collect(Collectors.toSet());
    for (Iterator<IndexedTensor.SubspaceIterator> ia = a.subspaceIterator(otherADimensions); ia.hasNext(); ) {
        IndexedTensor.SubspaceIterator iaSubspace = ia.next();
        TensorAddress aAddress = iaSubspace.address();
        for (Iterator<IndexedTensor.SubspaceIterator> ib = b.subspaceIterator(otherADimensions); ib.hasNext(); ) {
            IndexedTensor.SubspaceIterator ibSubspace = ib.next();
            while (ibSubspace.hasNext()) {
                Tensor.Cell bCell = ibSubspace.next();
                TensorAddress combinedAddress = combineAddresses(aAddress, aToIndexes, bCell.getKey(), bToIndexes, concatType, offset, dimension);
                // incompatible
                if (combinedAddress == null)
                    continue;
                builder.cell(combinedAddress, bCell.getValue());
            }
            iaSubspace.reset();
        }
    }
}
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) TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Aggregations

TensorAddress (com.yahoo.tensor.TensorAddress)9 Tensor (com.yahoo.tensor.Tensor)8 IndexedTensor (com.yahoo.tensor.IndexedTensor)6 HashMap (java.util.HashMap)5 TensorType (com.yahoo.tensor.TensorType)4 Map (java.util.Map)4 ImmutableList (com.google.common.collect.ImmutableList)2 DimensionSizes (com.yahoo.tensor.DimensionSizes)2 List (java.util.List)2 PartialAddress (com.yahoo.tensor.PartialAddress)1 EvaluationContext (com.yahoo.tensor.evaluation.EvaluationContext)1 TypeContext (com.yahoo.tensor.evaluation.TypeContext)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1