/* MLP mit Standard Error Backpropagation (batch) */ /* Netzgroesse: DIM-N1-N2 */ #define DIM 8 #define N1 3 #define N2 8 synapse input_link: std_synapse { input: real in; real delta_in; output: real out; private: real dw, w; public methods: void init(void) { w = rrand(-0.5,0.5); dw = 0; } void forward(void) { out = w * in; } void backward(void) { dw = dw + in * delta_in; } void learn(real eta) { w = w + eta * dw; dw = 0; } } synapse hidden_link: input_link { output: real delta_out; public methods: void backward(void) { delta_out = w * delta_in; dw = dw + in * delta_in; } } neuron sigmoid_neuron: std_neuron { input: real_vector u; bool teach; output: real y, delta_out; real square_error; private: real x, theta, dtheta; public methods: void init(void) { theta = rrand(-0.5,0.5); dtheta = 0; } void update(void) { x= reduce(+, u) - theta; y= sigmoid(x); } void error(void) { delta_out= (teach-y) * derived_sigmoid(x); square_error = sqr(teach-y); dtheta = dtheta + delta_out; } void learn(real eta) { theta = theta - eta * dtheta; dtheta = 0; } } neuron hidden_sigmoid_neuron: sigmoid_neuron { input: real_vector delta_in; public methods: void error(void) { delta_out= reduce(+,delta_in) * derived_sigmoid(x); dtheta = dtheta + delta_out; } } network mlp : std_network { input: real ext_input[DIM]; bool layer2.teach; output: real layer1.y; real layer2.y; private: hidden_sigmoid_neuron layer1[N1]; sigmoid_neuron layer2[N2]; input_link input_net = { full, ext_input, layer1, in = ext_input, out = layer1.u, delta_in = layer1.delta_out }; hidden_link net12 = { full, layer1, layer2, in = layer1.y, out = layer2.u, delta_in = layer2.delta_out, delta_out = layer1.delta_in }; public methods: void init(void) { input_net.init(); layer1.init(); net12.init(); layer2.init(); } real learn(real eta) { int eof; real total_error; total_error = 0; eof = read_inputs_first(); while (eof != -1) { input_net.forward(); layer1.update(); net12.forward(); layer2.update(); layer2.error(); total_error = total_error + reduce(+,layer2.square_error); net12.backward(); layer1.error(); input_net.backward(); write_outputs(); eof = read_inputs_next(); } trace_real(total_error,8,6); layer2.learn(eta); layer1.learn(eta); net12.learn(eta); input_net.learn(eta); return (total_error); } }