/** * Construct an ADALINE trainer. * * @param BasicNetwork network * The network to train. * @param MLDataSet training * The training data. * @param double learningRate * The learning rate. */ public function __construct(BasicNetwork $network, MLDataSet $training, $learningRate) { parent::__construct(TrainingImplementationType::Iterative); if ($network->getLayerCount() > 2) { throw new NeuralNetworkError("An ADALINE network only has two layers."); } $this->network = $network; $this->training = $training; $this->learningRate = $learningRate; }
/** * Generate the network. * * @return MLMethod The generated network. */ public function generate() { $network = new BasicNetwork(); $inputLayer = new BasicLayer(new ActivationLinear(), true, $this->inputNeurons); $outputLayer = new BasicLayer(new ActivationLinear(), false, $this->outputNeurons); $network->addLayer($inputLayer); $network->addLayer($outputLayer); $network->getStructure()->finalizeStructure(); (new RangeRandomizer(-0.5, 0.5))->randomizeMLMethod($network); return $network; }
/** * Randomize one level of a neural network. * * @param * BasicNetwork network * The network to randomize * @param * int fromLayer * The from level to randomize. */ public function randomizeNetwork(BasicNetwork $network, $fromLayer) { $fromCount = $network->getLayerTotalNeuronCount($fromLayer); $toCount = $network->getLayerNeuronCount($fromLayer + 1); for ($fromNeuron = 0; $fromNeuron < $fromCount; ++$fromNeuron) { for ($toNeuron = 0; $toNeuron < $toCount; ++$toNeuron) { $v = $network->getWeight($fromLayer, $fromNeuron, $toNeuron); $v = $this->randomizeDouble($v); $network->setWeight($fromLayer, $fromNeuron, $toNeuron, $v); } } }