Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a6e065

Browse files
committed
added a Clojure wrapper exampel for Java neural network code
1 parent 617a2eb commit 7a6e065

File tree

13 files changed

+81
-46
lines changed

13 files changed

+81
-46
lines changed

clojure_examples/README

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/target
2+
/lib
3+
/classes
4+
/checkouts
5+
pom.xml
6+
pom.xml.asc
7+
*.jar
8+
*.class
9+
.lein-deps-sum
10+
.lein-failures
11+
.lein-plugins
12+
.lein-repl-history
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# neural_network example backpropagation netowrk
2+
3+
This will compile the Java source files in the directory ../../src/neuralnetworks and add them to the project.
4+
5+
Run with:
6+
7+
~~~~~~~~
8+
lein test
9+
~~~~~~~~
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(defproject neural_network "0.1.0-SNAPSHOT"
2+
:description "FIXME: write description"
3+
:url "http://example.com/FIXME"
4+
:license {:name "Eclipse Public License"
5+
:url "http://www.eclipse.org/legal/epl-v10.html"}
6+
:dependencies [[org.clojure/clojure "1.5.1"]]
7+
;;:java-source [["../../src/neuralnetworks/Hopfield.java"]]
8+
:java-source-paths ["../../src/neuralnetworks"]
9+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(ns neural-network.core)
2+
3+
(defn foo
4+
"I don't do a whole lot."
5+
[x]
6+
(println x "Hello, World!"))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(ns neural-network.core-test
2+
(:use clojure.test
3+
neural-network.core))
4+
5+
(import '(neuralnetworks.Neural_2H_momentum))
6+
7+
(def nn (neuralnetworks.Neural_2H_momentum. 3 3 3 3 0.2))
8+
9+
(println nn)
10+
11+
(def in1 (float-array [0.1 0.1 0.9]))
12+
(println in1)
13+
14+
(def in2 (float-array [0.1 0.9 0.1]))
15+
(def in3 (float-array [0.9 0.1 0.1]))
16+
17+
(def out1 (float-array [0.9 0.1 0.1]))
18+
(def out2 (float-array [0.1 0.1 0.9]))
19+
(def out3 (float-array [0.1 0.9 0.1]))
20+
21+
(def test1 (float-array [0.1 0.1 0.9]))
22+
(def test2 (float-array [0.1 0.9 0.1]))
23+
(def test3 (float-array [0.9 0.1 0.1]))
24+
25+
(.addTrainingExample nn in1 out1)
26+
(.addTrainingExample nn in2 out2)
27+
(.addTrainingExample nn in3 out3)
28+
29+
(doseq [i (range 300)] (println (.train nn))) ;; train net 300 cycles
30+
31+
;; test to make sure we have learned the input patterns:
32+
33+
(println (seq (.recall nn test1)))
34+
(println (seq (.recall nn test2)))
35+
(println (seq (.recall nn test3)))
36+

clojure_examples/project.clj

Lines changed: 0 additions & 4 deletions
This file was deleted.

clojure_examples/src/clojure_examples/core.clj

Lines changed: 0 additions & 1 deletion
This file was deleted.

clojure_examples/test/clojure_examples/test/core.clj

Lines changed: 0 additions & 6 deletions
This file was deleted.

jruby_examples/README

Lines changed: 0 additions & 17 deletions
This file was deleted.

jruby_examples/lib/wrapper.rb

Whitespace-only changes.

jruby_examples/test.rb

Whitespace-only changes.

src/neuralnetworks/Neural_2H_momentum.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747

4848
public float TRAINING_RATE = 0.5f;
4949
private float alpha = 0f; // momentum scaling term that is applied to last delta weight
50-
50+
51+
// use a reasonable default momentum term (alpha):
52+
public Neural_2H_momentum(int num_in, int num_hidden1, int num_hidden2, int num_output) {
53+
this(num_in, num_hidden1, num_hidden1, num_output, 0.6f);
54+
}
55+
5156
public Neural_2H_momentum(int num_in, int num_hidden1, int num_hidden2, int num_output,
5257
float alpha) {
5358
this.alpha = alpha;
@@ -77,6 +82,8 @@ public void addTrainingExample(float[] inputs, float[] outputs) {
7782
System.out.println("addTrainingExample(): array size is wrong");
7883
return;
7984
}
85+
//System.out.println("addTrainingExample(): inputs: " + Arrays.toString(inputs));
86+
//System.out.println("addTrainingExample(): outputs: " + Arrays.toString(outputs));
8087
inputTraining.add(inputs);
8188
outputTraining.add(outputs);
8289
}
@@ -158,6 +165,7 @@ public void slightlyRandomizeWeights() {
158165
}
159166

160167
public float[] recall(float[] in) {
168+
//System.out.println("recall(" + Arrays.toString(inputs) + ")");
161169
for (int i = 0; i < numInputs; i++) inputs[i] = in[i];
162170
forwardPass();
163171
float[] ret = new float[numOutputs];

0 commit comments

Comments
 (0)