下面列出了com.google.common.io.PatternFilenameFilter#no.uib.cipr.matrix.Vector 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* weights(cat1) ... weights(cat2) ... ... bias(cat1) bias(cat2)
*/
public double[] getWeightVector() {
final double[] result = new double[(totalFeatures + 1) * lexicalCategories.size()];
int index = 0;
for (final Vector vector : weightMatrixRows) {
for (int i = 0; i < vector.size(); i++) {
result[index] = vector.get(i);
index++;
}
}
for (int i = 0; i < bias.size(); i++) {
result[index] = bias.get(i);
index++;
}
return result;
}
/**
* Constructor for SparseVector, and copies the contents from the supplied
* vector.
*
* @param x
* Vector to copy from
* @param deep
* True if a deep copy is to be made. If the copy is shallow,
* <code>x</code> must be a <code>SparseVector</code>
*/
public SparseVector(Vector x, boolean deep) {
super(x);
if (deep) {
int nz = Matrices.cardinality(x);
data = new double[nz];
index = new int[nz];
set(x);
} else {
SparseVector xs = (SparseVector) x;
data = xs.getData();
index = xs.getIndex();
used = xs.getUsed();
}
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = 0; i < numRows; ++i) {
// Get row i
SparseVector row = LU.getRow(i);
int[] index = row.getIndex();
double[] data = row.getData();
// xi = bi - sum[j<i] Lij * xj
double sum = 0;
for (int j = 0; j < diagind[i]; ++j)
sum += data[j] * xd[index[j]];
xd[i] = bd[i] - sum;
}
return x;
}
@Override
public Vector transSolve(Vector b, Vector x) {
if (!(x instanceof DenseVector))
return super.transSolve(b, x);
x.set(b);
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i) {
// Get row i
SparseVector row = LU.getRow(i);
int[] index = row.getIndex();
double[] data = row.getData();
// At this stage, x[i] is known, so move it over to the right
// hand side for the remaining equations
for (int j = 0; j < diagind[i]; ++j)
xd[index[j]] -= data[j] * xd[i];
}
return x;
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i) {
// Get row i
SparseVector row = LU.getRow(i);
int[] index = row.getIndex();
int used = row.getUsed();
double[] data = row.getData();
// xi = (bi - sum[j>i] Uij * xj) / Uii
double sum = 0;
for (int j = diagind[i] + 1; j < used; ++j)
sum += data[j] * xd[index[j]];
xd[i] = (bd[i] - sum) / data[diagind[i]];
}
return x;
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = 0; i < numRows; ++i) {
// xi = bi - sum[j<i] Lij * xj
double sum = 0;
for (int j = rowptr[i]; j < diagind[i]; ++j)
sum += data[j] * xd[colind[j]];
xd[i] = bd[i] - sum;
}
return x;
}
@Override
public Vector transSolve(Vector b, Vector x) {
if (!(x instanceof DenseVector))
return super.transSolve(b, x);
x.set(b);
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i)
// At this stage, x[i] is known, so move it over to the right hand
// side for the remaining equations
for (int j = rowptr[i]; j < diagind[i]; ++j)
xd[colind[j]] -= data[j] * xd[i];
return x;
}
@Override
protected boolean convergedI(double r, Vector x)
throws IterativeSolverNotConvergedException {
// Store initial residual
if (isFirst())
initR = r;
// Check for convergence
if (r < Math.max(rtol * (normA * x.norm(normType) + normb), atol))
return true;
// Check for divergence
if (r > dtol * initR)
throw new IterativeSolverNotConvergedException(
NotConvergedException.Reason.Divergence, this);
if (iter >= maxIter)
throw new IterativeSolverNotConvergedException(
NotConvergedException.Reason.Iterations, this);
if (Double.isNaN(r))
throw new IterativeSolverNotConvergedException(
NotConvergedException.Reason.Divergence, this);
// Neither convergence nor divergence
return false;
}
@Override
public Vector transMultAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMultAdd(alpha, x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData(), yd = ((DenseVector) y)
.getData();
// y = 1/alpha * y
y.scale(1. / alpha);
// y = A'x + y
for (int i = 0; i < numRows; ++i) {
SparseVector v = rowD[i];
int[] index = v.getIndex();
double[] data = v.getData();
int length = v.getUsed();
for (int j = 0; j < length; ++j)
yd[index[j]] += data[j] * xd[i];
}
// y = alpha*y = alpha * A'x + y
return y.scale(alpha);
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData(), yd = ((DenseVector) y)
.getData();
// y = 1/alpha * y
y.scale(1 / alpha);
// y = A*x + y
for (int i = 0; i < numColumns; ++i)
for (int j = columnPointer[i]; j < columnPointer[i + 1]; ++j)
yd[rowIndex[j]] += data[j] * xd[i];
// y = alpha*y = alpha*A*x + y
return y.scale(alpha);
}
@Override
public Vector transMult(Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMult(x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < numColumns; ++i) {
double dot = 0;
for (int j = columnPointer[i]; j < columnPointer[i + 1]; ++j)
dot += data[j] * xd[rowIndex[j]];
yd[i] = dot;
}
return y;
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i) {
// xi = (bi - sum[j>i] Uij * xj) / Uii
double sum = 0;
for (int j = diagind[i] + 1; j < rowptr[i + 1]; ++j)
sum += data[j] * xd[colind[j]];
xd[i] = (bd[i] - sum) / data[diagind[i]];
}
return x;
}
@Override
public Vector transSolve(Vector b, Vector x) {
if (!(x instanceof DenseVector))
return super.transSolve(b, x);
x.set(b);
double[] xd = ((DenseVector) x).getData();
for (int i = 0; i < numRows; ++i) {
// Solve for the current entry
xd[i] /= data[diagind[i]];
// Move this known solution over to the right hand side for the
// remaining equations
for (int j = diagind[i] + 1; j < rowptr[i + 1]; ++j)
xd[colind[j]] -= data[j] * xd[i];
}
return x;
}
/**
* Constructor for QMR. Uses the given vector as template for creating
* scratch vectors. Typically, the solution or the right hand side vector
* can be passed, and the template is not modified. Allows setting different
* right and left preconditioners
*
* @param template
* Vector to use as template for the work vectors needed in the
* solution process
* @param M1
* Left preconditioner
* @param M2
* Right preconditioner
*/
public QMR(Vector template, Preconditioner M1, Preconditioner M2) {
this.M1 = M1;
this.M2 = M2;
r = template.copy();
y = template.copy();
z = template.copy();
v = template.copy();
w = template.copy();
p = template.copy();
q = template.copy();
d = template.copy();
s = template.copy();
v_tld = template.copy();
w_tld = template.copy();
y_tld = template.copy();
z_tld = template.copy();
p_tld = template.copy();
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < numRows; ++i) {
double dot = 0;
for (int j = rowPointer[i]; j < rowPointer[i + 1]; ++j)
dot += data[j] * xd[columnIndex[j]];
yd[i] += alpha * dot;
}
return y;
}
@Override
public Vector transMult(Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMult(x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
y.zero();
for (int i = 0; i < numRows; ++i)
for (int j = rowPointer[i]; j < rowPointer[i + 1]; ++j)
yd[columnIndex[j]] += data[j] * xd[i];
return y;
}
@Override
public Vector transMultAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMultAdd(alpha, x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
// y = 1/alpha * y
y.scale(1. / alpha);
// y = A'x + y
for (int i = 0; i < numRows; ++i)
for (int j = rowPointer[i]; j < rowPointer[i + 1]; ++j)
yd[columnIndex[j]] += data[j] * xd[i];
// y = alpha*y = alpha*A'x + y
return y.scale(alpha);
}
@Override
public Vector mult(Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.mult(x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
y.zero();
for (int i = 0; i < ind.length; ++i) {
int row = ind[i] < 0 ? -ind[i] : 0;
int column = ind[i] > 0 ? ind[i] : 0;
double[] locDiag = diag[i];
for (int j = 0; j < locDiag.length; ++j, ++row, ++column)
yd[row] += locDiag[j] * xd[column];
}
return y;
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < ind.length; ++i) {
int row = ind[i] < 0 ? -ind[i] : 0;
int column = ind[i] > 0 ? ind[i] : 0;
double[] locDiag = diag[i];
for (int j = 0; j < locDiag.length; ++j, ++row, ++column)
yd[row] += alpha * locDiag[j] * xd[column];
}
return y;
}
@Override
public Vector transMultAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMultAdd(alpha, x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < ind.length; ++i) {
int row = ind[i] < 0 ? -ind[i] : 0;
int column = ind[i] > 0 ? ind[i] : 0;
double[] locDiag = diag[i];
for (int j = 0; j < locDiag.length; ++j, ++row, ++column)
yd[column] += alpha * locDiag[j] * xd[row];
}
return y;
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
// y = 1/alpha * y
y.scale(1. / alpha);
// y = A*x + y
for (int i = 0; i < numColumns; ++i) {
SparseVector v = colD[i];
int[] index = v.getIndex();
double[] data = v.getData();
int length = v.getUsed();
for (int j = 0; j < length; ++j)
yd[index[j]] += data[j] * xd[i];
}
// y = alpha*y = alpha * A'x + y
return y.scale(alpha);
}
private void loadVector(final Vector vector, final File file) throws IOException {
final Iterator<String> lines = Util.readFileLineByLine(file);
int row = 0;
while (lines.hasNext()) {
final String data = lines.next();
vector.set(row, Double.valueOf(data));
row++;
}
}
private Vector getVectorForWord(final List<InputWord> words, final int wordIndex) {
final double[] vector = new double[totalFeatures];
int vectorIndex = 0;
for (int sentencePosition = wordIndex - contextWindow; sentencePosition <= wordIndex
+ contextWindow; sentencePosition++) {
vectorIndex = addToFeatureVector(vectorIndex, vector, sentencePosition, words);
// If using lexical features, update the vector.
if (lexicalFeatures.size() > 0) {
if (sentencePosition >= 0 && sentencePosition < words.size()) {
final Integer index = lexicalFeatures.get(words.get(sentencePosition).word);
if (index != null) {
vector[vectorIndex + index] = 1;
}
}
vectorIndex = vectorIndex + lexicalFeatures.size();
}
// If using POS-tag features, update the vector.
if (posFeatures.size() > 0) {
if (sentencePosition >= 0 && sentencePosition < words.size()) {
vector[vectorIndex + posFeatures.get(words.get(sentencePosition).pos)] = 1;
}
vectorIndex = vectorIndex + posFeatures.size();
}
}
// System.out.println(words.get(wordIndex).word+ " " +
// Doubles.asList(vector));
return new DenseVector(vector);
}
/**
* Returns a list of @SyntaxTreeNode for this word, sorted by their probability.
*
* @param vector
* A vector
* @param word
* The word itself.
* @param wordIndex
* The position of the word in the sentence.
* @return
* @return
*/
private List<ScoredCategory> getTagsForWord(final Vector vector, final InputWord word) {
// If we're using a tag dictionary, consider those tags --- otherwise,
// try all tags.
Collection<Integer> possibleCategories = tagDict.get(word.word);
if (possibleCategories == null) {
possibleCategories = tagDict.get(TagDict.OTHER_WORDS);
}
return getTagsForWord(vector, possibleCategories);
}
private List<ScoredCategory> getTagsForWord(final Vector vector, final Collection<Integer> possibleCategories) {
final int size = Math.min(maxTagsPerWord, possibleCategories.size());
double bestScore = 0.0;
List<ScoredCategory> result = new ArrayList<>(possibleCategories.size());
for (final Integer cat : possibleCategories) {
final double score = weightMatrixRows.get(cat).dot(vector) + bias.get(cat);
result.add(new ScoredCategory(lexicalCategories.get(cat), score));
bestScore = Math.max(bestScore, score);
}
Collections.sort(result);
if (result.size() > size) {
result = result.subList(0, size);
}
final double threshold = beta * Math.exp(bestScore);
for (int i = 2; i < result.size(); i++) {
// TODO binary search
if (Math.exp(result.get(i).getScore()) < threshold) {
result = result.subList(0, i);
break;
}
}
return result;
}
private void loadVector(Vector vector, File file) throws IOException
{
Iterator<String> lines = Util.readFileLineByLine(file);
int row=0;
while (lines.hasNext()) {
String data = lines.next();
vector.set(row, Double.valueOf(data));
row++;
}
}
@Override
public double dot(Vector y) {
if (!(y instanceof DenseVector))
return super.dot(y);
checkSize(y);
double[] yd = ((DenseVector) y).getData();
double ret = 0;
for (int i = 0; i < used; ++i)
ret += data[i] * yd[index[i]];
return ret;
}
@Override
void testFactorization(Matrix A, Vector x) {
Vector b = A.mult(x, x.copy());
ICC icc = new ICC(new CompRowMatrix(A));
icc.setMatrix(A);
icc.apply(b, x);
Vector r = A.multAdd(-1, x, b.copy());
assertEquals(0, r.norm(Vector.Norm.TwoRobust), 1e-5);
}
public Vector apply(Vector b, Vector x) {
// Ly = b, y = L\b
L.solve(b, y);
// Ux = L\b = y
return U.solve(y, x);
}
public Vector transApply(Vector b, Vector x) {
// U'y = b, y = U'\b
U.transSolve(b, y);
// L'x = U'\b = y
return L.transSolve(y, x);
}