📄 layer.java
字号:
package org.scut.DataMining.Algorithm.NeuralNetwork.Core;
import org.scut.DataMining.Core.MiningException;
import java.util.ArrayList;
import java.util.Iterator;
public class Layer
{
/** Layer types */
public enum LayerType{ Input, Hidden, Output }
/** Layer type */
private LayerType layerType;
/** Neurons belongs to the layer*/
private ArrayList<Neuron> neurons = new ArrayList<Neuron>();
/** Input synapse set*/
private ArrayList<Synapse> inSynapses = new ArrayList<Synapse>();
/** Output synapse set */
private ArrayList<Synapse> outSynapses = new ArrayList<Synapse>();
/** Forward layer, can only be set by the link method */
private Layer forwardLayer = null;
/** Backward layer, can only be set by the link method */
private Layer backwardLayer = null;
/**
* Constructs a layer by specified layer type
* @param count node count of the layer
* @param layerType type of the layer
*/
public Layer(int count, LayerType layerType)
{
super();
// TODO Auto-generated constructor stub
this.layerType = layerType;
for(int i=0;i<count;i++)
{
Neuron neuron = new Neuron();
this.neurons.add(neuron);
}
}
/**
* Sets the activate function of all the neurons in the layer
* @param type activate function type
*/
public void setActivateFunction(ActivateFunction.Type type)
{
for(Neuron nu : this.neurons)
{
ActivateFunction af = ActivateFunction.getActivateFunction(type);
nu.setActivateFunction(af);
}
}
/**
* Sets the activate function of a specified neuron in the layer
* @param idx index of the neuron in the layer
* @param type neuron activate function type
*/
public void setActivateFunction(int idx,ActivateFunction.Type type)
{
ActivateFunction af = ActivateFunction.getActivateFunction(type);
this.neurons.get(idx).setActivateFunction(af);
}
/**
* Sets a neuron to be a bias neuron
* @param idx index position of a neuron
*/
public void setBiasNeuron(int idx)
{
Neuron nu = this.neurons.get(idx);
nu.lockInActivity(0.0);
nu.lockOutActivity(1.0);
nu.lockInFeedback(0);
nu.lockOutFeedback(0);
}
/**
* Sets all the input synapses' learning rate to a same value
* @param value input synapses' learning rate
*/
public void setInSynapseLearningRate(double value)
{
for(Synapse syn : this.inSynapses)
syn.setLearningRate(value);
}
/**
* Sets all the output synapses' learning rate to a same value
* @param value output synapses' learning rate
*/
public void setOutSynapseLearningRate(double value)
{
for(Synapse syn : this.outSynapses)
syn.setLearningRate(value);
}
/**
* Sets all the input synapses' random range to a same value
* @param value input synapses' random range
*/
public void setInSynapseInitRandomRange(double value)
{
for(Synapse syn : this.inSynapses)
syn.setInitRandomRange(value);
}
/**
* Sets all the output synapses' random range to a same value
* @param value output synapses' random range
*/
public void setOutSynapseInitRandomRange(double value)
{
for(Synapse syn : this.outSynapses)
syn.setInitRandomRange(value);
}
/**
* Sets a output specified syanpse's learning rate
* @param value learning rate to be set
* @param backwardIdx backward neuron index number
* @param forwardIdx forward neuron index number
* @throws MiningException
*/
public void setOutSynapseLearningRate(double value,int backwardIdx,int forwardIdx) throws MiningException
{
if(this.forwardLayer == null)
throw new MiningException("No forward layer,cannot setOutSynapseLearningRate");
Neuron backward = this.neurons.get(backwardIdx);
Neuron forward = this.forwardLayer.neurons.get(forwardIdx);
for(Synapse syn : this.outSynapses)
{
if(syn.getBackwardNeuron() == backward && syn.getForwardNeuron() == forward)
{
syn.setLearningRate(value);
break;
}
}
}
/**
* Sets a input specified syanpse's learning rate
* @param value learning rate to be set
* @param backwardIdx backward neuron index number
* @param forwardIdx forward neuron index number
* @throws MiningException
*/
public void setInSynapseLearningRate(double value,int backwardIdx,int forwardIdx) throws MiningException
{
if(this.backwardLayer == null)
throw new MiningException("No backward layer,cannot setInSynapseLearningRate");
Neuron forward = this.neurons.get(forwardIdx);
Neuron backward = this.backwardLayer.neurons.get(backwardIdx);
for(Synapse syn : this.outSynapses)
{
if(syn.getBackwardNeuron() == backward && syn.getForwardNeuron() == forward)
{
syn.setLearningRate(value);
break;
}
}
}
/**
* Sets a output specified syanpse's initial random range
* @param value initial random range to be set
* @param backwardIdx backward neuron index number
* @param forwardIdx forward neuron index number
* @throws MiningException
*/
public void setOutSynapseInitRandomRange(double value,int backwardIdx,int forwardIdx) throws MiningException
{
if(this.forwardLayer == null)
throw new MiningException("No forward layer,cannot setOutSynapseRandomRange");
Neuron backward = this.neurons.get(backwardIdx);
Neuron forward = this.forwardLayer.neurons.get(forwardIdx);
for(Synapse syn : this.outSynapses)
{
if(syn.getBackwardNeuron() == backward && syn.getForwardNeuron() == forward)
{
syn.setInitRandomRange(value);
break;
}
}
}
/**
* Sets a input specified syanpse's initial random range
* @param value initial random range to be set
* @param backwardIdx backward neuron index number
* @param forwardIdx forward neuron index number
* @throws MiningException
*/
public void setInSynapseInitRandomRange(double value,int backwardIdx,int forwardIdx) throws MiningException
{
if(this.backwardLayer == null)
throw new MiningException("No backward layer,cannot setInSynapseLearningRate");
Neuron forward = this.neurons.get(forwardIdx);
Neuron backward = this.backwardLayer.neurons.get(backwardIdx);
for(Synapse syn : this.outSynapses)
{
if(syn.getBackwardNeuron() == backward && syn.getForwardNeuron() == forward)
{
syn.setInitRandomRange(value);
break;
}
}
}
/**
* Gets the input activity of a specified neuron in the layer
* @param idx neuron position in the layer
* @return input activity of the specified neuron
*/
public double getInActivity(int idx)
{
return this.neurons.get(idx).getInActivity();
}
/**
* Gets all the input activity of the neurons in the layer
* @return input activities of all the neuron in the layer
*/
public double[] getInActivity()
{
double[] res = new double[this.neurons.size()];
for(int i=0;i<res.length;i++)
res[i] = this.neurons.get(i).getInActivity();
return res;
}
/**
* Gets the out activity of a specified neuron in the layer
* @param idx neuron position in the layer
* @return out activity of the specified neuron
*/
public double getOutActivity(int idx)
{
return this.neurons.get(idx).getOutActivity();
}
/**
* Gets all the out activity of the neurons in the layer
* @return out activities of all the neuron in the layer
*/
public double[] getOutActivity()
{
double[] res = new double[this.neurons.size()];
for(int i=0;i<res.length;i++)
res[i] = this.neurons.get(i).getOutActivity();
return res;
}
/**
* Gets the input feedback of a specified neuron in the layer
* @param idx neuron position in the layer
* @return input feedback of the specified neuron
*/
public double getInFeedback(int idx)
{
return this.neurons.get(idx).getInFeedback();
}
/**
* Gets all the input feedbacks of the neurons in the layer
* @return input feedbacks of all the neuron in the layer
*/
public double[] getInFeedback()
{
double[] res = new double[this.neurons.size()];
for(int i=0;i<res.length;i++)
res[i] = this.neurons.get(i).getInFeedback();
return res;
}
/**
* Gets the output feedback of a specified neuron in the layer
* @param idx neuron position in the layer
* @return output feedback of the specified neuron
*/
public double getOutFeedback(int idx)
{
return this.neurons.get(idx).getOutFeedback();
}
/**
* Gets all the output feedbacks of the neurons in the layer
* @return output feedbacks of all the neuron in the layer
*/
public double[] getOutFeedback()
{
double[] res = new double[this.neurons.size()];
for(int i=0;i<res.length;i++)
res[i] = this.neurons.get(i).getOutFeedback();
return res;
}
/**
* Locks the input activity of a specified neuron in the layer
* @param idx index of the neuron to be locked the input activity
* @param value locked value
*/
public void lockInActivity(int idx,double value)
{
this.neurons.get(idx).lockInActivity(value);
}
/**
* Locks the input activity of all the neurons in the layer
* @param value locked value array
*/
public void lockInActivity(double[] values)
{
int size = this.neurons.size();
for(int i=0;i<size;i++)
this.neurons.get(i).lockInActivity(values[i]);
}
/**
* Locks the output activity of a specified neuron in the layer
* @param idx index of the neuron to be locked the ouput activity
* @param value locked value
*/
public void lockOutActivity(int idx,double value)
{
this.neurons.get(idx).lockOutActivity(value);
}
/**
* Locks the output activity of all the neurons in the layer
* @param value locked value array
*/
public void lockOutActivity(double[] values)
{
int size = this.neurons.size();
for(int i=0;i<size;i++)
this.neurons.get(i).lockOutActivity(values[i]);
}
/**
* Locks the input feedback of a specified neuron in the layer
* @param idx index of the neuron to be locked the input feedback
* @param value locked value
*/
public void lockInFeedback(int idx,double value)
{
this.neurons.get(idx).lockInFeedback(value);
}
/**
* Locks the input feedback of all the neurons in the layer
* @param value locked value array
*/
public void lockInFeedback(double[] values)
{
int size = this.neurons.size();
for(int i=0;i<size;i++)
this.neurons.get(i).lockInFeedback(values[i]);
}
/**
* Locks the output feedback of a specified neuron in the layer
* @param idx index of the neuron to be locked the output feedback
* @param value locked value
*/
public void lockOutFeedback(int idx,double value)
{
this.neurons.get(idx).lockOutFeedback(value);
}
/**
* Locks the output feedback of all the neurons in the layer
* @param value locked value array
*/
public void lockOutFeedback(double[] values)
{
int size = this.neurons.size();
for(int i=0;i<size;i++)
this.neurons.get(i).lockOutFeedback(values[i]);
}
/**
* Forward activates the neurons of the layer
*/
public void activate()
{
for(Neuron nu : this.neurons) nu.activate();
}
/**
* Backward feedbacks the neurons of the layer
*/
public void feedback()
{
for(Neuron nu : this.neurons) nu.feedback();
}
/**
* Updates the delta weights of the input synapses
*
*/
public void updateBackwardDeltaWeights()
{
for(Synapse syn : this.inSynapses)
{
Neuron forward = syn.getForwardNeuron();
Neuron backward = syn.getBackwardNeuron();
double delta = forward.getOutFeedback()*backward.getOutActivity();
syn.updateDeltaWeight(delta);
}
}
/**
* Updates the input synapses' weights
*
*/
public void updateBackwardWeights()
{
for(Synapse syn : this.inSynapses)
syn.updateWeight();
}
/**
* Links two layers togther
* @param backward back layer
* @param forward forward layer
* @throws MiningException not matched exception
*/
public static void linkLayer(Layer backward,Layer forward) throws MiningException
{
if(backward == forward) return;
backward.forwardLayer = forward;
forward.backwardLayer = backward;
for(Neuron nb : backward.neurons)
{
for(Neuron nf : forward.neurons)
{
Synapse syn = new Synapse(nb,nf);
nb.addOutSynapse(syn);
nf.addInSynapse(syn);
forward.addInSynapse(syn);
backward.addOutSynapse(syn);
}
}
}
/**
* Adds an input synapse that belongs to the layer
* does nothing if the synapse not belongs to the layer
* @param syn
*/
public void addInSynapse(Synapse syn)
{
Neuron forward = syn.getForwardNeuron();
for(Neuron nu : this.neurons)
{
if(nu == forward) //: only add those mateched synapses
{
this.inSynapses.add(syn);
break;
}
}
}
/**
* Adds an input synapse that belongs to the layer
* does nothing if the synapse not belongs to the layer
* @param syn
*/
public void addOutSynapse(Synapse syn)
{
Neuron backward = syn.getBackwardNeuron();
for(Neuron nu : this.neurons)
{
if(nu == backward) //: only add those mateched synapses
{
this.outSynapses.add(syn);
break;
}
}
}
/**
* Gets the neuron count number
* @return neuron count
*/
public int getNeuronCount()
{
return this.neurons.size();
}
/**
* Gets the neurons, for read only, uses iterator
* @return iteration of the neurons
*/
public Iterator<Neuron> getNeurons()
{
return this.neurons.iterator();
}
/**
* Gets the input synapses, for read only, uses iterator
* @return
*/
public Iterator<Synapse> getInSynapses()
{
return this.inSynapses.iterator();
}
/**
* Gets the out synapses, for read only, uses iterator
* @return
*/
public Iterator<Synapse> getOutSynapses()
{
return this.outSynapses.iterator();
}
/**
* Gets the input synapse count
* @return
*/
public int getInSynapseCount()
{
return this.inSynapses.size();
}
/**
* Gets the output synapse count
* @return
*/
public int getOutSynapseCount()
{
return this.outSynapses.size();
}
/**
* Gets the input synapse at a specified position
* @param idx
* @return
*/
public Synapse getInSynapse(int idx)
{
return this.inSynapses.get(idx);
}
/**
* Gets the output synapse at a specified position
* @param idx
* @return
*/
public Synapse getOutSynapse(int idx)
{
return this.outSynapses.get(idx);
}
/**
* Gets the layer type of the layer
* @return type of the layer
*/
public LayerType getLayerType()
{
return this.layerType;
}
/**
* Gets the forward adjacent layer
* @return
*/
public Layer getForwardLayer()
{
return this.forwardLayer;
}
/**
* Gets the backward adjacent layer
* @return
*/
public Layer getBackwardLayer()
{
return this.backwardLayer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -