Search in sources :

Example 11 with DoubleStream

use of java.util.stream.DoubleStream in project MindsEye by SimiaCryptus.

the class MaxImageBandLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    assert 1 == inObj.length;
    final TensorList inputData = inObj[0].getData();
    inputData.addRef();
    inputData.length();
    @Nonnull final int[] inputDims = inputData.getDimensions();
    assert 3 == inputDims.length;
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    final Coordinate[][] maxCoords = inputData.stream().map(data -> {
        Coordinate[] coordinates = IntStream.range(0, inputDims[2]).mapToObj(band -> {
            return data.coordStream(true).filter(e -> e.getCoords()[2] == band).max(Comparator.comparing(c -> data.get(c))).get();
        }).toArray(i -> new Coordinate[i]);
        data.freeRef();
        return coordinates;
    }).toArray(i -> new Coordinate[i][]);
    return new Result(TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(dataIndex -> {
        Tensor tensor = inputData.get(dataIndex);
        final DoubleStream doubleStream = IntStream.range(0, inputDims[2]).mapToDouble(band -> {
            final int[] maxCoord = maxCoords[dataIndex][band].getCoords();
            double v = tensor.get(maxCoord[0], maxCoord[1], band);
            return v;
        });
        Tensor tensor1 = new Tensor(1, 1, inputDims[2]).set(Tensor.getDoubles(doubleStream, inputDims[2]));
        tensor.freeRef();
        return tensor1;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (inObj[0].isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).parallel().mapToObj(dataIndex -> {
                Tensor deltaTensor = delta.get(dataIndex);
                @Nonnull final Tensor passback = new Tensor(inputData.getDimensions());
                IntStream.range(0, inputDims[2]).forEach(b -> {
                    final int[] maxCoord = maxCoords[dataIndex][b].getCoords();
                    passback.set(new int[] { maxCoord[0], maxCoord[1], b }, deltaTensor.get(0, 0, b));
                });
                deltaTensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            inObj[0].accumulate(buffer, tensorArray);
        }
    }) {

        @Override
        protected void _free() {
            Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
            inputData.freeRef();
        }

        @Override
        public boolean isAlive() {
            return inObj[0].isAlive();
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) DoubleStream(java.util.stream.DoubleStream) JsonUtil(com.simiacryptus.util.io.JsonUtil) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DoubleStream(java.util.stream.DoubleStream) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 12 with DoubleStream

use of java.util.stream.DoubleStream in project MindsEye by SimiaCryptus.

the class DoubleArrayStatsFacade method sumSq.

/**
 * Sum sq double.
 *
 * @return the double
 */
public double sumSq() {
    final DoubleStream doubleStream = Arrays.stream(data).map((final double x) -> x * x);
    final DoubleSummaryStatistics statistics = doubleStream.summaryStatistics();
    return statistics.getSum();
}
Also used : DoubleStream(java.util.stream.DoubleStream) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics)

Example 13 with DoubleStream

use of java.util.stream.DoubleStream in project guava by google.

the class StreamsTest method testMapWithIndex_doubleStream_closeIsPropagated.

private void testMapWithIndex_doubleStream_closeIsPropagated(DoubleStream source) {
    AtomicInteger doubleStreamCloseCount = new AtomicInteger();
    DoubleStream doubleStream = source.onClose(doubleStreamCloseCount::incrementAndGet);
    Stream<String> withIndex = Streams.mapWithIndex(doubleStream, (str, i) -> str + ":" + i);
    withIndex.close();
    Truth.assertThat(doubleStreamCloseCount.get()).isEqualTo(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoubleStream(java.util.stream.DoubleStream)

Example 14 with DoubleStream

use of java.util.stream.DoubleStream in project groovy by apache.

the class DefaultTypeTransformation method asArray.

public static Object asArray(final Object object, final Class type) {
    if (type.isAssignableFrom(object.getClass())) {
        return object;
    }
    if (object instanceof IntStream) {
        if (type.equals(int[].class)) {
            return ((IntStream) object).toArray();
        } else if (type.equals(long[].class)) {
            return ((IntStream) object).asLongStream().toArray();
        } else if (type.equals(double[].class)) {
            return ((IntStream) object).asDoubleStream().toArray();
        } else if (type.equals(Integer[].class)) {
            return ((IntStream) object).boxed().toArray(Integer[]::new);
        }
    } else if (object instanceof LongStream) {
        if (type.equals(long[].class)) {
            return ((LongStream) object).toArray();
        } else if (type.equals(double[].class)) {
            return ((LongStream) object).asDoubleStream().toArray();
        } else if (type.equals(Long[].class)) {
            return ((LongStream) object).boxed().toArray(Long[]::new);
        }
    } else if (object instanceof DoubleStream) {
        if (type.equals(double[].class)) {
            return ((DoubleStream) object).toArray();
        } else if (type.equals(Double[].class)) {
            return ((DoubleStream) object).boxed().toArray(Double[]::new);
        }
    }
    Class<?> elementType = type.getComponentType();
    Collection<?> collection = asCollection(object);
    Object array = Array.newInstance(elementType, collection.size());
    int i = 0;
    for (Object element : collection) {
        Array.set(array, i++, castToType(element, elementType));
    }
    return array;
}
Also used : BigInteger(java.math.BigInteger) DoubleStream(java.util.stream.DoubleStream) NullObject(org.codehaus.groovy.runtime.NullObject) LongStream(java.util.stream.LongStream) IntStream(java.util.stream.IntStream)

Example 15 with DoubleStream

use of java.util.stream.DoubleStream in project assertj-core by joel-costigliola.

the class Assertions_assertThat_with_DoubleStream_Test method should_initialise_actual.

@SuppressWarnings("unchecked")
@Test
void should_initialise_actual() {
    DoubleStream iterator = DoubleStream.of(823952.8, 1947230585.9);
    List<? extends Double> actual = assertThat(iterator).actual;
    assertThat((List<Double>) actual).contains(823952.8, atIndex(0)).contains(1947230585.9, atIndex(1));
}
Also used : DoubleStream(java.util.stream.DoubleStream) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleStream (java.util.stream.DoubleStream)34 IntStream (java.util.stream.IntStream)11 Test (org.junit.jupiter.api.Test)10 List (java.util.List)9 Arrays (java.util.Arrays)7 Map (java.util.Map)6 Random (java.util.Random)6 Collectors (java.util.stream.Collectors)6 LongStream (java.util.stream.LongStream)6 Stream (java.util.stream.Stream)5 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 Function (java.util.function.Function)4 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)4 StorageConstants (org.apache.ignite.ml.math.StorageConstants)4 IgniteFunction (org.apache.ignite.ml.math.functions.IgniteFunction)4 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)4 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)4 LabeledVectorDouble (org.apache.ignite.ml.structures.LabeledVectorDouble)4