classes = $classes; $this->costFn = $costFn ?? new CrossEntropy(); $this->softmax = new Softmax(); } /** * Return the width of the layer. * * @return positive-int */ public function width() : int { return max(1, count($this->classes)); } /** * Initialize the layer with the fan in from the previous layer and return * the fan out for this layer. * * @param positive-int $fanIn * @throws InvalidArgumentException * @return positive-int */ public function initialize(int $fanIn) : int { $fanOut = count($this->classes); if ($fanIn !== $fanOut) { throw new InvalidArgumentException('Fan in must be' . " equal to fan out, $fanOut expected but" . " $fanIn given."); } return $fanOut; } /** * Compute a forward pass through the layer. * * @param Matrix $input * @return Matrix */ public function forward(Matrix $input) : Matrix { $output = $this->softmax->activate($input); $this->input = $input; $this->output = $output; return $output; } /** * Compute an inferential pass through the layer. * * @param Matrix $input * @throws RuntimeException * @return Matrix */ public function infer(Matrix $input) : Matrix { return $this->softmax->activate($input); } /** * Compute the gradient and loss at the output. * * @param string[] $labels * @param Optimizer $optimizer * @throws RuntimeException * @return (Deferred|float)[] */ public function back(array $labels, Optimizer $optimizer) : array { if (!$this->input or !$this->output) { throw new RuntimeException('Must perform forward pass' . ' before backpropagating.'); } $expected = []; foreach ($this->classes as $class) { $dist = []; foreach ($labels as $label) { $dist[] = $class == $label ? 1.0 : 0.0; } $expected[] = $dist; } $expected = Matrix::quick($expected); $input = $this->input; $output = $this->output; $gradient = new Deferred([$this, 'gradient'], [$input, $output, $expected]); $loss = $this->costFn->compute($output, $expected); $this->input = $this->output = null; return [$gradient, $loss]; } /** * Calculate the gradient for the previous layer. * * @param Matrix $input * @param Matrix $output * @param Matrix $expected * @return Matrix */ public function gradient(Matrix $input, Matrix $output, Matrix $expected) : Matrix { if ($this->costFn instanceof CrossEntropy) { return $output->subtract($expected) ->divide($output->n()); } $dLoss = $this->costFn->differentiate($output, $expected) ->divide($output->n()); return $this->softmax->differentiate($input, $output) ->multiply($dLoss); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return "Multiclass (cost function: {$this->costFn})"; } }