rate = $rate; } /** * Warm the parameter cache. * * @internal * * @param Parameter $param * @throws RuntimeException */ public function warm(Parameter $param) : void { $class = get_class($param->param()); if ($class === false) { throw new RuntimeException('Could not locate parameter class.'); } $this->cache[$param->id()] = $class::zeros(...$param->param()->shape()); } /** * Take a step of gradient descent for a given parameter. * * @internal * * @param Parameter $param * @param Tensor $gradient * @return Tensor */ public function step(Parameter $param, Tensor $gradient) : Tensor { $norm = $this->cache[$param->id()]; $norm = $norm->add($gradient->square()); $this->cache[$param->id()] = $norm; return $gradient->multiply($this->rate) ->divide($norm->sqrt()->clipLower(EPSILON)); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return "AdaGrad (rate: {$this->rate})"; } }