人工知性を作りたい

私が日々、挑戦したことや学んだことなどを紹介していく雑記ブログです。 (新しいAI技術HTM, 専門の音声信号処理, 趣味のアニメ等も書いてます。)

NuPICからhtm.coreへ移植!【ソースコード置き場】

f:id:hiro-htm877:20190715195411p:plain

 

ソースコード(モデルSave)

def runVadLoop(sigList):
    #############################################
    # SpatialPoolerの設定                        #
    #############################################
    sp = SpatialPooler(
      inputDimensions            = (20, 27),
      columnDimensions           = (10, 10),
      localAreaDensity           = 0.3,
      potentialPct               = 0.8,
      potentialRadius            = 269,
      globalInhibition           = True,
    #   synPermInactiveDec         = spParams["synPermInactiveDec"],
      synPermActiveInc           = 0.001,
    #   synPermConnected           = spParams["synPermConnected"],
    #   boostStrength              = spParams["boostStrength"],
    #   wrapAround                 = True
    seed = 1960
    )
    sp_info = Metrics( sp.getColumnDimensions(), 999999999 )

    #############################################
    # TemporalMemoryの設定                       #
    #############################################
    tm = TemporalMemory(
      columnDimensions          = (10, 10),
      cellsPerColumn            = 16,
      activationThreshold       = 10,
      initialPermanence         = 0.21,
      connectedPermanence       = 0.5,
      minThreshold              = 4,
      maxNewSynapseCount        = 20,
      permanenceIncrement       = 0.1,
      permanenceDecrement       = 0.1,
    #   predictedSegmentDecrement = 0.0,
      maxSegmentsPerCell        = 64,
      maxSynapsesPerSegment     = 16,
      seed = 1940
    )
    # tm_info = Metrics( [tm.numberOfCells()], 999999999 )

    inputs      = []
    anomaly     = []
    anomalyProb = []
    predictions = {1: [], 5: []}

    encodeList = []
    results = []
    activeCells = 0
    AnomalyScoreResult = []
    count = 0
    PRINTBORDER = 10
    printBorder=PRINTBORDER
    printBorderIncrement=PRINTBORDER
    for loop in range(10):
        for i, sig in enumerate(sigList):
            mel = melspectrogram(sig)
            mel = mel.T
            for t in range(len(mel)):
                ### printBorderの区切り毎にカウントを表示する
                ### 例(printBorder=100):0, 100, 200, 300, 400, ...
                if (t+count) == printBorder:
                    print (t+count)
                    printBorder += printBorderIncrement
                en = Encoder_2D(mel[t])
                activeColumns = SDR( sp.getColumnDimensions() )
                #############################################
                # SpatialPoolerの学習                        #
                #############################################
                sdr = SDR(dimensions = (20, 27))
                sdr.sparse = np.nonzero(en[0].reshape(-1))[0]

                sp.compute(sdr, True, activeColumns)
                #############################################
                # TemporalMemoryの学習                        #
                #############################################
                tm.compute(activeColumns, learn=True)
             
                #############################################
                # 異常値の計算                           #
                #############################################
                anomaly.append( tm.anomaly )
                print(tm.anomaly)

               


            ### TemporalMemoryでmonotonically increaseするために
            ### 1つの音声学習の学習に経過時間を足し続けて使用する
            count += t

    #############################################
    # 学習データの保存(SP, TM)                     #
    #############################################
    sp.saveToFile("sp.model")
    tm.saveToFile("tm.model")

    return anomaly, results
 

ソースコード(モデルLoad)

def runVadLoop(sigList):
    #############################################
    # SpatialPoolerの設定                        #
    #############################################
    sp = SpatialPooler()
    sp.loadFromFile("sp.model")


    #############################################
    # TemporalMemoryの設定                       #
    #############################################
    tm = TemporalMemory()
    tm.loadFromFile("tm.model")

    inputs      = []
    anomaly     = []
    anomalyProb = []
    predictions = {1: [], 5: []}

    encodeList = []
    results = []
    activeCells = 0
    AnomalyScoreResult = []
    count = 0
    PRINTBORDER = 10
    printBorder=PRINTBORDER
    printBorderIncrement=PRINTBORDER
    for loop in range(1):
        for i, sig in enumerate(sigList):
            mel = melspectrogram(sig)
            mel = mel.T
            for t in range(len(mel)):
                ### printBorderの区切り毎にカウントを表示する
                ### 例(printBorder=100):0, 100, 200, 300, 400, ...
                if (t+count) == printBorder:
                    print (t+count)
                    printBorder += printBorderIncrement

                en = Encoder_2D(mel[t])
               
                activeColumns = SDR( sp.getColumnDimensions() )

                sdr = SDR(dimensions = (20, 27))
                sdr.sparse = np.nonzero(en[0].reshape(-1))[0]

                sp.compute(sdr, False, activeColumns)

                tm.compute(activeColumns, learn=False)
               
                anomaly.append( tm.anomaly )
                
            count += t

    return anomaly, results