Example usage for org.apache.mahout.math.function Functions swapArgs

List of usage examples for org.apache.mahout.math.function Functions swapArgs

Introduction

In this page you can find the example usage for org.apache.mahout.math.function Functions swapArgs.

Prototype

public static DoubleDoubleFunction swapArgs(final DoubleDoubleFunction function) 

Source Link

Document

Constructs a function that returns function.apply(b,a), i.e.

Usage

From source file:org.carrot2.matrix.factorization.LocalNonnegativeMatrixFactorization.java

License:Open Source License

public void compute() {
    // Prototype Matlab code for the LNMF
    //        //from  w  w  w . j  av a2s . c o m
    // function [U, V, C] = lnmf(A)
    // [m, n] = size(A);
    // k = 2; % the desired number of base vectors
    // maxiter = 50; % the number of iterations
    // eps = 1e-9; % machine epsilon
    //        
    // U = rand(m, k); % initialize U randomly
    // V = rand(n, k); % initialize V randomly
    // O = ones(m, m); % a matrix of ones
    //        
    // for iter = 1:maxiter
    // V = sqrt( V .* (((A+eps) ./ (U*V'+eps))' * U)); % update V
    // U = U .* (((A+eps)./(U*V'+eps)) * V); % update U
    // U = U ./ (O * U); % normalise U's columns
    // C(1, iter) = norm((A-U*V'), 'fro'); % approximation quality
    // end
    //

    double eps = 1e-9;

    // Seed U and V with initial values
    U = new DenseDoubleMatrix2D(A.rows(), k);
    V = new DenseDoubleMatrix2D(A.columns(), k);
    seedingStrategy.seed(A, U, V);

    // Temporary matrices
    DoubleMatrix2D Aeps = A.copy();
    Aeps.assign(Functions.plus(eps));
    DoubleMatrix2D UV = new DenseDoubleMatrix2D(A.rows(), A.columns());
    DoubleMatrix2D VT = new DenseDoubleMatrix2D(A.columns(), k);
    DoubleMatrix2D UT = new DenseDoubleMatrix2D(A.rows(), k);
    double[] work = new double[U.columns()];

    // Colt functions
    DoubleDoubleFunction invDiv = Functions.swapArgs(Functions.DIV);
    DoubleDoubleFunction sqrtMult = Functions.chain(Functions.SQRT, Functions.MULT);
    DoubleFunction plusEps = Functions.plus(eps);

    if (stopThreshold >= 0) {
        updateApproximationError();
    }

    for (int i = 0; i < maxIterations; i++) {
        // Update V
        U.zMult(V, UV, 1, 0, false, true); // UV <- U*V'
        UV.assign(plusEps); // UV <- UV + eps
        UV.assign(Aeps, invDiv); // UV <- Aeps ./ UV
        UV.zMult(U, VT, 1, 0, true, false); // VT <- UV' * U
        V.assign(VT, sqrtMult); // V <- sqrt(V .* VT)

        // Update U
        U.zMult(V, UV, 1, 0, false, true); // UV <- U*V'
        UV.assign(plusEps); // UV <- UV + eps
        UV.assign(Aeps, invDiv); // UV <- Aeps ./ UV
        UV.zMult(V, UT, 1, 0, false, false); // UT <- UV * V
        U.assign(UT, Functions.MULT); // U <- U .* UT

        MatrixUtils.normalizeColumnL1(U, work);

        iterationsCompleted++;
        if (stopThreshold >= 0) {
            if (updateApproximationError()) {
                break;
            }
        }
    }

    if (ordered) {
        order();
    }
}

From source file:org.carrot2.matrix.factorization.NonnegativeMatrixFactorizationKL.java

License:Open Source License

public void compute() {
    // Prototype Matlab code for the NMF-KL
    //        //  w w  w  .  jav a2 s  .  c  om
    // function [U, V, C] = nmf-kl(A)
    // [m, n] = size(A);
    // k = 2; % the desired number of base vectors
    // maxiter = 50; % the number of iterations
    // eps = 1e-9; % machine epsilon
    //        
    // U = rand(m, k); % initialise U randomly
    // V = rand(n, k); % initialise V randomly
    // O = ones(m, m); % a matrix of ones
    //        
    // for iter = 1:maxiter
    // V = V.*(((A+eps)./(U*V'+eps))'*U); % update V
    // U = U.*(((A+eps)./(U*V'+eps))*V); % update U
    // U = U./(O*U); % normalise U's columns
    // C(1, iter) = norm((A-U*V'), 'fro'); % approximation quality
    // end

    int m = A.rows();
    int n = A.columns();
    double eps = 1e-9;

    // Seed U and V with initial values
    U = new DenseDoubleMatrix2D(m, k);
    V = new DenseDoubleMatrix2D(n, k);
    seedingStrategy.seed(A, U, V);

    // Temporary matrices
    DoubleMatrix2D Aeps = A.copy();
    Aeps.assign(Functions.plus(eps));
    DoubleMatrix2D UV = new DenseDoubleMatrix2D(m, n);
    DoubleMatrix2D VT = new DenseDoubleMatrix2D(n, k);
    DoubleMatrix2D UT = new DenseDoubleMatrix2D(m, k);
    double[] work = new double[U.columns()];

    // Colt functions
    DoubleDoubleFunction invDiv = Functions.swapArgs(Functions.DIV);
    DoubleFunction plusEps = Functions.plus(eps);

    if (stopThreshold >= 0) {
        updateApproximationError();
    }

    for (int i = 0; i < maxIterations; i++) {
        // Update V
        U.zMult(V, UV, 1, 0, false, true); // UV <- U*V'
        UV.assign(plusEps); // UV <- UV + eps
        UV.assign(Aeps, invDiv); // UV <- Aeps ./ UV
        UV.zMult(U, VT, 1, 0, true, false); // VT <- UV' * U
        V.assign(VT, Functions.MULT); // V <- V .* VT

        // Update U
        U.zMult(V, UV, 1, 0, false, true); // UV <- U*V'
        UV.assign(plusEps); // UV <- UV + eps
        UV.assign(Aeps, invDiv); // UV <- Aeps ./ UV
        UV.zMult(V, UT, 1, 0, false, false); // UT <- UV * V
        U.assign(UT, Functions.MULT); // U <- U .* UT

        MatrixUtils.normalizeColumnL1(U, work);

        iterationsCompleted++;
        if (stopThreshold >= 0) {
            if (updateApproximationError()) {
                break;
            }
        }
    }

    if (ordered) {
        order();
    }
}