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

📄 fann_cpp.h

📁 python 神经网络 数据挖掘 python实现的神经网络算法
💻 H
📖 第 1 页 / 共 5 页
字号:
        unsigned int max_epochs, unsigned int epochs_between_reports,
        float desired_error, unsigned int epochs, void *user_data);

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

    /* Class: training_data

        Encapsulation of a training data set <struct fann_train_data> and
        associated C API functions.
    */
    class training_data
    {
    public:
        /* Constructor: training_data
        
            Default constructor creates an empty neural net.
            Use <read_train_from_file>, <set_train_data> or <create_train_from_callback> to initialize.
        */
        training_data() : train_data(NULL)
        {
        }

        /* Constructor: training_data
        
            Copy constructor constructs a copy of the training data.
            Corresponds to the C API <fann_duplicate_train_data> function.
        */
        training_data(const training_data &data)
        {
            destroy_train();
            if (data.train_data != NULL)
            {
                train_data = fann_duplicate_train_data(data.train_data);
            }
        }

        /* Destructor: ~training_data

            Provides automatic cleanup of data.
            Define USE_VIRTUAL_DESTRUCTOR if you need the destructor to be virtual.

            See also:
                <destroy>
        */
#ifdef USE_VIRTUAL_DESTRUCTOR
        virtual
#endif
        ~training_data()
        {
            destroy_train();
        }

        /* Method: destroy
        
            Destructs the training data. Called automatically by the destructor.

            See also:
                <~training_data>
        */
        void destroy_train()
        {
            if (train_data != NULL)
            {
                fann_destroy_train(train_data);
                train_data = NULL;
            }
        }

        /* Method: read_train_from_file
           Reads a file that stores training data.
           
           The file must be formatted like:
           >num_train_data num_input num_output
           >inputdata seperated by space
           >outputdata seperated by space
           >
           >.
           >.
           >.
           >
           >inputdata seperated by space
           >outputdata seperated by space
           
           See also:
   	        <neural_net::train_on_data>, <save_train>, <fann_read_train_from_file>

            This function appears in FANN >= 1.0.0
        */ 
        bool read_train_from_file(const std::string &filename)
        {
            destroy_train();
            train_data = fann_read_train_from_file(filename.c_str());
            return (train_data != NULL);
        }

        /* Method: save_train
           
           Save the training structure to a file, with the format as specified in <read_train_from_file>

           Return:
           The function returns true on success and false on failure.
              
           See also:
   	        <read_train_from_file>, <save_train_to_fixed>, <fann_save_train>
        	
           This function appears in FANN >= 1.0.0.   	
         */ 
        bool save_train(const std::string &filename)
        {
            if (train_data == NULL)
            {
                return false;
            }
            if (fann_save_train(train_data, filename.c_str()) == -1)
            {
                return false;
            }
            return true;
        }

        /* Method: save_train_to_fixed
           
           Saves the training structure to a fixed point data file.
         
           This function is very usefull for testing the quality of a fixed point network.
           
           Return:
           The function returns true on success and false on failure.
           
           See also:
   	        <save_train>, <fann_save_train_to_fixed>

           This function appears in FANN >= 1.0.0.   	
         */ 
        bool save_train_to_fixed(const std::string &filename, unsigned int decimal_point)
        {
            if (train_data == NULL)
            {
                return false;
            }
            if (fann_save_train_to_fixed(train_data, filename.c_str(), decimal_point) == -1)
            {
                return false;
            }
            return true;
        }

        /* Method: shuffle_train_data
           
           Shuffles training data, randomizing the order. 
           This is recommended for incremental training, while it have no influence during batch training.
           
           This function appears in FANN >= 1.1.0.
         */ 
        void shuffle_train_data()
        {
            if (train_data != NULL)
            {
                fann_shuffle_train_data(train_data);
            }
        }

        /* Method: merge_train_data
           
           Merges the data into the data contained in the <training_data>.
           
           This function appears in FANN >= 1.1.0.
         */ 
        void merge_train_data(const training_data &data)
        {
            fann_train_data *new_data = fann_merge_train_data(train_data, data.train_data);
            if (new_data != NULL)
            {
                destroy_train();
                train_data = new_data;
            }
        }

        /* Method: length_train_data
           
           Returns the number of training patterns in the <training_data>.

           See also:
           <num_input_train_data>, <num_output_train_data>, <fann_length_train_data>

           This function appears in FANN >= 2.0.0.
         */ 
        unsigned int length_train_data()
        {
            if (train_data == NULL)
            {
                return 0;
            }
            else
            {
                return fann_length_train_data(train_data);
            }
        }

        /* Method: num_input_train_data

           Returns the number of inputs in each of the training patterns in the <training_data>.
           
           See also:
           <num_output_train_data>, <length_train_data>, <fann_num_input_train_data>

           This function appears in FANN >= 2.0.0.
         */ 
        unsigned int num_input_train_data()
        {
            if (train_data == NULL)
            {
                return 0;
            }
            else
            {
                return fann_num_input_train_data(train_data);
            }
        }

        /* Method: num_output_train_data
           
           Returns the number of outputs in each of the training patterns in the <struct fann_train_data>.
           
           See also:
           <num_input_train_data>, <length_train_data>, <fann_num_output_train_data>

           This function appears in FANN >= 2.0.0.
         */ 
        unsigned int num_output_train_data()
        {
            if (train_data == NULL)
            {
                return 0;
            }
            else
            {
                return fann_num_output_train_data(train_data);
            }
        }

        /* Grant access to the encapsulated data since many situations
            and applications creates the data from sources other than files
            or uses the training data for testing and related functions */

        /* Method: get_input
        
            Returns:
                A pointer to the array of input training data

            See also:
                <get_output>, <set_train_data>
        */
        fann_type **get_input()
        {
            if (train_data == NULL)
            {
                return NULL;
            }
            else
            {
                return train_data->input;
            }
        }

        /* Method: get_output
        
            Returns:
                A pointer to the array of output training data

            See also:
                <get_input>, <set_train_data>
        */
        fann_type **get_output()
        {
            if (train_data == NULL)
            {
                return NULL;
            }
            else
            {
                return train_data->output;
            }
        }

        /* Method: set_train_data

            Set the training data to the input and output data provided.

            A copy of the data is made so there are no restrictions on the
            allocation of the input/output data and the caller is responsible
            for the deallocation of the data pointed to by input and output.

           Parameters:
             num_data      - The number of training data
             num_input     - The number of inputs per training data
             num_output    - The number of ouputs per training data
             input      - The set of inputs (a pointer to an array of pointers to arrays of floating point data)
             output     - The set of desired outputs (a pointer to an array of pointers to arrays of floating point data)

            See also:
                <get_input>, <get_output>
        */
        void set_train_data(unsigned int num_data,
            unsigned int num_input, fann_type **input,
            unsigned int num_output, fann_type **output)
        {
            // Uses the allocation method used in fann
            struct fann_train_data *data =
                (struct fann_train_data *)malloc(sizeof(struct fann_train_data));
            data->input = (fann_type **)calloc(num_data, sizeof(fann_type *));
            data->output = (fann_type **)calloc(num_data, sizeof(fann_type *));

            data->num_data = num_data;
            data->num_input = num_input;
            data->num_output = num_output;

        	fann_type *data_input = (fann_type *)calloc(num_input*num_data, sizeof(fann_type));
        	fann_type *data_output = (fann_type *)calloc(num_output*num_data, sizeof(fann_type));

            for (unsigned int i = 0; i < num_data; ++i)
            {
                data->input[i] = data_input;
                data_input += num_input;
                for (unsigned int j = 0; j < num_input; ++j)
                {
                    data->input[i][j] = input[i][j];
                }
                data->output[i] = data_output;
		        data_output += num_output;
                for (unsigned int j = 0; j < num_output; ++j)
                {
                    data->output[i][j] = output[i][j];
                }
            }
            set_train_data(data);
        }

private:
        /* Set the training data to the struct fann_training_data pointer.
            The struct has to be allocated with malloc to be compatible
            with fann_destroy. */
        void set_train_data(struct fann_train_data *data)
        {
            destroy_train();
            train_data = data;
        }

⌨️ 快捷键说明

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