use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class RecommendationDatasetData method calculateGradient.
/**
* Calculates gradient of the loss function of recommendation system SGD training. The details about gradient
* calculation could be found here: https://tinyurl.com/y6cku9hr.
*
* @param objMatrix Object matrix obtained as a result of factorization of rating matrix.
* @param subjMatrix Subject matrix obtained as a result of factorization of rating matrix.
* @param batchSize Batch size of stochastic gradient descent. The size of a dataset used on each step of SGD.
* @param seed Seed (required to make randomized part behaviour repeatable).
* @param regParam Regularization parameter.
* @param learningRate Learning rate.
* @return Gradient of matrix factorization loss function.
*/
public MatrixFactorizationGradient<O, S> calculateGradient(Map<O, Vector> objMatrix, Map<S, Vector> subjMatrix, int batchSize, int seed, double regParam, double learningRate) {
Map<O, Vector> objGrads = new HashMap<>();
Map<S, Vector> subjGrads = new HashMap<>();
int[] rows = getRows(batchSize, seed);
for (int row : rows) {
ObjectSubjectRatingTriplet<O, S> triplet = ratings.get(row);
Vector objVector = objMatrix.get(triplet.getObj());
Vector subjVector = subjMatrix.get(triplet.getSubj());
double error = calculateError(objVector, subjVector, triplet.getRating());
Vector objGrad = (subjVector.times(error).plus(objVector.times(regParam))).times(learningRate);
Vector subjGrad = (objVector.times(error).plus(subjVector.times(regParam))).times(learningRate);
objGrads.put(triplet.getObj(), objGrad);
subjGrads.put(triplet.getSubj(), subjGrad);
}
return new MatrixFactorizationGradient<>(objGrads, subjGrads, rows.length);
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class Deltas method getStateVector.
/**
* @param mdl Model.
* @return vector of model weights with intercept.
*/
private Vector getStateVector(SVMLinearClassificationModel mdl) {
double intercept = mdl.intercept();
Vector weights = mdl.weights();
int stateVectorSize = weights.size() + 1;
Vector res = weights.isDense() ? new DenseVector(stateVectorSize) : new SparseVector(stateVectorSize);
res.set(0, intercept);
weights.nonZeroes().forEach(ith -> res.set(ith.index(), ith.get()));
return res;
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class Deltas method makeVectorWithInterceptElement.
/**
*/
private Vector makeVectorWithInterceptElement(LabeledVector row) {
Vector vec = row.features().like(row.features().size() + 1);
// set intercept element
vec.set(0, 1);
for (int j = 0; j < row.features().size(); j++) vec.set(j + 1, row.features().get(j));
return vec;
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class Deltas method calcDeltas.
/**
*/
private Deltas calcDeltas(double lb, Vector v, double alpha, double gradient, int vectorSize, int amountOfObservation) {
if (gradient != 0.0) {
double qii = v.dot(v);
double newAlpha = calcNewAlpha(alpha, gradient, qii);
Vector deltaWeights = v.times(lb * (newAlpha - alpha) / (this.getLambda() * amountOfObservation));
return new Deltas(newAlpha - alpha, deltaWeights);
} else
return new Deltas(0.0, initializeWeightsWithZeros(vectorSize));
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class Deltas method getDeltas.
/**
*/
private Deltas getDeltas(LabeledVectorSet data, Vector copiedWeights, int amountOfObservation, Vector tmpAlphas, int randomIdx) {
LabeledVector row = (LabeledVector) data.getRow(randomIdx);
Double lb = (Double) row.label();
Vector v = makeVectorWithInterceptElement(row);
double alpha = tmpAlphas.get(randomIdx);
return maximize(lb, v, alpha, copiedWeights, amountOfObservation);
}
Aggregations