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

📄 test.cpp

📁 本文件包实现了人工神经网络的实现和建模。采用BP算法。
💻 CPP
字号:
#include <afx.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "NeuralNetwork.h"

void main()
{
	int len, row, col;

	double rawdata[4][4];

	char buffer[2]={'\0','\0'};

	CString templine;
	CString oneline;
	
	row = 0;
	col = 0;

	for( int i=1; i<4; i++)
	{
		for(int j=1;j<4;j++)
		{
			rawdata[i][j]=0;
		}
	}
	
	CStdioFile file;
	file.Open("test.txt",CFile::modeRead);

	len= file.GetLength();

	while (len!=0)
	{
		oneline.Empty();
		templine.Empty();
		file.Read(buffer,1);		

		while(buffer[0]!='\t' && buffer[0]!='\n' && len!=0)
		{
			templine.Format("%s",buffer);
			oneline+=templine;
			file.Read(buffer,1);
			len--;
		}

		rawdata[row][col]=atof(oneline);		
		col++;

		if (buffer[0] == '\n' && len!=0)
		{
			row++;
			col=0;
		}
	}

	file.Close();

	for (i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			cout<<rawdata[i][j]<<" ";
		}
		cout<<endl;		
	}

	//***********************Neural Network******************//

	int numLayers = 4, lSz[4] = {3,3,3,1};
	double beta = 0.3, alpha = 0.1, Thresh =  4.6;
	//long num_iter = 20000;                  //number of iterations during training process
	int k=0;
	
	// Creating the net
	CNeuralNetwork *bp = new CNeuralNetwork(numLayers, lSz, beta, alpha);
	
	cout<< endl <<  "Step1: Now training the network." <<endl;	
	
//	for (long k =0; k<num_iter; k++)
//	{
		
		
	while(1)
	{
		bp->bpgt(rawdata[k%4], &rawdata[k%4][3]);
		
  		if (bp->mse(&rawdata[k%4][3]) >= Thresh)
		{
			if ( k%(/*num_iter*/10) == 0 )
				cout<<  endl <<  "MSE:  " << bp->mse(&rawdata[k%4][3]) << "... Training..." << endl;

		}
		else
		{
			cout << endl << "Network Trained. Threshold value achieved in " << k << " iterations." << endl;
			cout << "MSE:  " << bp->mse(&rawdata[k%4][3]) 
				<<  endl <<  endl;
			break;
		}
		k++;
	}
		
//   }
	
//	if ( k == num_iter )
//		cout << endl << k << " iterations completed..." << "MSE: " << bp->mse(&rawdata[(k-1)%4][3]) << endl;  	
	
	//cout<< "Step2: Using the trained network to make predctions on test data...." << endl << endl;	
	//for ( k = 0 ; k < 8 ; k++ )
	//{
	//	bp->ffwd(rawdata[k]);
	//	cout << rawdata[k][0]<< "  " << rawdata[k][1]<< "  "  << rawdata[k][2]<< "  " << bp->Out(0) << endl;
	//}


}

⌨️ 快捷键说明

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