Commit cb1a1d0e authored by mak28ma's avatar mak28ma
Browse files

*bin am verzweifeln,

*sobald eine regel gelernt wurde die über mind 1 kronecker expansion
erzeugt wurde enthält der backwards mapping pass null...
parent 2bec1b9d
......@@ -75,8 +75,8 @@ public class Instance {
FlexCompColMatrix denseInstanceMatrix = new FlexCompColMatrix(denseDimension, denseDimension);
for (Point denseIndices : lastMapping.getInverseMappingMap().keySet()) {
List<Point> features = MatrixUtil.determineFeaturesForIndex(denseIndices, mappings);
// check if this instance contains the features
if (containsFeature(features)) {
// if so then create a sprase matrix and put a 1 into the
......@@ -90,12 +90,17 @@ public class Instance {
FlexCompColMatrix expanedMatrix = new FlexCompColMatrix(kroneckerDimension, kroneckerDimension);
for (MatrixEntry e1 : denseInstanceMatrix) {
//asure that the mapping has those
if(!lastMapping.getInverseMappingMap().keySet().contains(new Point(e1.row(),e1.column()))){
System.out.println("IDK");
}
// iterate over all no sparse elements and expand
for (MatrixEntry e2 : denseInstanceMatrix) {
// skip diagonal expansion
if (e1.equals(e2))
if (e1.row() == e2.row() && e1.column() == e2.column())
continue;
// calculate the new indices
......@@ -104,7 +109,7 @@ public class Instance {
// check if this feature is available TODO is this already clear
// since we got both single features???
expanedMatrix.add(kroneckerRow, kroneckerCol, 1);
expanedMatrix.add(kroneckerCol, kroneckerRow, 1);
}
}
......@@ -159,7 +164,7 @@ public class Instance {
}
public int getNrRows() {
return featureArray[0].length;
}
......
......@@ -2,7 +2,7 @@ package de.uniwue.ls6.datastructure;
import java.awt.Point;
import java.util.HashMap;
import java.util.Set;
import java.util.List;
public class MatrixMapping {
......@@ -88,6 +88,8 @@ public class MatrixMapping {
public int getKroneckerMatrixDimension() {
return kroneckerDimension;
}
@Override
public String toString() {
......
......@@ -2,7 +2,9 @@ package de.uniwue.ls6.datastructure;
import java.awt.Point;
import java.util.Iterator;
import java.util.List;
import de.uniwue.ls6.util.MatrixUtil;
import no.uib.cipr.matrix.MatrixEntry;
import no.uib.cipr.matrix.sparse.FlexCompColMatrix;
......@@ -93,27 +95,33 @@ public class MatrixMcMatrixFace {
return bestEntry == null ? null :bestEntry;
}
@Override
public String toString() {
public String asString(List<MatrixMapping> mappings) {
StringBuilder sb = new StringBuilder();
sb.append("Matrix, Goldlabel=" + LabelAlphabet.getFeatureToId(goldLabel) + "\n");
// print the tp matrix
sb.append("TP-Matrix:\n");
sb.append(matrixToString(tpMatrix));
sb.append(matrixToString(tpMatrix,mappings));
// print the fp matrix
sb.append("FP-Matrix:\n");
sb.append(matrixToString(fpMatrix));
sb.append(matrixToString(fpMatrix,mappings));
return sb.toString();
}
private String matrixToString(FlexCompColMatrix matrix) {
public String matrixToString(FlexCompColMatrix matrix, List<MatrixMapping> mappings) {
StringBuilder sb = new StringBuilder();
Iterator<MatrixEntry> iterator = matrix.iterator();
while (iterator.hasNext()) {
MatrixEntry next = iterator.next();
sb.append("Col: " + next.column() + " Row: " + LabelAlphabet.getFeatureToId(next.row()) +"("+next.row()+")" + " " + " Value: "
+next.get()).append("\n");
//recalculate the features for each entry
List<Point> determineFeaturesForIndex = MatrixUtil.determineFeaturesForIndex(new Point(next.column(),next.row()), mappings);
String featureRepresentaion = MatrixUtil.convertPointListToFeatureString(determineFeaturesForIndex);
sb.append("Element at: " + "colnr " + next.column() + " and rownr " + next.row() +" Alphabet_ID:" +next.row() + " represents feature: " + featureRepresentaion).append("\n");
// sb.append("Col: " + next.column() + " Row: " + LabelAlphabet.getFeatureToId(next.row()) +"("+next.row()+")" + " " + " Value: "
// +next.get()).append("\n");
}
return sb.toString();
}
......
......@@ -5,11 +5,14 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import de.uniwue.ls6.datastructure.Instance;
import de.uniwue.ls6.datastructure.LabelAlphabet;
import de.uniwue.ls6.datastructure.MatrixMapping;
import de.uniwue.ls6.datastructure.MatrixMcMatrixFace;
import jdk.nashorn.internal.runtime.arrays.IteratorAction;
import no.uib.cipr.matrix.MatrixEntry;
import no.uib.cipr.matrix.sparse.FlexCompColMatrix;
......@@ -17,7 +20,7 @@ public class MatrixUtil {
public static MatrixMapping getMappingForMaximum(MatrixMcMatrixFace matrixface, int maximum) {
//totally unugly code not even necessary which makes it worse
// totally unugly code not even necessary which makes it worse
int formerMatrixSize = matrixface.getTpMatrix().numRows() == matrixface.getTpMatrix().numColumns()
? matrixface.getTpMatrix().numColumns() : -1;
......@@ -25,20 +28,21 @@ public class MatrixUtil {
for (MatrixEntry entry : matrixface.getTpMatrix()) {
//TODO do we need to add the false positives???
// double sum = entry.get() + matrixface.getFpMatrix().get(entry.row(), entry.column());
// TODO do we need to add the false positives???
// double sum = entry.get() +
// matrixface.getFpMatrix().get(entry.row(), entry.column());
double sum = entry.get();
if (sum >= maximum) {
matrixMapping.addEntry(new Point(entry.column(), entry.row()));
}
}
//infer the -> righthandside
// infer the -> righthandside
matrixMapping.inferDenseMapValues();
return matrixMapping;
}
public static MatrixMcMatrixFace performKroneckerExpansion(List<MatrixMapping> mappings, Collection<Instance> instances,
int label) {
public static MatrixMcMatrixFace performKroneckerExpansion(List<MatrixMapping> mappings,
Collection<Instance> instances, int label) {
MatrixMapping lastMapping = mappings.get(mappings.size() - 1);
......@@ -51,6 +55,7 @@ public class MatrixUtil {
// expand
FlexCompColMatrix expandedInstance = inst.expand(mappings);
// add to kronecker
if (inst.getLabel() == label) {
expandedMatrixFace.addToMatrix(expandedMatrixFace.getTpMatrix(), expandedInstance);
......@@ -70,23 +75,23 @@ public class MatrixUtil {
// revertmapping
revertMapping(currentMapping, reversedfeatures);
if (i != 0) {
revertKroneckerExpansion(currentMapping, reversedfeatures);
revertKroneckerExpansion(mappings.get(i-1), reversedfeatures);
}
}
return reversedfeatures;
}
private static void revertKroneckerExpansion(MatrixMapping currentMapping, List<Point> reversedfeatures) {
private static void revertKroneckerExpansion(MatrixMapping previousMapping, List<Point> reversedfeatures) {
List<Point> toRemove = new ArrayList<Point>();
List<Point> toAdd = new ArrayList<Point>();
for (Point p : reversedfeatures) {
// revert the kronecker expansion step this generates 2 points
// TODO is this correct?
int sizeBeforeExpansion = (int) Math.sqrt(currentMapping.getKroneckerMatrixDimension());
int sizeBeforeExpansion = (int) previousMapping.getDenseMatrixDimension();
int xBefore1 = (int) Math.floor(p.x / sizeBeforeExpansion);
int xBefore2 = p.x % sizeBeforeExpansion;
......@@ -100,11 +105,22 @@ public class MatrixUtil {
toRemove.add(p);
toAdd.add(secondBack);
toAdd.add(firstBack);
//assert that the recalculated points are contained in the mapping
if(!previousMapping.getInverseMappingMap().containsKey(secondBack)){
System.out.println("yayo");
System.out.println(sizeBeforeExpansion);
System.out.println(previousMapping.getMappingMap().size());
System.out.println(secondBack);
}
}
reversedfeatures.removeAll(toRemove);
reversedfeatures.addAll(toAdd);
}
......@@ -123,4 +139,32 @@ public class MatrixUtil {
reversedfeatures.addAll(toAdd);
}
public static String convertPointListToFeatureString(List<Point> determineFeaturesForIndex) {
StringBuilder sb = new StringBuilder();
for (Point p : determineFeaturesForIndex) {
sb.append("Pos_" + p.x + "=" + LabelAlphabet.getFeatureToId(p.y) + " AND ");
}
return sb.toString().substring(0, sb.toString().length() - 4);
}
public static String prettyMatrixFormat(FlexCompColMatrix matrix) {
StringBuilder sb = new StringBuilder();
for (int row = 0; row < matrix.numRows(); row++) {
for (int col = 0; col < matrix.numColumns(); col++) {
double d = matrix.get(row, col);
if(d==0.0d){
sb.append("<EMPTY>");
}
else{
sb.append(" " + d+" ");
}
}
sb.append("\n");
}
return sb.toString();
}
}
......@@ -124,7 +124,6 @@ public class BinaryRepresentationRuleLearningAlgorithm implements IRepresentatio
int maximumScore = 0;
Point maxEntryLocation = bestMatrix.getLocationOfMaximum();
while (true) {
maximumScore = bestMatrix.getMaximumScore();
......@@ -136,6 +135,7 @@ public class BinaryRepresentationRuleLearningAlgorithm implements IRepresentatio
// expand in kronecker fashion
bestMatrix = MatrixUtil.performKroneckerExpansion(mappings, instances, goldLabel);
if (!betterRuleCanBeLearned(maximumScore, bestMatrix)) {
mappings.remove(mappingForMaximum);
break;
......@@ -148,7 +148,7 @@ public class BinaryRepresentationRuleLearningAlgorithm implements IRepresentatio
if (maxEntryLocation == null)
return null;
List<Point> featuresAtMax = MatrixUtil.determineFeaturesForIndex(maxEntryLocation, mappings);
return new RepresentationRule(windowSize, featuresAtMax, goldLabel,maximumScore);
return new RepresentationRule(windowSize, featuresAtMax, goldLabel, maximumScore);
}
private boolean betterRuleCanBeLearned(int maximumScore, MatrixMcMatrixFace matrixInFocus) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment