📄 synapse.java
字号:
package org.joone.engine;import java.io.*;import java.util.TreeSet;import java.util.Collection;import java.util.ArrayList;import org.joone.log.*;import org.joone.inspection.Inspectable;import org.joone.inspection.implementations.WeightsInspection;/** * The Synapse is the connection element between two Layer objects. * Its connections are represented by weights that transport the patterns * from a layer to another. * These weights are modified in the learning cycles, and represent the 'memory' * of the trained neural net. */public abstract class Synapse implements InputPatternListener, OutputPatternListener, LearnableSynapse, Serializable, Inspectable { /** * Logger * */ private static final ILogger log = LoggerFactory.getLogger(Synapse.class); /** Count of synapses for naming purposes. */ private static int synapseCount = 0; /** Name set by default to "Synapse [synapse count]" */ private String fieldName = "Synapse " + ++synapseCount; private double learningRate = 0; private double momentum = 0; private int inputDimension = 0; private int outputDimension = 0; private boolean inputFull; private boolean outputFull; private Monitor monitor; private int ignoreBefore = -1; // not more used private boolean loopBack = false; // true if this synapse closes a loop of a recurrent neural network protected Matrix array; protected int m_batch = 0; protected boolean enabled = true; protected transient double[] inps = null; protected transient double[] outs = null; protected transient double[] bouts; protected transient int items = 0; protected transient int bitems = 0; /** * @label revPattern */ protected transient Pattern m_pattern; // The last fwd pattern read /** * @label fwdPattern */ protected transient Pattern b_pattern; // The last back pattern read protected transient int count = 0; protected transient boolean notFirstTime; protected transient boolean notFirstTimeB; protected transient Learner myLearner = null; // Objects used for synchronization protected transient volatile Object fwdLock = null; protected transient volatile Object revLock = null; /** Contains true if for the current Synapse must be used * a Learner instead of a built-in learning algorithm. * Set it in the constructor of any inherited class. * Used by the getLearner method. * @see getLearner */ protected boolean learnable = false; private static final long serialVersionUID = -5892822057908231022L; /** The constructor */ public Synapse() { //log.info ("Synapse instanciated"); } /** Adds a noise to the weights of the synapse * @param amplitude Amplitude of the noise: the value is centered around the zero. * e.g.: an amplitude = 0.2 means a noise range from -0.2 to 0.2 */ public void addNoise(double amplitude) { if (array != null) array.addNoise(amplitude); } /** Initializes all the weigths of the synapses with random values * @param amplitude Amplitude of the random values: the value is centered around the zero. * e.g.: an amplitude = 0.2 means a values' range from -0.2 to 0.2 */ public void randomize(double amplitude) { if (array != null)// array.randomize(-1.0 * amplitude, amplitude); array.initialize(); } /** * Funzione di TRAIN dell'elemento. * @param pattern double[] - pattern di input sul quale applicare la funzione di trasferimento */ protected abstract void backward(double[] pattern); /** Returns TRUE if the synapse calls the method nextStep() * on the Monitor object when the fwdGet() method is called * @return boolean */ public boolean canCountSteps() { return false; } /** * Recall function * @param pattern double[] - input pattern */ protected abstract void forward(double[] pattern); public Pattern fwdGet() { if (!isEnabled()) return null; synchronized (getFwdLock()) { if ((notFirstTime) || (!loopBack)) { while (items == 0) { try { fwdLock.wait(); } catch (InterruptedException e) { // log.warn ( "wait () was interrupted"); //e.printStackTrace(); reset(); fwdLock.notify(); return null; } } --items; m_pattern.setArray(outs); if (isLoopBack()) // To avoid sinc problems m_pattern.setCount(0); fwdLock.notify(); return m_pattern; } else { items = bitems = count = 0; notFirstTime = true; fwdLock.notify(); return null; } } } public void fwdPut(Pattern pattern) { if (isEnabled()) { synchronized (getFwdLock()) { while (items > 0) { try { fwdLock.wait(); } catch (InterruptedException e) { reset(); fwdLock.notify(); return; } // End of catch } m_pattern = pattern; count = m_pattern.getCount(); inps = (double[])pattern.getArray(); forward(inps); ++items; fwdLock.notify(); } } } /** Resets the internal state to be ready for the next run */ public void reset() { items = bitems = 0; notFirstTime = false; notFirstTimeB = false; } /** Returns the number of the ignored cycles at beginning of each epoch. * During these cycles the synapse returns null on the call to the xxxGet methods * * @return int * @see Synapse#setIgnoreBefore */ public int getIgnoreBefore() { return ignoreBefore; } /** Returns the input dimension of the synapse. * @return int */ public int getInputDimension() { return inputDimension; } /** Returns the value of the learning rate * @return double */ public double getLearningRate() { if (monitor != null) return monitor.getLearningRate(); else return 0.0; } /** Returns the value of the momentum * @return double */ public double getMomentum() { if (monitor != null) return monitor.getMomentum(); else return 0.0; } /** Returns the Monitor object attached to the synapse * @return neural.engine.Monitor */ public Monitor getMonitor() { return monitor; } /** Returns the name of the synapse * @return String * @see #setName */ public String getName() { return fieldName; } /** Returns the output dimension of the synapse. * @return int */ public int getOutputDimension() { return outputDimension; } protected Object readResolve() { setArrays(getInputDimension(), getOutputDimension()); return this; } public Pattern revGet() { if (!isEnabled()) return null; synchronized (getRevLock()) { if ((notFirstTimeB) || (!loopBack)) { while (bitems == 0) { try { revLock.wait(); } catch (InterruptedException e) { // log.warn ( "wait () was interrupted"); //e.printStackTrace(); reset(); revLock.notify(); return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -