Java Code Examples for org.numenta.nupic.encoders.MultiEncoder#getEncoders()

The following examples show how to use org.numenta.nupic.encoders.MultiEncoder#getEncoders() . 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 vote down vote up
/**
 * 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: Layer.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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 3
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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 4
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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 5
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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 6
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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 7
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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();
 }