Search in sources :

Example 6 with structures._Node

use of structures._Node in project IR_Base by Linda-sunshine.

the class GaussianFields method test.

// Test the data set.
@Override
public double test() {
    _Node node;
    /**
     *Construct the nearest neighbor graph***
     */
    constructGraph(true);
    /**
     *Perform matrix inverse.***
     */
    DenseDoubleAlgebra alg = new DenseDoubleAlgebra();
    DoubleMatrix2D result = alg.inverse(m_graph);
    /**
     *setting up the corresponding weight for the true labels**
     */
    for (int i = m_U; i < m_L + m_U; i++) m_nodeList[i].m_classifierPred *= m_M;
    /**
     *get some statistics**
     */
    for (int i = 0; i < m_U; i++) {
        node = m_nodeList[i];
        double pred = 0;
        for (int j = 0; j < m_U + m_L; j++) pred += result.getQuick(i, j) * m_nodeList[j].m_label;
        // prediction for the unlabeled based on the labeled data and pseudo labels
        node.m_pred = pred;
        for (int j = 0; j < m_classNo; j++) m_pYSum[j] += Math.exp(-Math.abs(j - node.m_pred));
    }
    /**
     *evaluate the performance**
     */
    double acc = 0;
    int pred, ans;
    for (int i = 0; i < m_U; i++) {
        pred = getLabel(m_nodeList[i].m_pred);
        ans = m_testSet.get(i).getYLabel();
        m_TPTable[pred][ans] += 1;
        if (pred != ans) {
            if (m_debugOutput != null)
                debug(m_testSet.get(i));
        } else
            acc++;
    }
    m_precisionsRecalls.add(calculatePreRec(m_TPTable));
    return acc / m_U;
}
Also used : DenseDoubleAlgebra(cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) SparseDoubleMatrix2D(cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D) structures._Node(structures._Node)

Example 7 with structures._Node

use of structures._Node in project IR_Base by Linda-sunshine.

the class GaussianFields method SimilarityCheck.

void SimilarityCheck() {
    _Node node;
    _Edge neighbor;
    int y, uPred, lPred, cPred;
    double dMean = 0, dStd = 0;
    // p@5, p@10, p@20; p, n; U, L;
    double[][][] prec = new double[3][2][2];
    double[][][] total = new double[3][2][2];
    // combined prediction, classifier's prediction, labeled neighbors prediction, unlabeled neighbors prediction, optimal
    int[][][] acc = new int[5][2][2];
    for (int i = 0; i < m_U; i++) {
        // nearest neighbor graph
        node = m_nodeList[i];
        y = (int) node.m_label;
        dMean += node.m_pred - node.m_classifierPred;
        dStd += (node.m_pred - node.m_classifierPred) * (node.m_pred - node.m_classifierPred);
        /**
         **Check different prediction methods' performance*****
         */
        cPred = (int) (node.m_classifierPred);
        lPred = (int) (node.weightAvgInLabeledNeighbors() + 0.5);
        uPred = (int) (node.weightAvgInUnlabeledNeighbors() + 0.5);
        acc[0][y][getLabel(node.m_pred)]++;
        acc[1][y][cPred]++;
        acc[2][y][lPred]++;
        acc[3][y][uPred]++;
        if (cPred == y || lPred == y || uPred == y)
            // one of these predictions is correct
            acc[4][y][y]++;
        else
            acc[4][y][1 - y]++;
        /**
         **Check the nearest unlabeled neighbors*****
         */
        double precision = 0;
        for (int pos = 0; pos < m_kPrime; pos++) {
            neighbor = node.m_unlabeledEdges.get(pos);
            if (// neighbor's prediction against the ground-truth
            getLabel(neighbor.getPred()) == y)
                precision++;
            if (pos == 4) {
                prec[0][y][0] += precision / 5.0;
                total[0][y][0]++;
            } else if (pos == 9) {
                prec[1][y][0] += precision / 10.0;
                total[1][y][0]++;
            } else if (pos == 19) {
                prec[2][y][0] += precision / 20.0;
                total[2][y][0]++;
                break;
            }
        }
        /**
         **Check the nearest labeled neighbors*****
         */
        precision = 0;
        for (int pos = 0; pos < m_k; pos++) {
            neighbor = node.m_labeledEdges.get(pos);
            if (// neighbor's true label against the ground-truth
            (int) neighbor.getLabel() == y)
                precision++;
            if (pos == 4) {
                prec[0][y][1] += precision / 5.0;
                total[0][y][1]++;
            } else if (pos == 9) {
                prec[1][y][1] += precision / 10.0;
                total[1][y][1]++;
            } else if (pos == 19) {
                prec[2][y][1] += precision / 20.0;
                total[2][y][1]++;
                break;
            }
        }
    }
    dMean /= m_U;
    dStd = Math.sqrt(dStd / m_U - dMean * dMean);
    System.out.println("\nQuery\tDocs\tP@5\tP@10\tP@20");
    System.out.format("Pos\tU\t%.3f\t%.3f\t%.3f\n", prec[0][1][0] / total[0][1][0], prec[1][1][0] / total[1][1][0], prec[2][1][0] / total[2][1][0]);
    System.out.format("Pos\tL\t%.3f\t%.3f\t%.3f\n", prec[0][1][1] / total[0][1][1], prec[1][1][1] / total[1][1][1], prec[2][1][1] / total[2][1][1]);
    System.out.format("Neg\tU\t%.3f\t%.3f\t%.3f\n", prec[0][0][0] / total[0][0][0], prec[1][0][0] / total[1][0][0], prec[2][0][0] / total[2][0][0]);
    System.out.format("Neg\tL\t%.3f\t%.3f\t%.3f\n\n", prec[0][0][1] / total[0][0][1], prec[1][0][1] / total[1][0][1], prec[2][0][1] / total[2][0][1]);
    System.out.format("W-C: %.4f/%.4f\n\n", dMean, dStd);
    System.out.format("W TN:%d\tFP:%d\tFN:%d\tTP:%d\n", acc[0][0][0], acc[0][0][1], acc[0][1][0], acc[0][1][1]);
    System.out.format("C TN:%d\tFP:%d\tFN:%d\tTP:%d\n", acc[1][0][0], acc[1][0][1], acc[1][1][0], acc[1][1][1]);
    System.out.format("L TN:%d\tFP:%d\tFN:%d\tTP:%d\n", acc[2][0][0], acc[2][0][1], acc[2][1][0], acc[2][1][1]);
    System.out.format("U TN:%d\tFP:%d\tFN:%d\tTP:%d\n", acc[3][0][0], acc[3][0][1], acc[3][1][0], acc[3][1][1]);
    System.out.format("O TN:%d\tFP:%d\tFN:%d\tTP:%d\n", acc[4][0][0], acc[4][0][1], acc[4][1][0], acc[4][1][1]);
}
Also used : structures._Edge(structures._Edge) structures._Node(structures._Node)

Example 8 with structures._Node

use of structures._Node in project IR_Base by Linda-sunshine.

the class GaussianFields method calcSimilarityInThreads.

protected void calcSimilarityInThreads() {
    // create the node list for constructing the nearest neighbor graph
    if (m_nodeList == null || m_nodeList.length < m_U + m_L)
        // create sufficient space
        m_nodeList = new _Node[(int) ((m_U + m_L) * 1.2)];
    // fill in the labeled parts
    for (int i = m_U; i < m_U + m_L; i++) {
        _Doc d = getLabeledDoc(i - m_U);
        m_nodeList[i] = new _Node(i - m_U, d.getYLabel(), d.getYLabel());
    }
    int cores = Runtime.getRuntime().availableProcessors();
    m_threadpool = new Thread[cores];
    System.out.format("Construct nearest neighbor graph nodes in parallel: L: %d, U: %d\n", m_L, m_U);
    WaitUntilFinish(ActionType.AT_node);
    System.out.format("Construct nearest neighbor graph edges in parallel: L: %d, U: %d\n", m_L, m_U);
    WaitUntilFinish(ActionType.AT_graph);
}
Also used : structures._Doc(structures._Doc) structures._Node(structures._Node)

Example 9 with structures._Node

use of structures._Node in project IR_Base by Linda-sunshine.

the class GaussianFieldsByRandomWalk method debugDetails.

void debugDetails(_Doc d) {
    int id = d.getID();
    _Node node = m_nodeList[id];
    try {
        m_debugWriter.write(d.toString() + "\n");
        /**
         **Get the top 5 elements from labeled neighbors*****
         */
        for (int k = 0; k < 5; k++) {
            _Edge item = node.m_labeledEdges.get(k);
            _Doc dj = getLabeledDoc(item.getNodeId());
            m_debugWriter.write(String.format("L(%d, %.4f)\t%s\n", (int) item.getClassifierPred(), item.getSimilarity(), dj.toString()));
        }
        /**
         **Get the top 5 elements from k'UU*****
         */
        for (int k = 0; k < 5; k++) {
            _Edge item = node.m_unlabeledEdges.get(k);
            _Doc dj = getTestDoc(item.getNodeId());
            m_debugWriter.write(String.format("U(%d, %.3f, %.4f)\t%s\n", (int) item.getClassifierPred(), item.getPred(), item.getSimilarity(), dj.toString()));
        }
        m_debugWriter.write("\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : structures._Doc(structures._Doc) structures._Edge(structures._Edge) IOException(java.io.IOException) structures._Node(structures._Node)

Example 10 with structures._Node

use of structures._Node in project IR_Base by Linda-sunshine.

the class GaussianFieldsByRandomWalk method debugSummary.

void debugSummary(_Doc d) {
    int id = d.getID();
    _Node node = m_nodeList[id];
    double sim, wijSumU = 0, wijSumL = 0;
    double fSumU = 0, fSumL = 0;
    try {
        m_debugWriter.write(String.format("%d\t%.4f(%d*,%d)\t%d\n", // ground-truth
        d.getYLabel(), // random walk's raw prediction
        node.m_pred, // map to discrete label
        getLabel(node.m_pred), // consider the prior
        getLabel3(node.m_pred), // multiple learner's prediction
        (int) node.m_classifierPred));
        double mean = 0, sd = 0;
        /**
         **Walk through the top k labeled data for the current data.***
         */
        for (_Edge edge : node.m_labeledEdges) {
            // get the similarity between two nodes.
            wijSumL += edge.getSimilarity();
            fSumL += edge.getSimilarity() * edge.getLabel();
            sd += edge.getSimilarity() * edge.getSimilarity();
        }
        mean = wijSumL / m_k;
        sd = Math.sqrt(sd / m_k - mean * mean);
        /**
         **Get the top 10 elements from labeled neighbors*****
         */
        for (int k = 0; k < 10; k++) {
            _Edge item = node.m_labeledEdges.get(k);
            sim = item.getSimilarity() / wijSumL;
            if (k == 0)
                m_debugWriter.write(String.format("L(%.2f)\t[%d:%.4f, ", fSumL / wijSumL, (int) item.getLabel(), sim));
            else if (k == 9)
                m_debugWriter.write(String.format("%d:%.4f]\t%.3f\t%.3f\n", (int) item.getLabel(), sim, mean, sd));
            else
                m_debugWriter.write(String.format("%d:%.4f, ", (int) item.getLabel(), sim));
        }
        sd = 0;
        /**
         **Construct the top k' unlabeled data for the current data.***
         */
        for (_Edge edge : node.m_unlabeledEdges) {
            // get the similarity between two nodes.
            wijSumU += edge.getSimilarity();
            fSumU += edge.getSimilarity() * edge.getPred();
            sd += edge.getSimilarity() * edge.getSimilarity();
        }
        mean = wijSumU / m_kPrime;
        sd = Math.sqrt(sd / m_kPrime - mean * mean);
        /**
         **Get the top 10 elements from k'UU*****
         */
        for (int k = 0; k < 10; k++) {
            _Edge item = node.m_unlabeledEdges.get(k);
            sim = item.getSimilarity() / wijSumU;
            if (k == 0)
                m_debugWriter.write(String.format("U(%.2f)\t[%.2f:%.4f, ", fSumU / wijSumU, item.getPred(), sim));
            else if (k == 9)
                m_debugWriter.write(String.format("%.2f:%.4f]\t%.3f\t%.3f\n", item.getPred(), sim, mean, sd));
            else
                m_debugWriter.write(String.format("%.2f:%.4f, ", item.getPred(), sim));
        }
        m_debugWriter.write("\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : structures._Edge(structures._Edge) IOException(java.io.IOException) structures._Node(structures._Node)

Aggregations

structures._Node (structures._Node)10 structures._Edge (structures._Edge)6 structures._Doc (structures._Doc)4 IOException (java.io.IOException)2 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)1 DenseDoubleAlgebra (cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra)1 SparseDoubleMatrix2D (cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D)1 MyPriorityQueue (structures.MyPriorityQueue)1 structures._RankItem (structures._RankItem)1