Accuracy is not improving while trying to train htm model for binary classification

Hi Team,

I am new to HTM and trying to run HTM model for Anomaly detection use case (wanted to train it as binary classification), took the MNIST example as reference tried to build the model (only difference is being, my dataset is multivariate and I had to concatenate SDRs). My accuracy score is not improving though I have used Optimizers.
here is my sample code snippet

default_parameters =  {
    'potentialRadius': 16,
    'boostStrength': 7.0,
    'columnDimensions': (32,),
    'dutyCyclePeriod': 1000,
    'minPctOverlapDutyCycle': 0.001,
    'potentialPct': 0.5,
    'stimulusThreshold': 12,
    'synPermActiveInc': 0.1,
    'synPermConnected': 0.1,
    'synPermInactiveDec': 0.01
}
rdse_params = RDSE_Parameters()

    rdse_params.sparsity = 0.3
    rdse_params.size = 2048
    rdse_params.radius = 10
    numeric_encoder = RDSE(rdse_params)
    

    total_size = (train.shape[1] -1) * numeric_encoder.size

    print("total size::",total_size)

    # Setup the AI.
    
    sp = SpatialPooler(
        inputDimensions            = (total_size,),
        columnDimensions           = (2048,),
        potentialRadius            = parameters['potentialRadius'],
        potentialPct               = parameters['potentialPct'],
        globalInhibition           = True,
        stimulusThreshold          = int(round(parameters['stimulusThreshold'])),
        synPermInactiveDec         = parameters['synPermInactiveDec'],
        synPermActiveInc           = parameters['synPermActiveInc'],
        synPermConnected           = parameters['synPermConnected'],
        minPctOverlapDutyCycle     = parameters['minPctOverlapDutyCycle'],
        dutyCyclePeriod            = int(round(parameters['dutyCyclePeriod'])),
        boostStrength              = parameters['boostStrength'],
        seed                       = 0, # this is important, 0="random" seed which changes on each invocation
        spVerbosity                = 99,
        wrapAround                 = False)
    columns = SDR( sp.getColumnDimensions() )
    columns_stats = Metrics( columns, 99999999 )
    sdrc = Classifier()


    cols = list(data_numeric.columns)
    cols.remove('attack')

    spatial_output = SDR(sp.getColumnDimensions())
    for index, row in train.iterrows():

        # Create an SDR for the concatenated encodings
        concatenated_sdr = SDR(total_size)

        # Keep track of the current position in the concatenated SDR
        position = 0

        # Encode each column's value and concatenate
        for col in cols :
            #print("Encode the value into an SDR")
            encoded_sdr = SDR(numeric_encoder.size)
            
            encoded_sdr = numeric_encoder.encode(row[col])
        # encoded_sdr = SDR(numeric_encoder.encode(row[col]))

            # Concatenate the encoded SDR to the larger SDR
            concatenated_sdr.dense[position:position + numeric_encoder.size] = encoded_sdr.dense
            position = position + numeric_encoder.size
        
        # Spatial Pooling
        
        sp.compute(concatenated_sdr, True, spatial_output)

        lbl= int(row['attack'])
        #print(lbl)
        #print("ok")
        sdrc.learn(spatial_output,lbl)

        # Temporal Memory
     

    print(str(sp))
    print(str(columns_stats))

    # Testing Loop
    score = 0
    
    for index, row in test.iterrows():
    # Create an SDR for the concatenated encodings
        concatenated_sdr = SDR(total_size)

        # Keep track of the current position in the concatenated SDR
        position = 0

        # Encode each column's value and concatenate
        for col in cols :
            #print("Encode the value into an SDR")
            encoded_sdr = SDR(numeric_encoder.size)
            
            encoded_sdr = numeric_encoder.encode(row[col])
        # encoded_sdr = SDR(numeric_encoder.encode(row[col]))

            # Concatenate the encoded SDR to the larger SDR
            concatenated_sdr.dense[position:position + numeric_encoder.size] = encoded_sdr.dense
            position = position + numeric_encoder.size
        
        # Spatial Pooling
        
        sp.compute(concatenated_sdr, False, spatial_output)
           
        
        lbl= int(row['attack'])
        #print(lbl)
        
        if lbl == np.argmax( sdrc.infer( spatial_output ) ):
            score += 1

    score = score / len(test)

    print('Score:', 100 * score, '%')
    return score

Please help me with the issue.
Thanks in advance.

1 Like

What were your results and what would you expect to be an improvement?

Have you tried this example?

PS your intention here seems to transfer mnist classification example for anomaly detection? Clasification is not what Numenta’s anomaly detection is meant for

1 Like