本文整理汇总了Java中pitt.search.semanticvectors.vectors.VectorUtils类的典型用法代码示例。如果您正苦于以下问题:Java VectorUtils类的具体用法?Java VectorUtils怎么用?Java VectorUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
VectorUtils类属于pitt.search.semanticvectors.vectors包,在下文中一共展示了VectorUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNegatedQueryVector
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Creates a vector, including orthogonalizing negated terms.
*
* @param queryTerms List of positive and negative terms.
* @param split Position in this list of the NOT mark: terms
* before this are positive, those after this are negative.
* @return Single query vector, the sum of the positive terms,
* projected to be orthogonal to all negative terms.
* @see VectorUtils#orthogonalizeVectors
*/
protected Vector getNegatedQueryVector(FlagConfig flagConfig, String[] queryTerms, int split) {
int numNegativeTerms = queryTerms.length - split - 1;
int numPositiveTerms = split;
logger.log(Level.FINER, "Number of negative terms: {0}", numNegativeTerms);
logger.log(Level.FINER, "Number of positive terms: {0}", numPositiveTerms);
ArrayList<Vector> vectorList = new ArrayList<Vector>();
for (int i = 1; i <= numNegativeTerms; ++i) {
Vector tmpVector = vecReader.getVector(queryTerms[split + i]);
if (tmpVector != null) {
vectorList.add(tmpVector);
}
}
String[] positiveTerms = new String[numPositiveTerms];
for (int i = 0; i < numPositiveTerms; ++i) {
positiveTerms[i] = queryTerms[i];
}
vectorList.add(getAdditiveQueryVector(flagConfig, positiveTerms));
VectorUtils.orthogonalizeVectors(vectorList);
return vectorList.get(vectorList.size() - 1);
}
示例2: getNumberVectors
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Gets a sequence of number vectors, the first and last of which are the demarcator vectors,
* and the intervening members being evenly linearly distributed between these.
*
* @param iStart The number represented by the beginning of the returned sequence.
* @param iEnd The number represented by the end of the returned sequence.
* @return VectorStore in memory whose keys are the integers between {@code iStart}
* and {@code iEnd} and whose values are appropriate linear combinations of
* the demarcator vectors.
*/
public VectorStoreRAM getNumberVectors(int iStart, int iEnd) {
if (numberVectorsCache.containsKey(iStart+":"+iEnd))
return numberVectorsCache.get(iStart+":"+iEnd);
if (iEnd < iStart) throw new IllegalArgumentException(
String.format(
"End index (%d) should be greater than start index (%d).", iEnd, iStart));
VectorStoreRAM theVSR = new VectorStoreRAM(flagConfig);
for (int i = 0; i <= iEnd - iStart; ++i) {
Vector ithNumberVector = VectorUtils.weightedSuperposition(vL, iEnd-iStart-i, vR, i);
theVSR.putVector(iStart + i, ithNumberVector);
}
theVSR.putVector("alpha", vL);
theVSR.putVector("omega", vR);
numberVectorsCache.put(iStart+":"+iEnd, theVSR);
return theVSR;
}
示例3: testOrthogonalizeVectors
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
@Test
public void testOrthogonalizeVectors() {
FlagConfig flagConfig = FlagConfig.getFlagConfig(null);
flagConfig.setDimension(3);
Vector vec1 = new RealVector(new float[] {1, 2, 1});
Vector vec2 = new RealVector(new float[] {2, 3, 1});
Vector vec3 = new RealVector(new float[] {2, 1, 1});
Vector vec4 = new RealVector(new float[] {2, 1, 5});
ArrayList<Vector> list = new ArrayList<Vector>();
list.add(vec1);
list.add(vec2);
list.add(vec3);
list.add(vec4);
VectorUtils.orthogonalizeVectors(list);
assertEquals(1.0, list.get(0).measureOverlap(list.get(0)), TOL);
assertEquals(1.0, list.get(1).measureOverlap(list.get(1)), TOL);
assertEquals(1.0, list.get(2).measureOverlap(list.get(2)), TOL);
assertEquals(0, list.get(0).measureOverlap(list.get(1)), TOL);
assertEquals(0, list.get(0).measureOverlap(list.get(2)), TOL);
assertEquals(0, list.get(1).measureOverlap(list.get(2)), TOL);
// If we try to orthogonalize more vectors than dimensions, we expect degeneracy eventually!
assertEquals(Float.NaN, ((RealVector) list.get(3)).getCoordinates()[0]);
}
示例4: getBoundProductQuerySubSpaceFromString
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Method gets a query subspace from a query string of the form:
* relation1*relation2+relation3*relation4
*
* The resulting subspace (or binary approximation) will be derived from the bound product of concept1 and r1*r2, and the
* bound product of the concept vector and relation r3*r4.
*
* This method facilitates the combination of single or dual predicate paths using the quantum OR operator, or a binary approximation thereof
*
* @param vecReader Vector store reader for input
* @param queryString Query expression to be turned into vector subspace
* @return List of vectors that are basis elements for subspace
*/
public static ArrayList<Vector> getBoundProductQuerySubSpaceFromString(
FlagConfig flagConfig, VectorStore vecReader, Vector conceptVector, String queryString) {
ArrayList<Vector> disjunctSpace = new ArrayList<Vector>();
// Split initially at "+" to construct derive components.
StringTokenizer subspaceTokenizer = new StringTokenizer(queryString,"+");
while (subspaceTokenizer.hasMoreTokens()) {
// Allow for binding of multiple concepts/relations.
StringTokenizer bindingTokenizer = new StringTokenizer(subspaceTokenizer.nextToken(),"*");
Vector boundQueryvector = vecReader.getVector(bindingTokenizer.nextToken()).copy();
while (bindingTokenizer.hasMoreTokens()) {
boundQueryvector.release(vecReader.getVector(bindingTokenizer.nextToken()));
}
Vector copyConceptVector = conceptVector.copy();
copyConceptVector.release(boundQueryvector);
disjunctSpace.add(copyConceptVector);
}
if (flagConfig.searchtype() != SearchType.BOUNDMINIMUM) VectorUtils.orthogonalizeVectors(disjunctSpace);
return disjunctSpace;
}
示例5: getNumberVectors
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Gets a sequence of number vectors, the first and last of which are the demarcator vectors,
* and the intervening members being evenly linearly distributed between these.
*
* @param iStart The number represented by the beginning of the returned sequence.
* @param iEnd The number represented by the end of the returned sequence.
* @return VectorStore in memory whose keys are the integers between {@code iStart}
* and {@code iEnd} and whose values are appropriate linear combinations of
* the demarcator vectors.
*/
public VectorStoreRAM getNumberVectors(int iStart, int iEnd) {
if (numberVectorsCache.containsKey(iStart+":"+iEnd))
return numberVectorsCache.get(iStart+":"+iEnd);
if (iEnd < iStart) throw new IllegalArgumentException(
String.format(
"End index (%d) should be greater than start index (%d).", iEnd, iStart));
VectorStoreRAM theVSR = new VectorStoreRAM(flagConfig);
for (int i = 0; i <= iEnd - iStart; ++i) {
Vector ithNumberVector = VectorUtils.weightedSuperposition(vL, iEnd-iStart-i, vR, i);
theVSR.putVector(iStart + i, ithNumberVector);
}
numberVectorsCache.put(iStart+":"+iEnd, theVSR);
return theVSR;
}
示例6: testOrthogonalizeVectors
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
@Test
public void testOrthogonalizeVectors() {
FlagConfig flagConfig = FlagConfig.getFlagConfig(null);
flagConfig.setDimension(3);
Vector vec1 = new RealVector(new float[] {1, 2, 1});
Vector vec2 = new RealVector(new float[] {2, 3, 1});
Vector vec3 = new RealVector(new float[] {2, 1, 1});
ArrayList<Vector> list = new ArrayList<Vector>();
list.add(vec1);
list.add(vec2);
list.add(vec3);
VectorUtils.orthogonalizeVectors(list);
assertEquals(1.0, list.get(0).measureOverlap(list.get(0)), TOL);
assertEquals(1.0, list.get(1).measureOverlap(list.get(1)), TOL);
assertEquals(1.0, list.get(2).measureOverlap(list.get(2)), TOL);
assertEquals(0, list.get(0).measureOverlap(list.get(1)), TOL);
assertEquals(0, list.get(0).measureOverlap(list.get(2)), TOL);
assertEquals(0, list.get(1).measureOverlap(list.get(2)), TOL);
}
示例7: VectorSearcherSubspaceSim
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* @param queryVecStore Vector store to use for query generation.
* @param searchVecStore The vector store to search.
* @param luceneUtils LuceneUtils object to use for query weighting. (May be null.)
* @param queryTerms Terms that will be parsed and used to generate a query subspace.
*/
public VectorSearcherSubspaceSim(VectorStore queryVecStore,
VectorStore searchVecStore,
LuceneUtils luceneUtils,
FlagConfig flagConfig,
String[] queryTerms)
throws ZeroVectorException {
super(queryVecStore, searchVecStore, luceneUtils, flagConfig);
this.disjunctSpace = new ArrayList<Vector>();
this.vectorType = flagConfig.vectortype();
for (int i = 0; i < queryTerms.length; ++i) {
System.out.println("\t" + queryTerms[i]);
// There may be compound disjuncts, e.g., "A NOT B" as a single argument.
String[] tmpTerms = queryTerms[i].split("\\s");
Vector tmpVector = CompoundVectorBuilder.getQueryVector(
queryVecStore, luceneUtils, flagConfig, tmpTerms);
if (tmpVector != null) {
this.disjunctSpace.add(tmpVector);
}
}
if (this.disjunctSpace.size() == 0) {
throw new ZeroVectorException("No nonzero input vectors ... no results.");
}
if (!vectorType.equals(VectorType.BINARY))
VectorUtils.orthogonalizeVectors(this.disjunctSpace);
else BinaryVectorUtils.orthogonalizeVectors(this.disjunctSpace);
}
示例8: getScore
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Scoring works by taking scalar product with disjunctSpace
* (which must by now be represented using an orthogonal basis).
* @param testVector Vector being tested.
*/
@Override
public double getScore(Vector testVector) {
if (!vectorType.equals(VectorType.BINARY))
return VectorUtils.compareWithProjection(testVector, disjunctSpace);
else return BinaryVectorUtils.compareWithProjection(testVector, disjunctSpace);
}
示例9: getVector
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Method gets a query vector from a query string of the form:
* S(concept1)*E(relation2)+S(concept3)*E(relation4)
*
* the resulting vector will be the bundle of the semantic vector for each concept
* bound to the elemental vector of the relevant relation
*
* @return the resulting query vector
*/
private static Vector getVector(
FlagConfig flagConfig, VectorStore elementalVectors, VectorStore semanticVectors, VectorStore predicateVectors, String term) {
try {
//permit negation across different vector stores
if (term.contains(NEGATION_TOKEN))
{
ArrayList<Vector> disjunctSpace = new ArrayList<Vector>();
String[] terms = term.split(NEGATION_TOKEN);
disjunctSpace.add(getVector( flagConfig, elementalVectors, semanticVectors, predicateVectors, terms[1]));
disjunctSpace.add(getVector( flagConfig, elementalVectors, semanticVectors, predicateVectors, terms[0]));
VectorUtils.orthogonalizeVectors(disjunctSpace);
return disjunctSpace.get(disjunctSpace.size()-1);
}
else if (term.startsWith("E(") && term.endsWith(")"))
return elementalVectors.getVector(term.substring(2,term.length()-1)).copy();
else if (term.startsWith("S(") && term.endsWith(")"))
return semanticVectors.getVector(term.substring(2,term.length()-1)).copy();
else if (term.startsWith("P(") && term.endsWith(")"))
return predicateVectors.getVector(term.substring(2,term.length()-1)).copy();
else return VectorFactory.createZeroVector(flagConfig.vectortype(), flagConfig.dimension());
}
catch (NullPointerException npe)
{
return VectorFactory.createZeroVector(flagConfig.vectortype(), flagConfig.dimension());
}
}
示例10: getBoundProductQuerySubSpaceFromString
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Method gets a query subspace from a query string of the form:
* relation1*relation2+relation3*relation4
*
* The resulting subspace (or binary approximation) will be derived from the bound product of concept1 and r1*r2, and the
* bound product of the concept vector and relation r3*r4.
*
* This method facilitates the combination of single or dual predicate paths using the quantum OR operator, or a binary approximation thereof
*
* @param vecReader Vector store reader for input
* @param queryString Query expression to be turned into vector subspace
* @return List of vectors that are basis elements for subspace
*/
public static ArrayList<Vector> getBoundProductQuerySubSpaceFromString(
FlagConfig flagConfig, VectorStore vecReader, Vector conceptVector, String queryString) {
ArrayList<Vector> disjunctSpace = new ArrayList<Vector>();
// Split initially at "+" to construct derive components.
StringTokenizer subspaceTokenizer = new StringTokenizer(queryString,"+");
while (subspaceTokenizer.hasMoreTokens()) {
// Allow for binding of multiple concepts/relations.
StringTokenizer bindingTokenizer = new StringTokenizer(subspaceTokenizer.nextToken(),"*");
Vector boundQueryvector = vecReader.getVector(bindingTokenizer.nextToken()).copy();
while (bindingTokenizer.hasMoreTokens()) {
if (flagConfig.bindnotreleasehack()) boundQueryvector.bind(vecReader.getVector(bindingTokenizer.nextToken()));
else boundQueryvector.release(vecReader.getVector(bindingTokenizer.nextToken()));
}
Vector copyConceptVector = conceptVector.copy();
if (flagConfig.bindnotreleasehack()) copyConceptVector.bind(boundQueryvector);
else copyConceptVector.release(boundQueryvector);
disjunctSpace.add(copyConceptVector);
}
if (flagConfig.searchtype() != SearchType.BOUNDMINIMUM) VectorUtils.orthogonalizeVectors(disjunctSpace);
return disjunctSpace;
}
示例11: processEmbeddings
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
private void processEmbeddings(
Vector embeddingVector, ArrayList<Vector> contextVectors,
ArrayList<Integer> contextLabels, double learningRate, BLAS blas) {
double scalarProduct = 0;
double error = 0;
int counter = 0;
//for each contextVector (there should be one "true" context vector, and a number of negative samples)
for (Vector contextVec : contextVectors) {
Vector duplicateContextVec = contextVec.copy();
scalarProduct = VectorUtils.scalarProduct(embeddingVector, duplicateContextVec, flagConfig, blas);
if (Math.abs(scalarProduct) > MAX_EXP) continue; //skipping cases with outsize scalar products, as per prior implementations - may avoid numerically unstable term vectors down the line
if (!flagConfig.vectortype().equals(VectorType.BINARY)) //sigmoid function
{
scalarProduct = sigmoidTable.sigmoid(scalarProduct);
//if label == 1, a context word - so the error is the (1-predicted probability of for this word) - ideally 0
//if label == 0, a negative sample - so the error is the (predicted probability for this word) - ideally 0
error = scalarProduct - contextLabels.get(counter++);
} else //RELU-like function for binary vectors
{
scalarProduct = Math.max(scalarProduct, 0);
error = scalarProduct - contextLabels.get(counter++);
//avoid passing floating points (the default behavior currently is to ignore these if the first superposition weight is an Integer)
error = Math.round(error*100);
}
//update the context vector and embedding vector, respectively
VectorUtils.superposeInPlace(embeddingVector, contextVec, flagConfig, blas, -learningRate * error);
VectorUtils.superposeInPlace(duplicateContextVec, embeddingVector, flagConfig, blas, -learningRate * error);
}
}
示例12: NumberRepresentation
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Initializes an instance of {@link NumberRepresentation} with its start and end vectors,
* checking that these demarcator vectors are not too close together.
*
* Allows for the specification of a start and end seed, so mutually near-orthogonal sets
* of demarcator vectors can be created
*
* @param flagConfig Flag configuration, used in particular to control vectortype and dimension.
*/
public NumberRepresentation(FlagConfig flagConfig, String startSeed, String endSeed) {
if (flagConfig == null) throw new NullPointerException("flagConfig cannot be null");
//enforce probabilistic normalization for binary vectors
if (flagConfig.vectortype().equals(VectorType.BINARY))
BinaryVector.setNormalizationMethod(BinaryVector.NORMALIZE_METHOD.PROBABILISTIC);
this.startRandomSeed = startSeed;
this.endRandomSeed = endSeed;
this.flagConfig = flagConfig;
// Generate a vector for the lowest number and one for the highest and make sure they
// have no significant overlap.
Random random = new Random(Bobcat.asLong(startRandomSeed));
vL = VectorFactory.generateRandomVector(
flagConfig.vectortype(), flagConfig.dimension(), flagConfig.seedlength(), random);
vL.normalize();
random.setSeed(Bobcat.asLong(endRandomSeed));
vR = VectorFactory.generateRandomVector(
flagConfig.vectortype(), flagConfig.dimension(), flagConfig.seedlength(), random);
vR.normalize();
// Small routine to guarantee that end vector has low similarity with start vector.
ArrayList<Vector> toOrthogonalize = new ArrayList<Vector>();
toOrthogonalize.add(vL);
toOrthogonalize.add(vR);
VectorUtils.orthogonalizeVectors(toOrthogonalize);
}
示例13: getProportionVector
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Returns a vector representing the proportion of the distance between zero and one.
* @param proportion Must be in the range [0, 1].
* */
public Vector getProportionVector(double proportion) {
if (proportion < 0 || proportion > 1) {
throw new IllegalArgumentException("Proportion must be a number in the range [0, 1]. Not: " + proportion);
}
Vector proportionVector = VectorUtils.weightedSuperposition(
vectorStart, proportion, vectorEnd, 1 - proportion);
return proportionVector;
}
示例14: getDoubleValueVector
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* Returns a vector appropriate for this value by interpolating endpoints.
* Only for type {@link SupportedType#DOUBLE}.
*/
public Vector getDoubleValueVector(FlagConfig flagConfig, double value) {
if (this.getType() != SupportedType.DOUBLE) {
throw new IllegalArgumentException("Bad call to getDoubleValue.");
}
if (value < minDoubleValue || value > maxDoubleValue) {
throw new IllegalArgumentException("Value out of bounds: " + value);
}
double doubleRange = maxDoubleValue - minDoubleValue;
Vector result = VectorUtils.weightedSuperposition(minBookendVector, (value - minDoubleValue),
maxBookendVector, (maxDoubleValue - value) );
return result;
}
示例15: VectorSearcherSubspaceSim
import pitt.search.semanticvectors.vectors.VectorUtils; //导入依赖的package包/类
/**
* @param queryVecStore Vector store to use for query generation.
* @param searchVecStore The vector store to search.
* @param luceneUtils LuceneUtils object to use for query weighting. (May be null.)
* @param queryTerms Terms that will be parsed and used to generate a query subspace.
*/
public VectorSearcherSubspaceSim(VectorStore queryVecStore,
VectorStore searchVecStore,
LuceneUtils luceneUtils,
FlagConfig flagConfig,
String[] queryTerms)
throws ZeroVectorException {
super(queryVecStore, searchVecStore, luceneUtils, flagConfig);
this.disjunctSpace = new ArrayList<Vector>();
for (int i = 0; i < queryTerms.length; ++i) {
System.out.println("\t" + queryTerms[i]);
// There may be compound disjuncts, e.g., "A NOT B" as a single argument.
String[] tmpTerms = queryTerms[i].split("\\s");
Vector tmpVector = CompoundVectorBuilder.getQueryVector(
queryVecStore, luceneUtils, flagConfig, tmpTerms);
if (tmpVector != null) {
this.disjunctSpace.add(tmpVector);
}
}
if (this.disjunctSpace.size() == 0) {
throw new ZeroVectorException("No nonzero input vectors ... no results.");
}
if (!vectorType.equals(VectorType.BINARY))
VectorUtils.orthogonalizeVectors(this.disjunctSpace);
else BinaryVectorUtils.orthogonalizeVectors(this.disjunctSpace);
}