📄 neuron.java
字号:
package org.scut.DataMining.Algorithm.NeuralNetwork.Core;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import org.scut.DataMining.Algorithm.NeuralNetwork.Core.ActivateFunctions.Sigmoid;
import org.scut.DataMining.Core.MiningException;
/***************************************************************************
* Neuron Simulation Design *
* TARGET: *
* 1. Simulates a biological neuron as simliar as possible *
* 2. Provides a flexible extention of the neuron to a neuron network *
* DESIGN: *
* There may be many features of a biological neuron, but the most *
* significient features we simulates are the followings: *
* 1) Synapses connects to the neuron including input and output ones *
* 2) How to responds to a stimulation of an input signal, that's is *
* the activation function type *
* 3) A neuron can adjust itself freely, it can not only lock its *
* input and output forward system but aslo the corresponding feed-*
* back system. *
* IMPLEMENT: *
* An activate function is employed to stimulates the activate archi- *
* techture of the neuron's responding to a input signal, both input and *
* output synapse are easy to be achieved via two synapse list. To provide *
* the free adjustment of a neuron, FOUR input and output features are inc-*
* luded indicating whether the input and output of both forward and feed- *
* back system are locked or not *
* @author Leiming Hong(Brainmaker) South China Univ. of Tech. *
***************************************************************************/
public class Neuron
{
/***********************************************************************/
/* Generates a bias neuron by locking output activity as 1.0 & locking */
/* output feedback as 0.0(or any value) & locking input feed back as */
/* 0.0(or any value) */
/***********************************************************************/
/** Activation function for this Neuron node*/
private ActivateFunction activateFunc;
/** input of the activity */
private double inActivity = 0.0;
/** output of the activity */
private double outActivity = 0.0;
/** input of the feedback */
private double inFeedback = 0.0;
/** output of the feed back*/
private double outFeedback = 0.0;
/** Locks the input activity if true */
private boolean lockInActivity = false;
/** Locks the output activity if true*/
private boolean lockOutActivity = false;
/** Locks the input of the neuron if true */
private boolean lockInFeedback = false;
/** Locks the output of the neuron if true */
private boolean lockOutFeedback = false;
/** Input synapses of the neuron */
private ArrayList<Synapse> inSynapses = new ArrayList<Synapse>();
/** Output synapses of the neuron */
private ArrayList<Synapse> outSynapses = new ArrayList<Synapse>();
public Neuron()
{
super();
// TODO Auto-generated constructor stub
this.activateFunc = new Sigmoid(); //:default actviation function
}
/** Forward activates the neuron*/
public void activate()
{
if(this.lockOutActivity) return; //: output activity locked
this.calcInActivity();
//: else has not input synapse, just use the inActivity value already set
this.outActivity = this.activateFunc.function(this.inActivity);
}
/** Calculates the input activity */
private void calcInActivity()
{
if(this.lockInActivity) return; //: input activity locked
this.inActivity = 0;
for(Synapse syn : this.inSynapses)
{
Neuron nu = syn.getBackwardNeuron();
this.inActivity += nu.outActivity * syn.getWeight();
}
}
/** Backward feedback the neuron */
public void feedback()
{
if(this.lockOutFeedback) return; //: output feedback locked
this.calcInFeedback();
//: else has not output synapse, just use the inFeedback value already set
this.outFeedback = this.inFeedback * this.activateFunc.derivation(this.inActivity);
}
/** Calculates the input feedback */
private void calcInFeedback()
{
if(this.lockInFeedback) return; //: input feedback locked
this.inFeedback = 0;
for(Synapse syn : this.outSynapses)
{
Neuron nu = syn.getForwardNeuron();
this.inFeedback += nu.outFeedback * syn.getWeight();
}
}
/**
* Adds an input synapse
* @param synapse input synapse
* @throws MiningException
*/
public void addInSynapse(Synapse synapse) throws MiningException
{
if(synapse.getForwardNeuron() != this)
throw new MiningException("Not matched int synapse!");
this.inSynapses.add(synapse);
}
/**
* Adds an out synapse
* @param synapse out synapse
* @throws MiningException
*/
public void addOutSynapse(Synapse synapse) throws MiningException
{
if(synapse.getBackwardNeuron() != this)
throw new MiningException("Not matched out synapse!");
this.outSynapses.add(synapse);
}
/**
* Gets the activation funcion
* @return ActivateFuncion object
*/
public ActivateFunction getActivateFunction()
{
return this.activateFunc;
}
/**
* Sets the activation function
* @param activateFunc ActivateFuncion object of the neuron
*/
public void setActivateFunction(ActivateFunction activateFunc)
{
this.activateFunc = activateFunc;
}
/**
* Gets the iterator of the input synapses
* @return iterator of synapse objects
*/
public Iterator<Synapse> getInSynapses()
{
return this.inSynapses.iterator();
}
/**
* Gets the iterator of the output synapses
* @return iterator of synapse objects
*/
public Iterator<Synapse> getOutSynapses()
{
return this.outSynapses.iterator();
}
/**
* Gets the input activity
* @return input activity value
*/
public double getInActivity()
{
return this.inActivity;
}
/**
* Gets the input feedback value
* @return input feedback value
*/
public double getInFeedback()
{
return this.inFeedback;
}
/**
* Gets output activity
* @return output activity value
*/
public double getOutActivity()
{
return this.outActivity;
}
/**
* Gets the output feedback
* @return output feedback
*/
public double getOutFeedback()
{
return this.outFeedback;
}
/**
* Unlocks the input activity
*/
public void unlockInActivity()
{
this.lockInActivity = false;
}
/**
* Unlocks the output activity
*/
public void unlockOutActivity()
{
this.lockOutActivity = false;
}
/**
* Unlocks the input feedback
*/
public void unlockInFeedback()
{
this.lockInFeedback = false;
}
/**
* Unlocks the ouput feedback
*/
public void unlockOutFeedback()
{
this.lockOutFeedback = false;
}
/**
* Locks the input activity at a fixed value
* @param value value to be locked as
*/
public void lockInActivity(double value)
{
this.lockInActivity = true;
this.inActivity = value;
}
/**
* Locks the output activity at a fixed value
* @param value value to be locked as
*/
public void lockOutActivity(double value)
{
this.lockOutActivity = true;
this.outActivity = value;
}
/**
* Locks the input feedback at a fixed value
* @param value value to be locked as
*/
public void lockInFeedback(double value)
{
this.lockInFeedback = true;
this.inFeedback = value;
}
/**
* Locks the output feedback at a fixed value
* @param value value to be locked as
*/
public void lockOutFeedback(double value)
{
this.lockOutFeedback = true;
this.outFeedback = value;
}
/**
* Tells whether the input activity locked or not
* @return true if locked
*/
public boolean isInActivityLocked()
{
return this.lockInActivity;
}
/**
* Tells whether the output activity locked or not
* @return true if locked
*/
public boolean isOutActivityLocked()
{
return this.lockOutActivity;
}
/**
* Tells whether the input feedback locked or not
* @return true if locked
*/
public boolean isInFeedbackLocked()
{
return this.lockInFeedback;
}
/**
* Tells whether the output feedback locked or not
* @return true if locked
*/
public boolean isOutFeedbackLocked()
{
return this.lockOutFeedback;
}
/**
* Reports the status of the neuron
*/
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Activate Function type: "+this.activateFunc.getClass() + "\n");
sb.append("Input Activity lock status: " + this.lockInActivity + " value =" + this.inActivity+"\n");
sb.append("Output Activity lock status: " + this.lockOutActivity + " value =" + this.outActivity+"\n");
sb.append("Input Feedback lock status: " + this.lockInFeedback + " value =" + this.inFeedback+"\n");
sb.append("Output Feedback lock status: " + this.lockOutFeedback+ " value =" + this.outFeedback);
return sb.toString();
}
/*********************************************************************/
public static void main(String[] args)
{
long start = new Date().getTime();
try
{
Neuron forward = new Neuron();
Neuron backward = new Neuron();
Synapse syn = new Synapse(backward,forward);
forward.lockInActivity(0);
forward.lockOutActivity(1);
forward.lockInFeedback(1.0);
forward.activate();
forward.feedback();
System.out.println(forward.toString());
forward.addInSynapse(syn);
}
catch (MiningException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = new Date().getTime();
System.out.println("Time eclipsed[s]: " + (end-start)/1000.0);
}
/*********************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -