Ponieważ bawię się trochę postanowiłem przepisać algo do scali :) I wrzucam go dla was - reszte jak ktoś chce może sobie dopisać.
Dla wprawionych może być wtf ale wrzucam to w Niewbie.
Oczywiście to jest napisane "po javowemu" i jest to wersja najprostsza z możliwych - ot zwykłe przeliczanie wartości macierzy.
import Array._
import scala.math._
class Neuron(val xIn: Array[Array[Double]]) {
var e: Double = 2.718281828; // liczba napera
var in: Array[Array[Double]] = xIn;
var tmpNet: Array[Array[Double]] = Array.ofDim[Double](2,4);
var net: Array[Array[Double]] = Array.ofDim[Double](2,1);
var faNet: Array[Double] = Array.ofDim[Double](2);
//var inlenDim1: Int = in.length-1;
//var inlenDim2: Int = in(0).length-1;
def oblicz_tmpNet() : Array[Array[Double]] = {
for(x <-0 to in.length-1){
for(y <- 0 to in(x).length-1){
if(x!=in.length-1)
tmpNet(x)(y) = in(0)(y) * in(x+1)(y);
}}
return tmpNet;
}
def obliczNet() : Array[Array[Double]] = {
for(x <- 0 to tmpNet.length-1){
for(y <- 0 to tmpNet(x).length-1){
net(x)(0) += tmpNet(x)(y);
}
}
return net;
}
def funkc_aktyw_ukr() : Array[Double] = {
for (x <- 0 to faNet.length-1) {
for(y <- 0 to net(x).length-1){
faNet(x) = 1/ (1 + pow(e, -net(x)(y)));
}
}
return faNet;
}
def wypisz(tab2dim: Array[Array[Double]]) {
// Wyświetlanie dwuwymiarowej tablicy double
for (x <- 0 to tab2dim.length-1) {
for ( y <- 0 to tab2dim(x).length-1) {
print(" " + tab2dim(x)(y));
}
println();
}
}
def wypisz(tab1dim: Array[Double]) {
for (x <- 0 to tab1dim.length-1) {
print(" " + tab1dim(x));
}
println();
}
}
object Neuron {
def main(args: Array[String]){
var arr = ofDim[Double](3,4);
// x0 x1 x2 x3 // x0 wymuszenie zewnętrzne (const) = 1.0
arr = Array(Array(1.0,0.4,0.2,0.7) // 1 tablica sygnały neuronów wejściowych
//N0 N1 N2 N3 neurony wejściowe
//w0a w1a w2a w3a
, Array(0.5,0.6,0.8,0.6) // tu wagi A
, Array(0.7,0.9,0.8,0.4));// tu wagi B
// itd...
val s = new Neuron(arr);
s.oblicz_tmpNet();
s.obliczNet();
s.funkc_aktyw_ukr();
s.wypisz(s.tmpNet);
s.wypisz(s.net);
s.wypisz(s.faNet);
}
}
Tablica A, B itd to neurony w warstwie ukrytej.
Jeśli chcemy zmienić ilość neuronów wej. to musimy zmienić ilość argumentów w każdej tablicy o tyle samo oraz wielkość zadeklarowanej tablicy (czy zdefiniowanej). Np dodajemy 1 neuron wej:
arr = Array(Array(1.0,0.4,0.2,0.7,0.7) // 1 tablica sygnały neuronów wejściowych
//N0 N1 N2 N3 N4 neurony wejściowe
//w0a w1a w2a w3a w4a
, Array(0.5,0.6,0.8,0.6,0.7) // tu wagi A
, Array(0.7,0.9,0.8,0.4,0.7));// tu wagi B neurony ukryte