use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class RPropParameterUpdate method sum.
/**
* Sums updates returned by different trainings.
*
* @param updates Updates.
* @return Sum of updates during returned by different trainings.
*/
public static RPropParameterUpdate sum(List<RPropParameterUpdate> updates) {
Vector totalUpdate = updates.stream().filter(Objects::nonNull).map(pu -> VectorUtils.elementWiseTimes(pu.updatesMask().copy(), pu.prevIterationUpdates())).reduce(Vector::plus).orElse(null);
Vector totalDelta = updates.stream().filter(Objects::nonNull).map(RPropParameterUpdate::deltas).reduce(Vector::plus).orElse(null);
Vector totalGradient = updates.stream().filter(Objects::nonNull).map(RPropParameterUpdate::prevIterationGradient).reduce(Vector::plus).orElse(null);
if (totalUpdate != null)
return new RPropParameterUpdate(totalUpdate, totalGradient, totalDelta, new DenseLocalOnHeapVector(Objects.requireNonNull(totalDelta).size()).assign(1.0));
return null;
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class AbstractMatrix method getCol.
/**
* {@inheritDoc}
*/
@Override
public Vector getCol(int col) {
checkColumnIndex(col);
Vector res;
if (isDistributed())
res = MatrixUtil.likeVector(this, rowSize());
else
res = new DenseLocalOnHeapVector(rowSize());
for (int i = 0; i < rowSize(); i++) res.setX(i, getX(i, col));
return res;
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class Blas method syr.
/**
* A := alpha * x * x^T + A.
*
* @param alpha a real scalar that will be multiplied to x * x^T^.
* @param x the vector x that contains the n elements.
* @param a the symmetric matrix A. Size of n x n.
*/
void syr(Double alpha, Vector x, DenseLocalOnHeapMatrix a) {
int mA = a.rowSize();
int nA = a.columnSize();
if (mA != nA)
throw new NonSquareMatrixException(mA, nA);
if (mA != x.size())
throw new CardinalityException(x.size(), mA);
// TODO: IGNITE-5535, Process DenseLocalOffHeapVector
if (x instanceof DenseLocalOnHeapVector)
syr(alpha, x, a);
else if (x instanceof SparseLocalVector)
syr(alpha, x, a);
else
throw new IllegalArgumentException("Operation 'syr' does not support vector [class=" + x.getClass().getName() + "].");
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method test2DLineClustering.
/**
* Test FCM on points that forms the line located on the plane.
*/
@Test
public void test2DLineClustering() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 50, null);
double[][] points = new double[][] { { 1, 2 }, { 3, 6 }, { 5, 10 } };
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
int k = 2;
FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
Vector[] centers = mdl.centers();
Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
Vector[] exp = { new DenseLocalOnHeapVector(new double[] { 1.5, 3 }), new DenseLocalOnHeapVector(new double[] { 4.5, 9 }) };
for (int i = 0; i < k; i++) {
Vector center = centers[i];
for (int j = 0; j < 2; j++) assertEquals(exp[i].getX(j), center.getX(j), 0.5);
}
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class KMeansDistributedClustererTestMultiNode method testClusterizationOnDatasetWithObviousStructure.
/**
*/
public void testClusterizationOnDatasetWithObviousStructure() throws IOException {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
int ptsCnt = 10000;
int squareSideLen = 10000;
Random rnd = new Random(123456L);
// Let centers be in the vertices of square.
Map<Integer, Vector> centers = new HashMap<>();
centers.put(100, new DenseLocalOnHeapVector(new double[] { 0.0, 0.0 }));
centers.put(900, new DenseLocalOnHeapVector(new double[] { squareSideLen, 0.0 }));
centers.put(3000, new DenseLocalOnHeapVector(new double[] { 0.0, squareSideLen }));
centers.put(6000, new DenseLocalOnHeapVector(new double[] { squareSideLen, squareSideLen }));
SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
List<Integer> permutation = IntStream.range(0, ptsCnt).boxed().collect(Collectors.toList());
Collections.shuffle(permutation, rnd);
int totalCnt = 0;
for (Integer count : centers.keySet()) {
for (int i = 0; i < count; i++) {
Vector pnt = new DenseLocalOnHeapVector(2).assign(centers.get(count));
// Perturbate point on random value.
pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
points.assignRow(permutation.get(totalCnt), pnt);
totalCnt++;
}
}
EuclideanDistance dist = new EuclideanDistance();
KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
clusterer.cluster(points, 4);
points.destroy();
}
Aggregations