Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package feedForward1;
- import java.util.LinkedList;
- public class Neuron {
- private double input, output, error, delta;
- private LinkedList<Double> inputs;
- private LinkedList<Double> weights;
- public Neuron() {
- weights = new LinkedList<Double>();
- inputs = new LinkedList<Double>();
- }
- public double getInput() {
- return input;
- }
- public double getOutput() {
- return output;
- }
- public double getError() {
- return error;
- }
- public double activate(boolean derivative, double i) {
- if(derivative)
- return i*(1.0-i);
- else
- return activate(i);
- }
- public double activate(double i) {
- output = 1.0 / (1.0 + Math.exp(-1.0 * i));
- return output;
- }
- public double activate() {
- return activate(input);
- }
- public double getWeightFor(int i) {
- return weights.get(i);
- }
- public void setWeightFor(int i, double d) {
- weights.set(i, d);
- }
- public void createWeight(double d) {
- weights.add(d);
- }
- public void incrementWeight(int i, double d) {
- double temp = getWeightFor(i);
- temp -= d;
- setWeightFor(i,temp);
- }
- public void incrementInput(double d) {
- inputs.add(d);
- input += d;
- }
- public void setInput(double d) {
- input = d;
- }
- public int getWeightSize() {
- return weights.size();
- }
- public double getDelta() {
- return delta;
- }
- public void updateWeight(double rate, Layer nextLayer) {
- if(nextLayer==null) return;
- for(int i = 0; i < weights.size(); i++) {
- incrementWeight(i, nextLayer.getNeuronAt(i).getDelta()*output*rate);
- }
- }
- //For output layer
- public void calcDelta(Layer layer, double ideal) {
- activate();
- error = Math.abs(ideal-output);
- error = Math.pow(error, 2);
- delta = BoundNumber.bound(error*activate(true,input));
- }
- //For hidden layer
- public void calcDelta(Layer layer, Layer nextLayer) {
- //For each weight
- double sumDelta = 0.0;
- for(int i = 0; i < weights.size(); i++)
- sumDelta += nextLayer.getNeuronAt(i).getDelta()*getWeightFor(i);
- delta = BoundNumber.bound(activate(true,input)*sumDelta);
- }
- public void clearInput() {
- input = 0;
- inputs.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement