Ejemplo n.º 1
0
  /**
   * This is the primary public method of the SpatialPooler class. This function takes a input
   * vector and outputs the indices of the active columns. If 'learn' is set to True, this method
   * also updates the permanences of the columns.
   *
   * @param inputVector An array of 0's and 1's that comprises the input to the spatial pooler. The
   *     array will be treated as a one dimensional array, therefore the dimensions of the array do
   *     not have to match the exact dimensions specified in the class constructor. In fact, even a
   *     list would suffice. The number of input bits in the vector must, however, match the number
   *     of bits specified by the call to the constructor. Therefore there must be a '0' or '1' in
   *     the array for every input bit.
   * @param activeArray An array whose size is equal to the number of columns. Before the function
   *     returns this array will be populated with 1's at the indices of the active columns, and 0's
   *     everywhere else.
   * @param learn A boolean value indicating whether learning should be performed. Learning entails
   *     updating the permanence values of the synapses, and hence modifying the 'state' of the
   *     model. Setting learning to 'off' freezes the SP and has many uses. For example, you might
   *     want to feed in various inputs and examine the resulting SDR's.
   */
  public void compute(Connections c, int[] inputVector, int[] activeArray, boolean learn) {
    if (inputVector.length != c.getNumInputs()) {
      throw new InvalidSPParamValueException(
          "Input array must be same size as the defined number of inputs: From Params: "
              + c.getNumInputs()
              + ", From Input Vector: "
              + inputVector.length);
    }

    updateBookeepingVars(c, learn);
    int[] overlaps = c.setOverlaps(calculateOverlap(c, inputVector));

    double[] boostedOverlaps;
    if (learn) {
      boostedOverlaps = ArrayUtils.multiply(c.getBoostFactors(), overlaps);
    } else {
      boostedOverlaps = ArrayUtils.toDoubleArray(overlaps);
    }

    int[] activeColumns = inhibitColumns(c, c.setBoostedOverlaps(boostedOverlaps));

    if (learn) {
      adaptSynapses(c, inputVector, activeColumns);
      updateDutyCycles(c, overlaps, activeColumns);
      bumpUpWeakColumns(c);
      updateBoostFactors(c);
      if (isUpdateRound(c)) {
        updateInhibitionRadius(c);
        updateMinDutyCycles(c);
      }
    }

    Arrays.fill(activeArray, 0);
    if (activeColumns.length > 0) {
      ArrayUtils.setIndexesTo(activeArray, activeColumns, 1);
    }
  }