Search code examples
cartificial-intelligenceneural-networkfann

How Get Weight Matrix from NN FANN?


I'm using FANN to use Neural Network. (Link to FANN)

I need to get the matrix of weight after trained the network, but I didn't find anything from documentation. (Link to documentation)

Do you know how get that matrix???

Thank you!


Solution

  • You need to use fann_get_connection_array() function. It gives you array of struct fann_connection, and struct fann_connection has field weight, so it's what you want.

    You can do something like this to print your weight matrix:

    int main(void)
    {
        struct fann *net;              /* your trained neural network */
        struct fann_connection *con;   /* weight matrix */
        unsigned int connum;           /* connections number */
        size_t i;
    
        /* Insert your net allocation and training code here */
        ...
    
        connum = fann_get_total_connections(net);
        if (connum == 0) {
            fprintf(stderr, "Error: connections count is 0\n");
            return EXIT_FAILURE;
        }
    
        con = calloc(connum, sizeof(*con));
        if (con == NULL) {
            fprintf(stderr, "Error: unable to allocate memory\n");
            return EXIT_FAILURE;
        }
    
        /* Get weight matrix */
        fann_get_connection_array(net, con);
    
        /* Print weight matrix */
        for (i = 0; i < connum; ++i) {
            printf("weight from %u to %u: %f\n", con[i].from_neuron,
                   con[i].to_neuron, con[i].weight);
        }
    
        free(con);
    
        return EXIT_SUCCESS;
    }
    

    Details:

    [1] fann_get_connection_array()

    [2] struct fann_connection

    [3] fann_type (type for weight)