Java Code Examples for org.numenta.nupic.Parameters#get()

The following examples show how to use org.numenta.nupic.Parameters#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FoxEatsDemoTest.java    From htm.java-examples with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCreateParameters() {
    setup();
    
    Parameters p = mockDemo.createParameters();
    assertEquals(14, p.keys().size());
    
    int[] dims = (int[])p.get(KEY.COLUMN_DIMENSIONS);
    assertEquals(1, dims.length);
    assertEquals(16384, dims[0]);
    
    int cellsPerCol = (int)p.get(KEY.CELLS_PER_COLUMN);
    assertEquals(8, cellsPerCol);
    
    double conPerm = (double)p.get(KEY.CONNECTED_PERMANENCE);
    assertEquals(0.5, conPerm, 0);
    
    double initPerm = (double)p.get(KEY.INITIAL_PERMANENCE);
    assertEquals(0.4, initPerm, 0);
    
    int minT = (int)p.get(KEY.MIN_THRESHOLD);
    assertEquals(164, minT);
    
    int maxSyns = (int)p.get(KEY.MAX_NEW_SYNAPSE_COUNT);
    assertEquals(164, maxSyns);
    
    double permInc = (double)p.get(KEY.PERMANENCE_INCREMENT);
    assertEquals(0.1, permInc, 0);
    
    double permDec = (double)p.get(KEY.PERMANENCE_DECREMENT);
    assertEquals(0, permDec, 0);
    
    int actT = (int)p.get(KEY.ACTIVATION_THRESHOLD);
    assertEquals(164, actT);
}
 
Example 2
Source File: Layer.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new {@code Layer} using the specified {@link Parameters}
 * 
 * @param name  the name identifier of this {@code Layer}
 * @param n     the parent {@link Network}
 * @param p     the {@link Parameters} to use with this {@code Layer}
 */
public Layer(String name, Network n, Parameters p) {
    this.name = name;
    this.parentNetwork = n;
    this.params = p;

    connections = new Connections();

    this.autoCreateClassifiers = (Boolean)p.get(KEY.AUTO_CLASSIFY);

    factory = new FunctionFactory();

    observableDispatch = createDispatchMap();
}
 
Example 3
Source File: Layer.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new {@code Layer} initialized with the specified algorithmic
 * components.
 * 
 * @param params                    A {@link Parameters} object containing configurations for a
 *                                  SpatialPooler, TemporalMemory, and Encoder (all or none may be used).
 * @param e                         (optional) The Network API only uses a {@link MultiEncoder} at
 *                                  the top level because of its ability to delegate to child encoders.
 * @param sp                        (optional) {@link SpatialPooler}
 * @param tm                        (optional) {@link TemporalMemory}
 * @param autoCreateClassifiers     (optional) Indicates that the {@link Parameters} object
 *                                  contains the configurations necessary to create the required encoders.
 * @param a                         (optional) An {@link Anomaly} computer.
 */
public Layer(Parameters params, MultiEncoder e, SpatialPooler sp, TemporalMemory tm, Boolean autoCreateClassifiers, Anomaly a) {

    // Make sure we have a valid parameters object
    if(params == null) {
        throw new IllegalArgumentException("No parameters specified.");
    }

    // Check to see if the Parameters include the encoder configuration.
    if(params.get(KEY.FIELD_ENCODING_MAP) == null && e != null) {
        throw new IllegalArgumentException("The passed in Parameters must contain a field encoding map " + 
            "specified by org.numenta.nupic.Parameters.KEY.FIELD_ENCODING_MAP");
    }

    this.params = params;
    this.encoder = e;
    this.spatialPooler = sp;
    this.temporalMemory = tm;
    this.autoCreateClassifiers = autoCreateClassifiers;
    this.anomalyComputer = a;

    connections = new Connections();
    factory = new FunctionFactory();

    observableDispatch = createDispatchMap();

    initializeMask();

    if(LOGGER.isDebugEnabled()) {
        LOGGER.debug("Layer successfully created containing: {}{}{}{}{}", 
            (encoder == null ? "" : "MultiEncoder,"), 
            (spatialPooler == null ? "" : "SpatialPooler,"), 
            (temporalMemory == null ? "" : "TemporalMemory,"), 
            (autoCreateClassifiers == null ? "" : "Auto creating Classifiers for each input field."),
            (anomalyComputer == null ? "" : "Anomaly"));
    }
}
 
Example 4
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Initializes this {@code HTMSensor}'s internal encoders if and 
 * only if the encoders have not been previously initialized.
 */
@SuppressWarnings("unchecked")
public void initEncoder(Parameters p) {
    this.localParameters = p;
    
    Map<String, Map<String, Object>> encoderSettings;
    if((encoderSettings = (Map<String, Map<String, Object>>)p.get(KEY.FIELD_ENCODING_MAP)) != null &&
        !encodersInitted) {
        
        initEncoders(encoderSettings);
        makeIndexEncoderMap();
        
        encodersInitted = true;
    }
}
 
Example 5
Source File: NetworkConsistencyTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
private Network getNetwork() {
    // Create Sensor publisher to push NAB input data to network
    PublisherSupplier supplier = PublisherSupplier.builder()
        .addHeader("dayOfWeek")
        .addHeader("number")
        .addHeader("B").build();

    // Get updated model parameters
    Parameters parameters = getParameters();
    Map<String, Map<String, Object>> fieldEncodings = getDayDemoFieldEncodingMap();
    parameters.set(KEY.FIELD_ENCODING_MAP, fieldEncodings);
    
    int cellsPerColumn = (int)parameters.get(KEY.CELLS_PER_COLUMN);
    
    Map<String, Object> params = new HashMap<>();
    params.put(KEY_MODE, Mode.PURE);

    // Create NAB Network
    Network network = Network.create("NAB Network", parameters)
        .add(Network.createRegion("NAB Region")
            .add(Network.createLayer("NAB Layer", parameters)
                .add(Anomaly.create(params))
                .add(new TemporalMemory())
                .add(new SpatialPooler())
                .add(Sensor.create(ObservableSensor::create,
                        SensorParams.create(SensorParams.Keys::obs, "Manual Input", supplier)))));
    
    network.observe().subscribe(new Observer<Inference>() { 
        @Override public void onCompleted() {}
        @Override public void onError(Throwable e) { e.printStackTrace(); }
        @Override
        public void onNext(Inference inf) {
            String layerInput = inf.getLayerInput().toString();
            
            if(inf.getRecordNum() % RECORDS_PER_CYCLE == 0 && doPrintout) {
                System.out.println("--------------------------------------------------------");
                System.out.println("Iteration: " + (inf.getRecordNum() / 7));
            }
            if(doPrintout) System.out.println("===== " + layerInput + "  - Sequence Num: " + (inf.getRecordNum() + 1) + " =====");
            
            if(doPrintout) System.out.println("ScalarEncoder Input = " + layerInput);
            if(doPrintout) System.out.println("ScalarEncoder Output = " + Arrays.toString(inf.getEncoding()));
            
            if(doPrintout) System.out.println("SpatialPooler Output = " + Arrays.toString(inf.getFeedForwardActiveColumns()));
            int[] predictedColumns = SDR.cellsAsColumnIndices(inf.getPredictiveCells(), cellsPerColumn); //Get the predicted column indexes
            if(doPrintout) System.out.println("TemporalMemory Input = " + Arrays.toString(inf.getFeedForwardSparseActives()));
            if(doPrintout) System.out.println("TemporalMemory Prediction = " + Arrays.toString(predictedColumns));
            Set<Cell> actives = inf.getActiveCells();
            int[] actCellIndices = SDR.asCellIndices(actives);
            if(doPrintout) System.out.println("TemporalMemory Active Cells = " + Arrays.toString(actCellIndices));
            Set<Cell> pred = inf.getPredictiveCells();
            int[] predCellIndices = SDR.asCellIndices(pred);
            if(doPrintout) System.out.println("TemporalMemory Predictive Cells = " + Arrays.toString(predCellIndices));
            
            //Anomaly 
            double score = inf.getAnomalyScore();
            if(doPrintout) System.out.println("Anomaly Score = " + score);
            
            if(inf.getRecordNum() / 7 == SAMPLE_WEEK) {
                napiSamples.add(new SampleWeek(inf.getRecordNum() + 1, inf.getEncoding(), inf.getFeedForwardActiveColumns(), 
                    inf.getFeedForwardSparseActives(), predictedColumns, actCellIndices, predCellIndices, score));
            }
        }
    });
    
    return network;
}