本文整理汇总了Java中cc.mallet.types.SparseVector类的典型用法代码示例。如果您正苦于以下问题:Java SparseVector类的具体用法?Java SparseVector怎么用?Java SparseVector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SparseVector类属于cc.mallet.types包,在下文中一共展示了SparseVector类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initConstraintsExpectations
import cc.mallet.types.SparseVector; //导入依赖的package包/类
private void initConstraintsExpectations ()
{
// Do the defaults first
defaultConstraints = new SparseVector [templates.length];
defaultExpectations = new SparseVector [templates.length];
for (int tidx = 0; tidx < templates.length; tidx++) {
SparseVector defaults = templates[tidx].getDefaultWeights();
defaultConstraints[tidx] = (SparseVector) defaults.cloneMatrixZeroed ();
defaultExpectations[tidx] = (SparseVector) defaults.cloneMatrixZeroed ();
}
// And now the others
constraints = new SparseVector [templates.length][];
expectations = new SparseVector [templates.length][];
for (int tidx = 0; tidx < templates.length; tidx++) {
ACRF.Template tmpl = templates [tidx];
SparseVector[] weights = tmpl.getWeights();
constraints [tidx] = new SparseVector [weights.length];
expectations [tidx] = new SparseVector [weights.length];
for (int i = 0; i < weights.length; i++) {
constraints[tidx][i] = (SparseVector) weights[i].cloneMatrixZeroed ();
expectations[tidx][i] = (SparseVector) weights[i].cloneMatrixZeroed ();
}
}
}
示例2: computePrior
import cc.mallet.types.SparseVector; //导入依赖的package包/类
private double computePrior ()
{
double retval = 0.0;
double priorDenom = 2 * gaussianPriorVariance;
for (int tidx = 0; tidx < templates.length; tidx++) {
SparseVector[] weights = templates [tidx].getWeights ();
for (int j = 0; j < weights.length; j++) {
for (int fnum = 0; fnum < weights[j].numLocations(); fnum++) {
double w = weights [j].valueAtLocation (fnum);
if (weightValid (w, tidx, j)) {
retval += -w*w/priorDenom;
}
}
}
}
return retval;
}
示例3: libSVMInstanceIndepFromMalletInstance
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public static svm_node[] libSVMInstanceIndepFromMalletInstance(
cc.mallet.types.Instance malletInstance) {
// TODO: maybe check that data is really a sparse vector? Should be in all cases
// except if we have an instance from MalletSeq
SparseVector data = (SparseVector) malletInstance.getData();
int[] indices = data.getIndices();
double[] values = data.getValues();
svm_node[] nodearray = new svm_node[indices.length];
int index = 0;
for (int j = 0; j < indices.length; j++) {
svm_node node = new svm_node();
node.index = indices[j]+1; // NOTE: LibSVM locations have to start with 1
node.value = values[j];
nodearray[index] = node;
index++;
}
return nodearray;
}
示例4: distance
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public double distance( SparseVector a, int hashCodeA,
SparseVector b, int hashCodeB) {
Double cachedA = (Double) hash.get (new Integer (hashCodeA));
Double cachedB = (Double) hash.get (new Integer (hashCodeB));
if (a == null || b == null)
return 1.0;
if (cachedA == null) {
cachedA = new Double (a.dotProduct (a));
hash.put (new Integer (hashCodeA), cachedA);
}
if (cachedB == null) {
cachedB = new Double (b.dotProduct (b));
hash.put (new Integer (hashCodeB), cachedB);
}
double ab = a.dotProduct (b);
if (cachedA == null || cachedB == null) {
throw new IllegalStateException ("cachedValues null");
}
double ret = a.dotProduct (b) / Math.sqrt (cachedA.doubleValue()*cachedB.doubleValue());
return 1.0 - ret;
}
示例5: testDotProduct
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public void testDotProduct () {
SparseVector t1 = new SparseVector (new int[] { 7 }, new double[] { 0.2 });
assertEquals (0.6, t1.dotProduct (s1), 0.00001);
assertEquals (0.6, s1.dotProduct (t1), 0.00001);
assertEquals (19.0, s1.dotProduct (s2), 0.00001);
assertEquals (19.0, s2.dotProduct (s1), 0.00001);
assertEquals (11.9, s1.dotProduct (d1), 0.00001);
assertEquals (10.1, s2.dotProduct (d1), 0.00001);
// test dotproduct when vector with more locations has a lower
// max-index than short vector
SparseVector t2 = new SparseVector (new int[] { 3, 30 }, new double[] { 0.2, 3.5 });
SparseVector t3 = new SparseVector (null, new double[] { 1, 1, 1, 1, });
assertEquals (0.2, t3.dotProduct (t2), 0.00001);
}
示例6: testBinaryVector
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public void testBinaryVector ()
{
SparseVector binary1 = new SparseVector (idxs, null, idxs.length, idxs.length,
false, false, false);
SparseVector binary2 = new SparseVector (idx2, null, idx2.length, idx2.length,
false, false, false);
assertEquals (3, binary1.dotProduct (binary2), 0.0001);
assertEquals (3, binary2.dotProduct (binary1), 0.0001);
assertEquals (15.0, binary1.dotProduct (s1), 0.0001);
assertEquals (15.0, s1.dotProduct (binary1), 0.0001);
assertEquals (9.0, binary2.dotProduct (s1), 0.0001);
assertEquals (9.0, s1.dotProduct (binary2), 0.0001);
SparseVector dblVec = (SparseVector) s1.cloneMatrix ();
dblVec.plusEqualsSparse (binary1);
checkAnswer (dblVec, new double[] { 2, 3, 4, 5, 6 });
SparseVector dblVec2 = (SparseVector) s1.cloneMatrix ();
dblVec2.plusEqualsSparse (binary2);
checkAnswer (dblVec2, new double[] { 2, 2, 4, 4, 6 });
}
示例7: testPrint
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public void testPrint ()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
PrintStream out = new PrintStream (baos);
PrintStream oldOut = System.out;
System.setOut (out);
SparseVector standard = new SparseVector (idxs, dbl2);
standard.print ();
assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.5\nSparseVector[7] = 2.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ());
baos.reset ();
SparseVector dense = new SparseVector (null, dbl2);
dense.print ();
assertEquals ("SparseVector[0] = 1.0\nSparseVector[1] = 1.5\nSparseVector[2] = 2.0\nSparseVector[3] = 1.0\nSparseVector[4] = 1.0\n", baos.toString ());
baos.reset ();
SparseVector binary = new SparseVector (idxs, null, idxs.length, idxs.length,
false, false, false);
binary.print ();
assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.0\nSparseVector[7] = 1.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ());
baos.reset ();
}
示例8: readSparse
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public List<SparseVector> readSparse(File mat) throws IOException {
Scanner sc = new Scanner(mat);
List<SparseVector> ret = new ArrayList<SparseVector>();
int nrows = sc.nextInt();
int ncols = sc.nextInt();
int nz = sc.nextInt();
for (int j = 0; j < ncols; j++) {
int cnz = sc.nextInt();
int[] indices = new int[cnz];
double[] values = new double[cnz];
for (int i = 0; i < cnz; i++) {
indices[i] = sc.nextInt();
values[i] = sc.nextDouble();
}
ret.add(new IndexedSparseVector(indices, values));
}
sc.close();
return ret;
}
示例9: run
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public void run() {
try {
int pivot;
for (int i = t; i < pivots.length; i += numThread) {
pivot = pivots[i];
SparseVector mask = masks[i];
SparseVector w = (SparseVector) mask.cloneMatrixZeroed();
// train a linear classifier for pivot feature
LinearClassifierByModifiedHuberLoss clf = new LinearClassifierByModifiedHuberLoss(
w, pivot, instances);
clf.train();
clf.evaluate();
ws[i] = w;
logger.info("Thread " + t + " finish training pivot(" + i
+ "): " + pivot);
}
} finally {
latch.countDown();
}
}
示例10: trainByMultiThread
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public SparseVector[] trainByMultiThread(final int numThread) {
final SparseVector[] ws = new SparseVector[pivots.length];
Thread[] threads = new Thread[numThread];
CountDownLatch latch = new CountDownLatch(numThread);
for (int t = 0; t < threads.length; t++) {
threads[t] = new Thread(new TrainThread(ws, t, numThread, latch));
threads[t].start();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return ws;
}
示例11: trainBySingleThread
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public SparseVector[] trainBySingleThread() {
SparseVector[] ws = new SparseVector[pivots.length];
int pivot;
for (int i = 0; i < pivots.length; i++) {
pivot = pivots[i];
SparseVector mask = masks[i];
SparseVector w = (SparseVector) mask.cloneMatrixZeroed();
// train a linear classifier for pivot feature
LinearClassifierByModifiedHuberLoss clf = new LinearClassifierByModifiedHuberLoss(
w, pivot, instances);
clf.train();
clf.evaluate();
ws[i] = w;
logger.info("Finish training pivot " + pivot);
}
return ws;
}
示例12: getValueGradient
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public SparseVector getValueGradient(int i) {
SparseVector x = clf.getX(i);
double y = clf.getY(i);
double p = w.dotProduct(x);
double py = p * y;
double phi_d;
if (py >= 1) {
phi_d = 0;
} else if (py <= -1) {
phi_d = -4 * y;
} else {
phi_d = -2 * (1 - py) * y;
}
g.setAll(0);
g.plusEqualsSparse(w, lambda);
g.plusEqualsSparse(x, phi_d);
return g;
}
示例13: getValue
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public double getValue() {
if (!isValueCacheValid) {
int n = getNumInstances();
double norm = w.twoNorm();
value = 0;
for (int i = 0; i < n; i++) {
SparseVector x = clf.getX(i);
double y = clf.getY(i);
double py = w.dotProduct(x) * y;
double loss;
if (py >= -1) {
py = Math.max(0, 1 - py);
loss = py * py;
} else {
loss = -4 * py;
}
value += loss;
}
value = -(value / n + lambda * norm * norm / 2);
isValueCacheValid = true;
}
return value;
}
示例14: read
import cc.mallet.types.SparseVector; //导入依赖的package包/类
public void read(DataInputStream in) throws IOException {
weightAlphabet.read(in);
int weightsLength = in.readInt();
weights = new SparseVector[weightsLength];
for (int i = 0; i < weightsLength; i++) {
SparseVector vector = new SparseVector();
vector.read(in);
weights[i] = vector;
}
defaultWeights = readDoubleArray(in);
weightsFrozen = readBooleanArray(in);
initialWeights = readDoubleArray(in);
finalWeights = readDoubleArray(in);
}