模糊神经网络算法

模糊神经网络的计算步骤如下: 测试数据如下:(胃病和非胃病) 输入X: 228 134 20 11 245 134 10 40 200 167 12 27 170 150 7 8 100 167 20 14 150 117 7 6 120 133 10 26 160 100 5 10 185 115 5 19 170 125

模糊神经网络的计算步骤如下:


测试数据如下:(胃病和非胃病)

输入X:

228 134 20 11
245 134 10 40
200 167 12 27
170 150 7 8
100 167 20 14
150 117 7 6
120 133 10 26
160 100 5 10
185 115 5 19
170 125 6 4
165 142 5 3
185 108 2 12
225 125 7 14
130 100 6 12

输出期望y:(1为胃病,2为非胃病)

1 1 1 1 1 2 2 2 2 2 2 2 2 2


代码如下:(该代码输出结果还是有些欠缺的,但从结果可以看出胃病和非胃病还是有比较明显的区别)



#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "vector"
using namespace std;


#define dimIn 4  //输入样本的维数
#define dimOut 1  //输出样本的维数
#define hidePoint 8  //隐藏节点
#define MAXITER 1000   //最大迭代次数
#define alpha 0.35   //学习率


typedef vector<double> doubleVector;


vector<doubleVector> getInputTrain(char *File);  //获取训练样本
vector<doubleVector> getOutputTrain(char *File);  //获取期望样本
vector<doubleVector> inputNormalization(vector<doubleVector> inputTrain);  //训练样本归一化
vector<doubleVector> outputNormalization(vector<doubleVector> outputTrain);  //输出期望归一化
void trainNet(vector<doubleVector> inputTrain, vector<doubleVector> outputTrain);  //开始训练样本


double maxInputTrain[dimIn], minInputTrain[dimIn];    //输入训练样本的最大最小值
double maxOutputTrain[dimOut], minOutputTrain[dimOut];   //期望样本的最大最小值


void main()
{
int i, j;
char *inputFile = "input.txt";
char *outputFile = "output.txt";


vector<doubleVector> inputTrain;  //输入训练样本
vector<doubleVector> outputTrain;  //输出期望样本


inputTrain = getInputTrain(inputFile);
outputTrain = getOutputTrain(outputFile);


inputTrain = inputNormalization(inputTrain);  //样本归一化
outputTrain = outputNormalization(outputTrain);  //输出期望归一化


trainNet(inputTrain, outputTrain);  //开始训练样本

}




//开始训练样本
void trainNet(vector<doubleVector> inputTrain, vector<doubleVector> outputTrain)
{
double Test[dimIn] = {100, 117, 7, 2};


int iter=0;
int i, j, k;
double p[dimIn+1][hidePoint], p_1[dimIn+1][hidePoint], d_p[hidePoint];
double c[hidePoint][dimIn], c_1[hidePoint][dimIn], d_c[hidePoint][dimIn];
double b[hidePoint][dimIn], b_1[hidePoint][dimIn], d_b[hidePoint][dimIn];
double u[dimIn][hidePoint];
double w[hidePoint];
double y[hidePoint];


//初始化参数
srand(time(NULL));
for(i=0; i<hidePoint; i++)
{
for(j=0; j<dimIn; j++)
{
c[i][j] = ((double)rand()/RAND_MAX);
b[i][j] = ((double)rand()/RAND_MAX);
c_1[i][j] = c[i][j];
b_1[i][j] = b[i][j];
}
}


for(i=0; i<dimIn+1; i++)
{
for(j=0; j<hidePoint; j++)
{
p[i][j] = 0.18;
p_1[i][j] = p[i][j];
}
}




//开始训练
double addw, addyw;
double e, yn;


for(iter=0; iter<MAXITER; iter++)
{
for(k=0; k<inputTrain.size(); k++)
{
//获取模糊参数
for(i=0; i<dimIn; i++)
for(j=0; j<hidePoint; j++)
u[i][j] = exp(-((inputTrain[k][i]-c[j][i])*(inputTrain[k][i]-c[j][i]))/b[j][i]);

//模糊隶属度计算
addw = 0;
for(i=0; i<hidePoint; i++)
{
double mul = 1;
for(j=0; j<dimIn; j++)
mul = mul*u[j][i];


w[i] = mul;
addw += w[i];
}

//计算输出
addyw = 0;
for(i=0; i<hidePoint; i++)
{
  double sumTemp =0;
  for(j=0; j<dimIn; j++)
sumTemp = sumTemp*p[j][i]*inputTrain[k][j];

y[i] = sumTemp+p[dimIn][i];

addyw += y[i]*w[i];


yn = addyw/addw;    //模糊输出值

e = outputTrain[k][0]-yn;

//修正系数p
for(i=0; i<hidePoint; i++)
d_p[i] = alpha*e*w[i]/addw;


for(i=0; i<hidePoint; i++)
{
for(j=0; j<dimIn; j++)
p[j][i] = p_1[j][i]+d_p[i]*inputTrain[k][j];
p[dimIn][i] = p_1[dimIn][i];
}




//修正系数b
for(i=0; i<hidePoint; i++)
for(j=0; j<dimIn; j++)
b[i][j] = b_1[i][j]+alpha*e*(y[i]*addw-addyw)*(inputTrain[k][j]-c[i][j])*(inputTrain[k][j]-c[i][j])*w[i]/(b[i][j]*b[i][j]*addw*addw);

//修正系数c
for(i=0; i<hidePoint; i++)
for(j=0; j<dimIn; j++)
c[i][j] = c_1[i][j]+alpha*e*(y[i]*addw-addyw)*2*(inputTrain[k][j]-c[i][j])*w[i]/(b[i][j]*addw*addw);

}
}




//测试网络
for(k=0; k<13; k++)
{
for(i=0; i<dimIn; i++)
for(j=0; j<hidePoint; j++)
u[i][j] = exp(-((inputTrain[k][i]-c[j][i])*(inputTrain[k][i]-c[j][i]))/b[j][i]);

//模糊隶属度计算
addw = 0;
for(i=0; i<hidePoint; i++)
{
double mul = 1;
for(j=0; j<dimIn; j++)
mul = mul*u[j][i];


w[i] = mul;
addw += w[i];
}

//计算输出
addyw = 0;
for(i=0; i<hidePoint; i++)
{
  double sumTemp =0;
for(j=0; j<dimIn; j++)
sumTemp = sumTemp+p[j][i]*inputTrain[k][j];

y[i] = sumTemp+p[dimIn][i];

addyw += y[i]*w[i];


yn = addw/addyw;    //模糊输出值
printf("%lf\n", yn);
}




}




//输出期望归一化
vector<doubleVector> outputNormalization(vector<doubleVector> outputTrain)
{
int i, j;
double y;
doubleVector temp;
vector<doubleVector> dst;

for(i=0; i<dimOut; i++)
{
maxOutputTrain[i] = minOutputTrain[i] = outputTrain[0][i];

for(j=1; j<outputTrain.size(); j++)
{
if(maxOutputTrain[i]<outputTrain[j][i])
maxOutputTrain[i] = outputTrain[j][i];

if(minOutputTrain[i]>outputTrain[j][i])
minOutputTrain[i] = outputTrain[j][i];
}
}

//归一化
for(i=0; i<outputTrain.size(); i++)
{
temp.clear();
for(j=0; j<dimIn; j++)
{
y = (0.02+0.996*(outputTrain[i][j]-minOutputTrain[j]))/(maxOutputTrain[j]-minOutputTrain[j]);
temp.push_back(y);
}

dst.push_back(temp);
}

return dst;


}




//训练样本归一化
vector<doubleVector> inputNormalization(vector<doubleVector> inputTrain)
{
int i, j;
double y;
double maxInputTrain[dimIn], minInputTrain[dimIn];
doubleVector temp;
vector<doubleVector> dst;


for(i=0; i<dimIn; i++)
{
maxInputTrain[i] = minInputTrain[i] = inputTrain[0][i];


for(j=1; j<inputTrain.size(); j++)
{
if(maxInputTrain[i]<inputTrain[j][i])
maxInputTrain[i] = inputTrain[j][i];


if(minInputTrain[i]>inputTrain[j][i])
minInputTrain[i] = inputTrain[j][i];
}
}


//归一化
for(i=0; i<inputTrain.size(); i++)
{
temp.clear();
for(j=0; j<dimIn; j++)
{
y = (0.02+0.996*(inputTrain[i][j]-minInputTrain[j]))/(maxInputTrain[j]-minInputTrain[j]);
temp.push_back(y);
}


dst.push_back(temp);
}


return dst;


}


//获取输出样本
vector<doubleVector> getInputTrain(char *File)
{
int i=1;
doubleVector temp;
vector<doubleVector> dst;
double num;


FILE *fp = fopen(File, "r");


if(fp==NULL)
{
printf("Open input train error\n");
exit(0);
}


temp.clear();
while(fscanf(fp, "%lf", &num)!=EOF)
{
temp.push_back(num);
if(i%dimIn==0)
{
dst.push_back(temp);
temp.clear();
}


i++;
}


return dst;
}




//获取输出样本
vector<doubleVector> getOutputTrain(char *File)
{
int i=1;
doubleVector temp;
vector<doubleVector> dst;
double num;

FILE *fp = fopen(File, "r");

if(fp==NULL)
{
printf("Open input train error\n");
exit(0);
}

temp.clear();
while(fscanf(fp, "%lf", &num)!=EOF)
{
temp.push_back(num);
if(i%dimOut==0)
{
dst.push_back(temp);
temp.clear();
}

i++;
}

return dst;
}

知秋君
上一篇 2024-09-02 18:02
下一篇 2024-09-02 17:36

相关推荐