NM500 / Mbed 2 deprecated NeuroShield_SimpleScript

Dependencies:   mbed NeuroShield

main.cpp

Committer:
nepes_ai
Date:
2018-01-25
Revision:
1:2d0abf41b7a3
Parent:
0:c56eb46c7bee
Child:
2:995d7426e3ba

File content as of revision 1:2d0abf41b7a3:

/******************************************************************************
 *  NM500 NeuroShield Board SimpleScript
 *  Simple Test Script to understand how the neurons learn and recognize
 *  revision 1.1.3, 01/03, 2018
 *  Copyright (c) 2017 nepes inc.
 *
 *  Please use the NeuroShield library v1.1.3 or later
 ******************************************************************************/

#include "mbed.h"
#include <NeuroShield.h>
#include <NeuroShieldSPI.h>

#define VECTOR_LENGTH 4
#define READ_COUNT 3

NeuroShield hnn;

DigitalOut sdcard_ss(D6);       // SDCARD_SSn
DigitalOut arduino_con(D5);     // SPI_SEL

int main()
{
    int i, j;
    uint8_t value;
    uint8_t vector[NEURON_SIZE];
    uint16_t vector16[NEURON_SIZE];
    uint16_t ncr, cat, aif, ncount, minif, response_nbr, norm_lsup = 0;
    uint16_t dists[READ_COUNT], cats[READ_COUNT], nids[READ_COUNT];
    uint16_t fpga_version;
    
    arduino_con = LOW;
    sdcard_ss = HIGH;
    wait(0.5);
    
    if (hnn.begin() != 0) {
        fpga_version = hnn.fpgaVersion();
        if ((fpga_version & 0xFF00) == 0x0000) {
            printf("\n\n#### NeuroShield Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
        }
        else if ((fpga_version & 0xFF00) == 0x0100) {
            printf("\n\n#### Prodigy Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
        }
        else {
            printf("\n\n#### Unknown Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
        }
        printf("\nNM500 is initialized!\n");
        printf("There are %d neurons\n", hnn.total_neurons);
    }
    else {
        minif = hnn.getMinif();
        printf("\nread value = %d", minif);
        printf("\nWarning!!! NM500 Shield board not properly responding!!");
        printf("\nCheck the connection and Reboot again!");
        while (1);
    }
    
    // if you want to run in lsup mode, uncomment below
    //norm_lsup = 0x80;
    hnn.setGcr(1 + norm_lsup);
    
    // build knowledge by learning 3 patterns with each constant values (respectively 11, 15 and 20)
    printf("\nLearning three patterns...");
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 11;
    hnn.learn(vector, VECTOR_LENGTH, 55);
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 15;
    hnn.learn(vector, VECTOR_LENGTH, 33);
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 20;
    ncount = hnn.learn(vector, VECTOR_LENGTH, 100);
    // display the content of the committed neurons
    printf("\nDisplay the neurons, count = %d", ncount);
    for (i = 1; i <= ncount; i++) {
        hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
        printf("\nneuron#%d \tmodel=", i);
        for (j = 0; j < VECTOR_LENGTH; j++)
            printf("%d, ", vector16[j]);
        if (cat & 0x8000)
            printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
        else
            printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
    }
    
    for (value = 12; value < 16; value++) {
        for (i = 0; i < VECTOR_LENGTH; i++)
            vector[i] = value;
        printf("\n\nRecognizing a new pattern: ");
        for (i = 0; i < VECTOR_LENGTH; i++)
            printf("%d, ", vector[i]);
        response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
        for (i = 0; i < response_nbr; i++) {
            if (cats[i] & 0x8000)
                printf("\nFiring neuron#%d, category=%d (degenerated), distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
            else
                printf("\nFiring neuron#%d, category=%d, distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
        }
    }
    
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 20;
    printf("\n\nRecognizing a new pattern using KNN classifier: ");
    for (i = 0; i < VECTOR_LENGTH; i++)
        printf("%d, ", vector[i]);
    hnn.setKnnClassifier();
    response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
    hnn.setRbfClassifier();
    for (i = 0; i < READ_COUNT; i++) {
        if (cats[i] & 0x8000)
            printf("\nFiring neuron#%d, category=%d (degenerated), distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
        else
            printf("\nFiring neuron#%d, category=%d, distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
    }
    
    printf("\n\nLearning a new example (13) falling between neuron1 and neuron2");
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 13;
    ncount = hnn.learn(vector, VECTOR_LENGTH, 100);
    // display the content of the committed neurons
    printf("\nDisplay the neurons, count = %d", ncount);
    for (i = 1; i <= ncount; i++) {
        hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
        printf("\nneuron#%d \tmodel=", i);
        for (j = 0; j < VECTOR_LENGTH; j++)
            printf("%d, ", vector16[j]);
        if (cat & 0x8000)
            printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
        else
            printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
    }
    printf("\n=> Notice the addition of neuron 4 and the shrinking of the influence fields of neuron1 and 2");
    
    printf("\n\nLearning a same example (13) using a different category 77");
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 13;
    ncount = hnn.learn(vector, VECTOR_LENGTH, 77);
    // display the content of the committed neurons
    printf("\nDisplay the neurons, count = %d", ncount);
    for (i = 1; i <= ncount; i++) {
        hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
        printf("\nneuron#%d \tmodel=", i);
        for (j = 0; j < VECTOR_LENGTH; j++)
            printf("%d, ", vector16[j]);
        if (cat & 0x8000)
            printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
        else
            printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
    }
    printf("\n=> Notice if the AIF of a neuron reaches the MINIF, the neuron will be degenerated");
    
    printf("\n\nLearning a new example (12) using context 5, category 200");
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 12;
    hnn.setContext(5);
    ncount = hnn.learn(vector, VECTOR_LENGTH, 200);
    hnn.setContext(1);
    // display the content of the committed neurons
    printf("\nDisplay the neurons, count = %d", ncount);
    for (i = 1; i <= ncount; i++) {
        hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
        printf("\nneuron#%d \tmodel=", i);
        for (j = 0; j < VECTOR_LENGTH; j++)
            printf("%d, ", vector16[j]);
        if (cat & 0x8000)
            printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
        else
            printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
    }
    
    for (i = 0; i < VECTOR_LENGTH; i++)
        vector[i] = 20;
    printf("\n\nRecognizing a new pattern using context 5: ");
    for (i = 0; i < VECTOR_LENGTH; i++)
        printf("%d, ", vector[i]);
    hnn.setContext(5);
    response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
    hnn.setContext(1);
    for (i = 0; i < response_nbr; i++) {
        if (cats[i] & 0x8000)
            printf("\nFiring neuron#%d, category=%d (degenerated), distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
        else
            printf("\nFiring neuron#%d, category=%d, distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
    }
    printf("\n=> Notice the neurons will not be recognize and shrink if the value of context is not equal");
}