step method

  1. @override
void step()
override

Performs a single optimization step using an efficient NAG update rule.

Implementation

@override
void step() {
  for (Tensor param in parameters) {
    dynamic v = _v[param]!;
    if (param.value is Vector) {
      Vector valVec = param.value as Vector;
      Vector gradVec = param.grad as Vector;
      Vector vVec = v as Vector;
      for (int i = 0; i < valVec.length; i++) {
        double vNew = momentum * vVec[i] + gradVec[i];
        vVec[i] = vNew;
        valVec[i] -= learningRate * (gradVec[i] + momentum * vNew);
      }
    } else if (param.value is Matrix) {
      Matrix valMat = param.value as Matrix;
      Matrix gradMat = param.grad as Matrix;
      Matrix vMat = v as Matrix;
      for (int r = 0; r < valMat.length; r++) {
        for (int c = 0; c < valMat[0].length; c++) {
          double vNew = momentum * vMat[r][c] + gradMat[r][c];
          vMat[r][c] = vNew;
          valMat[r][c] -= learningRate * (gradMat[r][c] + momentum * vNew);
        }
      }
    }
  }
}