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

📄 recurrent.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

#include <nn-utility.h>

using namespace nn_utility;

class MINE : public nn_utility_functions<float>{
	public:
		//using a MATRIX, not a VECTOR	
		void GetInput( int interation, MATRIX &send, MATRIX &target, int &inputs );
}derived;

void MINE::GetInput( int interation, MATRIX &send, MATRIX &target, int &inputs ){
	LoadVectorf( send[0], 5, 0.9, 0.1, 0.1, 0.1, 0.1 );
	LoadVectorf( send[1], 5, 0.1, 0.9, 0.1, 0.1, 0.1 );

	LoadVectorf( target[0], 3, 0.9, 0.1, 0.1 );
	LoadVectorf( target[1], 3, 0.1, 0.9, 0.1 );

	inputs = 2;
}

void SetConstant( layer<float> ** TOSET, float value ){
	for ( int i = 0; i < (*TOSET)->row; i++ ){
		for ( int e = 0; e < (*TOSET)->col; e++ ){
			(*TOSET)->matrix[i][e] = value;
		}
	}
}

int main(){
	//define a handler	
	layer<float> *parent = new layer<float>();	
	//define the handler as a recurrent network
	parent->define_recurrent();

	//define it's two layers
	SIGMOID *one = new SIGMOID();
	SIGMOID *two = new SIGMOID();

	//create buffers
	layer<float> *ppOne    = one;
	layer<float> *ppTwo    = two;
	
	//define the layer's sizes
	one->define( 10,5 );
	two->define( 10,5 );
	//add them to the parent handler
	parent->add( &ppOne );
	parent->add( &ppTwo );
	
	//make their matricies constant
	SetConstant( &ppOne, 0.2F );
	SetConstant( &ppTwo, 0.2F );

	float FINAL[NN_UTIL_SIZE][NN_UTIL_SIZE], INPUT[NN_UTIL_SIZE][NN_UTIL_SIZE];
	derived.LoadVectorf( INPUT[0], 5, 0.9F, 0.1F, 0.1F, 0.1F, 0.1F );
	derived.LoadVectorf( INPUT[1], 5, 0.1F, 0.9F, 0.1F, 0.1F, 0.1F );
	
	//Feed Forward with FeedForward_recurrent or FeedForward
	parent->FeedForward_recurrent( INPUT, FINAL, 2 );
	//Train the network
	derived.train( &parent, 20, 0.9F );

	cout << "output 1: ";
	derived.PrintVector( FINAL[0], 3 );
	cout << "output 2: ";
	derived.PrintVector( FINAL[1], 3 );

	cout << "MULTI 1: "
	     << one->matrix[0][0] << " " << one->matrix[1][0] << '\n';
	cout << "MULTI 2: "
	     << two->matrix[0][0] << " " << two->matrix[1][0] << '\n';

	parent->FeedForward_recurrent( INPUT, FINAL, 2 );
	
	cout << "output 1: ";
	derived.PrintVector( FINAL[0], 3 );
	cout << "output 2: ";
	derived.PrintVector( FINAL[1], 3 );
	
	cout << '\n';
	return 0;
}

⌨️ 快捷键说明

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