Skip to content

Commit

Permalink
toString for neural network
Browse files Browse the repository at this point in the history
  • Loading branch information
lpapailiou committed Oct 2, 2020
1 parent 12b8ce6 commit 78fe636
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ Add following snippets to your ``pom.xml`` file:
<dependency>
<groupId>neuralnetwork</groupId>
<artifactId>neural-network-repo</artifactId>
<version>1.4</version>
<version>1.5</version>
</dependency>
</dependencies>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
90ce733e2d3af43f745b6240cf6244e8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c3e01fe28a9fb81133ad0f5c68306dbe716c3b34
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8b84ce67aa39a1d7ad497ea05625ab40
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e5d1a9ed557d37e0e9829433546cecf7b7a84a91
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2ee03fdaac4b983835432e947683be66
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6dbbcebd0009036fa7ec90b98a3fbdc5ea3bb5f0
51 changes: 51 additions & 0 deletions neuralnetwork/neural-network-repo/1.5/neural-network-repo-1.5.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>neuralnetwork</groupId>
<artifactId>neural-network-repo</artifactId>
<version>1.5</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>20.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<github.repo>file:///C:/Users/Lena Papailiou/git/neuralnetwork/</github.repo>
</properties>

<distributionManagement>
<repository>
<id>neural-network-repo</id>
<url>${github.repo}</url>
</repository>
</distributionManagement>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
152df5e00b40c715e237b3dbfbc9fe04
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23033143be7679a8b3f3f685a03f438a969614a5
5 changes: 3 additions & 2 deletions neuralnetwork/neural-network-repo/maven-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
<groupId>neuralnetwork</groupId>
<artifactId>neural-network-repo</artifactId>
<versioning>
<release>1.4</release>
<release>1.5</release>
<versions>
<version>1.2</version>
<version>1.3</version>
<version>1.4</version>
<version>1.5</version>
</versions>
<lastUpdated>20201002132431</lastUpdated>
<lastUpdated>20201002202431</lastUpdated>
</versioning>
</metadata>
2 changes: 1 addition & 1 deletion neuralnetwork/neural-network-repo/maven-metadata.xml.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
00744c97490fa0d67c448d9bec1acc5c
ab1647b81c4a537e5a25503079cbdd32
2 changes: 1 addition & 1 deletion neuralnetwork/neural-network-repo/maven-metadata.xml.sha1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c67b0c7def4d13dd23de98f0f90e7cb0c0251618
fc0a14a7ab47ab06cf33af3c98a0d214d93acc21
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>neuralnetwork</groupId>
<artifactId>neural-network-repo</artifactId>
<version>1.4</version>
<version>1.5</version>
<build>
<plugins>
<plugin>
Expand All @@ -27,6 +27,12 @@
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>20.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/neuralnet/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ protected Layer clone() {
layer.bias = this.bias.clone();
return layer;
}

@Override
public String toString() {
String weight = "weight: " + this.weight.getType() + "\n" + this.weight.toString();
String bias = "bias: " + this.bias.getType() + "\n " + this.bias.toString();
return weight + "\n" + bias + "\n";
}

@Override
public int hashCode() {
return Integer.parseInt(weight.getCols() + "" + bias.getRows());
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Layer)) {
return false;
}
return this.toString().equals(o.toString());
}
}
11 changes: 8 additions & 3 deletions src/main/java/neuralnet/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Matrix dsigmoid() {
return tmp;
}

private String getType() {
String getType() {
return "(" + rows + ", " + cols + ")";
}

Expand Down Expand Up @@ -207,12 +207,17 @@ protected Matrix clone() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < rows; i++) {
sb.append("[");
for (int j = 0; j < cols; j++) {
sb.append(data[i][j] + " ");
sb.append(data[i][j] + ", ");
}
sb.append("\n");
sb.replace(sb.length()-2, sb.length(), "");
sb.append("],\n");
}
sb.replace(sb.length()-2, sb.length(), "");
sb.append("]");
return sb.toString();
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/neuralnet/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static int getMaxindex(List<Double> nodeList) {
* @param nodeList the Double list of results
* @return the standardized list, where all values will be rounded to 0 or 1
*/
public static List<Double> getStandardizedOutput(List<Double> nodeList) {
public static List<Double> getStandardizedOutputList(List<Double> nodeList) {
if (nodeList == null || nodeList.size() == 0) {
throw new IllegalArgumentException("Invalid input list!");
}
Expand All @@ -33,4 +33,5 @@ public static List<Double> getStandardizedOutput(List<Double> nodeList) {
}
return sList;
}

}
40 changes: 38 additions & 2 deletions src/main/java/neuralnet/NeuralNetwork.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package neuralnet;

import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
Expand Down Expand Up @@ -58,7 +60,9 @@ private NeuralNetwork(int inputLayerNodes, double randomizationRate, List<Layer>
* @return the predicted output nodes as Double List
*/
public List<Double> predict(double[] inputNodes) {
if (inputNodes.length != inputLayerNodes) {
if (inputNodes == null) {
throw new NullPointerException("inputNodes must not be null!");
} else if (inputNodes.length != inputLayerNodes) {
throw new IllegalArgumentException("input node count does not match neural network configuration! received " + inputNodes.length + " instead of " + inputLayerNodes + " input nodes.");
}

Expand All @@ -81,7 +85,9 @@ public List<Double> predict(double[] inputNodes) {
* @return the actual output nodes as Double List
*/
public List<Double> learn(double[] inputNodes, double[] expectedOutputNodes) {
if (inputNodes.length != inputLayerNodes) {
if (inputNodes == null || expectedOutputNodes == null) {
throw new NullPointerException("inputNodes and expectedOutputNodes are required!");
} else if (inputNodes.length != inputLayerNodes) {
throw new IllegalArgumentException("input node count does not match neural network configuration! received " + inputNodes.length + " instead of " + inputLayerNodes + " input nodes.");
}

Expand Down Expand Up @@ -125,6 +131,9 @@ public List<Double> learn(double[] inputNodes, double[] expectedOutputNodes) {
* @param rounds the count of repetitions of the batch training
*/
public void train(double[][] inputSet, double[][] expectedOutputSet, int rounds) {
if (inputSet == null || expectedOutputSet == null) {
throw new NullPointerException("inputSet and expectedOutputSet are required!");
}
for (int i = 0; i < rounds; i++) {
int sampleIndex = (int) (Math.random() * inputSet.length);
learn(inputSet[sampleIndex], expectedOutputSet[sampleIndex]);
Expand All @@ -138,6 +147,9 @@ public void train(double[][] inputSet, double[][] expectedOutputSet, int rounds)
* @return the neural network a merged with b
*/
public static NeuralNetwork merge(NeuralNetwork a, NeuralNetwork b) {
if (a == null || b == null) {
throw new NullPointerException("two NeuralNetwork instances required!");
}
for (int i = 0; i < a.layers.size(); i++) {
a.layers.get(i).weight = Matrix.merge(a.layers.get(i).weight, b.layers.get(i).weight);
a.layers.get(i).bias = Matrix.merge(a.layers.get(i).bias, b.layers.get(i).bias);
Expand Down Expand Up @@ -171,4 +183,28 @@ private void randomize(double factor) {
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("randomization rate: " + randomizationRate + "\n");
for (int i = 0; i < layers.size(); i++) {
sb.append(" ----- layer " + i + " -----\n");
sb.append(layers.get(i).toString() + "\n");
}
return sb.toString();
}

@Override
public int hashCode() {
return Integer.parseInt(randomizationRate + "" + layers.size());
}

@Override
public boolean equals(Object o) {
if (!(o instanceof NeuralNetwork)) {
return false;
}
return this.toString().equals(o.toString());
}

}
18 changes: 17 additions & 1 deletion src/test/java/neuralnet/NeuralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@

import java.util.List;

import static neuralnet.NetUtils.getStandardizedOutputList;
import static org.junit.Assert.assertEquals;

public class NeuralTest {

@Test
public void toStringTest() {
NeuralNetwork net = new NeuralNetwork(2, 15, 15, 1);
System.out.println(net.toString());
}

//@Test
public void testXOr() {
// testing xor function
double[][] in = {{0,0}, {1,0}, {0,1}, {1,1}};
double[][] out = {{0}, {1}, {1}, {0}};
NeuralNetwork net = new NeuralNetwork(2, 15, 15, 1);
net.train(in, out, 4000);
System.out.println("test: ");

//assertEquals(net.predict(in[0]), net.predict(in[0]), 0.2);
List<Double> expected = getStandardizedOutputList(net.predict(in[0]));
List<Double> actual = net.predict(in[0]);
for (int i = 0; i < expected.size(); i++) {
assertEquals(expected.get(i), actual.get(i), 0.2);
}

System.out.println("combo 1: " + net.predict(in[0]));
System.out.println("combo 2: " + net.predict(in[1]));
System.out.println("combo 3: " + net.predict(in[2]));
Expand Down

0 comments on commit 78fe636

Please sign in to comment.