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

📄 kohen_example.cpp

📁 BP神经网络算法的VC+源码
💻 CPP
字号:
/*
    nn-utility (Provides neural networking utilities for c++ programmers)
    Copyright (C) 2003 Panayiotis Thomakos

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
*/
//To contact the author send an email to panthomakos@users.sourceforge.net

/*Demonstrates Kohen Self Organizing Feature Map that learns four letters.*/

#include <nn-utility.h>
using namespace nn_utility;
//define a bitmap
BITMAP<float> *bit = new BITMAP<float>( "bitmap.txt" );
//define a vector array to hold the letters
nn_utility_functions<float>::VECTOR letters[4];

//globaly define the kohen single-layer network
KOHEN_SOFM *classify;

//overload nn_utility_functions<float> so that we can implement a GetInput function
class kohen_example : public nn_utility_functions<float>{
	public:
		typedef float VECTOR[NN_UTIL_SIZE];
		
		void GetInput( int interation, VECTOR &send, VECTOR &target );
}derived;  //define a global "derived"

void kohen_example::GetInput( int interation, VECTOR &send, VECTOR &target ){
	CopyVector( send, letters[interation % 4], 256 ); //get the current input by % 4
	
	//modify the radius, according to what interation the network is on
	if ( interation > 7000 )
		classify->radius = 1;
	else if ( interation > 5000 )
		classify->radius = 2;
	else if ( interation > 3000 )
		classify->radius = 3;
	else
		classify->radius = 4;
}

//set random weights for a layer's matrix. This function is defined by the user and can
//be found in other examples, if you wish to use the code for other examples, feel free to.
//Just change the KOHEN_SOFM to the kind of layer you are seting random weights for, for
//instance SIGMOID, or KOHEN.
void SetRandomWeights( KOHEN_SOFM **in, int row, int col ){
	for ( int i = 0; i < row; i++ ){
		for ( int e = 0; e < col; e++ ){
			float it = rand() % 10;
			(*in)->matrix[i][e] = it/100*( (rand() % 2) == 0 ? -1 : 1 );
		}
	}
}

int main(){

	//read the bitmap as it is in the file
	for ( int i = 0; i < 4; i++ )
		bit->readbitmap( letters[i], i );
	
	//define the classification layer
	classify = new KOHEN_SOFM();
	//define it's size
	classify->define( 256,9 );
	//set random weights
	SetRandomWeights( &classify, 256,9 );
	
	//define a floating-point vector called "FINAL"
	nn_utility_functions<float>::VECTOR FINAL;

	//create a buffer for the layer
	layer<float> *ppClassify = classify;
	
	//train the network for 9000 interations, at a learning rate of 0.9 with no output to the screen
	derived.train( &ppClassify, 9000, 0.9, false );

	//verify the results of training against noisy data
	cout << '\n';
	for ( int go = 0; go < 4; go++ ){
		classify->FeedForward( letters[go], FINAL );
		cout << go << " (Classification without noise) ";
		derived.PrintVector( FINAL, 9 ); //pre noise output

		bit->noise( letters[go], 256, 1, 5 ); //add noise
		classify->FeedForward( letters[go], FINAL );
		cout << go << " (Classification with noise   ) ";
		derived.PrintVector( FINAL, 9 ); //post noise output
	}

	cout << '\n';
	return 0;
}

⌨️ 快捷键说明

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