/**
  * This method implements weights update procedure for the single neuron
  *
  * @param neuron neuron to update weights
  */
 @Override
 protected void updateNeuronWeights(Neuron neuron) {
   double output = neuron.getOutput();
   for (Connection connection : neuron.getInputConnections()) {
     double input = connection.getInput();
     double weight = connection.getWeight().getValue();
     double deltaWeight = (input - output * weight) * output * this.learningRate;
     connection.getWeight().inc(deltaWeight);
   }
 }
 // get unit with closetst weight vector
 private Neuron getClosest() {
   //		Iterator<Neuron> i = this.neuralNetwork.getLayerAt(1)
   //				.getNeuronsIterator();
   Neuron winner = new Neuron();
   double minOutput = 100;
   for (Neuron n : this.neuralNetwork.getLayerAt(1).getNeurons()) {
     //		while (i.hasNext()) {
     //			Neuron n = i.next();
     double out = n.getOutput();
     if (out < minOutput) {
       minOutput = out;
       winner = n;
     } // if
   } // while
   return winner;
 }
 /**
  * This method implements weights update procedure for the single neuron
  *
  * @param neuron neuron to update weights desiredOutput desired output of the neuron
  */
 protected void updateNeuronWeights(Neuron neuron, double desiredOutput) {
   for (Connection connection : neuron.getInputConnections()) {
     double input = connection.getInput();
     double deltaWeight = input * desiredOutput * this.learningRate;
     connection.getWeight().inc(deltaWeight);
   }
 }
示例#4
0
  @Before
  public void setUp() {
    instance = new Max();
    inputNeurons = new ArrayList<>(4);
    for (int i = 0; i < 4; i++) inputNeurons.add(new InputNeuron());

    Neuron toNeuron = new Neuron();
    toNeuron.setInputFunction(instance);

    inputConnections = new ArrayList(4);
    for (int i = 0; i < 4; i++) {
      Connection inConn = new Connection(inputNeurons.get(i), toNeuron, 1);
      inputConnections.add(inConn);
      toNeuron.addInputConnection(inConn);
    }
  }
 private void adjustCellWeights(Neuron cell, int r) {
   // Iterator<Connection> i = cell.getInputsIterator();
   //		while (i.hasNext()) {
   //			Connection conn = i.next();
   for (Connection conn : cell.getInputConnections()) {
     double dWeight = (learningRate / (r + 1)) * (conn.getInput() - conn.getWeight().getValue());
     conn.getWeight().inc(dWeight);
   } // while
 }
  private void learnPattern(DataSetRow dataSetRow, int neighborhood) {
    neuralNetwork.setInput(dataSetRow.getInput());
    neuralNetwork.calculate();
    Neuron winner = getClosest();
    if (winner.getOutput() == 0) return; // ako je vec istrenirana jedna celija, izadji

    Layer mapLayer = neuralNetwork.getLayerAt(1);
    int winnerIdx = mapLayer.indexOf(winner);
    adjustCellWeights(winner, 0);

    int cellNum = mapLayer.getNeuronsCount();
    for (int p = 0; p < cellNum; p++) {
      if (p == winnerIdx) continue;
      if (isNeighbor(winnerIdx, p, neighborhood)) {
        Neuron cell = mapLayer.getNeuronAt(p);
        adjustCellWeights(cell, 1);
      } // if
    } // for
  }
  /**
   * Calculates predict values for every possible class that instance can be classified as that
   *
   * @param instnc Instance
   * @return Map<Object, Double>
   */
  @Override
  public Map<Object, Double> classDistribution(Instance instnc) {

    // Convert instance to double array
    double[] item = convertInstanceToDoubleArray(instnc);

    // set neural network input
    neuralNet.setInput(item);
    // calculate neural network output
    neuralNet.calculate();

    // find neuron with highest output
    Map<Object, Double> possibilities = new HashMap<Object, Double>();

    for (Neuron neuron : neuralNet.getOutputNeurons()) {
      possibilities.put(neuron.getLabel(), neuron.getOutput());
    }

    return possibilities;
  }
  /**
   * Classifies instance as one of possible classes
   *
   * @param instnc Instance to classify
   * @return Object class as Object
   */
  @Override
  public Object classify(Instance instnc) {

    double[] item = convertInstanceToDoubleArray(instnc);

    // set neural network input
    neuralNet.setInput(item);
    // calculate neural network output
    neuralNet.calculate();

    // find neuron with highest output
    Neuron maxNeuron = null;
    double maxOut = Double.NEGATIVE_INFINITY;
    for (Neuron neuron : neuralNet.getOutputNeurons()) {
      if (neuron.getOutput() > maxOut) {
        maxNeuron = neuron;
        maxOut = neuron.getOutput();
      }
    }

    // and return its label
    return maxNeuron.getLabel();
  }