/** * Call this to have the connection restore from the saved weights. */ @Override public void restoreWeights() { for (int i = 0; i < m_numInputs; i++) { m_inputList[i].restoreWeights(); } }
/** * Call this to have the connection save the current weights. */ @Override public void saveWeights() { for (int i = 0; i < m_numInputs; i++) { m_inputList[i].saveWeights(); } }
/** * This will go through all the nodes and check if they are connected to a * pure output unit. If so they will be set to be linear units. If not they * will be set to be sigmoid units. */ private void setEndsToLinear() { for (NeuralConnection m_neuralNode : m_neuralNodes) { if ((m_neuralNode.getType() & NeuralConnection.OUTPUT) == NeuralConnection.OUTPUT) { ((NeuralNode) m_neuralNode).setMethod(m_linearUnit); } else { ((NeuralNode) m_neuralNode).setMethod(m_sigmoidUnit); } } }
/** * This function calculates what the error value should be. * @param node The node to calculate the error for. * @return The error. */ public double errorValue(NeuralNode node) { //then calculate the error. NeuralConnection[] outputs = node.getOutputs(); int[] oNums = node.getOutputNums(); double error = 0; for (int noa = 0; noa < node.getNumOutputs(); noa++) { error += outputs[noa].errorValue(true) * outputs[noa].weightValue(oNums[noa]); } return error; }
/** * This function will calculate what the change in weights should be * and also update them. * @param node The node to update the weights for. * @param learn The learning rate to use. * @param momentum The momentum to use. */ public void updateWeights(NeuralNode node, double learn, double momentum) { NeuralConnection[] inputs = node.getInputs(); double[] cWeights = node.getChangeInWeights(); double[] weights = node.getWeights(); double learnTimesError = 0; learnTimesError = learn * node.errorValue(false); double c = learnTimesError + momentum * cWeights[0]; weights[0] += c; cWeights[0] = c; int stopValue = node.getNumInputs() + 1; for (int noa = 1; noa < stopValue; noa++) { c = learnTimesError * inputs[noa-1].outputValue(false); c += momentum * cWeights[noa]; weights[noa] += c; cWeights[noa] = c; } }
/** * This function calculates what the output value should be. * @param node The node to calculate the value for. * @return The value. */ public double outputValue(NeuralNode node) { double[] weights = node.getWeights(); NeuralConnection[] inputs = node.getInputs(); double value = weights[0]; for (int noa = 0; noa < node.getNumInputs(); noa++) { value += inputs[noa].outputValue(true) * weights[noa+1]; } return value; }
/** * Call this function to update the weight values at this unit. * After the weights have been updated at this unit, All the * input connections will then be called from this to have their * weights updated. * @param l The learning rate to use. * @param m The momentum to use. */ public void updateWeights(double l, double m) { if (!m_weightsUpdated && !Double.isNaN(m_unitError)) { m_methods.updateWeights(this, l, m); //note that the super call to update the inputs is done here and //not in the m_method updateWeights, because it is not deemed to be //required to update the weights at this node (while the error and output //value ao need to be recursively calculated) super.updateWeights(l, m); //to call all of the inputs. } }
/** * This function will remove all the inputs to this unit. * In doing so it will also terminate the connections at the other end. */ public void removeAllInputs() { super.removeAllInputs(); double temp1 = m_weights[0]; double temp2 = m_changeInWeights[0]; m_weights = new double[1]; m_changeInWeights = new double[1]; m_weights[0] = temp1; m_changeInWeights[0] = temp2; }
/** * Call this function to draw the node highlighted. * @param g The graphics context. * @param w The width of the drawing area. * @param h The height of the drawing area. */ public void drawHighlight(Graphics g, int w, int h) { drawNode(g, w, h); g.setColor(Color.yellow); g.fillOval((int)(m_x * w) - 5, (int)(m_y * h) - 5, 11, 11); }
/** * This function calculates what the error value should be. * @param node The node to calculate the error for. * @return The error. */ public double errorValue(NeuralNode node) { //then calculate the error. NeuralConnection[] outputs = node.getOutputs(); int[] oNums = node.getOutputNums(); double error = 0; for (int noa = 0; noa < node.getNumOutputs(); noa++) { error += outputs[noa].errorValue(true) * outputs[noa].weightValue(oNums[noa]); } double value = node.outputValue(false); error *= value * (1 - value); return error; }
/** * This function calculates what the output value should be. * @param node The node to calculate the value for. * @return The value. */ public double outputValue(NeuralNode node) { double[] weights = node.getWeights(); NeuralConnection[] inputs = node.getInputs(); double value = weights[0]; for (int noa = 0; noa < node.getNumInputs(); noa++) { value += inputs[noa].outputValue(true) * weights[noa+1]; } return value; }
/** * This will go through all the nodes and check if they are connected to a * pure output unit. If so they will be set to be linear units. If not they * will be set to be sigmoid units. */ private void setEndsToLinear() { for (NeuralConnection m_neuralNode : m_neuralNodes) { if ((m_neuralNode.getType() & NeuralConnection.OUTPUT) == NeuralConnection.OUTPUT) { ((NeuralNode) m_neuralNode).setMethod(m_linearUnit); } else { ((NeuralNode) m_neuralNode).setMethod(m_sigmoidUnit); } } }
/** * Call this to have the connection restore from the saved weights. */ @Override public void restoreWeights() { for (int i = 0; i < m_numInputs; i++) { m_inputList[i].restoreWeights(); } }
/** * Call this to have the connection save the current weights. */ @Override public void saveWeights() { for (int i = 0; i < m_numInputs; i++) { m_inputList[i].saveWeights(); } }
/** * This function will remove all the inputs to this unit. * In doing so it will also terminate the connections at the other end. */ public void removeAllInputs() { super.removeAllInputs(); double temp1 = m_weights[0]; double temp2 = m_changeInWeights[0]; m_weights = new double[1]; m_changeInWeights = new double[1]; m_weights[0] = temp1; m_changeInWeights[0] = temp2; }
/** * This function calculates what the error value should be. * @param node The node to calculate the error for. * @return The error. */ public double errorValue(NeuralNode node) { //then calculate the error. NeuralConnection[] outputs = node.getOutputs(); int[] oNums = node.getOutputNums(); double error = 0; for (int noa = 0; noa < node.getNumOutputs(); noa++) { error += outputs[noa].errorValue(true) * outputs[noa].weightValue(oNums[noa]); } return error; }
/** * Call this to have the connection restore from the saved * weights. */ public void restoreWeights() { // copy the saved best weights back into the weights System.arraycopy(m_bestWeights, 0, m_weights, 0, m_weights.length); // tell inputs to restore weights for (int i = 0; i < m_numInputs; i++) { m_inputList[i].restoreWeights(); } }
/** * Call this to have the connection save the current * weights. */ public void saveWeights() { // copy the current weights System.arraycopy(m_weights, 0, m_bestWeights, 0, m_weights.length); // tell inputs to save weights for (int i = 0; i < m_numInputs; i++) { m_inputList[i].saveWeights(); } }
/** * This function calculates what the error value should be. * @param node The node to calculate the error for. * @return The error. */ public double errorValue(NeuralNode node) { //then calculate the error. NeuralConnection[] outputs = node.getOutputs(); int[] oNums = node.getOutputNums(); double error = 0; for (int noa = 0; noa < node.getNumOutputs(); noa++) { error += outputs[noa].errorValue(true) * outputs[noa].weightValue(oNums[noa]); } double value = node.outputValue(false); error *= value * (1 - value); return error; }
/** * Call this to have the connection restore from the saved * weights. */ public void restoreWeights() { // copy the saved best weights back into the weights System.arraycopy(m_bestWeights, 0, m_weights, 0, m_weights.length); // tell inputs to restore weights for (int i = 0; i < m_numInputs; i++) { m_inputList[i].restoreWeights(); } }