Skip to content

Commit

Permalink
all done in once
Browse files Browse the repository at this point in the history
  • Loading branch information
TRextabat committed Nov 19, 2023
0 parents commit d4a5709
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
],
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m -Xlog:disable"
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Getting Started

Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.

## Folder Structure

The workspace contains two folders by default, where:

- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies

Meanwhile, the compiled output files will be generated in the `bin` folder by default.

> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management

The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
Binary file added bin/Data.class
Binary file not shown.
Binary file added bin/Main.class
Binary file not shown.
Binary file added bin/Neorun.class
Binary file not shown.
Binary file added bin/Train.class
Binary file not shown.
65 changes: 65 additions & 0 deletions src/Data.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
public class Data
{
private static double[][] valueMatrix =
{
{7.6, 11},
{8, 10},
{6.6, 8},
{8.4, 10},
{8.8, 12},
{7.2, 10},
{8.1, 11},
{9.5, 9},
{7.3, 9},
{8.9, 11},
{7.5, 11},
{7.6, 9},
{7.9, 10},
{8, 10},
{7.2, 9},
{8.8, 10},
{7.6, 11},
{7.5, 10},
{9, 10},
{7.7, 9},
{8.1, 11}
};
private static double[] targets = {77, 70, 55, 78, 95, 67, 80, 87, 60, 88, 72, 58, 70, 76, 58, 81, 74, 67, 82, 62, 82};
private static double[][] normedinput = new double[valueMatrix.length][valueMatrix[0].length];;
private static double[] normedtarget = new double[targets.length];

public static void norm()
{
normedinput = new double[valueMatrix.length][valueMatrix[0].length];
normedtarget = new double[targets.length];
for(int i=0; i<valueMatrix.length; i++)
{
normedinput[i][0] = valueMatrix[i][0]/10;
normedinput[i][1] = valueMatrix[i][1]/15;
normedtarget[i] = targets[i]/100;

}
}



public static double[][] getValueMatrix() {
return valueMatrix;
}
public static double[] getTargets() {
return targets;
}
public static double[][] getNormedinput() {
return normedinput;
}
public static double[] getNormedtarget() {
return normedtarget;
}







}
40 changes: 40 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.ArrayList;
public class Main
{

public static void main(String[] args)
{

int[] epochsArray = {10, 50, 100};
double[] rateArray = {0.05, 0.01, 0.025};
ArrayList<Double> mseArray = new ArrayList<>(9);
Data.norm();

for (int epochs : epochsArray)
{
for (double rate : rateArray )
{
System.out.println("Results for rate: " +rate+" epochs: " +epochs);
Neorun neorun = new Neorun(2, rate);
Train.trainNeuron(neorun, Data.getNormedinput(), Data.getNormedtarget(), epochs);
mseArray.add(Train.mse);

}
}

int i = 0;
System.out.println("\t"+rateArray[0] + "\t" + rateArray[1] + "\t" + rateArray[2] + "\t");
for(int epochs: epochsArray)
{
System.out.print(epochs+"\t");
for(double rate : rateArray)
{
System.out.print(String.format("%.5f", mseArray.get(i))+ "\t");
i++;
}
System.out.println();
}


}
}
55 changes: 55 additions & 0 deletions src/Neorun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class Neorun
{
private double[] weightMatrix;
private double[] valueMatrix;
private double output;
private double bias;
private double learningRate;

public Neorun(int inputCount, double learningRate)
{
this.valueMatrix = new double[inputCount];
this.weightMatrix = new double[inputCount];
// generate random values for weights at the first
for (int i = 0; i < inputCount; i++)
{
this.weightMatrix[i] = Math.random();
}
this.bias = Math.random();
this.learningRate = learningRate;

}

public void feedForward(double[] valueMatrix)
{
this.valueMatrix = valueMatrix;
double sum = 0;
for (int i=0; i < this.valueMatrix.length; i++)
{
sum += this.valueMatrix[i]*this.weightMatrix[i];

}
sum += this.bias;
this.output = sum;

}

public void backProp(double target)
{
double error = target - this.output;

for (int i = 0; i<this.weightMatrix.length; i++)
{
this.weightMatrix[i] = this.weightMatrix[i] + this.learningRate*error*this.valueMatrix[i];
}
this.bias += this.learningRate * error;
}

public double getOutput()
{
return output;
}



}
53 changes: 53 additions & 0 deletions src/Train.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

public class Train
{
public static double mse;
public static void trainNeuron(Neorun neorun,double[][] valueMatrix, double[] targets, int epochs)
{
double predictedValue;
double tSquaredError = 0.0;



// train a neuron
for(int epoch=0; epoch < epochs; epoch++)
{
for(int i=0; i < valueMatrix.length; i++)
{
neorun.feedForward(valueMatrix[i]);
neorun.backProp(targets[i]);
}
}


//get results for train data
System.out.println("Hour\t\tAttend\t\tTarget\t\tOutput");
for(int i = 0; i < targets.length; i++)
{
neorun.feedForward(valueMatrix[i]);
predictedValue = neorun.getOutput();
System.out.println(
String.format("%.2f", valueMatrix[i][0] * 15) + "\t\t" +
String.format("%.2f", valueMatrix[i][1] * 10) + "\t\t" +
String.format("%.2f", targets[i] * 100) + "\t\t" +
String.format("%.2f", predictedValue * 100)
);

//Calculate squared error
//double squaredError = Math.pow(targets[i] - predictedValue,2);
//tSquaredError += squaredError;
//make code more efficient
tSquaredError += Math.pow(targets[i] - predictedValue,2);
}
// mse: Mean Square Error
mse = tSquaredError / valueMatrix.length;
System.out.println("Mean Square Error (MSE):"+mse);



}




}

0 comments on commit d4a5709

Please sign in to comment.