Models

ModelFactory

class nupic.frameworks.opf.model_factory.ModelFactory

Static factory class that produces a nupic.frameworks.opf.model.Model based on a description dict.

static create(modelConfig, logLevel=40)

Create a new model instance, given a description dictionary.

Parameters:
  • modelConfig – (dict) A dictionary describing the current model, described here.
  • logLevel – (int) The level of logging output that should be generated
Raises:

Exception – Unsupported model type

Returns:

nupic.frameworks.opf.model.Model

static loadFromCheckpoint(savedModelDir, newSerialization=False)

Load saved model.

Parameters:savedModelDir – (string) Directory of where the experiment is to be or was saved
Returns:(nupic.frameworks.opf.model.Model) The loaded model instance.

Base Model

Module defining the OPF Model base class.

class nupic.frameworks.opf.model.Model(inferenceType=None, proto=None)

Bases: nupic.serializable.Serializable

This is the base class that all OPF Model implementations should subclass.

It includes a number of virtual methods, to be overridden by subclasses, as well as some shared functionality for saving/loading models

Parameters:inferenceType – (InferenceType) A value that specifies the type of inference.
disableInference()

Turn Inference off for the current model.

disableLearning()

Turn Learning off for the current model.

enableInference(inferenceArgs=None)

Enable inference for this model.

Parameters:inferenceArgs – (dict) A dictionary of arguments required for inference. These depend on the InferenceType of the current model
enableLearning()

Turn Learning on for the current model.

finishLearning()

Place the model in a permanent “finished learning” mode. In such a mode the model will not be able to learn from subsequent input records.

Note

Upon completion of this command, learning may not be resumed on the given instance of the model (e.g., the implementation may optimize itself by pruning data structures that are necessary for learning).

getFieldInfo(includeClassifierOnlyField=False)

Return the sequence of FieldMetaInfo objects specifying the format of Model’s output.

This may be different than the list of FieldMetaInfo objects supplied at initialization (e.g., due to the transcoding of some input fields into meta-fields, such as datetime -> dayOfWeek, timeOfDay, etc.).

Parameters:includeClassifierOnlyField – (bool) If True, any field which is only sent to the classifier (i.e. not sent in to the bottom of the network) is also included
Returns:(list) of FieldMetaInfo objects.
getInferenceArgs()

Return the dict of arguments for the current inference mode.

Returns:(dict) The arguments of the inference mode
getInferenceType()

Return the InferenceType of this model. This is immutable.

Returns:InferenceType
getRuntimeStats()

Get runtime statistics specific to this model, i.e. activeCellOverlapAvg.

Returns:(dict) A {statistic names: stats} dictionary
static getSchema()

Return the pycapnp proto type that the class uses for serialization.

This is used to convert the proto into the proper type before passing it into the read or write method of the subclass.

isInferenceEnabled()

Return the inference state of the current model.

Returns:(bool) The inference state
isLearningEnabled()

Return the Learning state of the current model.

Returns:(bool) The learning state
classmethod load(savedModelDir)

Load saved model.

Parameters:savedModelDir – (string) Directory of where the experiment is to be or was saved
Returns:(Model) The loaded model instance
classmethod read(proto)

Read state from proto object.

The type of proto is determined by getSchema().

classmethod readFromCheckpoint(checkpointDir)

Deserializes model from checkpointDir using capnproto

resetSequenceStates()

Signal that the input record is the start of a new sequence. Normally called to force the delineation of a sequence, such as between OPF tasks.

run(inputRecord)

Run one iteration of this model.

Parameters:inputRecord – (object) A record object formatted according to getNextRecord() or getNextRecordDict() result format.
Returns:(ModelResult) An ModelResult namedtuple. The contents of ModelResult.inferences depends on the the specific inference type of this model, which can be queried by getInferenceType().
save(saveModelDir)

Save the model in the given directory.

Parameters:saveModelDir – (string) Absolute directory path for saving the model. This directory should only be used to store a saved model. If the directory does not exist, it will be created automatically and populated with model data. A pre-existing directory will only be accepted if it contains previously saved model data. If such a directory is given, the full contents of the directory will be deleted and replaced with current model data.
setFieldStatistics(fieldStats)

Propagate field statistics to the model in case some of its machinery needs it.

Parameters:fieldStats – (dict) A dict of dicts with first key being the fieldname and the second key is min,max or other supported statistics.
write(proto)

Write state to proto object.

The type of proto is determined by getSchema().

writeBaseToProto(proto)

Save the state maintained by the Model base class

Parameters:proto – capnp ModelProto message builder
writeToCheckpoint(checkpointDir)

Serializes model using capnproto and writes data to checkpointDir

HTMPredictionModel

Encapsulation of HTM network that implements the base Model to perform temporal prediction.

class nupic.frameworks.opf.htm_prediction_model.HTMPredictionModel(sensorParams={}, inferenceType='TemporalNextStep', spEnable=True, spParams={}, trainSPNetOnlyIfRequested=False, tmEnable=True, tmParams={}, clEnable=True, clParams={}, anomalyParams={}, minLikelihoodThreshold=0.0001, maxPredictionsPerStep=8, network=None, baseProto=None)

Bases: nupic.frameworks.opf.model.Model

This model is for temporal predictions multiple steps ahead. After creating this model, you must call enableInference() to specify a predicted field, like this:

model.enableInference({"predictedField": "myPredictedField"})

Where myPredictedField is the field name in your data input that should be predicted.

Parameters:
  • inferenceType – (InferenceType)
  • sensorParams – (dict) specifying the sensor parameters.
  • spEnable – (bool) Whether or not to use a spatial pooler.
  • spParams – (dict) specifying the spatial pooler parameters. These are passed to the spatial pooler.
  • trainSPNetOnlyIfRequested – (bool) If set, don’t create an SP network unless the user requests SP metrics.
  • tmEnable – (bool) Whether to use a temporal memory.
  • tmParams – (dict) specifying the temporal memory parameters. These are passed to the temporal memory.
  • clEnable – (bool) Whether to use the classifier. If false, the classifier will not be created and no predictions will be generated.
  • clParams – (dict) specifying the classifier parameters. These are passed to the classifier.
  • anomalyParams – (dict) Anomaly detection parameters
  • minLikelihoodThreshold – (float) The minimum likelihood value to include in inferences. Currently only applies to multistep inferences.
  • maxPredictionsPerStep – (int) Maximum number of predictions to include for each step in inferences. The predictions with highest likelihood are included.
setAnomalyParameter(param, value)

Set a parameter of the anomaly classifier within this model.

Parameters:
  • param – (string) name of parameter to set
  • value – (object) value to set
getAnomalyParameter(param)

Get a parameter of the anomaly classifier within this model by key.

Parameters:param – (string) name of parameter to retrieve
anomalyRemoveLabels(start, end, labelFilter)

Remove labels from the anomaly classifier within this model. Removes all records if labelFilter==None, otherwise only removes the labels equal to labelFilter.

Parameters:
  • start – (int) index to start removing labels
  • end – (int) index to end removing labels
  • labelFilter – (string) If specified, only removes records that match
anomalyAddLabel(start, end, labelName)

Add labels from the anomaly classifier within this model.

Parameters:
  • start – (int) index to start label
  • end – (int) index to end label
  • labelName – (string) name of label
anomalyGetLabels(start, end)

Get labels from the anomaly classifier within this model.

Parameters:
  • start – (int) index to start getting labels
  • end – (int) index to end getting labels
getParameter(paramName)

Currently only supports a parameter named __numRunCalls.

Parameters:paramName – (string) name of parameter to get. If not __numRunCalls an exception is thrown.
Returns:(int) the value of self.__numRunCalls
getRuntimeStats()

Only returns data for a stat called numRunCalls. :return:

TwoGramModel

Module containing the two gram OPF model implementation.

class nupic.frameworks.opf.two_gram_model.TwoGramModel(inferenceType='TemporalNextStep', encoderParams=())

Bases: nupic.frameworks.opf.model.Model

Two-gram benchmark model.

Parameters:
classmethod read(proto)
Parameters:proto – capnp TwoGramModelProto message reader
setFieldStatistics(fieldStats)

Since the two-gram has no use for this information, this is a no-op

write(proto)
Parameters:proto – capnp TwoGramModelProto message builder

PreviousValueModel

Module containing the trivial predictor OPF model implementation.

class nupic.frameworks.opf.previous_value_model.PreviousValueModel(inferenceType='TemporalNextStep', fieldNames=[], fieldTypes=[], predictedField=None, predictionSteps=[])

Previous value model.

Parameters:
  • inferenceType – (nupic.frameworks.opf.opf_utils.InferenceType)
  • fieldNames – a list of field names
  • fieldTypes – a list of the types for the fields mentioned in fieldNames
  • predictedField – the field from fieldNames which is to be predicted
  • predictionSteps – a list of steps for which a prediction is made. This is only needed in the case of multi step predictions. For example, to get predictions 1, 5, and 10 steps ahead: [1,5,10].
finishLearning()

The PVM does not learn, so this function has no effect.

classmethod read(proto)

Deserialize via capnp

Parameters:proto – capnp PreviousValueModelProto message reader
Returns:new instance of PreviousValueModel deserialized from the given proto
setFieldStatistics(fieldStats)

Since the PVM has no use for this information, this is a no-op

write(proto)

Serialize via capnp

Parameters:proto – capnp PreviousValueModelProto message builder