Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %XOR calculator ( biases as weights )
- % training set
- % (the last number is bias coefficent which is always 1
- ts=[0 0 1;1 1 1;
- 1 0 1;0 1 1];
- % targets or desired outputs
- d=[0 0 1 1];
- % weights for hidden layers
- % bias ,weight1, weigth2
- wh=[rand() rand() rand() ;
- rand() rand() rand()];
- % wiegths from hidden layer to output layer
- % bias, weight1, weigth2
- wo=[rand() rand() rand()];
- %output for neuron 1 and 2 and bias for neuron3
- a=[1 1];
- %output for neuron 3
- a3=0;
- %learning rate
- n=0.1;
- iteration=100;
- i=0;
- while (i<iteration || e~=0)
- for tindex=1:4
- for neuron=1:2
- a(neuron)= logsig(wh(neuron,:)*ts(neuron,:)');
- end
- output_neuronsInput = wo(1)*a(1)+wo(2)*a(2)+wo(3)*1;
- a3 = hardlim(output_neuronsInput);
- e = d(tindex) - a3
- % neuron 3(output)
- localgrad_out = a3*e;
- deltaW = n * localgrad_out * output_neuronsInput;
- wo_new = wo + deltaW;
- % neuron 1
- grad_h = a(1)*(1-a(1)) * localgrad_out * wo(1)'
- deltaW = n * grad_h * wh(1,:);
- wh(1,:) = wh(1,:) + deltaW
- % neuron 2
- grad_h = a(2)*(1-a(2)) * localgrad_out*wo(2)'
- wo = wo_new;
- deltaW = n * grad_h * wh(2,:)
- wh(2,:) = wh(2,:) + deltaW
- end
- i=i+1;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement