Skip to content
Snippets Groups Projects
Commit fa5b5ed0 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Moved isZeroVector functions back to LBG class.

parent 235b181c
No related branches found
No related tags found
No related merge requests found
...@@ -317,7 +317,7 @@ public class LBGVectorQuantizer { ...@@ -317,7 +317,7 @@ public class LBGVectorQuantizer {
} }
// We always want to carry zero vector to next iteration. // We always want to carry zero vector to next iteration.
if (VectorQuantizer.isZeroVector(entryToSplit.getVector())) { if (isZeroVector(entryToSplit.getVector())) {
// Use zero vector in next iteration. // Use zero vector in next iteration.
newCodebook[cbIndex++] = entryToSplit; newCodebook[cbIndex++] = entryToSplit;
...@@ -333,8 +333,10 @@ public class LBGVectorQuantizer { ...@@ -333,8 +333,10 @@ public class LBGVectorQuantizer {
continue; continue;
} }
if (VectorQuantizer.isZeroVector(prtV)) { // Zero perturbation vector can't create two different entries.
// Zero perturbation vector can't create two different entries. // Also vector with values from range [0.0;1.0) is considered as 'zero' vector,
// because those values would be rounded down by the cast to integer.
if (isPotentialZeroVector(prtV)) {
// The original entry is going to be moved to the next codebook with the new // The original entry is going to be moved to the next codebook with the new
// random entry, which will get improved in the LBG algorithm. // random entry, which will get improved in the LBG algorithm.
final int[] randomEntryValues = generateRandomVector(); final int[] randomEntryValues = generateRandomVector();
...@@ -724,14 +726,7 @@ public class LBGVectorQuantizer { ...@@ -724,14 +726,7 @@ public class LBGVectorQuantizer {
// NOTE(Moravec): We can't select random training vector, because zero vector would create another zero vector. // NOTE(Moravec): We can't select random training vector, because zero vector would create another zero vector.
for (int i = 0; i < codebook.length; i++) { for (int i = 0; i < codebook.length; i++) {
if ((codebook[i].getVectorCount() > largestEntrySize) && !VectorQuantizer.isZeroVector(codebook[i].getVector())) { if ((codebook[i].getVectorCount() > largestEntrySize) && !isZeroVector(codebook[i].getVector())) {
/*
if (VectorQuantizer.isZeroVector(codebook[i].getVector())) {
System.out.println("ZERO vector is biggest??");
}
*/
largestEntryIndex = i; largestEntryIndex = i;
largestEntrySize = codebook[i].getVectorCount(); largestEntrySize = codebook[i].getVectorCount();
} }
...@@ -750,13 +745,7 @@ public class LBGVectorQuantizer { ...@@ -750,13 +745,7 @@ public class LBGVectorQuantizer {
// Choose random trainingVector from biggest partition and set it as new entry. // Choose random trainingVector from biggest partition and set it as new entry.
int randomIndex = new Random().nextInt(largestPartitionVectors.length); int randomIndex = new Random().nextInt(largestPartitionVectors.length);
// int counter = 0;
// while (VectorQuantizer.isZeroVector(largestPartitionVectors[randomIndex].getVector())) {
// randomIndex = new Random().nextInt(largestPartitionVectors.length);
// ++counter;
// // System.out.println("x");
// }
// System.out.println("FOund non zero random after " + counter);
// Plane the new entry on the index of the empty entry. // Plane the new entry on the index of the empty entry.
codebook[emptyEntryIndex] = new LearningCodebookEntry(largestPartitionVectors[randomIndex].getVector()); codebook[emptyEntryIndex] = new LearningCodebookEntry(largestPartitionVectors[randomIndex].getVector());
...@@ -832,7 +821,6 @@ public class LBGVectorQuantizer { ...@@ -832,7 +821,6 @@ public class LBGVectorQuantizer {
int index = 0; int index = 0;
// TrainingVector[] vectors = new TrainingVector[vectorCount];
int count = 0; int count = 0;
for (final TrainingVector trainingVector : trainingVectors) { for (final TrainingVector trainingVector : trainingVectors) {
if (trainingVector.getEntryIndex() == entryIndex) { if (trainingVector.getEntryIndex() == entryIndex) {
...@@ -840,11 +828,7 @@ public class LBGVectorQuantizer { ...@@ -840,11 +828,7 @@ public class LBGVectorQuantizer {
} }
} }
TrainingVector[] vectors = new TrainingVector[count]; TrainingVector[] vectors = new TrainingVector[count];
// if (count > vectorCount) {
// System.err.println(String.format(
// "WARNING: got more training vectors than vectorCount! DIFF: %d",
// count - vectorCount));
// }
for (final TrainingVector trainingVector : trainingVectors) { for (final TrainingVector trainingVector : trainingVectors) {
if (trainingVector.getEntryIndex() == entryIndex) { if (trainingVector.getEntryIndex() == entryIndex) {
vectors[index++] = trainingVector; vectors[index++] = trainingVector;
...@@ -853,6 +837,36 @@ public class LBGVectorQuantizer { ...@@ -853,6 +837,36 @@ public class LBGVectorQuantizer {
return vectors; return vectors;
} }
/**
* Check whether all vector elements less than 1.0 .
*
* @param vector Vector array.
* @return True if all elements less than 1.0.
*/
private boolean isPotentialZeroVector(final double[] vector) {
for (final double value : vector) {
if (value >= 1.0) {
return false;
}
}
return true;
}
/**
* Check whether all vector elements are equal to 0
*
* @param vector Vector array.
* @return True if all elements are zeros.
*/
private boolean isZeroVector(final int[] vector) {
for (final double value : vector) {
if (value != 0.0) {
return false;
}
}
return true;
}
/** /**
* Get codebook size. * Get codebook size.
* *
......
...@@ -123,7 +123,6 @@ public class VectorQuantizer { ...@@ -123,7 +123,6 @@ public class VectorQuantizer {
} }
private int findClosestCodebookEntryIndex(final int[] dataVector, final VectorDistanceMetric metric) { private int findClosestCodebookEntryIndex(final int[] dataVector, final VectorDistanceMetric metric) {
boolean closesIsZero = false;
double minDist = Double.MAX_VALUE; double minDist = Double.MAX_VALUE;
int closestEntryIndex = 0; int closestEntryIndex = 0;
for (int entryIndex = 0; entryIndex < codebook.length; entryIndex++) { for (int entryIndex = 0; entryIndex < codebook.length; entryIndex++) {
...@@ -133,48 +132,14 @@ public class VectorQuantizer { ...@@ -133,48 +132,14 @@ public class VectorQuantizer {
if (dist < minDist) { if (dist < minDist) {
minDist = dist; minDist = dist;
closestEntryIndex = entryIndex; closestEntryIndex = entryIndex;
closesIsZero = isZeroVector(codebook[entryIndex].getVector());
} }
} }
if (closesIsZero) {
// System.out.println("One of zero vectors.");
}
return closestEntryIndex; return closestEntryIndex;
} }
public CodebookEntry[] getCodebook() { public CodebookEntry[] getCodebook() {
return codebook; return codebook;
} }
/**
* Check whether all vector elements are equal to 0.0
*
* @param vector Vector array.
* @return True if all elements are zeros.
*/
public static boolean isZeroVector(final double[] vector) {
for (final double value : vector) {
if (value != 0.0) {
return false;
}
}
return true;
}
/**
* Check whether all vector elements are equal to 0
*
* @param vector Vector array.
* @return True if all elements are zeros.
*/
public static boolean isZeroVector(final int[] vector) {
for (final double value : vector) {
if (value != 0.0) {
return false;
}
}
return true;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment