Exemplo n.º 1
0
  /**
   * 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;
  }
Exemplo n.º 2
0
  /**
   * 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;
  }
Exemplo n.º 3
0
  /**
   * 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;
  }