= 1.0) { throw new InvalidArgumentException('Decay must be between' . " 0 and 1, $decay given."); } $this->rate = $rate; $this->decay = $decay; $this->rho = 1.0 - $decay; } /** * 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->multiply($this->rho) ->add($gradient->square()->multiply($this->decay)); $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 "RMS Prop (rate: {$this->rate}, decay: {$this->decay})"; } }