HEX
Server: Apache
System: Linux webm010.cluster103.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64
User: iestorre (46869)
PHP: 8.0.30
Disabled: _dyuweyrj4,_dyuweyrj4r,dl
Upload Files
File: /home/iestorre/moodleAW/moodle310/lib/mlbackend/php/phpml/src/Phpml/Helper/Optimizer/Optimizer.php
<?php

declare(strict_types=1);

namespace Phpml\Helper\Optimizer;

abstract class Optimizer
{
    /**
     * Unknown variables to be found
     *
     * @var array
     */
    protected $theta;

    /**
     * Number of dimensions
     *
     * @var int
     */
    protected $dimensions;

    /**
     * Inits a new instance of Optimizer for the given number of dimensions
     *
     * @param int $dimensions
     */
    public function __construct(int $dimensions)
    {
        $this->dimensions = $dimensions;

        // Inits the weights randomly
        $this->theta = [];
        for ($i = 0; $i < $this->dimensions; ++$i) {
            $this->theta[] = rand() / (float) getrandmax();
        }
    }

    /**
     * Sets the weights manually
     *
     * @param array $theta
     *
     * @return $this
     *
     * @throws \Exception
     */
    public function setInitialTheta(array $theta)
    {
        if (count($theta) != $this->dimensions) {
            throw new \Exception("Number of values in the weights array should be $this->dimensions");
        }

        $this->theta = $theta;

        return $this;
    }

    /**
     * Executes the optimization with the given samples & targets
     * and returns the weights
     *
     * @param array    $samples
     * @param array    $targets
     * @param \Closure $gradientCb
     */
    abstract protected function runOptimization(array $samples, array $targets, \Closure $gradientCb);
}