Java Code Examples for org.neuroph.util.TransferFunctionType#TANH

The following examples show how to use org.neuroph.util.TransferFunctionType#TANH . 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: TestTimeSeries.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
public void train() {
    // get the path to file with data
    String inputFileName = "C:\\timeseries\\BSW15";
    
    // create MultiLayerPerceptron neural network
    neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, 5, 10, 1);
    MomentumBackpropagation learningRule = (MomentumBackpropagation)neuralNet.getLearningRule();
    learningRule.setLearningRate(0.2);
    learningRule.setMomentum(0.5);
    // learningRule.addObserver(this);
    learningRule.addListener(this);        
    
    // create training set from file
     trainingSet = DataSet.createFromFile(inputFileName, 5, 1, "\t", false);
    // train the network with training set
    neuralNet.learn(trainingSet);         
          
    System.out.println("Done training.");          
}
 
Example 2
Source File: TestMatrixMLP.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
/**
 * Create and run MLP with XOR training set
 */
public static void main(String[] args) {
    // create training set (logical XOR function)
    DataSet trainingSet = new DataSet(2, 1);
    trainingSet.add(new DataSetRow(new double[]{0, 0}, new double[]{0}));
    trainingSet.add(new DataSetRow(new double[]{0, 1}, new double[]{1}));
    trainingSet.add(new DataSetRow(new double[]{1, 0}, new double[]{1}));
    trainingSet.add(new DataSetRow(new double[]{1, 1}, new double[]{0}));

    MultiLayerPerceptron nnet = new MultiLayerPerceptron( TransferFunctionType.TANH ,2, 3, 1);
    MatrixMultiLayerPerceptron mnet = new MatrixMultiLayerPerceptron(nnet);

    System.out.println("Training network...");

    mnet.learn(trainingSet);

    System.out.println("Done training network.");
}
 
Example 3
Source File: IrisFlowers.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
public void run() throws InterruptedException, ExecutionException {
    System.out.println("Creating training set...");
    // get path to training set
    String dataSetFile = "data_sets/iris_data_normalised.txt";
    int inputsCount = 4;
    int outputsCount = 3;

    // create training set from file
    DataSet dataSet = DataSet.createFromFile(dataSetFile, inputsCount, outputsCount, ",");
 //   dataSet.setColumnNames(new String[]{"sepal.length", "sepal.width", "petal.length", "petal.width",  "setosa", "versicolor", "virginica"});
    dataSet.setColumnNames(new String[]{"setosa", "versicolor", "virginica"});
    dataSet.shuffle();

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 5, outputsCount);

    String[] classLabels = new String[]{"setosa", "versicolor", "virginica"};
    neuralNet.setOutputLabels(classLabels);

    KFoldCrossValidation crossVal = new KFoldCrossValidation(neuralNet, dataSet, 5);
    EvaluationResult totalResult= crossVal.run();
    List<FoldResult> cflist= crossVal.getResultsByFolds();

}
 
Example 4
Source File: TestBinaryClass.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        DataSet trainingSet = new DataSet(2, 1);
        trainingSet.add(new DataSetRow(new double[]{0, 0}, new double[]{0}));
        trainingSet.add(new DataSetRow(new double[]{0, 1}, new double[]{1}));
        trainingSet.add(new DataSetRow(new double[]{1, 0}, new double[]{1}));
        trainingSet.add(new DataSetRow(new double[]{1, 1}, new double[]{0}));

        MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, 2, 3, 1);
        neuralNet.learn(trainingSet);

        Evaluation.runFullEvaluation(neuralNet, trainingSet);
    }
 
Example 5
Source File: IrisFlowers.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating data set...");
    String dataSetFile = "data_sets/ml10standard/irisdatanormalised.txt";
    int inputsCount = 4;
    int outputsCount = 3;

    // create data set from file
    DataSet dataSet = DataSet.createFromFile(dataSetFile, inputsCount, outputsCount, ",");

    // split data into training and test set
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 2, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener((event)->{
        MomentumBackpropagation bp = (MomentumBackpropagation) event.getSource();
        System.out.println(bp.getCurrentIteration() + ". iteration | Total network error: " + bp.getTotalNetworkError());
    });

    // set learning rate and max error
    learningRule.setLearningRate(0.2);
    learningRule.setMaxError(0.03);
    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");
}
 
Example 6
Source File: Banknote.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating data set...");
    String dataSetFile = "data_sets/ml10standard/databanknote.txt";
    int inputsCount = 4;
    int outputsCount = 1;

    // create training set from file
    DataSet dataSet = DataSet.createFromFile(dataSetFile, inputsCount, outputsCount, ",", false);
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    Normalizer norm = new MaxNormalizer(trainingSet);
    norm.normalize(trainingSet);
    norm.normalize(testSet);

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 1, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener((event) -> {
        MomentumBackpropagation bp = (MomentumBackpropagation) event.getSource();
        System.out.println(bp.getCurrentIteration() + ". iteration | Total network error: " + bp.getTotalNetworkError());
    });

    // set learning rate and max error
    learningRule.setLearningRate(0.1);
    learningRule.setMaxError(0.01);
    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");
}
 
Example 7
Source File: BostonHousePrice.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating data set...");
    String dataSetFile = "data_sets/ml10standard/bostonhouse.txt";
    int inputsCount = 13;
    int outputsCount = 1;

    // create data set from file
    DataSet dataSet = DataSet.createFromFile(dataSetFile, inputsCount, outputsCount, ",");

    // split data into training and test set
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    // normalize data
    Normalizer norm = new MaxNormalizer(trainingSet);
    norm.normalize(trainingSet);
    norm.normalize(testSet);

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 2, 2, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener(event -> {
        MomentumBackpropagation bp = (MomentumBackpropagation) event.getSource();
        System.out.println(bp.getCurrentIteration() + ". iteration | Total network error: " + bp.getTotalNetworkError());
    });

    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");

}
 
Example 8
Source File: Sonar.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    String dataSetFile = "data_sets/ml10standard/sonardata.txt";
    int numInputs = 60;
    int numOutputs = 1;

    // create data set from csv file
    DataSet dataSet = DataSet.createFromFile(dataSetFile, numInputs, numOutputs, ",");

    // split data into train and test set
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    // create neural network
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, numInputs, 35, 15, numOutputs);

    // set learning rule and add listener
    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener((event) -> {
        MomentumBackpropagation bp = (MomentumBackpropagation) event.getSource();
        System.out.println(bp.getCurrentIteration() + ". iteration | Total network error: " + bp.getTotalNetworkError());
    });

    // set learning rate and max error
    learningRule.setLearningRate(0.01);
    learningRule.setMaxError(0.01);

    // train the network with training set
    neuralNet.learn(trainingSet);

    // evaluate network performance on test set
    evaluate(neuralNet, testSet);

    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");

    //testNeuralNetwork(neuralNet, testSet);
}
 
Example 9
Source File: PimaIndiansDiabetes.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating data set...");
    String dataSetFile = "data_sets/ml10standard/pimadata.txt";
    int inputsCount = 8;
    int outputsCount = 1;

    // create data set from file
    DataSet dataSet = DataSet.createFromFile(dataSetFile, inputsCount, outputsCount, "\t", false);

    // split data into training and test set
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    // normalize training and test set
    Normalizer norm = new MaxNormalizer(trainingSet);
    norm.normalize(trainingSet);
    norm.normalize(testSet);

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 15, 5, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener((event) -> {
        MomentumBackpropagation bp = (MomentumBackpropagation) event.getSource();
        System.out.println(bp.getCurrentIteration() + ". iteration | Total network error: " + bp.getTotalNetworkError());
    });

    // set learning rate and max error
    learningRule.setLearningRate(0.1);
    learningRule.setMaxError(0.03);
    
    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");
}
 
Example 10
Source File: IrisFlowers.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating training set...");
    // get path to training set
    String trainingSetFileName = "data_sets/irisdatanormalised.txt";
    int inputsCount = 4;
    int outputsCount = 3;

    // create training set from file
    DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, ",");

    // splid data into training and test set
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 2, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener(this);

    // set learning rate and max error
    learningRule.setLearningRate(0.2);
    learningRule.setMaxError(0.03);
    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");

    System.out.println();
    System.out.println("Network outputs for test set");
    testNeuralNetwork(neuralNet, testSet);
}
 
Example 11
Source File: Banknote.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating training set...");
    // get path to training set
    String trainingSetFileName = "data_sets/databanknote.txt";
    int inputsCount = 4;
    int outputsCount = 1;

    // create training set from file
    DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, ",", false);
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    Normalizer norm = new MaxNormalizer(trainingSet);
    norm.normalize(trainingSet);
    norm.normalize(testSet);

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 1, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener(this);

    // set learning rate and max error
    learningRule.setLearningRate(0.1);
    learningRule.setMaxError(0.01);
    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");

    System.out.println();
    System.out.println("Network outputs for test set");
    testNeuralNetwork(neuralNet, testSet);
}
 
Example 12
Source File: BostonHousePrice.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating training set...");
    // get path to training set
    String trainingSetFileName = "data_sets/bostonhouse.txt";
    int inputsCount = 13;
    int outputsCount = 1;

    // create training set from file
    DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, ",");
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    // normalize data
    Normalizer norm = new MaxNormalizer(trainingSet);
    norm.normalize(trainingSet);
    norm.normalize(testSet);
    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 2, 2, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener(this);

    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");

    System.out.println();
    System.out.println("Network outputs for test set");
    testNeuralNetwork(neuralNet, testSet);
}
 
Example 13
Source File: PimaIndiansDiabetes.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
public void run() {
    System.out.println("Creating training set...");
    // get path to training set
    String trainingSetFileName = "data_sets/pimadata.txt";
    int inputsCount = 8;
    int outputsCount = 1;

    // create training set from file
    DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, "\t", false);

    // split data into training and test set
    DataSet[] trainTestSplit = dataSet.split(0.6, 0.4);
    DataSet trainingSet = trainTestSplit[0];
    DataSet testSet = trainTestSplit[1];

    // normalize training and test set
    Normalizer norm = new MaxNormalizer(trainingSet);
    norm.normalize(trainingSet);
    norm.normalize(testSet);

    System.out.println("Creating neural network...");
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(TransferFunctionType.TANH, inputsCount, 15, 5, outputsCount);

    neuralNet.setLearningRule(new MomentumBackpropagation());
    MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
    learningRule.addListener(this);

    // set learning rate and max error
    learningRule.setLearningRate(0.1);
    learningRule.setMaxError(0.03);
    System.out.println("Training network...");
    // train the network with training set
    neuralNet.learn(trainingSet);
    System.out.println("Training completed.");
    System.out.println("Testing network...");

    System.out.println("Network performance on the test set");
    evaluate(neuralNet, testSet);

    System.out.println("Saving network");
    // save neural network to file
    neuralNet.save("nn1.nnet");

    System.out.println("Done.");

    System.out.println();
    System.out.println("Network outputs for test set");
    testNeuralNetwork(neuralNet, testSet);
}
 
Example 14
Source File: NetworkcreatorController.java    From FakeImageDetection with GNU General Public License v3.0 4 votes vote down vote up
@FXML
private void saveNeuralNet(ActionEvent event) {
    String neuralNetLbl = neuralNetLabel.getText();
    Dimension samplingDimension = new Dimension(Integer.parseInt(width.getText()),
            Integer.parseInt(height.getText()));
    ColorMode mode;
    switch (colorMode.getSelectionModel().getSelectedItem()) {
        case "COLOR_RGB":
            mode = ColorMode.COLOR_RGB;
            break;
        case "COLOR_HSL":
            mode = ColorMode.COLOR_HSL;
            break;
        case "BLACK_AND_WHITE":
            mode = ColorMode.COLOR_RGB;
            break;
        default:
            mode = ColorMode.COLOR_RGB;
            break;
    }
    TransferFunctionType tFunction;
    switch (transferFunction.getSelectionModel().getSelectedItem()) {
        case "LINEAR":
            tFunction = TransferFunctionType.LINEAR;
            break;
        case "RAMP":
            tFunction = TransferFunctionType.RAMP;
            break;
        case "STEP":
            tFunction = TransferFunctionType.STEP;
            break;
        case "SIGMOID":
            tFunction = TransferFunctionType.SIGMOID;
            break;
        case "TANH":
            tFunction = TransferFunctionType.TANH;
            break;
        case "GAUSSIAN":
            tFunction = TransferFunctionType.GAUSSIAN;
            break;
        case "TRAPEZOID":
            tFunction = TransferFunctionType.TRAPEZOID;
            break;
        case "SGN":
            tFunction = TransferFunctionType.SGN;
            break;
        case "SIN":
            tFunction = TransferFunctionType.SIN;
            break;
        case "LOG":
            tFunction = TransferFunctionType.LOG;
            break;
        default:
            tFunction = TransferFunctionType.GAUSSIAN;
            break;
    }

    ArrayList<String> neuronLabels = new ArrayList(Arrays.asList(neuronLabelList.getText().split("[,]")));
    ArrayList<Integer> neuronCounts = new ArrayList();

    for (String neuronCount : neuronCountList.getText().split("[,]")) {
        neuronCounts.add(Integer.parseInt(neuronCount.replaceAll(" ", "")));
        System.out.println("neuronCounts = " + neuronCount);
    }

    //Show File save dialog
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Save Neural Network");
    File file = fileChooser.showSaveDialog(rootPane.getScene().getWindow());
    if (file == null) {
        Calert.showAlert("Not a valid File", "Select target again", Alert.AlertType.ERROR);
        return;
    }
    MLPNetworkMaker maker = new MLPNetworkMaker(neuralNetLbl, samplingDimension, mode, neuronLabels, neuronCounts, tFunction, file.getAbsolutePath());
    maker.setListener(this);
    Thread nnetCreator = new Thread(maker);
    nnetCreator.start();
    loadingSpinner.setVisible(true);
}
 
Example 15
Source File: ExampleNetworXOR.java    From NeurophFramework with Apache License 2.0 3 votes vote down vote up
/**
 * Returns multilayer percepton learned to approximate the XOR gate.
 * @return
 */
public static MultiLayerPerceptron getNetwork() {

	MultiLayerPerceptron mlPerceptron = new MultiLayerPerceptron(TransferFunctionType.TANH, 2, 3, 1);


	mlPerceptron.learn( getTrainingData() );

	return mlPerceptron;
}