代码之家  ›  专栏  ›  技术社区  ›  Teo

如何从NN FANN获取权重矩阵?

  •  0
  • Teo  · 技术社区  · 9 年前

    我正在使用FANN来使用神经网络。( Link to FANN )

    我需要在训练网络后得到权重矩阵,但我没有从文档中找到任何东西。( Link to documentation )

    你知道怎么得到矩阵吗???

    非常感谢。

    1 回复  |  直到 9 年前
        1
  •  1
  •   Sam Protsenko    9 年前

    你需要使用 fann_get_connection_array() 作用它为您提供了 struct fann_connection 结构fann_connection 有字段 weight ,所以这是你想要的。

    您可以执行以下操作来打印权重矩阵:

    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;
    }
    

    细节:

    [1] fann_get_connection_array()

    [2] struct fann_connection

    [3] fann_type (type for weight)