use of org.jblas.DoubleMatrix in project MultiTypeTree by tgvaughan.
the class SCMigrationModel method getRpowN.
@Override
public DoubleMatrix getRpowN(int n, boolean symmetric) {
updateMatrices();
List<DoubleMatrix> matPowerList;
DoubleMatrix mat, matPowerMax;
if (symmetric) {
matPowerList = RsymPowN;
mat = Rsym;
matPowerMax = RsymPowMax;
} else {
matPowerList = RpowN;
mat = R;
matPowerMax = RpowMax;
}
if (n >= matPowerList.size()) {
// Steady state of matrix iteration already reached
if ((symmetric && RsymPowSteady) || (!symmetric && RpowSteady)) {
// System.out.println("Assuming R SS.");
return matPowerList.get(matPowerList.size() - 1);
}
int startN = matPowerList.size();
for (int i = startN; i <= n; i++) {
matPowerList.add(matPowerList.get(i - 1).mmul(mat));
matPowerMax.maxi(matPowerList.get(i));
// Occasionally check whether matrix iteration has reached steady state
if (i % 10 == 0) {
double maxDiff = 0.0;
for (double el : matPowerList.get(i).sub(matPowerList.get(i - 1)).toArray()) maxDiff = Math.max(maxDiff, Math.abs(el));
if (!(maxDiff > 0)) {
if (symmetric)
RsymPowSteady = true;
else
RpowSteady = true;
return matPowerList.get(i);
}
}
}
}
return matPowerList.get(n);
}
use of org.jblas.DoubleMatrix in project MultiTypeTree by tgvaughan.
the class SCMigrationModel method updateMatrices.
/**
* Ensure all local fields including matrices and eigenvalue decomposition
* objects are consistent with current values held by inputs.
*/
public void updateMatrices() {
if (!dirty)
return;
mu = 0.0;
muSym = 0.0;
Q = new DoubleMatrix(nTypes, nTypes);
Qsym = new DoubleMatrix(nTypes, nTypes);
// transition rate matrix Qsym:
for (int i = 0; i < nTypes; i++) {
Q.put(i, i, 0.0);
Qsym.put(i, i, 0.0);
for (int j = 0; j < nTypes; j++) {
if (i != j) {
Q.put(i, j, getBackwardRate(i, j));
Q.put(i, i, Q.get(i, i) - Q.get(i, j));
Qsym.put(i, j, 0.5 * (getBackwardRate(i, j) + getBackwardRate(j, i)));
Qsym.put(i, i, Qsym.get(i, i) - Qsym.get(i, j));
}
}
if (-Q.get(i, i) > mu)
mu = -Q.get(i, i);
if (-Qsym.get(i, i) > muSym)
muSym = -Qsym.get(i, i);
}
// Set up uniformized backward transition rate matrices R and Rsym:
R = Q.mul(1.0 / mu).add(DoubleMatrix.eye(nTypes));
Rsym = Qsym.mul(1.0 / muSym).add(DoubleMatrix.eye(nTypes));
// Clear cached powers of R and Rsym and steady state flag:
RpowN.clear();
RsymPowN.clear();
RpowSteady = false;
RsymPowSteady = false;
// Power sequences initially contain R^0 = I
RpowN.add(DoubleMatrix.eye(nTypes));
RsymPowN.add(DoubleMatrix.eye(nTypes));
RpowMax = DoubleMatrix.eye(nTypes);
RsymPowMax = DoubleMatrix.eye(nTypes);
dirty = false;
}
use of org.jblas.DoubleMatrix in project MultiTypeTree by tgvaughan.
the class SCMigrationModel method main.
/**
* Main for debugging.
*
* @param args
*/
public static void main(String[] args) {
int n = 10;
DoubleMatrix Q = new DoubleMatrix(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Q.put(i, j, i * n + j);
}
}
MatrixFunctions.expm(Q.mul(0.001)).print();
Q.print();
}
use of org.jblas.DoubleMatrix in project MindsEye by SimiaCryptus.
the class FullyConnectedLayer method multiply.
/**
* Multiply.
*
* @param matrix the matrix
* @param in the in
* @param out the out
*/
public static void multiply(final double[] matrix, @Nonnull final double[] in, @Nonnull final double[] out) {
@Nonnull final DoubleMatrix matrixObj = new DoubleMatrix(out.length, in.length, matrix);
matrixObj.mmuli(new DoubleMatrix(in.length, 1, in), new DoubleMatrix(out.length, 1, out));
}
use of org.jblas.DoubleMatrix 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());
}
};
}
Aggregations