/** * Called to initialize the structural anatomy with configured values and prepare the anatomical * entities for activation. * * @param c */ public void initMatrices(final Connections c) { SparseObjectMatrix<Column> mem = c.getMemory(); c.setMemory(mem == null ? mem = new SparseObjectMatrix<>(c.getColumnDimensions()) : mem); c.setInputMatrix(new SparseBinaryMatrix(c.getInputDimensions())); // Calculate numInputs and numColumns int numInputs = c.getInputMatrix().getMaxIndex() + 1; int numColumns = c.getMemory().getMaxIndex() + 1; if (numColumns <= 0) { throw new InvalidSPParamValueException("Invalid number of columns: " + numColumns); } if (numInputs <= 0) { throw new InvalidSPParamValueException("Invalid number of inputs: " + numInputs); } c.setNumInputs(numInputs); c.setNumColumns(numColumns); // Fill the sparse matrix with column objects for (int i = 0; i < numColumns; i++) { mem.set(i, new Column(c.getCellsPerColumn(), i)); } c.setPotentialPools(new SparseObjectMatrix<Pool>(c.getMemory().getDimensions())); c.setConnectedMatrix(new SparseBinaryMatrix(new int[] {numColumns, numInputs})); // Initialize state meta-management statistics c.setOverlapDutyCycles(new double[numColumns]); c.setActiveDutyCycles(new double[numColumns]); c.setMinOverlapDutyCycles(new double[numColumns]); c.setMinActiveDutyCycles(new double[numColumns]); c.setBoostFactors(new double[numColumns]); Arrays.fill(c.getBoostFactors(), 1); }
/** * Maps a column to its input bits. This method encapsulates the topology of the region. It takes * the index of the column as an argument and determines what are the indices of the input vector * that are located within the column's potential pool. The return value is a list containing the * indices of the input bits. The current implementation of the base class only supports a 1 * dimensional topology of columns with a 1 dimensional topology of inputs. To extend this class * to support 2-D topology you will need to override this method. Examples of the expected output * of this method: * If the potentialRadius is greater than or equal to the entire input space, * (global visibility), then this method returns an array filled with all the indices * If the * topology is one dimensional, and the potentialRadius is 5, this method will return an array * containing 5 consecutive values centered on the index of the column (wrapping around if * necessary). * If the topology is two dimensional (not implemented), and the potentialRadius is * 5, the method should return an array containing 25 '1's, where the exact indices are to be * determined by the mapping from 1-D index to 2-D position. * * @param c {@link Connections} the main memory model * @param columnIndex The index identifying a column in the permanence, potential and connectivity * matrices. * @param wrapAround A boolean value indicating that boundaries should be ignored. * @return */ public int[] mapPotential(Connections c, int columnIndex, boolean wrapAround) { int index = mapColumn(c, columnIndex); TIntArrayList indices = getNeighborsND(c, index, c.getInputMatrix(), c.getPotentialRadius(), wrapAround); indices.add(index); // TODO: See https://github.com/numenta/nupic.core/issues/128 indices.sort(); int[] retVal = new int[(int) Math.round(indices.size() * c.getPotentialPct())]; return ArrayUtils.sample(indices, retVal, c.getRandom()); }
/** * The range of connectedSynapses per column, averaged for each dimension. This value is used to * calculate the inhibition radius. This variation of the function supports arbitrary column * dimensions. * * @param c the {@link Connections} (spatial pooler memory) * @param columnIndex the current column for which to avg. * @return */ public double avgConnectedSpanForColumnND(Connections c, int columnIndex) { int[] dimensions = c.getInputDimensions(); int[] connected = c.getColumn(columnIndex).getProximalDendrite().getConnectedSynapsesSparse(c); if (connected == null || connected.length == 0) return 0; int[] maxCoord = new int[c.getInputDimensions().length]; int[] minCoord = new int[c.getInputDimensions().length]; Arrays.fill(maxCoord, -1); Arrays.fill(minCoord, ArrayUtils.max(dimensions)); SparseMatrix<?> inputMatrix = c.getInputMatrix(); for (int i = 0; i < connected.length; i++) { maxCoord = ArrayUtils.maxBetween(maxCoord, inputMatrix.computeCoordinates(connected[i])); minCoord = ArrayUtils.minBetween(minCoord, inputMatrix.computeCoordinates(connected[i])); } return ArrayUtils.average(ArrayUtils.add(ArrayUtils.subtract(maxCoord, minCoord), 1)); }
/** * Maps a column to its respective input index, keeping to the topology of the region. It takes * the index of the column as an argument and determines what is the index of the flattened input * vector that is to be the center of the column's potential pool. It distributes the columns over * the inputs uniformly. The return value is an integer representing the index of the input bit. * Examples of the expected output of this method: * If the topology is one dimensional, and the * column index is 0, this method will return the input index 0. If the column index is 1, and * there are 3 columns over 7 inputs, this method will return the input index 3. * If the topology * is two dimensional, with column dimensions [3, 5] and input dimensions [7, 11], and the column * index is 3, the method returns input index 8. * * @param columnIndex The index identifying a column in the permanence, potential and connectivity * matrices. * @return A boolean value indicating that boundaries should be ignored. */ public int mapColumn(Connections c, int columnIndex) { int[] columnCoords = c.getMemory().computeCoordinates(columnIndex); double[] colCoords = ArrayUtils.toDoubleArray(columnCoords); double[] ratios = ArrayUtils.divide(colCoords, ArrayUtils.toDoubleArray(c.getColumnDimensions()), 0, 0); double[] inputCoords = ArrayUtils.multiply(ArrayUtils.toDoubleArray(c.getInputDimensions()), ratios, 0, 0); inputCoords = ArrayUtils.d_add( inputCoords, ArrayUtils.multiply( ArrayUtils.divide( ArrayUtils.toDoubleArray(c.getInputDimensions()), ArrayUtils.toDoubleArray(c.getColumnDimensions()), 0, 0), 0.5)); int[] inputCoordInts = ArrayUtils.clip(ArrayUtils.toIntArray(inputCoords), c.getInputDimensions(), -1); return c.getInputMatrix().computeIndex(inputCoordInts); }