Test Failed
Pull Request — master (#63)
by
unknown
02:47
created

Optimizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A setInitialTheta() 0 10 2
runOptimization() 0 1 ?
1
<?php declare(strict_types=1);
2
3
namespace Phpml\Helper\Optimizer;
4
5
abstract class Optimizer
6
{
7
    /**
8
     * Unknown variables to be found
9
     *
10
     * @var array
11
     */
12
    protected $theta;
13
14
    /**
15
     * Number of dimensions
16
     *
17
     * @var int
18
     */
19
    protected $dimensions;
20
21
    /**
22
     * Inits a new instance of Optimizer for the given number of dimensions
23
     *
24
     * @param int $dimensions
25
     */
26
    public function __construct(int $dimensions)
27
    {
28
        $this->dimensions = $dimensions;
29
30
        // Inits the weights randomly
31
        $this->theta = [];
32
        for ($i=0; $i < $this->dimensions; $i++) {
33
            $this->theta[] = rand() / (float) getrandmax();
34
        }
35
    }
36
37
    /**
38
     * Sets the weights manually
39
     *
40
     * @param array $theta
41
     */
42
    public function setInitialTheta(array $theta)
43
    {
44
        if (count($theta) != $this->dimensions) {
45
            throw new \Exception("Number of values in the weights array should be $this->dimensions");
46
        }
47
48
        $this->theta = $theta;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Executes the optimization with the given samples & targets
55
     * and returns the weights
56
     *
57
     */
58
    abstract protected function runOptimization(array $samples, array $targets, \Closure $gradientCb);
59
}
60