本文整理汇总了Java中org.nd4j.linalg.ops.transforms.Transforms.euclideanDistance方法的典型用法代码示例。如果您正苦于以下问题:Java Transforms.euclideanDistance方法的具体用法?Java Transforms.euclideanDistance怎么用?Java Transforms.euclideanDistance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.ops.transforms.Transforms
的用法示例。
在下文中一共展示了Transforms.euclideanDistance方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: testAllDistances1
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testAllDistances1() throws Exception {
INDArray initialX = Nd4j.create(5, 10);
INDArray initialY = Nd4j.create(7, 10);
for (int i = 0; i < initialX.rows(); i++) {
initialX.getRow(i).assign(i + 1);
}
for (int i = 0; i < initialY.rows(); i++) {
initialY.getRow(i).assign(i + 101);
}
INDArray result = Transforms.allEuclideanDistances(initialX, initialY, 1);
Nd4j.getExecutioner().commit();
assertEquals(5 * 7, result.length());
for (int x = 0; x < initialX.rows(); x++) {
INDArray rowX = initialX.getRow(x).dup();
for (int y = 0; y < initialY.rows(); y++) {
double res = result.getDouble(x, y);
double exp = Transforms.euclideanDistance(rowX, initialY.getRow(y).dup());
assertEquals("Failed for [" + x + ", " + y + "]", exp, res, 0.001);
}
}
}
示例3: testAllDistances3_Large
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testAllDistances3_Large() throws Exception {
INDArray initialX = Nd4j.create(5, 2000);
INDArray initialY = Nd4j.create(7, 2000);
for (int i = 0; i < initialX.rows(); i++) {
initialX.getRow(i).assign(i + 1);
}
for (int i = 0; i < initialY.rows(); i++) {
initialY.getRow(i).assign(i + 101);
}
INDArray result = Transforms.allEuclideanDistances(initialX, initialY, 1);
assertEquals(5 * 7, result.length());
for (int x = 0; x < initialX.rows(); x++) {
INDArray rowX = initialX.getRow(x).dup();
for (int y = 0; y < initialY.rows(); y++) {
double res = result.getDouble(x, y);
double exp = Transforms.euclideanDistance(rowX, initialY.getRow(y).dup());
assertEquals("Failed for [" + x + ", " + y + "]", exp, res, 0.001);
}
}
}
示例4: testAllDistances3_Large_Columns
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testAllDistances3_Large_Columns() throws Exception {
INDArray initialX = Nd4j.create(2000, 5);
INDArray initialY = Nd4j.create(2000, 7);
for (int i = 0; i < initialX.columns(); i++) {
initialX.getColumn(i).assign(i + 1);
}
for (int i = 0; i < initialY.columns(); i++) {
initialY.getColumn(i).assign(i + 101);
}
INDArray result = Transforms.allEuclideanDistances(initialX, initialY, 0);
assertEquals(5 * 7, result.length());
for (int x = 0; x < initialX.columns(); x++) {
INDArray colX = initialX.getColumn(x).dup();
for (int y = 0; y < initialY.columns(); y++) {
double res = result.getDouble(x, y);
double exp = Transforms.euclideanDistance(colX, initialY.getColumn(y).dup());
assertEquals("Failed for [" + x + ", " + y + "]", exp, res, 0.001);
}
}
}