1<?php 2 3namespace Rubix\ML\NeuralNet\Initializers; 4 5use Tensor\Matrix; 6use Rubix\ML\Exceptions\InvalidArgumentException; 7 8/** 9 * Constant 10 * 11 * Initialize the parameter to a user specified constant value. 12 * 13 * @category Machine Learning 14 * @package Rubix/ML 15 * @author Andrew DalPino 16 */ 17class Constant implements Initializer 18{ 19 /** 20 * The value to initialize the parameter to. 21 * 22 * @var float 23 */ 24 protected $value; 25 26 /** 27 * @param float $value 28 * @throws \Rubix\ML\Exceptions\InvalidArgumentException 29 */ 30 public function __construct(float $value = 0.0) 31 { 32 if (is_nan($value)) { 33 throw new InvalidArgumentException('Cannot initialize' 34 . ' weight values to NaN.'); 35 } 36 37 $this->value = $value; 38 } 39 40 /** 41 * Initialize a weight matrix W in the dimensions fan in x fan out. 42 * 43 * @internal 44 * 45 * @param int $fanIn 46 * @param int $fanOut 47 * @return \Tensor\Matrix 48 */ 49 public function initialize(int $fanIn, int $fanOut) : Matrix 50 { 51 return Matrix::fill($this->value, $fanOut, $fanIn); 52 } 53 54 /** 55 * Return the string representation of the object. 56 * 57 * @return string 58 */ 59 public function __toString() : string 60 { 61 return "Constant (value: {$this->value})"; 62 } 63} 64