use of com.simiacryptus.mindseye.lang.Tensor in project MindsEye by SimiaCryptus.
the class MaxPoolingLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
final Result in = inObj[0];
in.getData().length();
@Nonnull final int[] inputDims = in.getData().getDimensions();
final List<Tuple2<Integer, int[]>> regions = MaxPoolingLayer.calcRegionsCache.apply(new MaxPoolingLayer.CalcRegionsParameter(inputDims, kernelDims));
final Tensor[] outputA = IntStream.range(0, in.getData().length()).mapToObj(dataIndex -> {
final int[] newDims = IntStream.range(0, inputDims.length).map(i -> {
return (int) Math.ceil(inputDims[i] * 1.0 / kernelDims[i]);
}).toArray();
@Nonnull final Tensor output = new Tensor(newDims);
return output;
}).toArray(i -> new Tensor[i]);
Arrays.stream(outputA).mapToInt(x -> x.length()).sum();
@Nonnull final int[][] gradientMapA = new int[in.getData().length()][];
IntStream.range(0, in.getData().length()).forEach(dataIndex -> {
@Nullable final Tensor input = in.getData().get(dataIndex);
final Tensor output = outputA[dataIndex];
@Nonnull final IntToDoubleFunction keyExtractor = inputCoords -> input.get(inputCoords);
@Nonnull final int[] gradientMap = new int[input.length()];
regions.parallelStream().forEach(tuple -> {
final Integer from = tuple.getFirst();
final int[] toList = tuple.getSecond();
int toMax = -1;
double bestValue = Double.NEGATIVE_INFINITY;
for (final int c : toList) {
final double value = keyExtractor.applyAsDouble(c);
if (-1 == toMax || bestValue < value) {
bestValue = value;
toMax = c;
}
}
gradientMap[from] = toMax;
output.set(from, input.get(toMax));
});
input.freeRef();
gradientMapA[dataIndex] = gradientMap;
});
return new Result(TensorArray.wrap(outputA), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (in.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, in.getData().length()).parallel().mapToObj(dataIndex -> {
@Nonnull final Tensor backSignal = new Tensor(inputDims);
final int[] ints = gradientMapA[dataIndex];
@Nullable final Tensor datum = data.get(dataIndex);
for (int i = 0; i < datum.length(); i++) {
backSignal.add(ints[i], datum.get(i));
}
datum.freeRef();
return backSignal;
}).toArray(i -> new Tensor[i]));
in.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return in.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.Tensor in project MindsEye by SimiaCryptus.
the class MaxMetaLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result input = inObj[0];
input.addRef();
final int itemCnt = input.getData().length();
final Tensor input0Tensor = input.getData().get(0);
final int vectorSize = input0Tensor.length();
@Nonnull final int[] indicies = new int[vectorSize];
for (int i = 0; i < vectorSize; i++) {
final int itemNumber = i;
indicies[i] = IntStream.range(0, itemCnt).mapToObj(x -> x).max(Comparator.comparing(dataIndex -> {
Tensor tensor = input.getData().get(dataIndex);
double v = tensor.getData()[itemNumber];
tensor.freeRef();
return v;
})).get();
}
return new Result(TensorArray.wrap(input0Tensor.mapIndex((v, c) -> {
Tensor tensor = input.getData().get(indicies[c]);
double v1 = tensor.getData()[c];
tensor.freeRef();
return v1;
})), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (input.isAlive()) {
@Nullable final Tensor delta = data.get(0);
@Nonnull final Tensor[] feedback = new Tensor[itemCnt];
Arrays.parallelSetAll(feedback, i -> new Tensor(delta.getDimensions()));
input0Tensor.coordStream(true).forEach((inputCoord) -> {
feedback[indicies[inputCoord.getIndex()]].add(inputCoord, delta.get(inputCoord));
});
@Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
input.accumulate(buffer, tensorArray);
delta.freeRef();
}
}) {
@Override
public boolean isAlive() {
return input.isAlive();
}
@Override
protected void _free() {
input.freeRef();
input0Tensor.freeRef();
}
};
}
use of com.simiacryptus.mindseye.lang.Tensor in project MindsEye by SimiaCryptus.
the class MeanSqLossLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
if (2 != inObj.length)
throw new IllegalArgumentException();
final int leftLength = inObj[0].getData().length();
final int rightLength = inObj[1].getData().length();
Arrays.stream(inObj).forEach(ReferenceCounting::addRef);
if (leftLength != rightLength && leftLength != 1 && rightLength != 1) {
throw new IllegalArgumentException(leftLength + " != " + rightLength);
}
@Nonnull final Tensor[] diffs = new Tensor[leftLength];
return new Result(TensorArray.wrap(IntStream.range(0, leftLength).mapToObj(dataIndex -> {
@Nullable final Tensor a = inObj[0].getData().get(1 == leftLength ? 0 : dataIndex);
@Nullable final Tensor b = inObj[1].getData().get(1 == rightLength ? 0 : dataIndex);
if (a.length() != b.length()) {
throw new IllegalArgumentException(String.format("%s != %s", Arrays.toString(a.getDimensions()), Arrays.toString(b.getDimensions())));
}
@Nonnull final Tensor r = a.minus(b);
a.freeRef();
b.freeRef();
diffs[dataIndex] = r;
@Nonnull Tensor statsTensor = new Tensor(new double[] { r.sumSq() / r.length() }, 1);
return statsTensor;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (inObj[0].isAlive()) {
Stream<Tensor> tensorStream = IntStream.range(0, data.length()).parallel().mapToObj(dataIndex -> {
@Nullable Tensor tensor = data.get(dataIndex);
Tensor diff = diffs[dataIndex];
@Nullable Tensor scale = diff.scale(tensor.get(0) * 2.0 / diff.length());
tensor.freeRef();
return scale;
}).collect(Collectors.toList()).stream();
if (1 == leftLength) {
tensorStream = Stream.of(tensorStream.reduce((a, b) -> {
@Nullable Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).get());
}
@Nonnull final TensorList array = TensorArray.wrap(tensorStream.toArray(i -> new Tensor[i]));
inObj[0].accumulate(buffer, array);
}
if (inObj[1].isAlive()) {
Stream<Tensor> tensorStream = IntStream.range(0, data.length()).parallel().mapToObj(dataIndex -> {
@Nullable Tensor tensor = data.get(dataIndex);
@Nullable Tensor scale = diffs[dataIndex].scale(tensor.get(0) * 2.0 / diffs[dataIndex].length());
tensor.freeRef();
return scale;
}).collect(Collectors.toList()).stream();
if (1 == rightLength) {
tensorStream = Stream.of(tensorStream.reduce((a, b) -> {
@Nullable Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).get());
}
@Nonnull final TensorList array = TensorArray.wrap(tensorStream.map(x -> {
@Nullable Tensor scale = x.scale(-1);
x.freeRef();
return scale;
}).toArray(i -> new Tensor[i]));
inObj[1].accumulate(buffer, array);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
Arrays.stream(diffs).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return inObj[0].isAlive() || inObj[1].isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.Tensor in project MindsEye by SimiaCryptus.
the class FullyConnectedLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
final TensorList indata = inObj[0].getData();
indata.addRef();
for (@Nonnull Result result : inObj) {
result.addRef();
}
FullyConnectedLayer.this.addRef();
assert Tensor.length(indata.getDimensions()) == Tensor.length(this.inputDims) : Arrays.toString(indata.getDimensions()) + " == " + Arrays.toString(this.inputDims);
@Nonnull DoubleMatrix doubleMatrix = new DoubleMatrix(Tensor.length(indata.getDimensions()), Tensor.length(outputDims), this.weights.getData());
@Nonnull final DoubleMatrix matrixObj = FullyConnectedLayer.transpose(doubleMatrix);
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, indata.length()).parallel().mapToObj(dataIndex -> {
@Nullable final Tensor input = indata.get(dataIndex);
@Nullable final Tensor output = new Tensor(outputDims);
matrixObj.mmuli(new DoubleMatrix(input.length(), 1, input.getData()), new DoubleMatrix(output.length(), 1, output.getData()));
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]));
RecycleBin.DOUBLES.recycle(matrixObj.data, matrixObj.data.length);
this.weights.addRef();
return new Result(tensorArray, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(FullyConnectedLayer.this, this.weights.getData());
final int threads = 4;
IntStream.range(0, threads).parallel().mapToObj(x -> x).flatMap(thread -> {
@Nullable Stream<Tensor> stream = IntStream.range(0, indata.length()).filter(i -> thread == i % threads).mapToObj(dataIndex -> {
@Nonnull final Tensor weightDelta = new Tensor(Tensor.length(inputDims), Tensor.length(outputDims));
Tensor deltaTensor = delta.get(dataIndex);
Tensor inputTensor = indata.get(dataIndex);
FullyConnectedLayer.crossMultiplyT(deltaTensor.getData(), inputTensor.getData(), weightDelta.getData());
inputTensor.freeRef();
deltaTensor.freeRef();
return weightDelta;
});
return stream;
}).reduce((a, b) -> {
@Nullable Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).map(data -> {
@Nonnull Delta<Layer> layerDelta = deltaBuffer.addInPlace(data.getData());
data.freeRef();
return layerDelta;
});
deltaBuffer.freeRef();
}
if (inObj[0].isAlive()) {
@Nonnull final TensorList tensorList = TensorArray.wrap(IntStream.range(0, indata.length()).parallel().mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nonnull final Tensor passback = new Tensor(indata.getDimensions());
FullyConnectedLayer.multiply(this.weights.getData(), deltaTensor.getData(), passback.getData());
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
inObj[0].accumulate(buffer, tensorList);
}
}) {
@Override
protected void _free() {
indata.freeRef();
FullyConnectedLayer.this.freeRef();
for (@Nonnull Result result : inObj) {
result.freeRef();
}
FullyConnectedLayer.this.weights.freeRef();
}
@Override
public boolean isAlive() {
return !isFrozen() || Arrays.stream(inObj).anyMatch(x -> x.isAlive());
}
};
}
use of com.simiacryptus.mindseye.lang.Tensor in project MindsEye by SimiaCryptus.
the class HyperbolicActivationLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final TensorList indata = inObj[0].getData();
indata.addRef();
inObj[0].addRef();
weights.addRef();
HyperbolicActivationLayer.this.addRef();
final int itemCnt = indata.length();
return new Result(TensorArray.wrap(IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
@Nullable final Tensor input = indata.get(dataIndex);
@Nullable Tensor map = input.map(v -> {
final int sign = v < 0 ? negativeMode : 1;
final double a = Math.max(0, weights.get(v < 0 ? 1 : 0));
return sign * (Math.sqrt(Math.pow(a * v, 2) + 1) - a) / a;
});
input.freeRef();
return map;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (!isFrozen()) {
IntStream.range(0, delta.length()).forEach(dataIndex -> {
@Nullable Tensor deltaI = delta.get(dataIndex);
@Nullable Tensor inputI = indata.get(dataIndex);
@Nullable final double[] deltaData = deltaI.getData();
@Nullable final double[] inputData = inputI.getData();
@Nonnull final Tensor weightDelta = new Tensor(weights.getDimensions());
for (int i = 0; i < deltaData.length; i++) {
final double d = deltaData[i];
final double x = inputData[i];
final int sign = x < 0 ? negativeMode : 1;
final double a = Math.max(0, weights.getData()[x < 0 ? 1 : 0]);
weightDelta.add(x < 0 ? 1 : 0, -sign * d / (a * a * Math.sqrt(1 + Math.pow(a * x, 2))));
}
deltaI.freeRef();
inputI.freeRef();
buffer.get(HyperbolicActivationLayer.this, weights.getData()).addInPlace(weightDelta.getData()).freeRef();
weightDelta.freeRef();
});
}
if (inObj[0].isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
@Nullable Tensor inputTensor = indata.get(dataIndex);
Tensor deltaTensor = delta.get(dataIndex);
@Nullable final double[] deltaData = deltaTensor.getData();
@Nonnull final int[] dims = indata.getDimensions();
@Nonnull final Tensor passback = new Tensor(dims);
for (int i = 0; i < passback.length(); i++) {
final double x = inputTensor.getData()[i];
final double d = deltaData[i];
final int sign = x < 0 ? negativeMode : 1;
final double a = Math.max(0, weights.getData()[x < 0 ? 1 : 0]);
passback.set(i, sign * d * a * x / Math.sqrt(1 + a * x * a * x));
}
deltaTensor.freeRef();
inputTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
inObj[0].accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
indata.freeRef();
inObj[0].freeRef();
weights.freeRef();
HyperbolicActivationLayer.this.freeRef();
}
@Override
public boolean isAlive() {
return inObj[0].isAlive() || !isFrozen();
}
};
}
Aggregations