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

📄 fann_cpp_subclass.h

📁 python 神经网络 数据挖掘 python实现的神经网络算法
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef FANN_CPP_SUBCLASS_H_INCLUDED
#define FANN_CPP_SUBCLASS_H_INCLUDED

#include <stdarg.h>
#include <string>
#include <fann_cpp.h>

#include <iostream>
/* Namespace: FANN
    The FANN namespace groups the C++ wrapper definitions */
namespace FANN
{
	
    template <typename T>
    class helper_array
    {
    	public:
            helper_array()
            {
                array=0;
                array_len=0;
		can_delete=true;
            }
            void set (T * array, unsigned int len)
            {
                this->array=array;
                this->array_len=array_len;
            }
            T* array;
            unsigned int array_len;
	    bool can_delete;
    };
    
    template <typename T>
    class helper_array_array
    {
        public:
            helper_array_array()
            {
                arrays=0;
                array_len=0;
                array_num=0;
                can_delete=false;
            }
            void set (T ** arrays, unsigned int len, unsigned int nun)
            {
                this->arrays=arrays;
                this->array_len=array_len;
                this->array_num=array_num;
            }
            T** arrays;
            unsigned int array_len;
            unsigned int array_num;
            bool can_delete;
    };
	
    /* Forward declaration of class neural_net and training_data */
    class Neural_net;
    class Training_data;


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

    /* Class: training_data

        Encapsulation of a training data set <struct fann_train_data> and
        associated C API functions.
    */
    class Training_data : public 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() : training_data()
        {
        }

        /* 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();
        }



        /* 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>
        */
        helper_array_array<fann_type>* get_input()
        {
            if (train_data == NULL)
            {
                return NULL;
            }
            else
            {
                helper_array_array<fann_type>* ret = new helper_array_array<fann_type>;
                
                ret->arrays=train_data->input;
                ret->array_num=train_data->num_data;
                ret->array_len=train_data->num_input;
		ret->can_delete=false;
                return ret;
            }
        }

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

            See also:
                <get_input>, <set_train_data>
        */

        helper_array_array<fann_type>* get_output()
        {
            if (train_data == NULL)
            {
                return NULL;
            }
            else
            {
                helper_array_array<fann_type>* ret = new helper_array_array<fann_type>;
                
                ret->arrays=train_data->output;
                ret->array_num=train_data->num_data;
                ret->array_len=train_data->num_output;
		ret->can_delete=false;
                return ret;
            }
        }


        /* 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.
            
            See also:
                <get_input>, <get_output>
        */

        void set_train_data(helper_array_array< fann_type >* input,
            helper_array_array< fann_type >* output)
        {
            if (input->array_num!=output->array_num) 
            {
                std::cerr<<"Error: input and output must have the same dimension!"<<std::endl;
                return;
            }
            input->can_delete=true;
            output->can_delete=true;
            
	    training_data::set_train_data(input->array_num, input->array_len, input->arrays, output->array_len, output->arrays);
        }  


    };

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

    /* Class: Neural_net

        Encapsulation of a neural network <struct fann> and
        associated C API functions.
    */
    class Neural_net : public neural_net
    {
    public:
        /* Constructor: neural_net
        
            Default constructor creates an empty neural net.
            Use one of the create functions to create the neural network.

            See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <create_standard_array>, <create_sparse_array>, <create_shortcut_array>
        */
        Neural_net() : neural_net()
        {
        }

        /* Destructor: ~neural_net

            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
        ~Neural_net()
        {
            destroy();
        }


        /* Method: create_standard_array

           Just like <create_standard>, but with an array of layer sizes
           instead of individual parameters.

	        See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <fann_create_standard>

	        This function appears in FANN >= 2.0.0.
        */ 

        bool create_standard_array( helper_array<unsigned int>* layers)
        {
            return neural_net::create_standard_array( layers->array_len, layers->array);
        }

        /* Method: create_sparse_array
           Just like <create_sparse>, but with an array of layer sizes
           instead of individual parameters.

           See <create_sparse> for a description of the parameters.

	        See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <fann_create_sparse_array>

	        This function appears in FANN >= 2.0.0.
        */

        bool create_sparse_array(float connection_rate,
            helper_array<unsigned int>* layers)
         {
            return neural_net::create_sparse_array( connection_rate, layers->array_len, layers->array);
         }

        /* Method: create_shortcut_array

           Just like <create_shortcut>, but with an array of layer sizes
           instead of individual parameters.

	        See <create_standard_array> for a description of the parameters.

	        See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <fann_create_shortcut_array>

	        This function appears in FANN >= 2.0.0.
        */

        bool create_shortcut_array( helper_array<unsigned int>* layers)
        {

⌨️ 快捷键说明

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