org.numenta.nupic.encoders.MultiEncoder Java Examples
The following examples show how to use
org.numenta.nupic.encoders.MultiEncoder.
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: AbstractHTMInferenceOperator.java From flink-htm with GNU Affero General Public License v3.0 | 6 votes |
/** * Initialize the input function to map input elements to HTM encoder input. * @throws Exception */ protected void initInputFunction() throws Exception { // it is premature to call getInputNetwork, because no 'key' is available // when the operator is first opened. Network network = networkFactory.createNetwork(null); MultiEncoder encoder = network.getEncoder(); if(encoder == null) throw new IllegalArgumentException("a network encoder must be provided"); // handle the situation where an encoder parameter map was supplied rather than a fully-baked encoder. if(encoder.getEncoders(encoder) == null || encoder.getEncoders(encoder).size() < 1) { Map<String, Map<String, Object>> encoderParams = (Map<String, Map<String, Object>>) network.getParameters().get(Parameters.KEY.FIELD_ENCODING_MAP); if(encoderParams == null || encoderParams.size() < 1) { throw new IllegalStateException("No field encoding map found for MultiEncoder"); } encoder.addMultipleEncoders(encoderParams); } // generate the encoder input function final GenerateEncoderInputFunction<IN> generator = new GenerateEncoderInputFunction<>((CompositeType<IN>) inputType, encoder, executionConfig); inputFunction = generator.generate(); }
Example #2
Source File: TestHarness.java From flink-htm with GNU Affero General Public License v3.0 | 6 votes |
@Override public Network createNetwork(Object key) { Parameters p = getParameters(); p = p.union(getEncoderParams()); p.set(Parameters.KEY.COLUMN_DIMENSIONS, new int[] { 30 }); p.set(Parameters.KEY.SYN_PERM_INACTIVE_DEC, 0.1); p.set(Parameters.KEY.SYN_PERM_ACTIVE_INC, 0.1); p.set(Parameters.KEY.SYN_PERM_TRIM_THRESHOLD, 0.05); p.set(Parameters.KEY.SYN_PERM_CONNECTED, 0.4); p.set(Parameters.KEY.MAX_BOOST, 10.0); p.set(Parameters.KEY.DUTY_CYCLE_PERIOD, 7); p.set(Parameters.KEY.RANDOM, new MersenneTwister(42)); Map<String, Object> params = new HashMap<>(); params.put(KEY_MODE, Anomaly.Mode.PURE); Network n = Network.create("DayDemoNetwork", p) .add(Network.createRegion("r1") .add(Network.createLayer("1", p) .alterParameter(Parameters.KEY.AUTO_CLASSIFY, Boolean.TRUE) .alterParameter(Parameters.KEY.INFERRED_FIELDS, getInferredFieldsMaps()) .add(new TemporalMemory()) .add(new SpatialPooler()) .add(MultiEncoder.builder().name("").build()))); return n; }
Example #3
Source File: RunLayer.java From htm.java with GNU Affero General Public License v3.0 | 6 votes |
public static MultiEncoder createEncoder() { MultiEncoder encoder = MultiEncoder.builder().name("").build(); ScalarEncoder se = ScalarEncoder.builder() .n(50) .w(21) .minVal(0) .maxVal(100) .periodic(false) .clipInput(true) .name("value") .build(); encoder.addEncoder("value", se); DateEncoder de = DateEncoder.builder() .timeOfDay(21, 9.5) .name("timestamp") .build(); encoder.addEncoder("timestamp", de); return encoder; }
Example #4
Source File: Layer.java From htm.java with GNU Affero General Public License v3.0 | 6 votes |
/** * Returns the resident {@link MultiEncoder} or the encoder residing in this * {@code Layer}'s {@link Sensor}, if any. * * @return */ public MultiEncoder getEncoder() { if(encoder != null) { return encoder; } if(hasSensor()) { return sensor.getEncoder(); } MultiEncoder e = parentNetwork.getEncoder(); if(e != null) { return e; } return null; }
Example #5
Source File: RegionTest.java From htm.java with GNU Affero General Public License v3.0 | 6 votes |
/** * Test that we automatically calculate the input dimensions despite * there being an improper Parameter setting. */ @Test public void testInputDimensionsAutomaticallyInferredFromEncoderWidth() { Parameters p = NetworkTestHarness.getParameters(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); // Purposefully set this to be wrong p.set(KEY.INPUT_DIMENSIONS, new int[] { 40, 40 }); Network.create("test network", p) .add(Network.createRegion("r1") .add(Network.createLayer("4", p) .add(MultiEncoder.builder().name("").build())) .close()); // Should correct the above ( {40,40} ) to have only one dimension whose width is 8 ( {8} ) assertTrue(Arrays.equals(new int[] { 8 }, (int[])p.get(KEY.INPUT_DIMENSIONS))); }
Example #6
Source File: RegionTest.java From htm.java with GNU Affero General Public License v3.0 | 6 votes |
@Test public void testAdd() { Parameters p = NetworkTestHarness.getParameters(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); Network n = Network.create("test network", p) .add(Network.createRegion("r1") .add(Network.createLayer("4", p) .add(MultiEncoder.builder().name("").build()))); Region r1 = n.lookup("r1"); Layer<?>layer4 = r1.lookup("4"); assertNotNull(layer4); assertEquals("r1:4", layer4.getName()); try { r1.add(Network.createLayer("4", p)); fail(); }catch(Exception e) { assertTrue(e.getClass().isAssignableFrom(IllegalArgumentException.class)); assertEquals("A Layer with the name: 4 has already been added to this Region.", e.getMessage()); } }
Example #7
Source File: RegionTest.java From htm.java with GNU Affero General Public License v3.0 | 6 votes |
@Test public void testAutomaticClose() { Parameters p = NetworkTestHarness.getParameters(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); Network n = Network.create("test network", p) .add(Network.createRegion("r1") .add(Network.createLayer("4", p) .add(MultiEncoder.builder().name("").build()))); //.close(); // Not necessary due to implicit call during start() or compute() Region r1 = n.lookup("r1"); r1.start(); assertTrue(r1.isClosed()); try { r1.add(Network.createLayer("5", p)); fail(); }catch(Exception e) { assertTrue(e.getClass().isAssignableFrom(IllegalStateException.class)); assertEquals("Cannot add Layers when Region has already been closed.", e.getMessage()); } }
Example #8
Source File: RegionTest.java From htm.java with GNU Affero General Public License v3.0 | 6 votes |
@Test public void testClose() { Parameters p = NetworkTestHarness.getParameters(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); Network n = Network.create("test network", p) .add(Network.createRegion("r1") .add(Network.createLayer("4", p) .add(MultiEncoder.builder().name("").build())) .close()); assertTrue(n.lookup("r1").isClosed()); try { n.lookup("r1").add(Network.createLayer("5", p)); fail(); }catch(Exception e) { assertTrue(e.getClass().isAssignableFrom(IllegalStateException.class)); assertEquals("Cannot add Layers when Region has already been closed.", e.getMessage()); } }
Example #9
Source File: RunLayer.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
public static int[] testEncoder(MultiEncoder encoder) { DateTime timestamp = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2014-04-01 00:00:00"); double value = 20.0; Map<String, Object> d = new HashMap<String, Object>(); d.put("value", value); d.put("timestamp", timestamp); int[] output = encoder.encode(d); System.out.println("ScalarEncoder Output = " + Arrays.toString(output)); System.out.println("len = " + output.length); return output; }
Example #10
Source File: LayerTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
@Test public void TestMakeClassifiersWithInvalidInferredFieldsKey() { // Setup Parameters Parameters p = Parameters.getAllDefaultParameters(); Map<String, Class<? extends Classifier>> inferredFieldsMap = new HashMap<>(); inferredFieldsMap.put("field1", Classifier.class); p.set(KEY.INFERRED_FIELDS, inferredFieldsMap); // Create MultiEncoder and add the fields' encoders to it MultiEncoder me = MultiEncoder.builder().name("").build(); me.addEncoder( "field1", RandomDistributedScalarEncoder.builder().resolution(1).build() ); // Create a Layer with Parameters and MultiEncoder Layer<Map<String, Object>> l = new Layer<>( p, me, new SpatialPooler(), new TemporalMemory(), true, null ); // Make sure the makeClassifiers() method throws exception due to // absence of KEY.INFERRED_FIELDS in the Parameters object try { NamedTuple nt = l.makeClassifiers(l.getEncoder()); } catch (IllegalStateException e) { assertTrue(e.getMessage().contains("Invalid Classifier class token")); } }
Example #11
Source File: LayerTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
@Test public void TestMakeClassifiersWithNoInferredFieldsKey() { // Setup Parameters Parameters p = Parameters.getAllDefaultParameters(); // Create MultiEncoder MultiEncoder me = MultiEncoder.builder().name("").build(); // Create a Layer with Parameters and MultiEncoder Layer<Map<String, Object>> l = new Layer<>( p, me, new SpatialPooler(), new TemporalMemory(), true, null ); // Make sure the makeClassifiers() method throws exception due to // absence of KEY.INFERRED_FIELDS in the Parameters object try { l.makeClassifiers(l.getEncoder()); } catch (IllegalStateException e) { assertTrue(e.getMessage().contains("KEY.INFERRED_FIELDS")); assertTrue(e.getMessage().contains("null")); assertTrue(e.getMessage().contains("empty")); } }
Example #12
Source File: LayerTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void isClosedAddMultiEncoderTest() { Parameters p = NetworkTestHarness.getParameters(); p = p.union(NetworkTestHarness.getNetworkDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); Layer<?> l = Network.createLayer("l", p); l.close(); l.add(MultiEncoder.builder().name("").build()); }
Example #13
Source File: LayerTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Simple test to verify data gets passed through the {@link CLAClassifier} * configured within the chain of components. */ @Test public void testBasicClassifierSetup() { Parameters p = NetworkTestHarness.getParameters().copy(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new UniversalRandom(42)); p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("dayOfWeek", CLAClassifier.class)); MultiEncoder me = MultiEncoder.builder().name("").build(); Layer<Map<String, Object>> l = new Layer<>(p, me, new SpatialPooler(), new TemporalMemory(), Boolean.TRUE, null); TestObserver<Inference> tester; l.subscribe(tester = new TestObserver<Inference>() { @Override public void onCompleted() {} @Override public void onNext(Inference i) { assertNotNull(i); assertEquals(42, i.getSDR().length); } }); // Now push some fake data through so that "onNext" is called above Map<String, Object> multiInput = new HashMap<>(); multiInput.put("dayOfWeek", 0.0); l.compute(multiInput); // Check for exception during the TestObserver's onNext() execution. checkObserver(tester); }
Example #14
Source File: LayerTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
@Test public void testBasicSetupEncoder_UsingObserve() { Parameters p = NetworkTestHarness.getParameters().copy(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); MultiEncoder me = MultiEncoder.builder().name("").build(); Layer<Map<String, Object>> l = new Layer<>(p, me, null, null, null, null); final int[][] expected = new int[7][8]; expected[0] = new int[] { 1, 1, 0, 0, 0, 0, 0, 1 }; expected[1] = new int[] { 1, 1, 1, 0, 0, 0, 0, 0 }; expected[2] = new int[] { 0, 1, 1, 1, 0, 0, 0, 0 }; expected[3] = new int[] { 0, 0, 1, 1, 1, 0, 0, 0 }; expected[4] = new int[] { 0, 0, 0, 1, 1, 1, 0, 0 }; expected[5] = new int[] { 0, 0, 0, 0, 1, 1, 1, 0 }; expected[6] = new int[] { 0, 0, 0, 0, 0, 1, 1, 1 }; Observable<Inference> o = l.observe(); TestObserver<Inference> tester; o.subscribe(tester = new TestObserver<Inference>() { int seq = 0; @Override public void onCompleted() {} @Override public void onNext(Inference output) { assertTrue(Arrays.equals(expected[seq++], output.getSDR())); } }); Map<String, Object> inputs = new HashMap<String, Object>(); for(double i = 0;i < 7;i++) { inputs.put("dayOfWeek", i); l.compute(inputs); } // Check for exception during the TestObserver's onNext() execution. checkObserver(tester); }
Example #15
Source File: LayerTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
@Test public void testBasicSetupEncoder_UsingSubscribe() { Parameters p = NetworkTestHarness.getParameters().copy(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); MultiEncoder me = MultiEncoder.builder().name("").build(); Layer<Map<String, Object>> l = new Layer<>(p, me, null, null, null, null); final int[][] expected = new int[7][8]; expected[0] = new int[] { 1, 1, 0, 0, 0, 0, 0, 1 }; expected[1] = new int[] { 1, 1, 1, 0, 0, 0, 0, 0 }; expected[2] = new int[] { 0, 1, 1, 1, 0, 0, 0, 0 }; expected[3] = new int[] { 0, 0, 1, 1, 1, 0, 0, 0 }; expected[4] = new int[] { 0, 0, 0, 1, 1, 1, 0, 0 }; expected[5] = new int[] { 0, 0, 0, 0, 1, 1, 1, 0 }; expected[6] = new int[] { 0, 0, 0, 0, 0, 1, 1, 1 }; TestObserver<Inference> tester; l.subscribe(tester = new TestObserver<Inference>() { int seq = 0; @Override public void onCompleted() {} @Override public void onNext(Inference output) { assertTrue(Arrays.equals(expected[seq++], output.getSDR())); } }); Map<String, Object> inputs = new HashMap<String, Object>(); for(double i = 0;i < 7;i++) { inputs.put("dayOfWeek", i); l.compute(inputs); } // Check for exception during the TestObserver's onNext() execution. checkObserver(tester); }
Example #16
Source File: RegionTest.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Test encoder bubbles up to L1 */ @Test public void testEncoderPassesUpToTopLayer() { Parameters p = NetworkTestHarness.getParameters(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.RANDOM, new MersenneTwister(42)); Map<String, Object> params = new HashMap<>(); params.put(KEY_MODE, Mode.PURE); Network n = Network.create("test network", p) .add(Network.createRegion("r1") .add(Network.createLayer("1", p) .alterParameter(KEY.AUTO_CLASSIFY, Boolean.TRUE)) .add(Network.createLayer("2", p) .add(Anomaly.create(params))) .add(Network.createLayer("3", p) .add(new TemporalMemory())) .add(Network.createLayer("4", p) .add(new SpatialPooler()) .add(MultiEncoder.builder().name("").build()))); Region r1 = n.lookup("r1"); r1.connect("1", "2").connect("2", "3").connect("3", "4"); assertNotNull(r1.lookup("1").getEncoder()); }
Example #17
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Searches through the specified {@link MultiEncoder}'s previously configured * encoders to find and return one that is of type {@link DateEncoder} * * @param enc the containing {@code MultiEncoder} * @return */ private Optional<DateEncoder> getDateEncoder(MultiEncoder enc) { for(EncoderTuple t : enc.getEncoders(enc)) { if(t.getEncoder() instanceof DateEncoder) { return Optional.of((DateEncoder)t.getEncoder()); } } return Optional.empty(); }
Example #18
Source File: GenerateEncoderInputFunction.java From flink-htm with GNU Affero General Public License v3.0 | 5 votes |
public GenerateEncoderInputFunction(CompositeType<T> inputTypeInfo, MultiEncoder inputTypeEncoder, ExecutionConfig config) { if(inputTypeEncoder == null) throw new IllegalArgumentException("a network encoder must be provided"); this.inputTypeInfo = inputTypeInfo; this.inputTypeEncoder = inputTypeEncoder; this.config = config; }
Example #19
Source File: Layer.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * 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 #20
Source File: Layer.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Adds a {@link MultiEncoder} to this {@code Layer} * * @param encoder the added MultiEncoder * @return this Layer instance (in fluent-style) */ public Layer<T> add(MultiEncoder encoder) { if(isClosed) { throw new IllegalStateException("Layer already \"closed\""); } this.encoder = encoder; return this; }
Example #21
Source File: Layer.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Creates the {@link NamedTuple} of names to encoders used in the * observable sequence. * * @param encoder * @return */ @SuppressWarnings("unchecked") NamedTuple makeClassifiers(MultiEncoder encoder) { Map<String, Class<? extends Classifier>> inferredFields = (Map<String, Class<? extends Classifier>>) params.get(KEY.INFERRED_FIELDS); if(inferredFields == null || inferredFields.entrySet().size() == 0) { throw new IllegalStateException( "KEY.AUTO_CLASSIFY has been set to \"true\", but KEY.INFERRED_FIELDS is null or\n\t" + "empty. Must specify desired Classifier for at least one input field in\n\t" + "KEY.INFERRED_FIELDS or set KEY.AUTO_CLASSIFY to \"false\" (which is its default\n\t" + "value in Parameters)." ); } String[] names = new String[encoder.getEncoders(encoder).size()]; Classifier[] ca = new Classifier[names.length]; int i = 0; for(EncoderTuple et : encoder.getEncoders(encoder)) { names[i] = et.getName(); Class fieldClassifier = inferredFields.get(et.getName()); if(fieldClassifier == null) { LOGGER.info("Not classifying \"" + et.getName() + "\" input field"); } else if(CLAClassifier.class.isAssignableFrom(fieldClassifier)) { LOGGER.info("Classifying \"" + et.getName() + "\" input field with CLAClassifier"); ca[i] = new CLAClassifier(); } else if(SDRClassifier.class.isAssignableFrom(fieldClassifier)) { LOGGER.info("Classifying \"" + et.getName() + "\" input field with SDRClassifier"); ca[i] = new SDRClassifier(); } else { throw new IllegalStateException( "Invalid Classifier class token, \"" + fieldClassifier + "\",\n\t" + "specified for, \"" + et.getName() + "\", input field.\n\t" + "Valid class tokens are CLAClassifier.class and SDRClassifier.class" ); } i++; } return new NamedTuple(names, (Object[])ca); }
Example #22
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Called internally during construction to build the encoders * needed to process the configured field types. */ @SuppressWarnings("unchecked") private void createEncoder() { encoder = MultiEncoder.builder().name("MultiEncoder").build(); Map<String, Map<String, Object>> encoderSettings; if(localParameters != null && (encoderSettings = (Map<String, Map<String, Object>>)localParameters.get(KEY.FIELD_ENCODING_MAP)) != null && !encoderSettings.isEmpty()) { initEncoders(encoderSettings); makeIndexEncoderMap(); } }
Example #23
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Searches through the specified {@link MultiEncoder}'s previously configured * encoders to find and return one that is of type {@link CoordinateEncoder} or * {@link GeospatialCoordinateEncoder} * * @param enc the containing {@code MultiEncoder} * @return */ private Optional<Encoder<?>> getCoordinateEncoder(MultiEncoder enc) { for(EncoderTuple t : enc.getEncoders(enc)) { if((t.getEncoder() instanceof CoordinateEncoder) || (t.getEncoder() instanceof GeospatialCoordinateEncoder)) { return Optional.of(t.getEncoder()); } } return Optional.empty(); }
Example #24
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Searches through the specified {@link MultiEncoder}'s previously configured * encoders to find and return one that is of type {@link CategoryEncoder} or * {@link SDRCategoryEncoder} * * @param enc the containing {@code MultiEncoder} * @return */ private Optional<Encoder<?>> getCategoryEncoder(MultiEncoder enc) { for(EncoderTuple t : enc.getEncoders(enc)) { if((t.getEncoder() instanceof CategoryEncoder) || (t.getEncoder() instanceof SDRCategoryEncoder)) { return Optional.of(t.getEncoder()); } } return Optional.empty(); }
Example #25
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Searches through the specified {@link MultiEncoder}'s previously configured * encoders to find and return one that is of type {@link DateEncoder} * * @param enc the containing {@code MultiEncoder} * @return */ private Optional<SDRPassThroughEncoder> getSDRPassThroughEncoder(MultiEncoder enc) { for(EncoderTuple t : enc.getEncoders(enc)) { if(t.getEncoder() instanceof SDRPassThroughEncoder) { return Optional.of((SDRPassThroughEncoder)t.getEncoder()); } } return Optional.empty(); }
Example #26
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Searches through the specified {@link MultiEncoder}'s previously configured * encoders to find and return one that is of type {@link ScalarEncoder}, * {@link RandomDistributedScalarEncoder}, {@link AdaptiveScalarEncoder}, * {@link LogEncoder} or {@link DeltaEncoder}. * * @param enc the containing {@code MultiEncoder} * @return */ private Optional<Encoder<?>> getNumberEncoder(MultiEncoder enc) { for(EncoderTuple t : enc.getEncoders(enc)) { if((t.getEncoder() instanceof RandomDistributedScalarEncoder) || (t.getEncoder() instanceof ScalarEncoder) || (t.getEncoder() instanceof AdaptiveScalarEncoder) || (t.getEncoder() instanceof LogEncoder) || (t.getEncoder() instanceof DeltaEncoder)) { return Optional.of(t.getEncoder()); } } return Optional.empty(); }
Example #27
Source File: HTMSensor.java From htm.java with GNU Affero General Public License v3.0 | 5 votes |
/** * Called internally to initialize this sensor's encoders * @param encoderSettings */ private void initEncoders(Map<String, Map<String, Object>> encoderSettings) { if(encoder instanceof MultiEncoder) { if(encoderSettings == null || encoderSettings.isEmpty()) { throw new IllegalArgumentException( "Cannot initialize this Sensor's MultiEncoder with a null settings"); } } MultiEncoderAssembler.assemble(encoder, encoderSettings); }
Example #28
Source File: RegionTest.java From htm.java with GNU Affero General Public License v3.0 | 4 votes |
@Test public void testIsLearn() { Parameters p = NetworkTestHarness.getParameters().copy(); p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); p.set(KEY.COLUMN_DIMENSIONS, new int[] { 30 }); p.set(KEY.SYN_PERM_INACTIVE_DEC, 0.1); p.set(KEY.SYN_PERM_ACTIVE_INC, 0.1); p.set(KEY.SYN_PERM_TRIM_THRESHOLD, 0.05); p.set(KEY.SYN_PERM_CONNECTED, 0.4); p.set(KEY.MAX_BOOST, 10.0); p.set(KEY.DUTY_CYCLE_PERIOD, 7); p.set(KEY.RANDOM, new MersenneTwister(42)); p.set(KEY.INFERRED_FIELDS, getInferredFieldsMap("dayOfWeek", CLAClassifier.class)); Map<String, Object> params = new HashMap<>(); params.put(KEY_MODE, Mode.PURE); Network n = Network.create("test network", p) .add(Network.createRegion("r1") .add(Network.createLayer("1", p) .alterParameter(KEY.AUTO_CLASSIFY, Boolean.TRUE)) .add(Network.createLayer("2", p) .add(Anomaly.create(params))) .add(Network.createLayer("3", p) .add(new TemporalMemory())) .add(Network.createLayer("4", p) .add(new SpatialPooler()) .add(MultiEncoder.builder().name("").build())) .connect("1", "2") .connect("2", "3") .connect("3", "4")); n.lookup("r1").close(); n.setLearn(false); assertFalse(n.isLearn()); Region r1 = n.lookup("r1"); assertFalse(n.isLearn()); Layer<?> layer = r1.getTail(); assertFalse(layer.isLearn()); while(layer.getNext() != null) { layer = layer.getNext(); assertFalse(layer.isLearn()); } }
Example #29
Source File: HTMSensorTest.java From htm.java with GNU Affero General Public License v3.0 | 4 votes |
/** * Special test case for extra processing for category encoder lists */ @Test public void testCategoryEncoderCreation() { Sensor<File> sensor = Sensor.create( FileSensor::create, SensorParams.create( Keys::path, "", ResourceLocator.path("rec-center-hourly-4period-cat.csv"))); // Cast the ValueList to the more complex type (Header) HTMSensor<File> htmSensor = (HTMSensor<File>)sensor; Header meta = (Header)htmSensor.getMetaInfo(); assertTrue(meta.getFieldTypes().stream().allMatch( l -> l.equals(FieldMetaType.DATETIME) || l.equals(FieldMetaType.FLOAT) || l.equals(FieldMetaType.LIST))); assertTrue(meta.getFieldNames().stream().allMatch( l -> l.equals("timestamp") || l.equals("consumption") || l.equals("type"))); assertTrue(meta.getFlags().stream().allMatch( l -> l.equals(SensorFlags.T) || l.equals(SensorFlags.B) || l.equals(SensorFlags.C))); // Set the parameters on the sensor. // This enables it to auto-configure itself; a step which will // be done at the Region level. Encoder<Object> multiEncoder = htmSensor.getEncoder(); assertNotNull(multiEncoder); assertTrue(multiEncoder instanceof MultiEncoder); // Set the Local parameters on the Sensor htmSensor.initEncoder(getCategoryEncoderParams()); List<EncoderTuple> encoders = multiEncoder.getEncoders(multiEncoder); assertEquals(3, encoders.size()); DateEncoder dateEnc = (DateEncoder)encoders.get(1).getEncoder(); SDRCategoryEncoder catEnc = (SDRCategoryEncoder)encoders.get(2).getEncoder(); assertEquals("[0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", Arrays.toString(catEnc.encode("ES"))); // Now test the encoding of an input row Map<String, Object> d = new HashMap<String, Object>(); d.put("timestamp", dateEnc.parse("7/12/10 13:10")); d.put("consumption", 35.3); d.put("type", "ES"); int[] output = multiEncoder.encode(d); System.out.println("output = "+ Arrays.toString(output)); int[] expected = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; assertTrue(Arrays.equals(expected, output)); }
Example #30
Source File: HTMSensorTest.java From htm.java with GNU Affero General Public License v3.0 | 4 votes |
/** * Tests the auto-creation of Encoders from Sensor meta data. */ @Test public void testInternalEncoderCreation() { Sensor<File> sensor = Sensor.create( FileSensor::create, SensorParams.create( Keys::path, "", ResourceLocator.path("rec-center-hourly.csv"))); // Cast the ValueList to the more complex type (Header) HTMSensor<File> htmSensor = (HTMSensor<File>)sensor; Header meta = (Header)htmSensor.getMetaInfo(); assertTrue(meta.getFieldTypes().stream().allMatch( l -> l.equals(FieldMetaType.DATETIME) || l.equals(FieldMetaType.FLOAT))); assertTrue(meta.getFieldNames().stream().allMatch( l -> l.equals("timestamp") || l.equals("consumption"))); assertTrue(meta.getFlags().stream().allMatch( l -> l.equals(SensorFlags.T) || l.equals(SensorFlags.B))); // Set the parameters on the sensor. // This enables it to auto-configure itself; a step which will // be done at the Region level. Encoder<Object> multiEncoder = htmSensor.getEncoder(); assertNotNull(multiEncoder); assertTrue(multiEncoder instanceof MultiEncoder); // Set the Local parameters on the Sensor htmSensor.initEncoder(getTestEncoderParams()); List<EncoderTuple> encoders = multiEncoder.getEncoders(multiEncoder); assertEquals(2, encoders.size()); // Test date specific encoder configuration // // All encoders in the MultiEncoder are accessed in a particular // order (the alphabetical order their corresponding fields are in), // so alphabetically "consumption" proceeds "timestamp" // so we need to ensure that the proper order is preserved (i.e. exists at index 1) DateEncoder dateEnc = (DateEncoder)encoders.get(1).getEncoder(); try { dateEnc.parseEncode("7/12/10 13:10"); dateEnc.parseEncode("7/12/2010 13:10"); // Should fail here due to conflict with configured format dateEnc.parseEncode("13:10 7/12/10"); fail(); }catch(Exception e) { assertEquals("Invalid format: \"13:10 7/12/10\" is malformed at \":10 7/12/10\"", e.getMessage()); } RandomDistributedScalarEncoder rdse = (RandomDistributedScalarEncoder)encoders.get(0).getEncoder(); int[] encoding = rdse.encode(35.3); System.out.println(Arrays.toString(encoding)); // Now test the encoding of an input row Map<String, Object> d = new HashMap<String, Object>(); d.put("timestamp", dateEnc.parse("7/12/10 13:10")); d.put("consumption", 35.3); int[] output = multiEncoder.encode(d); int[] expected = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; assertTrue(Arrays.equals(expected, output)); }