use of org.apache.mahout.math.SequentialAccessSparseVector in project pyramid by cheng-li.
the class CMLCRFElasticNet method iterate.
public void iterate() {
// System.out.println("weights: " + cmlcrf.getWeights().getAllWeights());
// O(NdL)
// System.out.println(Arrays.toString(cmlcrf.getCombinationLabelPartScores()));
updateClassScoreMatrix();
cmlcrf.updateCombLabelPartScores();
updateAssignmentScoreMatrix();
updateAssignmentProbMatrix();
updateCombProbSums();
updatePredictedCounts();
updateClassProbMatrix();
// update for each support label set
Vector accumulateWeights = new SequentialAccessSparseVector(numParameters);
Vector oldWeights = cmlcrf.getWeights().deepCopy().getAllWeights();
for (int l = 0; l < numSupport; l++) {
// System.out.println("label: " + supportedCombinations.get(l));
DataSet newData = expandData(l);
iterateForOneComb(newData, l);
accumulateWeights = accumulateWeights.plus(cmlcrf.getWeights().getAllWeights());
cmlcrf.getWeights().setWeightVector(oldWeights);
}
// lineSearch
if (true) {
Vector searchDirection = accumulateWeights;
Vector gradient = this.predictedCounts.minus(empiricalCounts).divide(numData);
lineSearch(searchDirection, gradient);
}
this.terminator.add(getValue());
}
use of org.apache.mahout.math.SequentialAccessSparseVector 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.SequentialAccessSparseVector in project pyramid by cheng-li.
the class SerializableVectorTest method test1.
private static void test1() throws Exception {
Vector vector = new SequentialAccessSparseVector(10);
vector.set(0, 2);
vector.set(5, 8);
SerializableVector serializableVector = new SerializableVector(vector);
Serialization.serialize(serializableVector, new File(TMP, "v.ser"));
Vector loaded = ((SerializableVector) Serialization.deserialize(new File(TMP, "v.ser"))).getVector();
System.out.println(loaded.size());
System.out.println(loaded.getClass().getName());
System.out.println(loaded);
}
use of org.apache.mahout.math.SequentialAccessSparseVector in project pyramid by cheng-li.
the class Vectors method concatenate.
public static Vector concatenate(Vector vector, double number) {
Vector con = null;
if (vector instanceof DenseVector) {
con = new DenseVector(vector.size() + 1);
}
if (vector instanceof RandomAccessSparseVector) {
con = new RandomAccessSparseVector(vector.size() + 1);
}
if (vector instanceof SequentialAccessSparseVector) {
con = new SequentialAccessSparseVector(vector.size() + 1);
}
for (Vector.Element nonZeros : vector.nonZeroes()) {
int index = nonZeros.index();
double value = nonZeros.get();
con.set(index, value);
}
con.set(con.size() - 1, number);
return con;
}
use of org.apache.mahout.math.SequentialAccessSparseVector in project pyramid by cheng-li.
the class SerializableVector method readObject.
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (type == Type.DENSE) {
double[] values = (double[]) in.readObject();
vector = new DenseVector(values);
} else if (type == Type.SPARSE_RANDOM) {
int[] indices = (int[]) in.readObject();
double[] values = (double[]) in.readObject();
vector = new RandomAccessSparseVector(size);
for (int i = 0; i < indices.length; i++) {
vector.set(indices[i], values[i]);
}
} else if (type == Type.SPARSE_SEQUENTIAL) {
int[] indices = (int[]) in.readObject();
double[] values = (double[]) in.readObject();
vector = new SequentialAccessSparseVector(size);
for (int i = 0; i < indices.length; i++) {
vector.set(indices[i], values[i]);
}
}
}
Aggregations