/** * Uses the specified {@link Connections} object to Build the structural * anatomy needed by this {@code TemporalMemory} to implement its algorithms. * * The connections object holds the {@link Column} and {@link Cell} infrastructure, * and is used by both the {@link SpatialPooler} and {@link TemporalMemory}. Either of * these can be used separately, and therefore this Connections object may have its * Columns and Cells initialized by either the init method of the SpatialPooler or the * init method of the TemporalMemory. We check for this so that complete initialization * of both Columns and Cells occurs, without either being redundant (initialized more than * once). However, {@link Cell}s only get created when initializing a TemporalMemory, because * they are not used by the SpatialPooler. * * @param c {@link Connections} object */ public static void Init(Connections c) { SparseObjectMatrix <Column> matrix = c.GetMemory() ?? new SparseObjectMatrix <Column>(c.GetColumnDimensions()); c.SetMemory(matrix); int numColumns = matrix.GetMaxIndex() + 1; c.SetNumColumns(numColumns); int cellsPerColumn = c.GetCellsPerColumn(); Cell[] cells = new Cell[numColumns * cellsPerColumn]; //Used as flag to determine if Column objects have been created. Column colZero = matrix.GetObject(0); for (int i = 0; i < numColumns; i++) { Column column = colZero == null ? new Column(cellsPerColumn, i) : matrix.GetObject(i); for (int j = 0; j < cellsPerColumn; j++) { cells[i * cellsPerColumn + j] = column.GetCell(j); } //If columns have not been previously configured if (colZero == null) { matrix.Set(i, column); } } //Only the TemporalMemory initializes cells so no need to test for redundancy c.SetCells(cells); }
/** * Return the internal topDownMapping matrix used for handling the * {@link #getBucketInfo(int[])} and {@link #topDownCompute(int[])} methods. This is a matrix, one row per * category (bucket) where each row contains the encoded output for that * category. * * @return {@link SparseObjectMatrix} */ public SparseObjectMatrix <int[]> GetTopDownMapping() { if (topDownMapping == null) { topDownMapping = new SparseObjectMatrix <int[]>(new int[] { _sdrByCategory.Count }); int[] outputSpace = new int[GetN()]; List <string> categories = _sdrByCategory.Keys.ToList(); int inx = 0; foreach (string category in categories) { EncodeIntoArray(category, outputSpace); topDownMapping.Set(inx, Arrays.CopyOf(outputSpace, outputSpace.Length)); inx++; } } return(topDownMapping); }
/** * Return the internal topDownMapping matrix used for handling the * bucketInfo() and topDownCompute() methods. This is a matrix, one row per * category (bucket) where each row contains the encoded output for that * category. * * @param c the connections memory * @return the internal topDownMapping */ public SparseObjectMatrix <int[]> GetTopDownMapping() { if (base.topDownMapping == null) { //The input scalar value corresponding to each possible output encoding if (IsPeriodic()) { SetTopDownValues( ArrayUtils.Arrange(GetMinVal() + GetResolution() / 2.0, GetMaxVal(), GetResolution())); } else { //Number of values is (max-min)/resolutions SetTopDownValues( ArrayUtils.Arrange(GetMinVal(), GetMaxVal() + GetResolution() / 2.0, GetResolution())); } } //Each row represents an encoded output pattern int numCategories = GetTopDownValues().Length; SparseObjectMatrix <int[]> topDownMapping; SetTopDownMapping( topDownMapping = new SparseObjectMatrix <int[]>( new int[] { numCategories })); double[] topDownValues = GetTopDownValues(); int[] outputSpace = new int[GetN()]; double minVal = GetMinVal(); double maxVal = GetMaxVal(); for (int i = 0; i < numCategories; i++) { double value = topDownValues[i]; value = Math.Max(value, minVal); value = Math.Min(value, maxVal); EncodeIntoArray(value, outputSpace); topDownMapping.Set(i, Arrays.CopyOf(outputSpace, outputSpace.Length)); } return(topDownMapping); }