Methoden Code-Verbesserung

schemil053

Mitglied
Mein Code:
https://pastebin.com/HhWvDukM

Ich habe vor, ein Neuronales Netz in Java zu entwickeln. Schön und gut.
Mein Problem:
Immer wenn ich etwas am Netzwerk verändere, muss ich alle Zahlen neu Tippen. Gibt es eine Möglichkeit das zu ändern?

"neurons" ist eine Liste mit Neuronen,
die Compute-Funktion in einem "Neuron" schluckt sechs doubles und gibt ein double zurück.

Danke für jede Hilfe, um den Code zu verbessern.
Am besten wäre es, wenn ich sage: 4 Layer - dann wird der Code abgegangen.

LG
 
Bei sowas muss man sich eine vernünftige Datenstruktur überlegen um die Daten zu verwalten, die man durchgehen muss.
Und dann baust Du die Instanzen entsprechend auf z.B. durch einen Algorithmus oder Du liest Daten aus einem speziellen Format ein.

Ich verstehe die Logik da in Deinem Code nicht wirklich, aber Du hast ja immer einen Aufruf mit Subaufrufen. Also einfach ein Ausschnitt:
Java:
neurons.get(252).compute(
                        neurons.get(216).compute(
                                neurons.get(0).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(1).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(2).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(3).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(4).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(5).compute(input1, input2, input3, input4, input5, input6)
                        ),
                        neurons.get(217).compute(
                                neurons.get(6).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(7).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(8).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(9).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(10).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(11).compute(input1, input2, input3, input4, input5, input6)
                                ),
                        neurons.get(218).compute(
                                neurons.get(12).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(13).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(14).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(15).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(16).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(17).compute(input1, input2, input3, input4, input5, input6)
                                ),
                        neurons.get(219).compute(
                                neurons.get(18).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(19).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(20).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(21).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(22).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(23).compute(input1, input2, input3, input4, input5, input6)
                        ),
                        neurons.get(220).compute(
                                neurons.get(24).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(25).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(26).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(27).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(28).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(29).compute(input1, input2, input3, input4, input5, input6)
                        ),
                        neurons.get(221).compute(
                                neurons.get(30).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(31).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(32).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(33).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(34).compute(input1, input2, input3, input4, input5, input6),
                                neurons.get(35).compute(input1, input2, input3, input4, input5, input6)
                        )
                ),

Du hast Neuronen, die verbunden sind. Neuron 252 hat eine Verbindung zu 216, 217, 218, 219.
216 hat eine Verbindung zu 1, 2, 3, 4, 5

Die Verbindung kann entweder eine Verbindung zu einem Neuron sein, oder eine Eingabe (input1 - input6)

Das kann man entweder Objektorientiert abbilden oder man hat zwei Strukturen und wenn keine Children da sind, dann werden die Inputs genommen .... Bleiben wir mal bei dem Objektorientierten Ansatz. Dann wäre das etwas, das ein Interface implementiert, das diese compute Methode verlangt (Ohne parameter - die Paremeter sind ja dann in der Klasse als Instanzvariablen hinterlegt!).

Ein Neuron macht dann das compute mit dem child-Neuronen. Compute bei einem input Wert wäre dann einfach nur die Rückgabe des Wertes.

Mit so einem Aufbau bleibt dann nur noch der Aufruf compute() beim ersten Node übrig.

Das wäre so ein einfacher Ansatz, den Code, den Du hast, kürzer zu schreiben.

Natürlich musst Du die Datenstruktur noch irgendwie aufbauen. Du hast da die input Werte, dann erzeugst Du die Neuronen alle. Aber das kann man auch ggf. aus einer Datei generieren, so dass Du nur das Layout in einem einfachen Textfile erarbeiten musst.

Das wäre dann z.B. etwas wie
252 : 216
252 : 217
252 : 218
252 : 219
216 : 1
216 : 2
216 : 3
....

Dann kann man die Neuronen erzeugen und in z.B. eine Map speichern. Bei 252 : 216 würde dann geschaut, ob es die Neuronen schon gibt. Wenn nicht werden diese erzeugt. Dann wird 216 als Child in 252 hinzu gefügt.

Oder als eine Alternative: Evtl. kannst Du das auch einfach beschreiben mit einem Algorithmus. Dann weisst Du: Du startest mit einem Neuron an und jedes Neuron bekommt 5 childs. Und das bis zu einer Tiefe x. Und die Namen sind dann auch eine Formel, die Du aufstellen kannst.

Das alles nur einfach als ein paar einfach dahin geworfene Ideen. WIchtig ist halt, dass Du einen Objektorientierten Ansatz verfolgst und dann hast Du Neuronen-Instanzen und die Interagieren. Da brauchst Du nicht die Struktur so in Code schreiben!
 

Zurück
Oben