Exemplo n.º 1
0
def experiment2():
    """
    Disambiguation experiment.
    Experiment setup:
      Train TM on sequences ABCDE and XBCDF with feedback
      Test TM on partial ambiguous sequences BCDE and BCDF
        With correct feedback

    Desired result:
      With correct feedback, TM should make correct prediction after D
    """
    print "Initializing temporal memory..."
    params = dict(DEFAULT_TEMPORAL_MEMORY_PARAMS)
    tmFeedback = MonitoredGeneralTemporalMemory(mmName="TM2", **params)
    feedback_n = 400
    trials = 30
    print "Done."

    # Create two sequences with format "ABCDE" and "XBCDF"
    sequences1 = generateSequences(2048, 40, 5, 1)

    sequences2 = [x for x in sequences1]
    sequences2[0] = set([random.randint(0, 2047) for _ in sequences1[0]])
    sequences2[-2] = set([random.randint(0, 2047) for _ in sequences1[-2]])

    fixed_feedback1 = set([random.randint(0, 2047) for _ in range(feedback_n)])
    fixed_feedback2 = set([random.randint(0, 2047) for _ in range(feedback_n)])
    feedback_seq1 = shiftingFeedback(fixed_feedback1, len(sequences1))
    feedback_seq2 = shiftingFeedback(fixed_feedback2, len(sequences2))

    sequences = sequences1 + sequences2
    feedback_seq = feedback_seq1 + feedback_seq2
    alphabet = getAlphabet(sequences)
    partial_sequences1 = sequences1[1:]
    partial_sequences2 = sequences2[1:]

    print "train TM on sequences ABCDE and XBCDF"
    test_sequence = partial_sequences1
    print "test TM on sequence BCDE, evaluate responses to E"

    testFeedback = feedback_seq1
    print 'Feedback is in "ABCDE" state'
    print 'Desired outcome: '
    print '\t no extra prediction with feedback (~0 predicted inactive cell)'
    print

    feedbackBuffer = 10
    sanityModel = FeedbackExperimentSanityModel(tmFeedback, trials, sequences,
                                                feedback_seq, test_sequence,
                                                testFeedback, feedbackBuffer,
                                                feedback_n, alphabet)
    runner = SanityRunner(sanityModel)
    runner.start()
Exemplo n.º 2
0
def experiment2():
    """
    Disambiguation experiment.
    Experiment setup:
      Train TM on sequences ABCDE and XBCDF with feedback
      Test TM on partial ambiguous sequences BCDE and BCDF
        With correct feedback

    Desired result:
      With correct feedback, TM should make correct prediction after D
    """
    print "Initializing temporal memory..."
    params = dict(DEFAULT_TEMPORAL_MEMORY_PARAMS)
    tmFeedback = MonitoredGeneralTemporalMemory(mmName="TM2", **params)
    feedback_n = 400
    trials = 30
    print "Done."

    # Create two sequences with format "ABCDE" and "XBCDF"
    sequences1 = generateSequences(2048, 40, 5, 1)

    sequences2 = [x for x in sequences1]
    sequences2[0] = set([random.randint(0, 2047) for _ in sequences1[0]])
    sequences2[-2] = set([random.randint(0, 2047) for _ in sequences1[-2]])

    fixed_feedback1 = set([random.randint(0, 2047) for _ in range(feedback_n)])
    fixed_feedback2 = set([random.randint(0, 2047) for _ in range(feedback_n)])
    feedback_seq1 = shiftingFeedback(fixed_feedback1, len(sequences1))
    feedback_seq2 = shiftingFeedback(fixed_feedback2, len(sequences2))

    sequences = sequences1 + sequences2
    feedback_seq = feedback_seq1 + feedback_seq2
    alphabet = getAlphabet(sequences)
    partial_sequences1 = sequences1[1:]
    partial_sequences2 = sequences2[1:]

    print "train TM on sequences ABCDE and XBCDF"
    test_sequence = partial_sequences1
    print "test TM on sequence BCDE, evaluate responses to E"

    testFeedback = feedback_seq1
    print 'Feedback is in "ABCDE" state'
    print 'Desired outcome: '
    print '\t no extra prediction with feedback (~0 predicted inactive cell)'
    print

    feedbackBuffer = 10
    sanityModel = FeedbackExperimentSanityModel(tmFeedback, trials, sequences,
                                                feedback_seq, test_sequence, testFeedback,
                                                feedbackBuffer, feedback_n, alphabet)
    runner = SanityRunner(sanityModel)
    runner.start()
Exemplo n.º 3
0
        self.predictedLines.set_xdata(self.convertedDates)
        self.predictedLines.set_ydata(self.predictedValues)

        if self.shouldScheduleDraw:
            # If we're stepping the model really quickly, coalesce the redraws.
            self.shouldScheduleDraw = False
            t = threading.Timer(0.2, self.draw)
            t.start()

    def getInputDisplayText(self):
        return (["time", self.timestampStr],
                ["power consumption (kW)", self.consumptionStr])


if __name__ == '__main__':
    model = ModelFactory.create(MODEL_PARAMS)
    model.enableInference({"predictedField": "kw_energy_consumption"})

    inputPath = os.path.join(os.path.dirname(__file__),
                             "data/rec-center-hourly.csv")

    inputFile = open(inputPath, "rb")
    csvReader = csv.reader(inputFile)
    csvReader.next()
    csvReader.next()
    csvReader.next()

    runner = SanityRunner(HotGym(model, csvReader))
    runner.start(useBackgroundThread=True)
    plt.show()
Exemplo n.º 4
0
from model_params.vaccination.vaccine_record_model_params import MODEL_PARAMS
from htmsanity.nupic.runner import SanityRunner
from htmsanity.nupic.model import CLASanityModel


# You create something like this.
class HelloModel(CLASanityModel):
    def __init__(self):
        """MODEL_PARAMS = {
            # Your choice
        }"""
        self.model = ModelFactory.create(MODEL_PARAMS)
        self.lastInput = -1
        super(HelloModel, self).__init__(self.model)

    def step(self):
        self.lastInput = (self.lastInput + 1) % 12
        self.model.run({
            'myInput': self.lastInput,
        })

    def getInputDisplayText(self):
        return {
            'myInput': self.lastInput,
        }


sanityModel = HelloModel()
runner = SanityRunner(sanityModel)
runner.start()
    # Dimensionality of sequence patterns
    patternDimensionality = 1024

    # Cardinality (ON / true bits) of sequence patterns
    patternCardinality = 50

    # Length of sequences shown to network
    sequenceLength = 50

    # Number of sequences used. Sequences may share common elements.
    numberOfSequences = 1

    # Generate a sequence list and an associated labeled list (both containing a
    # set of sequences separated by None)
    print "\nGenerating sequences..."
    patternAlphabetSize = sequenceLength * numberOfSequences
    patternMachine = PatternMachine(patternDimensionality, patternCardinality,
                                    patternAlphabetSize)
    sequenceMachine = SequenceMachine(patternMachine)

    numbers = sequenceMachine.generateNumbers(numberOfSequences, sequenceLength)
    generatedSequences = sequenceMachine.generateFromNumbers(numbers)

    inputs = list(generatedSequences) * 100
    labels = list(numbers) * 100

    # TODO: show higher-level sequence *and* current value in display text.
    sanityModel = UnionPoolingExperimentSanityModel(experiment, inputs, labels)
    runner = SanityRunner(sanityModel)
    runner.start()
Exemplo n.º 6
0
        self.actualLines.set_ydata(self.actualValues)
        self.predictedLines.set_xdata(self.convertedDates)
        self.predictedLines.set_ydata(self.predictedValues)

        if self.shouldScheduleDraw:
            # If we're stepping the model really quickly, coalesce the redraws.
            self.shouldScheduleDraw = False
            t = threading.Timer(0.2, self.draw)
            t.start()

    def getInputDisplayText(self):
        return (["time", self.timestampStr],
                ["power consumption (kW)", self.consumptionStr])

if __name__ == '__main__':
    model = ModelFactory.create(MODEL_PARAMS)
    model.enableInference({"predictedField": "kw_energy_consumption"})

    inputPath = os.path.join(os.path.dirname(__file__),
                             "data/rec-center-hourly.csv")

    inputFile = open(inputPath, "rb")
    csvReader = csv.reader(inputFile)
    csvReader.next()
    csvReader.next()
    csvReader.next()

    runner = SanityRunner(HotGym(model, csvReader))
    runner.start(useBackgroundThread=True)
    plt.show()