Models

Model

class nupic.frameworks.opf.model.Model(inferenceType)

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

disableInference()

Turn Inference off for the current model.

disableLearning()

Turn Learning off for the current model.

enableInference(inferenceArgs=None)

Enable inference for this model. @param 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.). @param 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<nupic.data.fieldmeta.FieldMetaInfo>)
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 (nupic.frameworks.opf.opfutils.InferenceType) An inference type

static getProtoType()

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.

getRuntimeStats()

Get runtime statistics specific to this model, i.e. activeCellOverlapAvg. @returns (dict) A {statistic names: stats} dictionary

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. @param 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 getProtoType().

classmethod readFromCheckpoint(checkpointDir)

Deerializes model from checkpointDir using capnproto

resetSequenceStates()

Signal that the input record is the start of a new sequence.

run(inputRecord)

Run one iteration of this model. @param inputRecord (object)

A record object formatted according to nupic.data.record_stream.RecordStreamIface.getNextRecord() or nupic.data.record_stream.RecordStreamIface.getNextRecordDict() result format.
@returns (nupic.frameworks.opf.opfutils.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. @param 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. @param 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 getProtoType().

writeToCheckpoint(checkpointDir)

Serializes model using capnproto and writes data to checkpointDir

CLAModel

This class is poorly named.

class nupic.frameworks.opf.clamodel.CLAModel(sensorParams={}, inferenceType='TemporalNextStep', predictedField=None, spEnable=True, spParams={}, trainSPNetOnlyIfRequested=False, tpEnable=True, tpParams={}, clEnable=True, clParams={}, anomalyParams={}, minLikelihoodThreshold=0.0001, maxPredictionsPerStep=8, network=None)

Bases: nupic.frameworks.opf.model.Model

disableLearning()

[override] Turn Learning off for the current model

enableLearning()

[override] Turn Learning on for the current model

finishLearning()

[virtual method override] Places the model in a permanent “finished learning” mode where it 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)
[virtual method override]
Returns the sequence of FieldMetaInfo objects specifying this Model’s output; note that 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.)

Returns: List of FieldMetaInfo objects (see description above)

getRuntimeStats()

[virtual method override] get runtime statistics specific to this model, i.e. activeCellOverlapAvg

return:
a dict where keys are statistic names and values are the stats
resetSequenceStates()

[virtual method override] Resets the model’s sequence states. Normally called to force the delineation of a sequence, such as between OPF tasks.

run(inputRecord)

run one iteration of this model. args:

inputRecord is a record object formatted according to
nupic.data.RecordStream.getNextRecordDict() result format.
return:
An ModelResult class (see opfutils.py) The contents of ModelResult.inferences depends on the the specific inference type of this model, which can be queried by getInferenceType()

TwoGramModel

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

Bases: nupic.frameworks.opf.model.Model

Two-gram benchmark model.

finishLearning()

Places the model in a permanent “finished learning” mode.

Once called, the model will not be able to learn from subsequent input records. Learning may not be resumed on a given instance of the model once this is called as the implementation may optimize itself by pruning data structures that are necessary for learning.

getFieldInfo()

Returns the metadata specifying the format of the model’s output.

The result may be different than the list of nupic.data.fieldmeta.FieldMetaInfo objects supplied at initialization due to the transcoding of some input fields into meta- fields, such as datetime -> dayOfWeek, timeOfDay, etc.

getRuntimeStats()

Get the runtime statistics specific to the model.

I.E. activeCellOverlapAvg

Returns:
A dict mapping statistic names to values.
resetSequenceStates()

Called to indicate the start of a new sequence.

The next call to run should not perform learning.

run(inputRecord)

Run one iteration of this model.

Args:
inputRecord: A record object formatted according to
nupic.data.FileSource.getNext() result format.
Returns:
A ModelResult named tuple (see opfutils.py). The contents of ModelResult.inferences depends on the specific inference type of this model, which can be queried by getInferenceType(). TODO: Implement getInferenceType()?
setFieldStatistics(fieldStats)

This method is used for the data source to communicate to the model any statistics that it knows about the fields Since the two-gram has no use for this information, this is a no-op

ModelFactory

class nupic.frameworks.opf.modelfactory.ModelFactory

Static factory class that produces a Model based on a description dict. Eventually this will be the source for all Model creation, CLA and otherwise. We may also implement building the description dict from a database or a description.py file. For now, this is a very skeletal implementation.

static create(modelConfig, logLevel=40)

Create a new model instance, given a description dictionary. @param modelConfig (dict)

A dictionary describing the current model (TODO: schema)

@param logLevel (int) The level of logging output that should be generated @exception (Exception) Unsupported model type @returns (nupic.frameworks.opf.model.Model) A model.

static loadFromCheckpoint(savedModelDir, newSerialization=False)

Load saved model. @param savedModelDir (string)

Directory of where the experiment is to be or was saved

@returns (nupic.frameworks.opf.model.Model) The loaded model instance.