Skip to content

Commit

Permalink
Added new neighborhood function: Mexican Hat
Browse files Browse the repository at this point in the history
  • Loading branch information
vicente committed Dec 6, 2015
1 parent 2e3fce2 commit 210067a
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 67 deletions.
Binary file modified lfsom/contents/libraries/lfsom/library/lfsom.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions lfsom/contents/selforganizing/Layers.pde
Expand Up @@ -534,8 +534,8 @@ public void compruebaAccesoSubnet() {

fill(40);

textSize((float) (tamtexto*1.5));
text(this.nombre, posxLeyen,(posyTransla-rad-70)/constescala);
textSize((float) (tamtexto*1.7));
text(this.nombre, posxLeyen,(posyTransla-rad-72)/constescala);
}


Expand Down
2 changes: 1 addition & 1 deletion lfsom/contents/selforganizing/declarations.pde
Expand Up @@ -91,7 +91,7 @@ float[] listaHits = null;

GCheckbox cbxBatch, cbxOnline;
GCheckbox cbxPCA, cbxRandom, cbxInter, cbxVector;
GCheckbox cbxGauss, cbxCutGauss, cbxBobble;
GCheckbox cbxGauss, cbxCutGauss, cbxBobble,cbxMH;

float sinRad;
String tituloSOM = "";
Expand Down
6 changes: 4 additions & 2 deletions lfsom/contents/selforganizing/gui.pde
Expand Up @@ -48,6 +48,7 @@ public void handleToggleControlEvents(GToggleControl option, GEvent event) {
if (option == cbxVector) propi.setInitVector(option.isSelected());

if (option == cbxGauss) propi.setNeighGauss(option.isSelected());
if (option == cbxMH) propi.setNeighMH(option.isSelected());
if (option == cbxCutGauss) propi.setNeighCutGauss(option.isSelected());
if (option == cbxBobble) propi.setNeighBobble(option.isSelected());

Expand All @@ -72,7 +73,7 @@ public void handleButtonEvents(GButton button, GEvent event) {

String[] datosFich = G4P.selectInputDif("Seleccione fichero", "CSV", "Ficheros CSV");
String fname = datosFich[0];
if (fname!=null) {
if (fname!=null && !modoCalculando) {
if (trim(propi.getExpName()).equals("")) propi.setExpName(datosFich[1]);
//lbCurFile.setText(fname, GAlign.LEFT, GAlign.MIDDLE);
propi.setFicheroEntrada(fname);
Expand Down Expand Up @@ -127,7 +128,7 @@ public void handleButtonEvents(GButton button, GEvent event) {
if (button == btCloseWIteration) wIteration.setVisible(false);

if (button == btDelete) {
int reply = G4P.selectOption(this, "Delete current net? This process is undoable", "Delete", G4P.QUERY, G4P.YES_NO);
int reply = G4P.selectOption(this, "Delete current net?", "Delete", G4P.QUERY, G4P.YES_NO);
if (reply== G4P.OK) {
File fil = new File(actualFolder);
deleteFile(fil);
Expand Down Expand Up @@ -217,6 +218,7 @@ public void EnviaEntrenador () {
propi.setInitVector(cbxVector.isSelected());
propi.setNeighCutGauss(cbxCutGauss.isSelected());
propi.setNeighGauss(cbxGauss.isSelected());
propi.setNeighMH(cbxMH.isSelected());
propi.setNeighBobble(cbxBobble.isSelected());
propi.setNumCPUs(gtCPU.getText());

Expand Down
11 changes: 9 additions & 2 deletions lfsom/contents/selforganizing/guiWIteration.pde
Expand Up @@ -200,7 +200,7 @@ public void customize_gui_train() {
lbCurCsv.setTextBold();
//lbCurFile = new GLabel(this, 0, 0, 180,60);
//lbCurFile.setText(propi.getFicheroEntrada(), GAlign.LEFT, GAlign.LEFT);
btnInput = new GButton(this, 0, 0, 140, 20, "Train from CSV file");
btnInput = new GButton(this, 0, 0, 140, 20, "Train from data file");
btnSelected = new GButton(this, 0, 0, 140, 20, "Train from selection");

altactual++;
Expand Down Expand Up @@ -288,7 +288,7 @@ public void createGUIPSelection(){
snRadius = new GCustomSlider(this, 100, 75, 100, 70, "grey_blue");
snRadius.setShowDecor(false, true, true, true);
snRadius.setNumberFormat(G4P.EXPONENT, 1);
snRadius.setLimits(0,10);
snRadius.setLimits(0,14);
snRadius.setValue(radiusSel);
snRadius.setOpaque(false);

Expand Down Expand Up @@ -464,6 +464,12 @@ public void createGUIWIteration(){
cbxBobble.setText("Bubble");
wIteration.addControl(cbxBobble,20,210+desp);

cbxMH = new GCheckbox(this, 20, 180, 120, 20);
cbxMH.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
cbxMH.setText("Mexican Hat");
wIteration.addControl(cbxMH, 170, 210 + desp);


cbxGauss = new GCheckbox(this, 20, 180, 120, 20);
cbxGauss.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
cbxGauss.setText("Gauss");
Expand Down Expand Up @@ -544,6 +550,7 @@ public void createGUIWIteration(){
cbxGauss.setSelected(propi.getNeighGauss());
cbxCutGauss.setSelected(propi.getNeighCutGauss());
cbxBobble.setSelected(propi.getNeighBobble());
cbxMH.setSelected(propi.getNeighMH());
cbxOnline.setSelected(propi.getUseOnline());
cbxBatch.setSelected(propi.getUseBatch());
gtLearning.setText(propi.getStrBucleLearnRate());
Expand Down
1 change: 1 addition & 0 deletions lfsom/contents/selforganizing/selforganizing.pde
Expand Up @@ -20,6 +20,7 @@ import processing.pdf.*;
//*** FUNCIONES DE DESPLAZAMIENTO

public void keyReleased() {

if (key == 'P'&&!PDFtotal) {
PDFtotal=true;
fPDFOutput = G4P.selectOutput("PDF output");
Expand Down
50 changes: 25 additions & 25 deletions lfsom/src/lfsom/experiment/TrainSelector.java
Expand Up @@ -57,7 +57,7 @@ public class TrainSelector {
/**
* Version to show in client-side
*/
private String versionprog = "v1.4.2";
private String versionprog = "v1.4.3";

/**
* Progress
Expand Down Expand Up @@ -594,7 +594,7 @@ public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {
LFSSOMProperties props = new LFSSOMProperties(1, 1, seed,
trainingCycles, trainingIterations, 1, 1, tau, metric,
false, false, LFSUnit.INIT_RANDOM,
LFSGrowingLayer.NEIGH_GAUSS, 1, nameExp, false, 0.0,
LFSGrowingLayer.NEIGH_BUBBLE, 1, nameExp, false, 0.0,
false, false, false, 1);
props.setDataPath(dataPath);
LFSGrowingSOM mapa1Celda = new LFSGrowingSOM(props.getExpName());
Expand Down Expand Up @@ -626,9 +626,12 @@ public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {
// Calculate number of nets to train
int numItera = 0;
for (int element : bucleNeighFunc) {
if (element == LFSGrowingLayer.NEIGH_GAUSS) { // GAUSS doesn't
// use
// neighwidth
if (element == LFSGrowingLayer.NEIGH_GAUSS
|| element == LFSGrowingLayer.NEIGH_MH) {
// GAUSS
// doesn't
// use
// neighwidth
numItera += numRepe * bucleLearnRate.length
* bucleUseBatch.length * bucleSigma.length
* bucleInitializationMode.length;
Expand All @@ -640,8 +643,7 @@ public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {
* bucleInitializationMode.length
* buclePcNeighWidth.length;
}
if (element == LFSGrowingLayer.NEIGH_EP
|| element == LFSGrowingLayer.NEIGH_CUTGAUSS) {
if (element == LFSGrowingLayer.NEIGH_CUTGAUSS) {
numItera += numRepe * bucleLearnRate.length
* bucleUseBatch.length * bucleSigma.length
* bucleInitializationMode.length
Expand Down Expand Up @@ -702,7 +704,7 @@ public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {
// Si es Gauss, solo se ejecuta una vez para
// un Neighwidth
if (nbNeighWidth > 0
&& bNeighFunc == LFSGrowingLayer.NEIGH_GAUSS) {
&& (bNeighFunc == LFSGrowingLayer.NEIGH_MH || bNeighFunc == LFSGrowingLayer.NEIGH_GAUSS)) {
ejecuta = false;
}

Expand Down Expand Up @@ -770,7 +772,8 @@ public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {
datos1.getMinValues());
mapaMejorQuan.escribeProps(fiche + "props");

mapaMejorKaski.getLayer().mapCompleteDataAfterTraining(datos1);
mapaMejorKaski.getLayer().mapCompleteDataAfterTraining(datos1,
true);
mapaMejorKaski.clusteriza(numClusters, nThreads, sensiCluster);
// String fich = getfTopo();
fiche = dataPath + "/kaski.xml";
Expand Down Expand Up @@ -804,15 +807,14 @@ public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {

if ((isHier || isGCHSOM) && prof < maxProf) {

LFSGrowingLayer mMejor = mapaMejorKaski.getLayer();
double qActualRef = mMejor.getQualityMeasure("QError")
.getMapQuality(criterioSu);
if (isGCHSOM) {
lanza_experimento_clusters(tau2, mMejor, dataPath,
rootPath, datos1, qRef);
lanza_experimento_clusters(tau2,
mapaMejorKaski.getLayer(), dataPath, rootPath,
datos1, qRef0);
} else if (isHier) {
lanza_experimento_units(tau2, mMejor, dataPath,
rootPath, datos1, qRef0, qRef, qActualRef);
lanza_experimento_units(tau2,
mapaMejorKaski.getLayer(), dataPath, rootPath,
datos1, qRef0);
}

}
Expand Down Expand Up @@ -853,8 +855,8 @@ private void lanza_experimento_clusters(double tau2,

if (quality > tau2 * mqeRef) {
// No cumple, se envia a entrenar el cluster completo
generaExp(mMejor, dataPath, rootPath, mqeRef, datos1,
arrInc, iteini, z);
generaExp(mMejor, dataPath, rootPath, datos1, arrInc,
iteini, z);
}

}
Expand All @@ -863,16 +865,16 @@ private void lanza_experimento_clusters(double tau2,
}

private void generaExp(LFSGrowingLayer mMejor, String dataPath,
String rootPath, Double mqeRef, LFSData datos1,
ArrayList<Integer> arrInc, long iteini, int z) {
String rootPath, LFSData datos1, ArrayList<Integer> arrInc,
long iteini, int z) {
try {
LFSExpProps nexprops = new LFSExpProps(dataPath + "/ExpProps.xml");
String directorio = dataPath + "/n" + z;
mMejor.getgSOM().saveMapCSVParcial(datos1, arrInc, directorio,
directorio + "/data.csv");
nexprops.setRootPath(rootPath);
nexprops.setIsSubred(true);
nexprops.setMqeIni(mqeRef);
nexprops.setMqeIni(mMejor.devuelve_mqe_units(arrInc));
nexprops.setFicheroEntrada(directorio + "/data.csv");
nexprops.setSubredOrigen(arrInc);
nexprops.setFPadre("kaski.xml");
Expand All @@ -891,8 +893,7 @@ private void generaExp(LFSGrowingLayer mMejor, String dataPath,

// Generates a new experiment with data mapped to a unit
private void lanza_experimento_units(double tau2, LFSGrowingLayer mMejor,
String dataPath, String rootPath, LFSData datos1, Double qRef0,
Double qRef, Double qActualRef) {
String dataPath, String rootPath, LFSData datos1, Double qRef0) {

ArrayList<LFSUnit> ExpUnits = mMejor.getExpandedUnits(
mMejor.getQualityMeasure("QError"), "mqe", tau2, qRef0,
Expand All @@ -908,8 +909,7 @@ private void lanza_experimento_units(double tau2, LFSGrowingLayer mMejor,

arrInc.add(unidad.getYPos() * mMejor.getXSize()
+ unidad.getXPos());
generaExp(mMejor, dataPath, rootPath, qActualRef, datos1,
arrInc, iteini, z);
generaExp(mMejor, dataPath, rootPath, datos1, arrInc, iteini, z);

}
} catch (Exception e) {
Expand Down
71 changes: 53 additions & 18 deletions lfsom/src/lfsom/layers/LFSGrowingLayer.java
Expand Up @@ -118,7 +118,7 @@ public class LFSGrowingLayer {

public static final int NEIGH_BUBBLE = 30;

public static final int NEIGH_EP = 40;
public static final int NEIGH_MH = 40;

private int maxYSize, maxXSize;

Expand Down Expand Up @@ -164,6 +164,26 @@ public LFSGrowingLayer(boolean normalized, LFSData data,

}

/*
* Returns mqe of a group of units
*/

public double devuelve_mqe_units(ArrayList<Integer> arrInc) {
double KL = 0;

int numCells = 0;
double tempKL = 0;

for (int z = 0; z < arrInc.size(); z++) {
int filaac = (int) Math.floor(arrInc.get(z) / xSize);
int colac = arrInc.get(z) % xSize;
numCells += units[colac][filaac].getNumberOfMappedInputs();
tempKL += units[colac][filaac].getMQEError();
}
KL = tempKL / numCells;
return KL;
}

/**
* Save csv with data mapped to a list of units
*
Expand Down Expand Up @@ -480,11 +500,13 @@ private double getHCI(double distancia, double learnrate, double sigma,
try {

switch (nFunc) {

case NEIGH_GAUSS:
hci = learnrate
* Math.exp(-1 * distancia * distancia
/ (2 * sigma * sigma));
break;

case NEIGH_CUTGAUSS:
if (distancia <= neigh) {
hci = learnrate
Expand All @@ -501,13 +523,13 @@ private double getHCI(double distancia, double learnrate, double sigma,
hci = 0;
}
break;
case NEIGH_EP:
if (distancia <= neigh) {
hci = learnrate * (1 - distancia) / (sigma / 2);
} else {
hci = 0;
}
case NEIGH_MH:

hci = learnrate
* (2 * 0.7511 / Math.sqrt(3))
* (1 - distancia / sigma)
* Math.exp(-1 * distancia * distancia
/ (2 * sigma * sigma));
break;
}

Expand All @@ -528,11 +550,23 @@ private LFSUnit getMostDissimilarNeighbor(LFSUnit u) {
LFSUnit neighbor = null;
double largestDistance = 0;
double distance = 0;
try {
if (getNeighbouringUnits(u).size() == 0) {
Logger.getLogger("at.tuwien.ifs.somtoolbox").severe(
"No neighbour.");
System.exit(-1);
return null;
}
} catch (LFSException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
for (LFSUnit neighbouringUnit : getNeighbouringUnits(u)) {
try {
distance = LFSL2Metric.distance(u.getWeightVector(),
neighbouringUnit.getWeightVector());
distance = Math.abs(LFSL2Metric.distance(
u.getWeightVector(),
neighbouringUnit.getWeightVector()));
} catch (LFSException e) {
Logger.getLogger("at.tuwien.ifs.somtoolbox").severe(
e.getMessage());
Expand Down Expand Up @@ -838,7 +872,7 @@ public void mapCompleteDataAfterTraining(LFSData data) {
* @param data
* @param Calc
*/
private void mapCompleteDataAfterTraining(LFSData data, boolean Calc) {
public void mapCompleteDataAfterTraining(LFSData data, boolean Calc) {

LFSInputDatum datum = null;
LFSUnit winner = null;
Expand Down Expand Up @@ -1063,6 +1097,10 @@ public void train(LFSData data, LFSSOMProperties trainingProps) {
int i = 0;
int ultControl = i;

boolean grows = !trainingProps.isGrowing();
boolean isGauss = trainingProps.getNeighbourFunc() == LFSGrowingLayer.NEIGH_GAUSS
|| trainingProps.getNeighbourFunc() == LFSGrowingLayer.NEIGH_CUTGAUSS
|| trainingProps.getNeighbourFunc() == LFSGrowingLayer.NEIGH_MH;
int ciclos = trainingProps.numCycles();
long totIter = numIterations * ciclos;

Expand Down Expand Up @@ -1099,8 +1137,8 @@ public void train(LFSData data, LFSSOMProperties trainingProps) {
// **************************************************
// Learnrate

// learnRate modification. Doesn't apply to growing
if (hazControl && !trainingProps.isGrowing()) {
// learnRate modification.
if (hazControl) {
double nLrate = trainingProps.learnrate()
* Math.exp(-1.0
* (i / totIter)
Expand All @@ -1116,8 +1154,7 @@ public void train(LFSData data, LFSSOMProperties trainingProps) {
}

// Neighbourwidth
if (hazControl
&& trainingProps.getNeighbourFunc() != LFSGrowingLayer.NEIGH_GAUSS) {
if (hazControl && !isGauss && !grows) {
double nPCNeigh = trainingProps.pcNeighbourWidth()
* Math.exp(-1.0 * (i / totIter)
/ (2 * sigmaDecayNeigh * sigmaDecayNeigh));
Expand All @@ -1132,11 +1169,9 @@ public void train(LFSData data, LFSSOMProperties trainingProps) {
}

// Sigma
if (hazControl
&& (trainingProps.getNeighbourFunc() == LFSGrowingLayer.NEIGH_GAUSS || trainingProps
.getNeighbourFunc() == LFSGrowingLayer.NEIGH_CUTGAUSS)) {
if (hazControl && isGauss) {
double nSigma = trainingProps.sigma()
* Math.exp(-1.0 * (totIter / i)
* Math.exp(-1.0 * (i / totIter)
/ (2 * sigmaDecaySigma * sigmaDecaySigma));
if (nSigma < 0.01) {
nSigma = 0.01;
Expand Down
4 changes: 4 additions & 0 deletions lfsom/src/lfsom/layers/LFSUnit.java
Expand Up @@ -356,4 +356,8 @@ public double getQError() {
return quantizationError;
}

public double getMQEError() {
return quantizationError / this.getNumberOfMappedInputs();
}

}

0 comments on commit 210067a

Please sign in to comment.