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

📄 functions.cpp

📁 neural network utility is a Neural Networks library for the C++ Programmer. It is entirely object o
💻 CPP
字号:
#include "nn-utility.h"

using namespace nn_utility;

/*User defined function to recieve input for training patterns*/
template<class T>
void nn_utility_functions<T>::GetInput( int interation, VECTOR &send, VECTOR &target ){};
template<class T>
void nn_utility_functions<T>::GetInput( int interation, MATRIX &send, MATRIX &target, int &inputs ){};
/*User defined function to determine whether or not to train after a result and target are presented*/
template<class T>
bool nn_utility_functions<T>::CheckTrain( VECTOR output, VECTOR target, int length ){ return true; };
template<class T>
bool nn_utility_functions<T>::
CheckTrain( MATRIX output, MATRIX target, int length_of_output, int length ){ return true; };

/*Function to print a vector to the screen.*/
template<class T> void nn_utility_functions<T>::PrintVector( char * str_, VECTOR &vector, int length ){
	cout << str_ << '\n';
	PrintVector( vector, length ); }
template<class T> void nn_utility_functions<T>::PrintVector( VECTOR &vector, int length ){
	cout << "[\t";
	for ( int vector_i = 0; vector_i < length; vector_i++ )
		cout << setprecision(2) << vector[ vector_i ] << "\t";
	cout << "]\n"; }
template<class T> void nn_utility_functions<T>::PrintMatrix( char *str_, MATRIX &matrix, int row, int col ){
	cout << str_ << '\n';
	PrintMatrix( matrix, row, col );  }
template<class T> void nn_utility_functions<T>::PrintMatrix( MATRIX &matrix, int row, int col ){
	for ( int i = 0; i < row; i++ )
	PrintVector( matrix[i], col ); }
/*Function to print a vector to the screen exactly*/
template<class T> void nn_utility_functions<T>::PrintVectorExact( VECTOR &vector, int length ){
	cout << "[\t";
	for ( int vector_i = 0; vector_i < length; vector_i++ )
		cout << vector[vector_i] << "\t";
	cout << "]\n";
}
/*Function to clear a vector*/
template<class T> void nn_utility_functions<T>::ClearVector( VECTOR &vector, int length )
	{ ClearVector( vector, length, 0 ); }
template<class T> void nn_utility_functions<T>::ClearVector( VECTOR &vector, int length, T clear_value ){
	for ( int vec = 0; vec < length; vec++ )
		vector[vec] = clear_value; }
/*Function to load a vector with values*/
template<class T> void nn_utility_functions<T>::LoadVectori( VECTOR &vector, int len, T value, ... ){
	va_list argument_ptr;
	vector[0] =  value;
	va_start( argument_ptr, value );
	int counter = 1;
	while( counter < len ){
		value = (T)va_arg(argument_ptr, int );
		vector[counter++] = value; }
	va_end(argument_ptr);
};

/*Function to load a vector with values*/
template<class T> void nn_utility_functions<T>::LoadVectorf( VECTOR &vector, int len, T value, ... ){
	va_list argument_ptr;
	vector[0] =  value;
	va_start( argument_ptr, value );
	int counter = 1;
	while( counter < len ){
		value = (T)va_arg(argument_ptr, double );
		vector[counter++] = value; }
	va_end(argument_ptr);
};

/*Function for copying the contents of one vector to another vector*/
template<class T> void nn_utility_functions<T>::CopyVector( VECTOR &destination, VECTOR source, int length ){
	for ( int t = 0; t < length; t++ )
	destination[t] = source[t]; }
/*Add two vectors*/
template<class T>
void nn_utility_functions<T>::AddVectors( VECTOR &destination, VECTOR source1, VECTOR source2, int length ){
	for ( int t = 0; t < length; t++ )
	destination[t] = source1[t] + source2[t]; }
/*Function to insert a layer at the end of a multi-layer network*/
template<class T> bool nn_utility_functions<T>::Insert( layer<T> **START, layer<T> **ADD ){
	if ( !((*START)->Next) ){
		(*START)->Next = (*ADD);
		(*ADD)->Previous = (*START);
		return true; }
	Insert( &(*START)->Next, &(*ADD) );
}
/*Function to insert a layer after the "First" layer*/
template<class T> void nn_utility_functions<T>::InsertAfter( layer<T> **First, layer<T> **Second ){
	(*Second)->Next = (*First)->Next;
	(*Second)->Previous = (*First);
	(*First)->Next = (*Second);
}

/*Function to dispaly a multi-layer network through a screen dump*/
template<class T> void nn_utility_functions<T>::DisplayLAYER( layer<T> **start ){
	(*start)->Print();
	if ( !((*start)->Next) ){
		cout << "\n--Output Layer (Eventually fires modified output)--\n";}
	else{
		cout << "\n--Hidden Layer (Fires to next layer)-->\n";
		if ( (*start)->Next )
			DisplayLAYER( &(*start)->Next );
	}
}
/*Function to train a neural network for a given number of interations*/
template<class T>
void nn_utility_functions<T>::train( layer<T> ** input, int interations, T learning_rate, bool print_info ){
	train( &(*input), &(*input), interations, learning_rate, print_info ); }
template<class T> void nn_utility_functions<T>::train( layer<T> **TRACK, int interations, T learning_rate ){
	MATRIX target, SEND, FINAL;
	int inputs;
	for ( int inter = 0; inter < interations; inter++ ){
		int inputs;
		GetInput( inter, SEND, target, inputs );

		(*TRACK)->FeedForward_recurrent( SEND, FINAL, inputs );
		if ( CheckTrain( FINAL, target, (*TRACK)->layers[(*TRACK)->length-1]->col, inputs ) ){
			(*TRACK)->BackPropagate_recurrent( SEND, target, learning_rate, inputs ); }
	}
}

template<class T>
void nn_utility_functions<T>::
train( layer<T> ** input, layer<T> **output, int interations, T learning_rate, bool print_info ){
	if ( (*input)->network_recurrent )
		train( &(*input), interations, learning_rate );
	else{
	for ( int inter = 0; inter < interations; inter++ ){
		VECTOR target;
		VECTOR SEND;
		if ( !((*input)->annealing_on) )
			GetInput( inter, SEND, target );
		else{
			CopyVector( SEND,
				    (*input)->annealing_input,
				    (*input)->row );
		}	
		if ( print_info ){
			cout << "Presented Network with : "; PrintVector( SEND, (*input)->row );}
		VECTOR FINAL;
		(*input)->FeedForward( SEND, FINAL );
		if ( print_info ){
			cout << "Network fired : "; PrintVector( FINAL, (*output)->col );}
		if ( (*input)->annealing_on ){
			(*input)->annealing( SEND, (*input)->row, FINAL, (*output)->col ); }
		else if ( CheckTrain( FINAL, target, (*output)->col ) ){
			(*output)->BackPropagate( SEND, target, learning_rate, (*input)->row );
			if ( print_info ){
				cout << "Updated network on interation " << inter << "\n\n";}
		}
	}
	}
}

template nn_utility_functions<int>;
template nn_utility_functions<float>;
template nn_utility_functions<double>;

⌨️ 快捷键说明

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