/** * Calculates the AUC measurment. * * @param algorithm The optimisation algorithm with a NNTrainingProblem. * @return A Vector with the AUC for each NN output. */ @Override public Vector getValue(Algorithm algorithm) { Vector solution = (Vector) algorithm.getBestSolution().getPosition(); NNTrainingProblem problem = (NNTrainingProblem) algorithm.getOptimisationProblem(); StandardPatternDataTable generalisationSet = problem.getGeneralisationSet(); NeuralNetwork neuralNetwork = problem.getNeuralNetwork(); neuralNetwork.setWeights(solution); // Arrange outputs and target values into ArrayLists. ArrayList<ArrayList<Real>> targets = new ArrayList<ArrayList<Real>>(); ArrayList<ArrayList<Real>> outputs = new ArrayList<ArrayList<Real>>(); // case of multiple outputs if (generalisationSet.getRow(0).getTarget() instanceof Vector) { int size = ((Vector) generalisationSet.getRow(0).getTarget()).size(); for (int i = 0; i < size; ++i) { targets.add(new ArrayList<Real>()); outputs.add(new ArrayList<Real>()); } for (StandardPattern pattern : generalisationSet) { Vector target = (Vector) pattern.getTarget(); Vector output = neuralNetwork.evaluatePattern(pattern); for (int curOutput = 0; curOutput < target.size(); ++curOutput) { targets.get(curOutput).add((Real) target.get(curOutput)); outputs.get(curOutput).add((Real) output.get(curOutput)); } } } // case of single output else { targets.add(new ArrayList<Real>()); outputs.add(new ArrayList<Real>()); for (StandardPattern pattern : generalisationSet) { Real target = (Real) pattern.getTarget(); Vector output = neuralNetwork.evaluatePattern(pattern); targets.get(0).add(target); outputs.get(0).add((Real) output.get(0)); } } // Calculate the Vector of AUC values Vector results = Vector.of(); for (int curOutput = 0; curOutput < outputs.size(); ++curOutput) { results.add(Real.valueOf(areaUnderCurve(targets.get(curOutput), outputs.get(curOutput)))); } return results; }
/** * Reinitialises all the output weights of a cascade network within a PSO. * * <p>{@inheritDoc} */ @Override public void performReaction(E algorithm) { NNDataTrainingProblem problem = (NNDataTrainingProblem) algorithm.getOptimisationProblem(); NeuralNetwork network = problem.getNeuralNetwork(); int precedingLayersSize = network.getArchitecture().getArchitectureBuilder().getLayerConfigurations().get(0).getSize() + network .getArchitecture() .getArchitectureBuilder() .getLayerConfigurations() .get(1) .getSize(); if (network.getArchitecture().getArchitectureBuilder().getLayerConfigurations().get(0).isBias()) precedingLayersSize++; int outputLayerSize = network .getArchitecture() .getArchitectureBuilder() .getLayerConfigurations() .get(2) .getSize(); int nrOfweightsToDo = precedingLayersSize * outputLayerSize; Topology<? extends Entity> entities = algorithm.getTopology(); for (Entity entity : entities) { DynamicParticle particle = (DynamicParticle) entity; Vector position = (Vector) particle.getPosition(); Vector velocity = (Vector) particle.getVelocity(); for (int curElement = position.size() - nrOfweightsToDo; curElement < position.size(); ++curElement) { ((Real) position.get(curElement)).randomize(randomGenerator); velocity.setReal(curElement, 0.0); } } }
@Test public void testBuildArchitecture() { NeuralNetwork network = new NeuralNetwork(); network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(5)); network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(3)); network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(3, false)); network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(2)); network.getArchitecture().getArchitectureBuilder().getLayerBuilder().setDomain("R(-3,3)"); network.initialize(); // assert num layers Assert.assertEquals(4, network.getArchitecture().getNumLayers()); // assert fully connected Assert.assertEquals(36, network.getWeights().size()); // assert biasses Assert.assertEquals(true, network.getArchitecture().getLayers().get(0).isBias()); int layerSize = network.getArchitecture().getLayers().get(0).size(); Assert.assertEquals(6, layerSize); Assert.assertEquals( -1, network.getArchitecture().getLayers().get(0).getNeuralInput(layerSize - 1), Maths.EPSILON); Assert.assertEquals(true, network.getArchitecture().getLayers().get(1).isBias()); layerSize = network.getArchitecture().getLayers().get(1).size(); Assert.assertEquals(4, layerSize); Assert.assertEquals( -1, network.getArchitecture().getLayers().get(1).getNeuralInput(layerSize - 1), Maths.EPSILON); Assert.assertEquals(false, network.getArchitecture().getLayers().get(2).isBias()); layerSize = network.getArchitecture().getLayers().get(2).size(); Assert.assertEquals(3, layerSize); Assert.assertEquals(false, network.getArchitecture().getLayers().get(3).isBias()); layerSize = network.getArchitecture().getLayers().get(3).size(); Assert.assertEquals(2, layerSize); }