use of org.apache.mahout.math.RandomAccessSparseVector in project elephant-bird by twitter.
the class VectorWritableConverter method convertSparseVectorDataToVector.
private Vector convertSparseVectorDataToVector(Tuple value) throws IOException {
Vector v;
// determine output vector size and fetch bag containing entries from input
int size = 0;
DataBag entries = null;
if (value.size() == 2) {
// cardinality defined by input
size = (Integer) value.get(0);
if (cardinality != null) {
// cardinality defined by VectorWritableConverter instance
size = cardinality;
}
entries = (DataBag) value.get(1);
} else {
Preconditions.checkNotNull(cardinality, "Cardinality is undefined");
size = cardinality;
entries = (DataBag) value.get(0);
}
// create vector, allowing conversion of sparse input vector data to dense output vector
if (dense) {
// TODO(Andy Schlaikjer): Test for OOM before it happens
v = new DenseVector(size);
} else {
// more efficient to build sparse vector with this impl
v = new RandomAccessSparseVector(size);
}
// populate vector
for (Tuple entry : entries) {
validateSparseVectorEntryData(entry);
int i = (Integer) entry.get(0);
// check index bounds
if (i < 0 || i >= size) {
counterHelper.incrCounter(Counter.INDEX_OUT_OF_BOUNDS, 1);
continue;
}
double n = ((Number) entry.get(1)).doubleValue();
v.setQuick(i, n);
}
// convert to (sparse) sequential vector if requested
if (sequential) {
v = new SequentialAccessSparseVector(v);
}
return v;
}
use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.
the class CBM method toString.
public String toString() {
Vector vector = new RandomAccessSparseVector(numFeatures);
double[] mixtureCoefficients = multiClassClassifier.predictClassProbs(vector);
final StringBuilder sb = new StringBuilder("CBM{\n");
sb.append("numLabels=").append(numLabels).append("\n");
sb.append("numComponents=").append(numComponents).append("\n");
for (int k = 0; k < numComponents; k++) {
sb.append("cluster ").append(k).append(":\n");
sb.append("proportion = ").append(mixtureCoefficients[k]).append("\n");
}
sb.append("multi-class component = \n");
sb.append(multiClassClassifier);
sb.append("binary components = \n");
for (int k = 0; k < numComponents; k++) {
for (int l = 0; l < numLabels; l++) {
sb.append("component ").append(k).append(" class ").append(l).append("\n");
sb.append(binaryClassifiers[k][l]).append("\n");
}
}
sb.append('}');
return sb.toString();
}
use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.
the class Vectors method concatenate.
public static Vector concatenate(Vector vector, Vector vector2) {
Vector con = null;
if (vector instanceof DenseVector) {
con = new DenseVector(vector.size() + vector2.size());
}
if (vector instanceof RandomAccessSparseVector) {
con = new RandomAccessSparseVector(vector.size() + vector2.size());
}
if (vector instanceof SequentialAccessSparseVector) {
con = new SequentialAccessSparseVector(vector.size() + vector2.size());
}
for (Vector.Element nonZeros : vector.nonZeroes()) {
int index = nonZeros.index();
double value = nonZeros.get();
con.set(index, value);
}
for (Vector.Element nonZeros : vector2.nonZeroes()) {
int index = nonZeros.index();
double value = nonZeros.get();
con.set(index + vector.size(), value);
}
return con;
}
use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.
the class Vectors method concatenate.
public static Vector concatenate(Vector vector, double[] numbers) {
Vector con = null;
if (vector instanceof DenseVector) {
con = new DenseVector(vector.size() + numbers.length);
}
if (vector instanceof RandomAccessSparseVector) {
con = new RandomAccessSparseVector(vector.size() + numbers.length);
}
if (vector instanceof SequentialAccessSparseVector) {
con = new SequentialAccessSparseVector(vector.size() + numbers.length);
}
for (Vector.Element nonZeros : vector.nonZeroes()) {
int index = nonZeros.index();
double value = nonZeros.get();
con.set(index, value);
}
for (int i = 0; i < numbers.length; i++) {
con.set(i + vector.size(), numbers[i]);
}
return con;
}
use of org.apache.mahout.math.RandomAccessSparseVector in project pyramid by cheng-li.
the class BackTrackingLineSearcher method moveAlongDirection.
/**
* move to a new position along the direction
*/
public MoveInfo moveAlongDirection(Vector searchDirection) {
Vector localSearchDir;
if (logger.isDebugEnabled()) {
logger.debug("start line search");
// don't want to show too much; only show it on small problems
if (searchDirection.size() < 100) {
logger.debug("direction=" + searchDirection);
}
}
MoveInfo moveInfo = new MoveInfo();
double stepLength = initialStepLength;
double value = function.getValue();
moveInfo.setOldValue(value);
Vector gradient = function.getGradient();
double product = gradient.dot(searchDirection);
if (product < 0) {
localSearchDir = searchDirection;
} else {
if (logger.isWarnEnabled()) {
logger.warn("Bad search direction! Use negative gradient instead. Product of gradient and search direction = " + product);
}
localSearchDir = gradient.times(-1);
}
Vector initialPosition;
// keep a copy of initial parameters
if (function.getParameters().isDense()) {
initialPosition = new DenseVector(function.getParameters());
} else {
initialPosition = new RandomAccessSparseVector(function.getParameters());
}
while (true) {
Vector step = localSearchDir.times(stepLength);
Vector target = initialPosition.plus(step);
function.setParameters(target);
double targetValue = function.getValue();
if (logger.isDebugEnabled()) {
logger.debug("step length = " + stepLength + ", target value = " + targetValue);
// logger.debug("requirement = "+(value + c*stepLength*product));
}
// todo: if equal ok?
if ((targetValue <= value + c * stepLength * product && value < Double.POSITIVE_INFINITY) || stepLength == 0) {
moveInfo.setStep(step);
moveInfo.setStepLength(stepLength);
moveInfo.setNewValue(targetValue);
break;
}
stepLength *= shrinkage;
}
if (logger.isDebugEnabled()) {
logger.debug("line search done. " + moveInfo);
}
return moveInfo;
}
Aggregations