⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fann_extensions.h

📁 改进的人工神经网络源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
            }
            case FANN_SHORTCUT: {
                /* Bias for current shortcut net is the same as for a layered net */                /* TODO When shortcut has one bias in first layer change to:                    if (layer_it == ann->first_layer) */                if (layer_it != ann->last_layer-1)                    *bias = 1;                else                    *bias = 0;                break;
            }
            default: {
                /* Unknown network type, assume no bias present  */
                *bias = 0;                break;
            }
        }
    }}/** Get the connections in the network.    The connections array must be preallocated to at least    sizeof(struct fann_connection) * fann_get_total_connections() long. */void fann_get_connection_array(struct fann *ann, struct fann_connection *connections){    struct fann_neuron **connected_neurons;    fann_type *internal_weights;    struct fann_neuron *first_neuron;    struct fann_layer *layer_it;    struct fann_neuron *neuron_it;    unsigned int index;    unsigned int source_index;    unsigned int destination_index;    /* Same as internal function fann_get_connections */    connected_neurons = (ann->first_layer+1)->first_neuron->connected_neurons;    /* Same as internal function fann_get_weights */    internal_weights = (ann->first_layer+1)->first_neuron->weights;    first_neuron = ann->first_layer->first_neuron;    source_index = 0;    destination_index = 0;        /* The following assumes that the last unused bias has no connections */    /* for each layer */    for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++){        /* for each neuron */        for(neuron_it = layer_it->first_neuron; neuron_it != layer_it->last_neuron; neuron_it++){            /* for each connection */            for (index = 0; index < neuron_it->num_connections; index++){                /* Assign the source, destination and weight */                connections->from_neuron = connected_neurons[source_index] - first_neuron;                connections->to_neuron = destination_index;                connections->weight = internal_weights[source_index];                connections++;                source_index++;            }            destination_index++;        }    }}/** Set weights in the network.    Only the weights can be changed, connections and weights are ignored    if they do not already exist in the network.    The array must have sizeof(struct fann_connection) * num_connections size. */void fann_set_weight_array(struct fann *ann,    struct fann_connection *connections, unsigned int num_connections){    unsigned int index;    for (index = 0; index < num_connections; index++) {        fann_set_weight(ann, connections[index].from_neuron,            connections[index].to_neuron, connections[index].weight);    }}/** Set a weight in the network.    Only the weights can be changed. The connection/weight is    ignored if it does not already exist in the network. */void fann_set_weight(struct fann *ann,    unsigned int from_neuron, unsigned int to_neuron, fann_type weight){    struct fann_neuron **connected_neurons;    fann_type *internal_weights;    struct fann_neuron *first_neuron;    struct fann_layer *layer_it;    struct fann_neuron *neuron_it;    unsigned int index;    unsigned int source_index;    unsigned int destination_index;    /* Same as internal function fann_get_connections */    connected_neurons = (ann->first_layer+1)->first_neuron->connected_neurons;    /* Same as internal function fann_get_weights */    internal_weights = (ann->first_layer+1)->first_neuron->weights;    first_neuron = ann->first_layer->first_neuron;    source_index = 0;    destination_index = 0;    /* Find the connection, simple brute force search through the network       for one or more connections that match to minimize datastructure dependencies.       Nothing is done if the connection does not already exist in the network. */    /* for each layer */    for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++){        /* for each neuron */        for(neuron_it = layer_it->first_neuron; neuron_it != layer_it->last_neuron; neuron_it++){            /* for each connection */            for (index = 0; index < neuron_it->num_connections; index++){                /* If the source and destination neurons match, assign the weight */                if (((int)from_neuron == connected_neurons[source_index] - first_neuron) &&                    (to_neuron == destination_index))                {                    internal_weights[source_index] = weight;                }                source_index++;            }            destination_index++;        }    }}/*****************************************************************************/
/** FIX: Save_..._ex functions return -1 when an error occurs. */#ifdef USE_SAVE_EX/** Save the network, return -1 when an error occurs. */FANN_EXTERNAL int FANN_API fann_save_ex(struct fann *ann, const char *configuration_file){    return fann_save_internal(ann, configuration_file, 0);}/** Save training data to a file, return -1 when an error occurs */FANN_EXTERNAL int FANN_API fann_save_train_ex(struct fann_train_data* data, char *filename){    return fann_save_train_internal_ex(data, filename, 0, 0);}/** Save training data to a file in fixed point algebra, return -1 when an error occurs.    (Good for testing a network in fixed point) */FANN_EXTERNAL int FANN_API fann_save_train_to_fixed_ex(struct fann_train_data* data, char *filename, unsigned int decimal_point){    return fann_save_train_internal_ex(data, filename, 1, decimal_point);}/** INTERNAL FUNCTION    Save the train data structure, return -1 when an error occurs. */int fann_save_train_internal_ex(struct fann_train_data* data, char *filename, unsigned int save_as_fixed, unsigned int decimal_point){    FILE *file = fopen(filename, "w");    if(!file){        fann_error(NULL, FANN_E_CANT_OPEN_TD_W, filename);        return -1;    }    fann_save_train_internal_fd(data, file, filename, save_as_fixed, decimal_point);    fclose(file);    return 0;}#endif /* USE_SAVE_EX *//*****************************************************************************/
/* #define SIMPLE_TEST_CASE */#ifdef SIMPLE_TEST_CASE/**  @example sample.c */
/**
 *  Simple test case showing sample usage of how to get
 *  layer/bias arrays and how to get/set weights in the
 *  connection array. @see Examples
 */
void simple_test_extensions(struct fann *ann)
{
    unsigned int num_layers;    unsigned int *layers;    unsigned int num_connections;    struct fann_connection *connections;    num_layers = fann_get_num_layers(ann);    layers = (unsigned int *)malloc(sizeof(unsigned int) * num_layers);    bias = (unsigned int *)malloc(sizeof(unsigned int) * num_layers);    if (layers != NULL)    {        fann_get_layer_array(ann, layers);        fann_get_bias_array(ann, bias);        free(bias);        free(layers);    }    num_connections = fann_get_total_connections(ann);    connections = (struct fann_connection *)        malloc(sizeof(struct fann_connection) * num_connections);    if (connections != NULL)    {        fann_get_connection_array(ann, connections);        fann_set_connection_array(ann, connections, num_connections);        free(connections);    }}
#endif /* SIMPLE_TEST_CASE */
/*****************************************************************************/

#endif /* FANN_EXTENSIONS_H_INCLUDED */

/*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -