/**
   * Convert to an array. This is used with some training algorithms that require that the "memory"
   * of the neuron(the weight and bias values) be expressed as a linear array.
   *
   * @param network The network to encode.
   * @return The memory of the neuron.
   */
  public static double[] networkToArray(final BasicNetwork network) {
    final int size = network.getStructure().calculateSize();

    // allocate an array to hold
    final double[] result = new double[size];

    int index = 0;

    for (final Layer layer : network.getStructure().getLayers()) {
      // process layer bias
      if (layer.hasBias()) {
        for (int i = 0; i < layer.getNeuronCount(); i++) {
          result[index++] = layer.getBiasWeight(i);
        }
      }

      // process synapses
      for (final Synapse synapse : network.getStructure().getPreviousSynapses(layer)) {
        if (synapse.getMatrix() != null) {
          // process each weight matrix
          for (int x = 0; x < synapse.getToNeuronCount(); x++) {
            for (int y = 0; y < synapse.getFromNeuronCount(); y++) {
              result[index++] = synapse.getMatrix().get(y, x);
            }
          }
        }
      }
    }

    return result;
  }
Example #2
0
 /** @return The list of layers that the outbound synapses connect to. */
 public Collection<Layer> getNextLayers() {
   final Collection<Layer> result = new HashSet<Layer>();
   for (final Synapse synapse : this.next) {
     result.add(synapse.getToLayer());
   }
   return result;
 }
Example #3
0
 /** @return True if this layer is connected to intself. */
 public boolean isSelfConnected() {
   for (final Synapse synapse : this.next) {
     if (synapse.isSelfConnected()) {
       return true;
     }
   }
   return false;
 }
Example #4
0
 /**
  * Determine if this layer is connected to another layer.
  *
  * @param layer A layer to check and see if this layer is connected to.
  * @return True if the two layers are connected.
  */
 public boolean isConnectedTo(final Layer layer) {
   for (final Synapse synapse : this.next) {
     if (synapse.getToLayer() == layer) {
       return true;
     }
   }
   return false;
 }
  /**
   * Process a fully connected synapse.
   *
   * @param network The network to process.
   * @param layer The layer to process.
   * @param array The array to process.
   * @param index The current index.
   * @return The index after this synapse has been read.
   */
  private static int processSynapseFull(
      final BasicNetwork network, final Layer layer, final double[] array, final int index) {
    int result = index;
    // process synapses
    for (final Synapse synapse : network.getStructure().getPreviousSynapses(layer)) {
      if (synapse.getMatrix() != null) {
        // process each weight matrix
        for (int x = 0; x < synapse.getToNeuronCount(); x++) {
          for (int y = 0; y < synapse.getFromNeuronCount(); y++) {
            synapse.getMatrix().set(y, x, array[result++]);
          }
        }
      }
    }

    return result;
  }
  /**
   * Process a partially connected synapse.
   *
   * @param network The network to process.
   * @param layer The layer to process.
   * @param array The array to process.
   * @param index The current index.
   * @return The index after this synapse has been read.
   */
  private static int processSynapseLimited(
      final BasicNetwork network, final Layer layer, final double[] array, final int index) {
    int result = index;
    // process synapses
    for (final Synapse synapse : network.getStructure().getPreviousSynapses(layer)) {
      if (synapse.getMatrix() != null) {
        // process each weight matrix
        for (int x = 0; x < synapse.getToNeuronCount(); x++) {
          for (int y = 0; y < synapse.getFromNeuronCount(); y++) {
            final double oldValue = synapse.getMatrix().get(y, x);
            double value = array[result++];
            if (Math.abs(oldValue) < network.getStructure().getConnectionLimit()) {
              value = 0;
            }
            synapse.getMatrix().set(y, x, value);
          }
        }
      }
    }

    return result;
  }