use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class AvgMetaLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result input = inObj[0];
input.addRef();
TensorList inputData = input.getData();
final int itemCnt = inputData.length();
@Nullable Tensor thisResult;
boolean passback;
if (null == lastResult || inputData.length() > minBatchCount) {
@Nonnull final ToDoubleFunction<Coordinate> f = (c) -> IntStream.range(0, itemCnt).mapToDouble(dataIndex -> {
Tensor tensor = inputData.get(dataIndex);
double v = tensor.get(c);
tensor.freeRef();
return v;
}).sum() / itemCnt;
Tensor tensor = inputData.get(0);
thisResult = tensor.mapCoords(f);
tensor.freeRef();
passback = true;
if (null != lastResult)
lastResult.freeRef();
lastResult = thisResult;
lastResult.addRef();
} else {
passback = false;
thisResult = lastResult;
thisResult.freeRef();
}
return new Result(TensorArray.create(thisResult), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (passback && input.isAlive()) {
@Nullable final Tensor delta = data.get(0);
@Nonnull final Tensor[] feedback = new Tensor[itemCnt];
Arrays.parallelSetAll(feedback, i -> new Tensor(delta.getDimensions()));
thisResult.coordStream(true).forEach((inputCoord) -> {
for (int inputItem = 0; inputItem < itemCnt; inputItem++) {
feedback[inputItem].add(inputCoord, delta.get(inputCoord) / itemCnt);
}
});
delta.freeRef();
@Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
input.accumulate(buffer, tensorArray);
}
}) {
@Override
public boolean isAlive() {
return input.isAlive();
}
@Override
protected void _free() {
thisResult.freeRef();
input.freeRef();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class CudaTensorList method addAndFree.
@Override
public TensorList addAndFree(@Nonnull final TensorList right) {
assertAlive();
right.assertAlive();
if (right instanceof ReshapedTensorList)
return addAndFree(((ReshapedTensorList) right).getInner());
if (1 < currentRefCount()) {
TensorList sum = add(right);
freeRef();
return sum;
}
assert length() == right.length();
if (heapCopy == null) {
if (right instanceof CudaTensorList) {
@Nonnull final CudaTensorList nativeRight = (CudaTensorList) right;
if (nativeRight.getPrecision() == this.getPrecision()) {
if (nativeRight.heapCopy == null) {
assert (!nativeRight.gpuCopy.equals(CudaTensorList.this.gpuCopy));
CudaMemory rightMem = gpuCopy.memory;
CudaMemory leftMem = rightMem;
if (null != leftMem && null != rightMem)
return CudaSystem.run(gpu -> {
if (gpu.getDeviceId() == leftMem.getDeviceId()) {
return gpu.addInPlace(this, nativeRight);
} else {
assertAlive();
right.assertAlive();
TensorList add = add(right);
freeRef();
return add;
}
}, this, right);
}
}
}
}
if (right.length() == 0)
return this;
if (length() == 0)
throw new IllegalArgumentException();
assert length() == right.length();
return TensorArray.wrap(IntStream.range(0, length()).mapToObj(i -> {
Tensor a = get(i);
Tensor b = right.get(i);
@Nullable Tensor r = a.addAndFree(b);
b.freeRef();
return r;
}).toArray(i -> new Tensor[i]));
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class CudaTensorList method add.
@Override
public TensorList add(@Nonnull final TensorList right) {
assertAlive();
right.assertAlive();
assert length() == right.length();
if (right instanceof ReshapedTensorList)
return add(((ReshapedTensorList) right).getInner());
if (heapCopy == null) {
if (right instanceof CudaTensorList) {
@Nonnull final CudaTensorList nativeRight = (CudaTensorList) right;
if (nativeRight.getPrecision() == this.getPrecision()) {
if (nativeRight.heapCopy == null) {
return CudaSystem.run(gpu -> {
return gpu.add(this, nativeRight);
}, this);
}
}
}
}
if (right.length() == 0)
return this;
if (length() == 0)
throw new IllegalArgumentException();
assert length() == right.length();
return TensorArray.wrap(IntStream.range(0, length()).mapToObj(i -> {
Tensor a = get(i);
Tensor b = right.get(i);
@Nullable Tensor r = a.addAndFree(b);
b.freeRef();
return r;
}).toArray(i -> new Tensor[i]));
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class BiasMetaLayer method eval.
@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
final int itemCnt = inObj[0].getData().length();
Tensor tensor1 = inObj[1].getData().get(0);
final Tensor[] tensors = IntStream.range(0, itemCnt).parallel().mapToObj(dataIndex -> {
Tensor tensor = inObj[0].getData().get(dataIndex);
Tensor mapIndex = tensor.mapIndex((v, c) -> {
return v + tensor1.get(c);
});
tensor.freeRef();
return mapIndex;
}).toArray(i -> new Tensor[i]);
tensor1.freeRef();
Tensor tensor0 = tensors[0];
tensor0.addRef();
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
return new Result(TensorArray.wrap(tensors), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (inObj[0].isAlive()) {
data.addRef();
inObj[0].accumulate(buffer, data);
}
if (inObj[1].isAlive()) {
@Nonnull final ToDoubleFunction<Coordinate> f = (c) -> {
return IntStream.range(0, itemCnt).mapToDouble(i -> {
Tensor tensor = data.get(i);
double v = tensor.get(c);
tensor.freeRef();
return v;
}).sum();
};
@Nullable final Tensor passback = tensor0.mapCoords(f);
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, inObj[1].getData().length()).mapToObj(i -> {
if (i == 0)
return passback;
else {
@Nullable Tensor map = passback.map(v -> 0);
passback.freeRef();
return map;
}
}).toArray(i -> new Tensor[i]));
inObj[1].accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
tensor0.freeRef();
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return inObj[0].isAlive() || inObj[1].isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class BinaryNoiseLayer method eval.
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
final Result input = inObj[0];
if (!enabled)
return input;
@Nonnull final int[] dimensions = input.getData().getDimensions();
if (maskList.size() > 1 && !Arrays.equals(maskList.get(0).getDimensions(), dimensions)) {
maskList.clear();
}
final int length = input.getData().length();
@Nonnull final Tensor tensorPrototype = new Tensor(dimensions);
while (length > maskList.size()) {
maskList.add(tensorPrototype.map(v -> FastRandom.INSTANCE.random() < getValue() ? 0 : (1.0 / getValue())));
}
@Nonnull final TensorList mask = TensorArray.create(maskList.stream().limit(length).toArray(i -> new Tensor[i]));
return new Result(mask, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
data.addRef();
input.accumulate(buffer, data);
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return input.isAlive();
}
};
}
Aggregations