OSDN Git Service

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/simplenn/trunk@22 dd34cd95-496f...
authoru6k <u6k@dd34cd95-496f-4e97-851c-65f59ff6709a>
Mon, 21 Apr 2008 04:24:11 +0000 (04:24 +0000)
committeru6k <u6k@dd34cd95-496f-4e97-851c-65f59ff6709a>
Mon, 21 Apr 2008 04:24:11 +0000 (04:24 +0000)
simplenn/src/main/java/jp/gr/java_conf/u6k/simplenn/SimpleNN.java
simplenn/src/main/java/jp/gr/java_conf/u6k/simplenn/SimpleNNApplet.java

index 098db8e..519e29b 100644 (file)
 package jp.gr.java_conf.u6k.simplenn;\r
 \r
 import java.io.BufferedReader;\r
-import java.io.BufferedWriter;\r
-import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.InputStreamReader;\r
-import java.io.OutputStreamWriter;\r
 \r
 /**\r
  * <p>\r
@@ -84,7 +81,7 @@ public final class SimpleNN {
      * 入力層と隠れ層の間の重み係数。\r
      * </p>\r
      */\r
-    private double[][]          weightInHidden;\r
+    private double[]            weightInHidden;\r
 \r
     /**\r
      * <p>\r
@@ -98,7 +95,7 @@ public final class SimpleNN {
      * 隠れ層と出力層の間の重み係数。\r
      * </p>\r
      */\r
-    private double[][]          weightHiddenOut;\r
+    private double[]            weightHiddenOut;\r
 \r
     /**\r
      * <p>\r
@@ -151,9 +148,9 @@ public final class SimpleNN {
          * ニューラル・ネットワークの状態を初期化します。\r
          */\r
         this.thresholdHidden = new double[hiddenNumber];\r
-        this.weightInHidden = new double[inputNumber][hiddenNumber];\r
+        this.weightInHidden = new double[inputNumber * hiddenNumber];\r
         this.thresholdOut = new double[outputNumber];\r
-        this.weightHiddenOut = new double[hiddenNumber][outputNumber];\r
+        this.weightHiddenOut = new double[hiddenNumber * outputNumber];\r
         // TODO ちゃんとランダムに初期化する。\r
         try {\r
             BufferedReader r = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("random.txt")));\r
@@ -161,13 +158,13 @@ public final class SimpleNN {
                 for (int i = 0; i < hiddenNumber; i++) {\r
                     this.thresholdHidden[i] = Double.parseDouble(r.readLine()) - SimpleNN.VALUE_HALF;\r
                     for (int j = 0; j < inputNumber; j++) {\r
-                        this.weightInHidden[j][i] = Double.parseDouble(r.readLine()) - SimpleNN.VALUE_HALF;\r
+                        this.weightInHidden[j * this.hiddenNumber + i] = Double.parseDouble(r.readLine()) - SimpleNN.VALUE_HALF;\r
                     }\r
                 }\r
                 for (int i = 0; i < outputNumber; i++) {\r
                     this.thresholdOut[i] = Double.parseDouble(r.readLine()) - SimpleNN.VALUE_HALF;\r
                     for (int j = 0; j < hiddenNumber; j++) {\r
-                        this.weightHiddenOut[j][i] = Double.parseDouble(r.readLine()) - SimpleNN.VALUE_HALF;\r
+                        this.weightHiddenOut[j * this.outputNumber + i] = Double.parseDouble(r.readLine()) - SimpleNN.VALUE_HALF;\r
                     }\r
                 }\r
             } finally {\r
@@ -293,7 +290,7 @@ public final class SimpleNN {
         for (int i = 0; i < hiddenOutput.length; i++) {\r
             hiddenOutput[i] = -this.thresholdHidden[i];\r
             for (int j = 0; j < input.length; j++) {\r
-                hiddenOutput[i] += input[j] * this.weightInHidden[j][i];\r
+                hiddenOutput[i] += input[j] * this.weightInHidden[j * this.hiddenNumber + i];\r
             }\r
             hiddenOutput[i] = this.sigmoid(hiddenOutput[i]);\r
         }\r
@@ -304,7 +301,7 @@ public final class SimpleNN {
         for (int i = 0; i < output.length; i++) {\r
             output[i] = -this.thresholdOut[i];\r
             for (int j = 0; j < hiddenOutput.length; j++) {\r
-                output[i] += hiddenOutput[j] * this.weightHiddenOut[j][i];\r
+                output[i] += hiddenOutput[j] * this.weightHiddenOut[j * this.outputNumber + i];\r
             }\r
             output[i] = this.sigmoid(output[i]);\r
         }\r
@@ -372,7 +369,7 @@ public final class SimpleNN {
         for (int i = 0; i < hiddenError.length; i++) {\r
             double err = 0;\r
             for (int j = 0; j < output.length; j++) {\r
-                err += outputError[j] * this.weightHiddenOut[i][j];\r
+                err += outputError[j] * this.weightHiddenOut[i * this.outputNumber + j];\r
             }\r
             hiddenError[i] = hiddenOutput[i] * (1.0 - hiddenOutput[i]) * err;\r
         }\r
@@ -382,12 +379,12 @@ public final class SimpleNN {
          */\r
         for (int i = 0; i < outputError.length; i++) {\r
             for (int j = 0; j < hiddenOutput.length; j++) {\r
-                this.weightHiddenOut[j][i] += this.learningCoefficient * outputError[i] * hiddenOutput[j];\r
+                this.weightHiddenOut[j * this.outputNumber + i] += this.learningCoefficient * outputError[i] * hiddenOutput[j];\r
             }\r
         }\r
         for (int i = 0; i < hiddenError.length; i++) {\r
             for (int j = 0; j < input.length; j++) {\r
-                this.weightInHidden[j][i] += this.learningCoefficient * hiddenError[i] * input[j];\r
+                this.weightInHidden[j * this.hiddenNumber + i] += this.learningCoefficient * hiddenError[i] * input[j];\r
             }\r
         }\r
 \r
@@ -450,40 +447,4 @@ public final class SimpleNN {
         return 1.0 / (1.0 + Math.exp(-x));\r
     }\r
 \r
-    /**\r
-     * <p>\r
-     * インスタンスの状態を「C:/result.txt」に出力します。\r
-     * </p>\r
-     * \r
-     * @throws IOException\r
-     *             出力に失敗した場合。\r
-     */\r
-    public void outputState() throws IOException {\r
-        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/result.txt")));\r
-        try {\r
-            for (int i = 0; i < this.weightInHidden.length; i++) {\r
-                for (int j = 0; j < this.weightInHidden[i].length; j++) {\r
-                    w.write("weightInHidden[" + i + "][" + j + "]=" + this.weightInHidden[i][j]);\r
-                    w.newLine();\r
-                }\r
-            }\r
-            for (int i = 0; i < this.thresholdHidden.length; i++) {\r
-                w.write("thresholdHidden[" + i + "]=" + this.thresholdHidden[i]);\r
-                w.newLine();\r
-            }\r
-            for (int i = 0; i < this.weightHiddenOut.length; i++) {\r
-                for (int j = 0; j < this.weightHiddenOut[i].length; j++) {\r
-                    w.write("weightHiddenOut[" + i + "][" + j + "]=" + this.weightHiddenOut[i][j]);\r
-                    w.newLine();\r
-                }\r
-            }\r
-            for (int i = 0; i < this.thresholdOut.length; i++) {\r
-                w.write("thresholdOut[" + i + "]=" + this.thresholdOut[i]);\r
-                w.newLine();\r
-            }\r
-        } finally {\r
-            w.close();\r
-        }\r
-    }\r
-\r
 }\r
index dfce56e..60d86aa 100644 (file)
@@ -429,11 +429,12 @@ public final class SimpleNNApplet extends Applet implements MouseListener, Mouse
         if (ae.getSource() == this.button5) {\r
             // 「状態出力」\r
             System.out.println("状態出力開始。");\r
-            try {\r
-                this.simpleNN.outputState();\r
-            } catch (IOException e) {\r
-                e.printStackTrace();\r
-            }\r
+            // TODO\r
+            // try {\r
+            // this.simpleNN.outputState();\r
+            // } catch (IOException e) {\r
+            // e.printStackTrace();\r
+            // }\r
             System.out.println("状態出力完了。");\r
         }\r
     }\r