Java Code Examples for org.neuroph.nnet.MultiLayerPerceptron#calculate()

The following examples show how to use org.neuroph.nnet.MultiLayerPerceptron#calculate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MomentumBackPropagationTest.java    From NeurophFramework with Apache License 2.0 7 votes vote down vote up
@Test
public void testXorMSE() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

    myMlPerceptron.setLearningRule(instance);
    myMlPerceptron.learn(xorDataSet);

    MeanSquaredError mse = new MeanSquaredError();
    for (DataSetRow testSetRow : xorDataSet.getRows()) {
        myMlPerceptron.setInput(testSetRow.getInput());
        myMlPerceptron.calculate();
        double[] networkOutput = myMlPerceptron.getOutput();
        mse.addPatternError(networkOutput, testSetRow.getDesiredOutput());
    }
    assertTrue(mse.getTotalError() < maxError);
}
 
Example 2
Source File: BackPropagationTest.java    From NeurophFramework with Apache License 2.0 7 votes vote down vote up
@Test
public void testXorMSE() {
    MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
    myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

    myMlPerceptron.setLearningRule(instance);
    myMlPerceptron.learn(xorDataSet);

    MeanSquaredError mse = new MeanSquaredError();
    for (DataSetRow testSetRow : xorDataSet.getRows()) {
        myMlPerceptron.setInput(testSetRow.getInput());
        myMlPerceptron.calculate();
        double[] networkOutput = myMlPerceptron.getOutput();
        mse.addPatternError(networkOutput, testSetRow.getDesiredOutput());
    }
    assertTrue(mse.getTotalError() < maxError);
}
 
Example 3
Source File: MlpNetworkTrainer.java    From developerWorks with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the specified network using the double array as input data. This data should
 * be normalized or things will get real weird, real quick.
 * 
 * @param network
 *          The MLP network to be run
 * @param input
 *          The normalized input data to use to run the network
 * @return double[] - The network's "answers" from running the network. For the networks
 *         CTV'd with input data created by DataCreator, this means two doubles, whose range is
 *         between 0.0 and 1.0:
 *         <ol>
 *         <li>ret[0] - The probability the Home team wins (Away team loses)</li>
 *         <li>ret[1] - The probability the Home team loses (Away team wins)</li>
 *         </ol>
 */
protected double[] runNetwork(MultiLayerPerceptron network, double[] input) {
  double[] ret;
  network.setInput(input);
  network.calculate();
  // Return value is the network's output
  ret = network.getOutput();
  if (log.isTraceEnabled()) {
    StringBuilder sb = new StringBuilder();
    sb.append("Comparison: Input to Output:\n");
    sb.append("Input : ");
    sb.append(Arrays.toString(input));
    sb.append('\n');
    sb.append("Output: ");
    sb.append(Arrays.toString(ret));
    log.trace(sb.toString());
  }
  if (log.isTraceEnabled()) {
    log.trace("Network Input : " + Arrays.toString(input));
    log.trace("Network Output: " + Arrays.toString(ret));
  }
  return ret;
}
 
Example 4
Source File: AutoTrainer.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
private void testNeuralNetwork(MultiLayerPerceptron neuralNet, DataSet testSet) {
    // not implemented
    for (DataSetRow testSetRow : testSet.getRows()) {
        neuralNet.setInput(testSetRow.getInput());
        neuralNet.calculate();
    }
}
 
Example 5
Source File: JDBCSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Runs this sample
 */    
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {

    // create neural network
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(2, 3, 1);

    // Load the database driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    // Get a connection to the database
    String dbName = "neuroph";
    String dbUser = "root";
    String dbPass = "";
    // create a connection to database
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUser, dbPass);

    // ise this sql to get input from database table
    String inputSql = "SELECT * FROM input_data";        
    // create dinput adapter using specidfied database connection and sql query
    JDBCInputAdapter in = new JDBCInputAdapter(connection, inputSql);
    String outputTable = "output_data"; // write output to this table
    // create output adapter using specified connection and output table
    JDBCOutputAdapter out = new JDBCOutputAdapter(connection, outputTable);

    
    double[] input;
    // read input using input adapter
    while ((input = in.readInput()) != null) {
        neuralNet.setInput(input);
        neuralNet.calculate();
        double[] output = neuralNet.getOutput();
        // and write output using output aadapter
        out.writeOutput(output);
    }

    in.close();
    out.close();
    connection.close();
}
 
Example 6
Source File: FileIOSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Runs this sample
 */    
public static void main(String[] args) throws FileNotFoundException, IOException {
    
    // create neural network
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(2, 3, 1); 
    
    // use file provided in org.neuroph.sample.data package
    String inputFileName = FileIOSample.class.getResource("data/xor_data.txt").getFile();
    // create file input adapter using specifed file
    FileInputAdapter fileIn = new FileInputAdapter(inputFileName);
    // create file output  adapter using specified file name
    FileOutputAdapter fileOut = new FileOutputAdapter("some_output_file.txt");
          
    
    double[] input; // input buffer used for reading network input from file
    // read network input using input adapter
    while( (input = fileIn.readInput()) != null) {
        // feed neywork with input    
        neuralNet.setInput(input);
        // calculate network ...
        neuralNet.calculate();  
        // .. and get network output
        double[] output = neuralNet.getOutput();
        // write network output using output adapter
        fileOut.writeOutput(output);
    }
    
    // close input and output files
    fileIn.close();
    fileOut.close();     
    
    // Also note that shorter way for this is using org.neuroph.util.io.IOHelper class
}