Search in sources :

Example 21 with RealVector

use of org.apache.commons.math3.linear.RealVector in project narchy by automenta.

the class GreedyGQ method update.

public double update(RealVector x_t, A a_t, double r_tp1, double gamma_tp1, double z_tp1, RealVector x_tp1, A a_tp1) {
    rho_t = 0.0;
    if (a_t != null && x_t != null) /*!Vectors.isNull(x_t)*/
    {
        target.update(x_t);
        behaviour.update(x_t);
        rho_t = target.pi(a_t) / behaviour.pi(a_t);
    }
    // assert Utils.checkValue(rho_t);
    VectorPool pool = VectorPools.pool(prototype, gq.v.getDimension());
    RealVector sa_bar_tp1 = pool.newVector();
    // if (!Vectors.isNull(x_t) && !Vectors.isNull(x_tp1)) {
    if (x_t != null && x_tp1 != null) {
        target.update(x_tp1);
        for (A a : actions) {
            double pi = target.pi(a);
            if (pi == 0)
                continue;
            sa_bar_tp1.combineToSelf(1, pi, toStateAction.stateAction(x_tp1, a));
        }
    }
    RealVector phi_stat = x_t != null ? toStateAction.stateAction(x_t, a_t) : null;
    double delta_t = gq.update(phi_stat, rho_t, r_tp1, sa_bar_tp1, z_tp1);
    pool.releaseAll();
    return delta_t;
}
Also used : VectorPool(nars.rl.horde.math.VectorPool) RealVector(org.apache.commons.math3.linear.RealVector)

Example 22 with RealVector

use of org.apache.commons.math3.linear.RealVector in project narchy by automenta.

the class EnvironmentScriptingAccessor method convertVerticesFromArrayRealVectorGenericList.

private static Vec2[] convertVerticesFromArrayRealVectorGenericList(List verticesPoints) {
    Vec2[] vectorArray = new Vec2[verticesPoints.size()];
    for (int i = 0; i < verticesPoints.size(); i++) {
        if (!(verticesPoints.get(i) instanceof ArrayRealVector)) {
            throw new RuntimeException("element is a RealVector");
        }
        ArrayRealVector realVector = (ArrayRealVector) verticesPoints.get(i);
        vectorArray[i] = new Vec2((float) realVector.getDataRef()[0], (float) realVector.getDataRef()[1]);
    }
    return vectorArray;
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 23 with RealVector

use of org.apache.commons.math3.linear.RealVector in project narchy by automenta.

the class GTDLambda method update.

@Override
public double update(double pi_t, double b_t, RealVector x_t, RealVector x_tp1, double r_tp1, double gamma_tp1, double z_tp1) {
    if (x_t == null)
        return initEpisode(gamma_tp1);
    VectorPool pool = VectorPools.pool(e.vect());
    v_t = v.dotProduct(x_t);
    delta_t = r_tp1 + (1 - gamma_tp1) * z_tp1 + gamma_tp1 * v.dotProduct(x_tp1) - v_t;
    // Update traces
    e.update(gamma_t * lambda, x_t);
    double rho_t = pi_t / b_t;
    e.vect().mapMultiplyToSelf(rho_t);
    // Compute correction
    ArrayRealVector correctionVector = pool.newVector();
    if (x_tp1 != null) {
        correction = e.vect().dotProduct(w);
        correctionVector.combineToSelf(1, correction * gamma_tp1 * (1 - lambda), x_tp1);
    }
    // Update parameters
    RealVector deltaE = pool.newVector(e.vect()).mapMultiplyToSelf(delta_t);
    v.combineToSelf(1, alpha_v, pool.newVector(deltaE).combineToSelf(1, -1, correctionVector));
    w.combineToSelf(1, alpha_w, deltaE.combineToSelf(1, -w.dotProduct(x_t), x_t));
    deltaE = null;
    gamma_t = gamma_tp1;
    pool.releaseAll();
    return delta_t;
}
Also used : VectorPool(nars.rl.horde.math.VectorPool) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 24 with RealVector

use of org.apache.commons.math3.linear.RealVector in project MindsEye by SimiaCryptus.

the class ObjectLocation method run.

/**
 * Run.
 *
 * @param log the log
 */
public void run(@Nonnull final NotebookOutput log) {
    @Nonnull String logName = "cuda_" + log.getName() + ".log";
    log.p(log.file((String) null, logName, "GPU Log"));
    CudaSystem.addLog(new PrintStream(log.file(logName)));
    ImageClassifier classifier = getClassifierNetwork();
    Layer classifyNetwork = classifier.getNetwork();
    ImageClassifier locator = getLocatorNetwork();
    Layer locatorNetwork = locator.getNetwork();
    ArtistryUtil.setPrecision((DAGNetwork) classifyNetwork, Precision.Float);
    ArtistryUtil.setPrecision((DAGNetwork) locatorNetwork, Precision.Float);
    Tensor[][] inputData = loadImages_library();
    // Tensor[][] inputData = loadImage_Caltech101(log);
    double alphaPower = 0.8;
    final AtomicInteger index = new AtomicInteger(0);
    Arrays.stream(inputData).limit(10).forEach(row -> {
        log.h3("Image " + index.getAndIncrement());
        final Tensor img = row[0];
        log.p(log.image(img.toImage(), ""));
        Result classifyResult = classifyNetwork.eval(new MutableResult(row));
        Result locationResult = locatorNetwork.eval(new MutableResult(row));
        Tensor classification = classifyResult.getData().get(0);
        List<CharSequence> categories = classifier.getCategories();
        int[] sortedIndices = IntStream.range(0, categories.size()).mapToObj(x -> x).sorted(Comparator.comparing(i -> -classification.get(i))).mapToInt(x -> x).limit(10).toArray();
        logger.info(Arrays.stream(sortedIndices).mapToObj(i -> String.format("%s: %s = %s%%", i, categories.get(i), classification.get(i) * 100)).reduce((a, b) -> a + "\n" + b).orElse(""));
        Map<CharSequence, Tensor> vectors = new HashMap<>();
        List<CharSequence> predictionList = Arrays.stream(sortedIndices).mapToObj(categories::get).collect(Collectors.toList());
        Arrays.stream(sortedIndices).limit(10).forEach(category -> {
            CharSequence name = categories.get(category);
            log.h3(name);
            Tensor alphaTensor = renderAlpha(alphaPower, img, locationResult, classification, category);
            log.p(log.image(img.toRgbImageAlphaMask(0, 1, 2, alphaTensor), ""));
            vectors.put(name, alphaTensor.unit());
        });
        Tensor avgDetection = vectors.values().stream().reduce((a, b) -> a.add(b)).get().scale(1.0 / vectors.size());
        Array2DRowRealMatrix covarianceMatrix = new Array2DRowRealMatrix(predictionList.size(), predictionList.size());
        for (int x = 0; x < predictionList.size(); x++) {
            for (int y = 0; y < predictionList.size(); y++) {
                Tensor l = vectors.get(predictionList.get(x)).minus(avgDetection);
                Tensor r = vectors.get(predictionList.get(y)).minus(avgDetection);
                covarianceMatrix.setEntry(x, y, l.dot(r));
            }
        }
        @Nonnull final EigenDecomposition decomposition = new EigenDecomposition(covarianceMatrix);
        for (int objectVector = 0; objectVector < 10; objectVector++) {
            log.h3("Eigenobject " + objectVector);
            double eigenvalue = decomposition.getRealEigenvalue(objectVector);
            RealVector eigenvector = decomposition.getEigenvector(objectVector);
            Tensor detectionRegion = IntStream.range(0, eigenvector.getDimension()).mapToObj(i -> vectors.get(predictionList.get(i)).scale(eigenvector.getEntry(i))).reduce((a, b) -> a.add(b)).get();
            detectionRegion = detectionRegion.scale(255.0 / detectionRegion.rms());
            CharSequence categorization = IntStream.range(0, eigenvector.getDimension()).mapToObj(i -> {
                CharSequence category = predictionList.get(i);
                double component = eigenvector.getEntry(i);
                return String.format("<li>%s = %.4f</li>", category, component);
            }).reduce((a, b) -> a + "" + b).get();
            log.p(String.format("Object Detected: <ol>%s</ol>", categorization));
            log.p("Object Eigenvalue: " + eigenvalue);
            log.p("Object Region: " + log.image(img.toRgbImageAlphaMask(0, 1, 2, detectionRegion), ""));
            log.p("Object Region Compliment: " + log.image(img.toRgbImageAlphaMask(0, 1, 2, detectionRegion.scale(-1)), ""));
        }
        // final int[] orderedVectors = IntStream.range(0, 10).mapToObj(x -> x)
        // .sorted(Comparator.comparing(x -> -decomposition.getRealEigenvalue(x))).mapToInt(x -> x).toArray();
        // IntStream.range(0, orderedVectors.length)
        // .mapToObj(i -> {
        // //double realEigenvalue = decomposition.getRealEigenvalue(orderedVectors[i]);
        // return decomposition.getEigenvector(orderedVectors[i]).toArray();
        // }
        // ).toArray(i -> new double[i][]);
        log.p(String.format("<table><tr><th>Cosine Distance</th>%s</tr>%s</table>", Arrays.stream(sortedIndices).limit(10).mapToObj(col -> "<th>" + categories.get(col) + "</th>").reduce((a, b) -> a + b).get(), Arrays.stream(sortedIndices).limit(10).mapToObj(r -> {
            return String.format("<tr><td>%s</td>%s</tr>", categories.get(r), Arrays.stream(sortedIndices).limit(10).mapToObj(col -> {
                return String.format("<td>%.4f</td>", Math.acos(vectors.get(categories.get(r)).dot(vectors.get(categories.get(col)))));
            }).reduce((a, b) -> a + b).get());
        }).reduce((a, b) -> a + b).orElse("")));
    });
    log.setFrontMatterProperty("status", "OK");
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) VGG16_HDF5(com.simiacryptus.mindseye.models.VGG16_HDF5) HashMap(java.util.HashMap) RealVector(org.apache.commons.math3.linear.RealVector) Caltech101(com.simiacryptus.mindseye.test.data.Caltech101) Result(com.simiacryptus.mindseye.lang.Result) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) Layer(com.simiacryptus.mindseye.lang.Layer) VGG19_HDF5(com.simiacryptus.mindseye.models.VGG19_HDF5) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) Util(com.simiacryptus.util.Util) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) SoftmaxActivationLayer(com.simiacryptus.mindseye.layers.cudnn.SoftmaxActivationLayer) Logger(org.slf4j.Logger) BufferedImage(java.awt.image.BufferedImage) BandReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandReducerLayer) IOException(java.io.IOException) TestUtil(com.simiacryptus.mindseye.test.TestUtil) Collectors(java.util.stream.Collectors) File(java.io.File) ConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer) MutableResult(com.simiacryptus.mindseye.lang.MutableResult) Hdf5Archive(com.simiacryptus.mindseye.models.Hdf5Archive) List(java.util.List) Stream(java.util.stream.Stream) CudaSystem(com.simiacryptus.mindseye.lang.cudnn.CudaSystem) EigenDecomposition(org.apache.commons.math3.linear.EigenDecomposition) PoolingLayer(com.simiacryptus.mindseye.layers.cudnn.PoolingLayer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Comparator(java.util.Comparator) PrintStream(java.io.PrintStream) Tensor(com.simiacryptus.mindseye.lang.Tensor) MutableResult(com.simiacryptus.mindseye.lang.MutableResult) Nonnull(javax.annotation.Nonnull) HashMap(java.util.HashMap) Layer(com.simiacryptus.mindseye.lang.Layer) SoftmaxActivationLayer(com.simiacryptus.mindseye.layers.cudnn.SoftmaxActivationLayer) BandReducerLayer(com.simiacryptus.mindseye.layers.cudnn.BandReducerLayer) ConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer) PoolingLayer(com.simiacryptus.mindseye.layers.cudnn.PoolingLayer) Result(com.simiacryptus.mindseye.lang.Result) MutableResult(com.simiacryptus.mindseye.lang.MutableResult) EigenDecomposition(org.apache.commons.math3.linear.EigenDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RealVector(org.apache.commons.math3.linear.RealVector)

Example 25 with RealVector

use of org.apache.commons.math3.linear.RealVector in project pyramid by cheng-li.

the class GMMTrainer method mStep.

public void mStep() {
    IntStream.range(0, gmm.getNumComponents()).parallel().forEach(k -> {
        double sumGamma = computeSumGamma(k);
        gmm.setMixtureCoefficient(k, sumGamma / data.getRowDimension());
        RealVector mean = computeMean(k, sumGamma);
        gmm.getGaussianDistributions()[k].setMean(mean);
        RealMatrix cov = computeCov(k, mean, sumGamma);
        RealMatrix stabilizedCov = stablize(cov);
        gmm.getGaussianDistributions()[k].setCovariance(stabilizedCov);
    });
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Aggregations

RealVector (org.apache.commons.math3.linear.RealVector)41 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)30 RealMatrix (org.apache.commons.math3.linear.RealMatrix)22 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)10 Test (org.junit.jupiter.api.Test)5 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)4 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)4 VectorPool (nars.rl.horde.math.VectorPool)3 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)3 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)3 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)3 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)3 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)3 SingularValueDecomposition (org.apache.commons.math3.linear.SingularValueDecomposition)3 CombinedAttributeValues (org.knime.base.node.mine.treeensemble2.data.BinaryNominalSplitsPCA.CombinedAttributeValues)3 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)3 File (java.io.File)2 Comparator (java.util.Comparator)2 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)2 ValueAndJacobianFunction (org.apache.commons.math3.fitting.leastsquares.ValueAndJacobianFunction)2