use of structures._Edge in project IR_Base by Linda-sunshine.
the class GaussianFieldsByRandomWalk method randomWalkByWeightedSum.
// The random walk algorithm to generate new labels for unlabeled data.
// Take the average of all neighbors as the new label until they converge.
double randomWalkByWeightedSum() {
// construct the sparse graph on the fly every time
double wL = m_alpha, wU = m_beta, acc = 0;
_Node node;
/**
** Construct the C+scale*\Delta matrix and Y vector. ***
*/
for (int i = 0; i < m_U; i++) {
node = m_nodeList[i];
double wijSumU = 0, wijSumL = 0;
double fSumU = 0, fSumL = 0;
/**
**Walk through the top k' unlabeled neighbor for the current data.***
*/
for (_Edge edge : node.m_unlabeledEdges) {
// get the similarity between two nodes.
wijSumU += m_simFlag ? edge.getSimilarity() : 1;
fSumU += m_simFlag ? edge.getSimilarity() * edge.getPred() : edge.getPred();
}
/**
**Walk through the top k labeled neighbor for the current data.***
*/
for (_Edge edge : node.m_labeledEdges) {
// get the similarity between two nodes.
wijSumL += m_simFlag ? edge.getSimilarity() : 1;
fSumL += m_simFlag ? edge.getSimilarity() * edge.getPred() : edge.getPred();
}
node.m_pred = m_eta * (fSumL * wL + fSumU * wU) / (wijSumL * wL + wijSumU * wU) + (1 - m_eta) * node.m_classifierPred;
if (Double.isNaN(node.m_pred)) {
System.out.format("Encounter NaN in random walk!\nfSumL: %.3f, fSumU: %.3f, wijSumL: %.3f, wijSumU: %.3f\n", fSumL, fSumU, wijSumL, wijSumU);
System.exit(-1);
} else if ((int) node.m_label == getLabel(node.m_pred))
acc++;
}
return acc / m_U;
}
use of structures._Edge in project IR_Base by Linda-sunshine.
the class GaussianFieldsByRandomWalk method calcMeanVar4OneNode.
public double[] calcMeanVar4OneNode(_Node n) {
double[] stat4OneNode = new double[4];
double sum = 0, mean = 0, var = 0;
_Edge neighbor;
for (int j = 0; j < m_kPrime; j++) {
neighbor = n.m_unlabeledEdges.get(j);
sum += neighbor.getSimilarity();
}
mean = sum / m_kPrime;
// mean of unlabeled neighbors' similarities.
stat4OneNode[0] = mean;
for (int j = 0; j < m_kPrime; j++) {
neighbor = n.m_unlabeledEdges.get(j);
var += (neighbor.getSimilarity() - mean) * (neighbor.getSimilarity() - mean);
}
var = Math.sqrt(var / m_kPrime);
// variance of unlabeled neighbors' similarities.
stat4OneNode[1] = var;
// clear for the calculation of the labeled neighbors.
sum = 0;
// clear for the calculation of the labeled neighbors.
mean = 0;
// clear for the calculation of the labeled neighbors.
var = 0;
for (int j = 0; j < m_k; j++) {
neighbor = n.m_labeledEdges.get(j);
sum += neighbor.getSimilarity();
}
mean = sum / m_k;
// mean of unlabeled neighbors' similarities.
stat4OneNode[2] = mean;
for (int j = 0; j < m_k; j++) {
neighbor = n.m_labeledEdges.get(j);
var += (neighbor.getSimilarity() - mean) * (neighbor.getSimilarity() - mean);
}
var = Math.sqrt(var / m_k);
// variance of unlabeled neighbors' similarities.
stat4OneNode[3] = var;
return stat4OneNode;
}
use of structures._Edge in project IR_Base by Linda-sunshine.
the class GaussianFieldsByRandomWalk method randomWalkByMajorityVote.
// Take the majority of all neighbors(k+k') as the new label until they converge.
double randomWalkByMajorityVote() {
// construct the sparse graph on the fly every time
double similarity = 0, acc = 0;
int label;
double wL = m_eta * m_alpha, wU = m_eta * m_beta;
_Node node;
/**
** Construct the C+scale*\Delta matrix and Y vector. ***
*/
for (int i = 0; i < m_U; i++) {
node = m_nodeList[i];
Arrays.fill(m_cProbs, 0);
/**
**Walk through the top k' unlabeled neighbor for the current data.***
*/
for (_Edge edge : node.m_unlabeledEdges) {
// Item n's label.
label = getLabel(edge.getPred());
similarity = edge.getSimilarity();
m_cProbs[label] += m_simFlag ? similarity * wU : wU;
}
/**
**Walk through the top k labeled neighbor for the current data.***
*/
for (_Edge edge : node.m_labeledEdges) {
label = (int) edge.getLabel();
similarity = edge.getSimilarity();
m_cProbs[label] += m_simFlag ? similarity * wL : wL;
}
/**
**Multiple learner's prediction.***
*/
label = (int) node.m_classifierPred;
m_cProbs[label] += 1 - m_eta;
node.m_pred = Utils.argmax(m_cProbs);
if (node.m_label == node.m_pred)
acc++;
}
return acc / m_U;
}
use of structures._Edge 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]);
}
use of structures._Edge 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();
}
}
Aggregations