本文整理汇总了Java中org.nd4j.linalg.ops.transforms.Transforms.cosineSim方法的典型用法代码示例。如果您正苦于以下问题:Java Transforms.cosineSim方法的具体用法?Java Transforms.cosineSim怎么用?Java Transforms.cosineSim使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.ops.transforms.Transforms
的用法示例。
在下文中一共展示了Transforms.cosineSim方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeSimilarity
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public double computeSimilarity(Concept c1, Concept c2) {
if (c1.name.toLowerCase().equals(c2.name.toLowerCase()))
return 1;
if (wordVectors == null) {
this.loadWordVectors(type, dimension);
int[] shape = wordVectors.lookupTable().getWeights().shape();
System.out.println("word embeddings loaded, " + shape[0] + " " + shape[1]);
}
INDArray cVector1 = this.getConceptVector(c1);
INDArray cVector2 = this.getConceptVector(c2);
if (cVector1 == null || cVector2 == null)
return Double.NaN;
double dist = Transforms.cosineSim(cVector1, cVector2);
if (Double.isNaN(dist))
System.err.println("Embedding NaN");
return dist;
}
示例2: getSimilarity
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
public double getSimilarity(String sentence1, String sentence2){
double predictedScore = 0;
if (PARAGRAPHVECS != null) {
try {
INDArray inferredVectorA = produceParagraphVectorOfGivenSentence(sentence1);
INDArray inferredVectorB = produceParagraphVectorOfGivenSentence(sentence2);
predictedScore = Transforms.cosineSim(inferredVectorA, inferredVectorB);
} catch (Exception e) {
logger.error("No word is matched with the given sentence and any sentence in training set - model file. " + sentence1
+ ";" + sentence2);
System.out.println("No word is matched with the given sentence and any sentence in training set - model file. " + sentence1
+ ";" + sentence2);
StringMetric metric = StringMetrics.qGramsDistance();
predictedScore = metric.compare(sentence1, sentence2);
}
}
return predictedScore;
}
示例3: nearestLabels
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* This method returns top N labels nearest to specified features vector
*
* @param labelVector a vector for the source label
* @param topN no. of nearest labels to find
* @return a {@code Collection} of the nearest labels to the given document
*/
public Collection<String> nearestLabels(INDArray labelVector, int topN) {
if (labelsMatrix == null || labelsList == null || labelsList.isEmpty()) {
extractLabels();
}
List<BasicModelUtils.WordSimilarity> result = new ArrayList<>();
// if list still empty - return empty collection
if (labelsMatrix == null || labelsList == null || labelsList.isEmpty()) {
log.warn("Labels list is empty!");
return new ArrayList<>();
}
if (!normalizedLabels) {
synchronized (this) {
if (!normalizedLabels) {
labelsMatrix.diviColumnVector(labelsMatrix.norm1(1));
normalizedLabels = true;
}
}
}
INDArray similarity = Transforms.unitVec(labelVector).mmul(labelsMatrix.transpose());
List<Double> highToLowSimList = getTopN(similarity, topN + 20);
for (Double aHighToLowSimList : highToLowSimList) {
String word = labelsList.get(aHighToLowSimList.intValue()).getLabel();
if (word != null && !word.equals("UNK") && !word.equals("STOP")) {
INDArray otherVec = lookupTable.vector(word);
double sim = Transforms.cosineSim(labelVector, otherVec);
result.add(new BasicModelUtils.WordSimilarity(word, sim));
}
}
result.sort(new BasicModelUtils.SimilarityComparator());
return BasicModelUtils.getLabels(result, topN);
}
示例4: getScores
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* This method accepts vector, that represents any document,
* and returns distances between this document, and previously trained categories
* @return
*/
public List<Pair<String, Double>> getScores(@NonNull INDArray vector) {
List<Pair<String, Double>> result = new ArrayList<>();
for (String label: labelsUsed) {
INDArray vecLabel = lookupTable.vector(label);
if (vecLabel == null) throw new IllegalStateException("Label '"+ label+"' has no known vector!");
double sim = Transforms.cosineSim(vector, vecLabel);
result.add(new Pair<String, Double>(label, sim));
}
return result;
}
示例5: map
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public List<Writable> map(List<Writable> writables) {
int idxFirst = inputSchema.getIndexOfColumn(firstCol);
int idxSecond = inputSchema.getIndexOfColumn(secondCol);
INDArray arr1 = ((NDArrayWritable) writables.get(idxFirst)).get();
INDArray arr2 = ((NDArrayWritable) writables.get(idxSecond)).get();
double d;
switch (distance) {
case COSINE:
d = Transforms.cosineSim(arr1, arr2);
break;
case EUCLIDEAN:
d = Transforms.euclideanDistance(arr1, arr2);
break;
case MANHATTAN:
d = Transforms.manhattanDistance(arr1, arr2);
break;
default:
throw new UnsupportedOperationException("Unknown or not supported distance metric: " + distance);
}
List<Writable> out = new ArrayList<>(writables.size() + 1);
out.addAll(writables);
out.add(new DoubleWritable(d));
return out;
}
示例6: testCosineSim
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testCosineSim() {
INDArray vec1 = Nd4j.create(new double[]{1,2,3,4});
INDArray vec2 = Nd4j.create(new double[]{1,2,3,4});
double sim = Transforms.cosineSim(vec1,vec2);
assertEquals(1,sim,1e-1);
INDArray vec3 = Nd4j.create(new float[]{0.2f,0.3f,0.4f,0.5f});
INDArray vec4 = Nd4j.create(new float[]{0.6f,0.7f,0.8f,0.9f});
sim = Transforms.cosineSim(vec3,vec4);
assertEquals(0.98,sim,1e-1);
}
示例7: getScores
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* This method accepts vector, that represents any document,
* and returns distances between this document, and previously trained categories
*
* @return
*/
@Nonnull
public List<Pair<String, Double>> getScores(@Nonnull INDArray vector) {
List<Pair<String, Double>> result = new ArrayList<>();
for (String label : labelsUsed) {
INDArray vecLabel = lookupTable.vector(label);
if (vecLabel == null) throw new IllegalStateException("Label '" + label + "' has no known vector!");
double sim = Transforms.cosineSim(vector, vecLabel);
result.add(new Pair<>(label, sim));
}
return result;
}
示例8: testReduce3_2
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Ignore
@Test
public void testReduce3_2() throws Exception {
INDArray array1 = Nd4j.create(new float[]{2.01f, 2.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f});
INDArray array2 = Nd4j.create(new float[]{1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f});
double similarity = Transforms.cosineSim(array1, array2);
System.out.println("Cosine similarity: " + similarity);
assertEquals(0.95f, similarity, 0.01f);
}
示例9: testPinnedCosineSim
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Norm2 + cuBlas dot call
*
* @throws Exception
*/
@Test
public void testPinnedCosineSim() throws Exception {
// simple way to stop test if we're not on CUDA backend here
INDArray array1 = Nd4j.create(new float[]{2.01f, 2.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f});
INDArray array2 = Nd4j.create(new float[]{1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f});
double similarity = Transforms.cosineSim(array1, array2);
System.out.println("Cosine similarity: " + similarity);
assertEquals(0.95f, similarity, 0.01f);
}
示例10: testCosineSimilarity
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testCosineSimilarity() {
INDArray vec1 = Nd4j.create(new float[] {1, 2, 3, 4, 5});
INDArray vec2 = Nd4j.create(new float[] {1, 2, 3, 4, 5});
double sim = Transforms.cosineSim(vec1, vec2);
assertEquals(getFailureMessage(), 1, sim, 1e-1);
}
示例11: testCosineSimilarity
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testCosineSimilarity() {
INDArray vec1 = Nd4j.create(new float[] {1, 2, 3, 4, 5});
INDArray vec2 = Nd4j.create(new float[] {1, 2, 3, 4, 5});
double sim = Transforms.cosineSim(vec1, vec2);
assertEquals(getFailureMessage(), 1, sim, 1e-1);
}
示例12: testCosineSim
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testCosineSim() {
INDArray vec1 = Nd4j.create(new double[] {1, 2, 3, 4});
INDArray vec2 = Nd4j.create(new double[] {1, 2, 3, 4});
double sim = Transforms.cosineSim(vec1, vec2);
assertEquals(getFailureMessage(), 1, sim, 1e-1);
INDArray vec3 = Nd4j.create(new float[] {0.2f, 0.3f, 0.4f, 0.5f});
INDArray vec4 = Nd4j.create(new float[] {0.6f, 0.7f, 0.8f, 0.9f});
sim = Transforms.cosineSim(vec3, vec4);
assertEquals(0.98, sim, 1e-1);
}
示例13: testCosineSim
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testCosineSim() {
INDArray vec1 = Nd4j.create(new double[] {1, 2, 3, 4});
INDArray vec2 = Nd4j.create(new double[] {1, 2, 3, 4});
double sim = Transforms.cosineSim(vec1, vec2);
assertEquals(getFailureMessage(), 1, sim, 1e-1);
INDArray vec3 = Nd4j.create(new float[] {0.2f, 0.3f, 0.4f, 0.5f});
INDArray vec4 = Nd4j.create(new float[] {0.6f, 0.7f, 0.8f, 0.9f});
sim = Transforms.cosineSim(vec3, vec4);
assertEquals(getFailureMessage(), 0.98, sim, 1e-1);
}
示例14: computeSimilarity
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
public double computeSimilarity(Object[] synsetRepresentation, String[] windowWords, int[] synset2WordIndex, int k, int j){
INDArray[] windowWordsSenseEmbeddings = (INDArray[])synsetRepresentation;
return Transforms.cosineSim(windowWordsSenseEmbeddings[k], windowWordsSenseEmbeddings[j]);
}
示例15: apply
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public Float apply(INDArray input) {
return (float) Transforms.cosineSim(input, base);
}