📄 sigmoid_example.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 a sigmoid network that learns to classify four letters.*/
#include <nn-utility.h>
using namespace nn_utility;
BITMAP<float> *bit = new BITMAP<float>( "bit.txt" );
typedef nn_utility_functions<float>::VECTOR VECTOR;
class sigmoid_example : public nn_utility_functions<float>{
public:
typedef float VECTOR[NN_UTIL_SIZE];
void GetInput( int interation, VECTOR &send, VECTOR &target );
bool CheckTrain( VECTOR output, VECTOR target, int length );
};
VECTOR letters[4];
void sigmoid_example::GetInput( int interation, VECTOR &send, VECTOR &target ){
CopyVector( send, letters[interation % 4], 25 );
switch ( interation % 4 ){
case 0 : LoadVectorf( target, 9, 0.9, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ); break;
case 1 : LoadVectorf( target, 9, 0.1, 0.9, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ); break;
case 2 : LoadVectorf( target, 9, 0.1, 0.1, 0.9, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ); break;
case 3 : LoadVectorf( target, 9, 0.1, 0.1, 0.1, 0.9, 0.1, 0.1, 0.1, 0.1, 0.1 ); break;
}
}
bool sigmoid_example::CheckTrain( VECTOR output, VECTOR target, int length ){
for ( int ir = 0; ir < length; ir++ ){
if ( output[ir] < target[ir]-0.2 || output[ir] > target[ir]+0.2 ){
return true; }
}
return false;
}
void SetRandomWeights( layer<float> **in, int row, int col ){
for ( int i = 0; i < col; i++ ){
float it2 = rand() % 10;
(*in)->weight[i] = it2/10*( (rand() % 2) == 0 ? -1 : 1 );
for ( int e = 0; e < row; e++ ){
float it = rand() % 10;
(*in)->matrix[e][i] = it/10*( (rand() % 2) == 0 ? -1 : 1 );
}
}
}
int Largest( VECTOR output, int length ){
int index = 0;
for ( int i = 1; i < length; i++ ){
if ( output[i] > output[index] ){ index = i; }
}
return index;
}
void classify_result( VECTOR input, VECTOR output ){
bit->Print( input, 5, 25 );
cout << "Network fired: ";
int big = Largest( output, 9 );
switch( big ){
case 0 : cout << "A\n"; break;
case 1 : cout << "B\n"; break;
case 2 : cout << "C\n"; break;
case 3 : cout << "D\n"; break;
default: cout << "unknown\n"; break;
};
}
int main(){
sigmoid_example derived;
SIGMOID *hidden = new SIGMOID();
hidden->define( 25,6 );
SIGMOID *classify = new SIGMOID();
classify->define( 6,9 );
layer<float> *ppHidden = hidden;
layer<float> *ppClassify = classify;
SetRandomWeights( &ppHidden, 25,6 );
SetRandomWeights( &ppClassify, 6,9 );
for ( int i = 0; i < 4; i++ ){
bit->readbitmap( letters[i], i );
hidden->sigmoid( letters[i], 25 ); }
derived.Insert( &ppHidden, &ppClassify );
VECTOR FINAL;
derived.ClearVector( FINAL, 9 );
derived.train( &ppHidden, &ppClassify, 100, 0.9F, true );
cout << "\n\n";
cout << "Trained for 100 epochs at a learning rate of 0.3 under supervised learning.\n";
cout << "The bitmap noise() function added noise to the following letters and the network\n";
cout << "classified them according to the results below each 5x5 bitmap\n";
cout << '\n';
for ( int go = 0; go < 4; go++ ){
cout << "Given noisy input pattern:\n";
bit->noise( letters[go], 25, 0, 10 );
hidden->FeedForward( letters[go], FINAL );
classify_result( letters[go], FINAL );
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -