+1 (315) 557-6473 

Program To Estimate Weights on Nut Training Data Using Java Programming Language Assignment Solutions.


Instructions

Objective
Write a java assignment to estimate weights on nut training data using Java programming language.

Requirements and Specifications

  1.  Programming
A sample data set for nut quantity, sugar level and health rating is provided in “Assignment2- TrainingData.csv”, containing 25 cereal records. Your program will train the network to estimate the weights (or constants) A, B and C, based on the training data, and then applies the estimated weights to the nut quantity and sugar level in the file “Assignment2-TestingData.csv” to compute the unknown health rating.You can use the following class descriptions as a guideline for your design.
  1. Neuron class
  2. This is to store the number of inputs, output, weight, bias, output before activation, and the activation function. You can use the class used in your tutorials and modify as necessary. You should also modify the toString() method to display output properly.

  3.  Neural Exception class
  4. The purpose of this class is to generate appropriate messages for different exceptions handled.

  5. NeuralNetTest Main class
  6. This class will create objects using the above classes and execute the application creating the required number of generations and displaying the output, error, and overall error.

  7. Math classes
You can use the IAActivation function, Linear, and RandomGenerator classes used in your tutorials.
Source Code
NEURAL NET TEST
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
public class NeuralNetTest {
    public static final String Training_Data_Filename = "training.csv";
    public static final String Testing_Data_Filename = "testing.csv";
    public static final double Learning_Rate = 0.001; //Learning rate for Delta Rule
    public static final int Number_Of_Iteration = 500; //Number of Iteration
    public static void main(String[] args) {
        Collection trainingData = parseTrainingData();
        Collection testingData = parseTestingData();
        Neuron neuron = new Neuron(2, Learning_Rate);
        neuron.calc();
        System.out.println("Basic State: ");
        System.out.println(neuron);
        System.out.println();
        for (int i = 0; i < Number_Of_Iteration; i++) {
            double error = 0;
            for (TrainingData dataRow : trainingData) {
                double[] neuralInput = {dataRow.nutQuantity, dataRow.sugarLevel};
                neuron.setInputs(neuralInput);
                neuron.learn(dataRow.healthRating);
                error += neuron.calcError(dataRow.healthRating);
            }
            System.out.println("After iteration " + (i+1) + ": ");
            System.out.println("Total error: " + error);
            System.out.println(neuron);
        }
        System.out.println("Apply the estimated weight on the test set:");
        System.out.printf("%-15s%-15s%-15s%n", "Nut_Quantity", "Sugar Level", "Health Rating");
        for (TestingData dataRow : testingData) {
            double[] neuralInput = {dataRow.nutQuantity, dataRow.sugarLevel};
            neuron.setInputs(neuralInput);
            neuron.calc();
            double healthRating = neuron.getOutput();
            System.out.printf("%-15s%-15s%-15s%n", String.format("%.0f", dataRow.nutQuantity), String.format("%.0f", dataRow.sugarLevel), String.format("%.1f", healthRating));
        }
    }
    private static Collection parseTrainingData() {
        try(Scanner scanner = new Scanner(new File(Training_Data_Filename))) {
            Collection result = new ArrayList<>();
            scanner.nextLine();
            while(scanner.hasNextLine()) {
                String line = scanner.nextLine();
                line = line.replace(',', '.');
                String[] parts = line.split(";");
                result.add(new TrainingData(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
            }
            return result;
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static Collection parseTestingData() {
        try(Scanner scanner = new Scanner(new File(Testing_Data_Filename))) {
            Collection result = new ArrayList<>();
            scanner.nextLine();
            while(scanner.hasNextLine()) {
                String line = scanner.nextLine();
                line = line.replace(',', '.');
                String[] parts = line.split(";");
                result.add(new TestingData(Double.parseDouble(parts[1]), Double.parseDouble(parts[2])));
            }
            return result;
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static class TrainingData {
        private final double nutQuantity;
        private final double sugarLevel;
        private final double healthRating;
        public TrainingData(double nutQuantity, double sugarLevel, double healthRating) {
            this.nutQuantity = nutQuantity;
            this.sugarLevel = sugarLevel;
            this.healthRating = healthRating;
        }
    }
    private static class TestingData {
        private final double nutQuantity;
        private final double sugarLevel;
        public TestingData(double nutQuantity, double sugarLevel) {
            this.nutQuantity = nutQuantity;
            this.sugarLevel = sugarLevel;
        }
    }
}
NEURON
import mathClasses.IActivationFunction;
import mathClasses.Linear;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;
public class Neuron {
    private final int numberOfInputs;
    private final double[] weight;
    private final double[] input;
    private final IActivationFunction activationFunction;
    private final double bias;
    private final double delta;
    private double output;
    private double outputBeforeActivation;
    public Neuron(int numberOfInputs, double delta) {
        if (numberOfInputs <= 0) {
            throw new NeuralException("Number of inputs is not positive");
        }
        this.numberOfInputs = numberOfInputs;
        this.delta = delta;
        weight = new double[numberOfInputs + 1];
        input = new double[numberOfInputs];
        activationFunction = new Linear( 1.0 );
        bias = 1.0;
        init();
    }
    private void init() {
        Random random = new Random();
        if (numberOfInputs > 0) {
            for (int i = 0; i <= numberOfInputs; i++) {
                weight[i] = random.nextDouble();
            }
        }
    }
    public void calc() {
        outputBeforeActivation = 0.0;
        if (input != null && weight != null) {
            for (int i = 0; i <= numberOfInputs; i++) {
                outputBeforeActivation += (i == numberOfInputs ? bias : input[i]) * weight[i];
            }
        }
        output = activationFunction.calc(outputBeforeActivation);
    }
    public double calcError(double target) {
        calc();
        return 0.5 * (output - target)*(output - target);
    }
    public void learn(double target) {
        double errorRecord = Double.POSITIVE_INFINITY;
        double deltaRecord = 0;
        int weightRecord = -1;
        for(int i = 0; i <= numberOfInputs; i++) {
            double oldWeight = weight[i];
            for (int sign = -1; sign < 2; sign += 2) {
                double signedDelta = sign * delta;
                weight[i] += signedDelta;
                double deltaError = calcError(target);
                if (errorRecord > deltaError) {
                    errorRecord = deltaError;
                    deltaRecord = signedDelta;
                    weightRecord = i;
                }
                weight[i] = oldWeight;
            }
        }
        weight[weightRecord] += deltaRecord;
    }
    public void setInputs(double[] values) {
        if (values.length == numberOfInputs) {
            System.arraycopy(values, 0, input, 0, numberOfInputs);
        }
        else {
            throw new NeuralException("Invalid number of inputs");
        }
    }
    public double getOutput() {
        return output;
    }
    @Override
    public String toString() {
        return "Neuron Inputs\n" +
                Arrays.stream(input).mapToObj(d -> Double.valueOf(d).toString()).collect(Collectors.joining(" ")) +
                "\n" +
                "Neuron Weights\n" +
                Arrays.stream(weight).mapToObj(d -> Double.valueOf(d).toString()).collect(Collectors.joining(" ")) +
                "\n" +
                String.format("S Output before activation: %f%n", outputBeforeActivation) +
                "Activation function:\n" +
                String.format(" g(s) Neuron Output: %f%n", output);
    }
}