/** * Validate a network for training. * * @param ContainsFlat $network * The network to validate. * @param MLDataSet $training * The training set to validate. */ function validateNetworkForTraining(ContainsFlat $network, MLDataSet $training) { $inputCount = $network->getFlat()->getInputCount(); $outputCount = $network->getFlat()->getOutputCount(); if ($inputCount != $training->getInputSize()) { throw new NeuralNetworkError("The input layer size of " + $inputCount + " must match the training input size of " + $training->getInputSize() + "."); } if ($training->getIdealSize() > 0 && $outputCount != $training->getIdealSize()) { throw new NeuralNetworkError("The output layer size of " + $outputCount + " must match the training input size of " + $training->getIdealSize() + "."); } }
/** * Copy whatever dataset type is specified into a memory dataset. * * @param * MLDataSet set * The dataset to copy. */ public function copy(MLDataSet $set) { $inputCount = $set->getInputSize(); $idealCount = $set->getIdealSize(); foreach ($set as $pair) { $input = null; $ideal = null; if ($inputCount > 0) { $input = new BasicMLData($inputCount); EngineArray\arrayCopy($pair->getInputArray(), $input->getData()); } if ($idealCount > 0) { $ideal = new BasicMLData($idealCount); EngineArray\arrayCopy($pair->getIdealArray(), $ideal->getData()); } $this->add(new BasicMLDataPair($input, $ideal)); } }
/** * Calculate the error for this neural network. * The error is calculated * using root-mean-square(RMS). * * @param * MLDataSet data * The training set. * @return double The error percentage. */ public function calculateError(MLDataSet $data) { $errorCalculation = new ErrorCalculation(); $actual = array(); $pair = BasicMLDataPair::createPair($data->getInputSize(), $data->getIdealSize()); for ($i = 0; $i < $data->getRecordCount(); ++$i) { $data->getRecord($i, $pair); $this->compute($pair->getInputArray(), $actual); $errorCalculation->updateError($actual, $pair->getIdealArray(), $pair->getSignificance()); } return $errorCalculation->calculate(); }