#include"highgui.h"
#include "cv.h"
#include "ml.h"
#include"cxcore.h"
#include<stdio.h>
#pragma comment(lib,"cv210d.lib")
#pragma comment(lib,"highgui210d.lib")
#pragma comment(lib,"cxcore210d.lib")
#pragma comment(lib,"ml210d.lib")
///定义一个神经网络的类
//用的数据是UCI的iris的数据
CvANN_MLP ML;
int flag ;
void machinetrain()
{
int sample_count;
float data[150][7];
FILE*fp = fopen("train2.txt","r");
fscanf(fp,"%d",&sample_count);
printf("Find %d samples\n",sample_count);
///创建神经网络基本框架
CvMat *traindatas = cvCreateMat(sample_count,4,CV_32FC1);
CvMat *trainout = cvCreateMat(sample_count,3,CV_32FC1);
CvMat *netrulweight = cvCreateMat(sample_count,1,CV_32FC1);
CvMat *netrullayers = cvCreateMat(3,1,CV_32SC1);
///设置神经网络的输入,隐层,输出的神经元的个数
cvSet1D(netrullayers,0,cvScalar(4));
cvSet1D(netrullayers,1,cvScalar(10));
cvSet1D(netrullayers,2,cvScalar(3));
///读入训练的数据
for(int i=0;i<sample_count;i++)
{
for(int j=0;j<7;j++)
{
fscanf(fp,"%f,",&data[i][j]);
printf("%f ",data[i][j]);
/* if ( (j+1)%4==0 )
{ printf("\n");}*/
}
///为什么加了这句我输出不了前面的sample_count那些数值呢?????是从第二行开始输入的 好奇怪啊!!!!
printf("\n");
}
fclose(fp);
///把读入的数据依次赋给输入矩阵和输出矩阵
for(int i=0;i<sample_count;i++)
{
for(int j=0;j<4;j++)
cvSetReal2D(traindatas, i, j, data[i][j]);
}
for(int i=0;i<sample_count;i++)
{
for(int j=4;j<7;j++)
cvSet2D( trainout, i,j-4, cvScalar(data[i][j]) );
}
for(int i=0;i<sample_count;i++)
cvSet1D(netrulweight,i,cvScalar(1));///初始权重全设为1
/*void create( const CvMat* _layer_sizes,
int _activ_func=SIGMOID_SYM,
double _f_param1=0, double _f_param2=0 );*/
ML.create(netrullayers);///调用库文件里的成员函数
/* int train( const CvMat* _inputs, const CvMat* _outputs,
const CvMat* _sample_weights, const CvMat* _sample_idx=0,
CvANN_MLP_TrainParams _params = CvANN_MLP_TrainParams(),
int flags=0 );*/
ML.train(traindatas,trainout,netrulweight,0,
CvANN_MLP_TrainParams(
cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,///迭代类型
1000,///迭代次数
0.000001),///误差
CvANN_MLP_TrainParams::BACKPROP,///BP神经网络
0.01,0.05),0);///调用库文件里的成员函数
cvReleaseMat(&traindatas);
cvReleaseMat(&trainout);
cvReleaseMat(&netrulweight);
cvReleaseMat(&netrullayers);
}
///根据训练的神经网络预测输出
void predict(CvMat * data)
{
CvMat *output = cvCreateMat(1,3,CV_32FC1);
ML.predict(data,output);///调用库文件里的成员函数
/* double *min_val = 0;
double *max_val = 0;
CvPoint *min_loc = 0,*max_loc = 0;
cvMinMaxLoc(output,min_val,max_val);
printf(" %f",*max_val);
//printf("%d",*max_val);*/
int best = 0;
float max = output->data.fl[0];
printf("%f ",max);
float outdata[3];
for(int i=1;i<3;i++)
{
printf("%f ",output->data.fl[i]);
outdata[i] = output->data.fl[i];
if(outdata[i]>max)
{
max = outdata[i];
best = i;
}
}
printf("\n");
printf("%d %f\n",best+1,max);
flag = best+1;
cvReleaseMat(&output);
}
///主函数:
int main(int argc,char *argv[])
{
machinetrain();
/* float value[4]= {6.5,3.0,5.0,2.1};
CvMat inputdata;
cvInitMatHeader(&inputdata,1,4,CV_32FC1,value);
predict(&inputdata);*/
float test_data[30][4];
FILE *fp = fopen("test2.txt","r");
CvMat *test_mat = cvCreateMat(30,4,CV_32FC1);
for(int i=0;i<30;i++)
{ for(int j=0;j<4;j++)
{
fscanf(fp,"%f,",&test_data[i][j]);
cvSetReal2D(test_mat,i,j,test_data[i][j]);
}
}
CvMat mmat=cvMat(30,4,CV_32FC1, test_data);
CvMat *mat=&mmat;
CvMat *rowmat = cvCreateMat(1,4,CV_32FC1);
//CvMat *submat=cvCreateMat(3,4,CV_32FC1);
CvMat submat;
int count1=0,count2=0,count3 = 0;
for(int i=0;i<30;i++)
{
rowmat = cvGetRow(mat,&submat,i);//取第i行
if(i>=0&&i<10)
{
predict(rowmat);
if(flag!=1)
count1++;
}
if(i>=10&&i<20)
{
predict(rowmat);
if(flag!=2)
count2++;
}
if(i>=20&&i<30)
{
predict(rowmat);
if(flag!=3)
count3++;
}
}
double mse;
mse = (count1+count2+count3)/30.0;
printf("误差为 :%f\n",mse);
cvReleaseMat(&test_mat);
//cvReleaseMat(&rowmat);
//cvReleaseMat(&mat);
return 0;
}