BP神经网络 C++实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<cmath>
using namespace std;
#define ITERATIONS 10000
#define POSITION 5
#define FILENAME "iris_training.csv"
#define INPUTLEN 4
#define HIDELEN 6
#define RESULTLEN 1
#define RANGE 10
const double parm = 0.01;
#define RATESTUDY 0.009
typedef struct iris {
	double inf[INPUTLEN];
	double predict;
	//花萼长度(Sepal Length)、花萼宽度(Sepal Width)、花瓣长度(Petal Length)、花瓣宽度(Petal Width)
	struct iris* next;
}*myiris;
myiris head;
double** W0;
double** W1;
double* B0;
double* B1;
double* input;
double* hide;
double* shide;
double* result;
double* sresult;
void show(double** map, int row, int col) {
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout << setw(POSITION) << map[i][j];
		}
		cout << endl;
	}
}
void InitWeight() {
	input = new double[INPUTLEN];
	hide = new double[HIDELEN];
	shide = new double[HIDELEN];
	result = new double[RESULTLEN];
	sresult = new double[RESULTLEN];
	head = new struct iris;
	W0 = new double* [HIDELEN];
	for (int i = 0; i < HIDELEN; i++) {
		W0[i] = new double[INPUTLEN];
		for (int j = 0; j < INPUTLEN; j++) {
			W0[i][j] = parm*(double)(rand() % RANGE);
		}
	}
	W1 = new double* [RESULTLEN];
	for (int i = 0; i < RESULTLEN; i++) {
		W1[i] = new double[HIDELEN];
		for (int j = 0; j < HIDELEN; j++) {
			W1[i][j] = parm * (double)(rand() % RANGE);
		}
	}
	B0 = new double[HIDELEN];
	for (int i = 0; i < HIDELEN; i++) {
		B0[i] = parm * (double)(rand() % RANGE);
	}
	B1 = new double[RESULTLEN];
	for (int i = 0; i < RESULTLEN; i++) {
		B1[i] = parm * (double)(rand() % RANGE);
	}

}
void DATALOAD() {
	FILE* file = fopen(FILENAME, "r");
	char line[100];
	fscanf(file, "%s", line);
	myiris temp, ptr=head;
	temp = new struct iris;
	while ((fscanf(file, "%lf,%lf,%lf,%lf,%lf", &temp->inf[0], &temp->inf[1], &temp->inf[2], &temp->inf[3], &temp->predict)) != EOF) {
		myiris mytemp = new struct iris;
		mytemp->next = NULL;
		for (int i = 0; i < 4; i++)mytemp->inf[i] = temp->inf[i];
		mytemp->predict = temp->predict;
		ptr->next = mytemp;
		ptr = ptr->next;
	}
	/*ptr = head;
	while (ptr && ptr->next) {
		for (int i = 0; i < 4; i++)cout << setw(POSITION) << ptr->next->inf[i];
		cout << setw(POSITION) << ptr->next->predict;
		cout << endl;
		ptr = ptr->next;

	}*/
	//cout << "endl" << endl;
}
double sigmoid(double x) {
	return 1 / (1 + exp(-x));
}
double movement(myiris myi) {
	double temp[HIDELEN];
	double mytemp;
	for (int i = 0; i < HIDELEN; i++) {
		hide[i] = 0;
		for (int k = 0; k < INPUTLEN; k++) {
			hide[i] += myi->inf[k] * W0[i][k];
		}
		temp[i] = hide[i] + B0[i];
		shide[i] = sigmoid(temp[i]);
	}
	for (int i = 0; i < RESULTLEN; i++) {
		result[i] = 0;
		for (int k = 0; k < HIDELEN; k++) {
			result[i] += W1[i][k] * shide[k];
		}
		mytemp = result[i] + B1[i];
		sresult[i] = sigmoid(mytemp);
	}
	cout << "\tpredict>" << setw(POSITION) << sresult[0] << endl;
	double pre = 2 * (sresult[0] - myi->predict) * sresult[0]*sigmoid(1-mytemp);
	for (int i = 0; i < RESULTLEN; i++) {
		B1[i] -= pre * RATESTUDY;
		for (int j = 0; j < HIDELEN; j++) {
			W1[i][j] -= pre * hide[j] * RATESTUDY;
		}
	}
	for (int i = 0; i < HIDELEN; i++) {
		double preback = pre * W1[0][i] * hide[i] * sigmoid(1 - temp[i]);
		B0[i] -= preback * RATESTUDY;
		for (int j = 0; j < INPUTLEN; j++) {
			W0[i][j] -= preback * myi->inf[j]*RATESTUDY;
		}
	}
	return sresult[0];
	//double delta_B1 = pre;
	//double delta_W1=pre*Hk

}
int main(void) {
	srand(time(NULL));
	InitWeight();
	DATALOAD();
	myiris ptr = head;
	for (int k = 0;;k++) {
		double correct = 0;
		double sum = 0;
		while (ptr && ptr->next) {
			for (int i = 0; i < INPUTLEN; i++) {
				cout << setw(POSITION) << ptr->next->inf[i];
			}
			cout << setw(POSITION) << ptr->next->predict;
			if (movement(ptr->next) >= 0.5) {
				if (ptr->next->predict > 0)
					correct += 1;
			}
			else if (ptr->next->predict == 0)
				correct += 1;
			sum += 1;
			ptr = ptr->next;
		}
		show(W0, INPUTLEN, HIDELEN);
		cout << "正确率为>" << 100*correct / sum<<"%" << endl<<endl;
		if ((k + 1) % ITERATIONS == 0) {
			cout << "回车确定..." << endl;
			getchar();
		}
		ptr = head;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alasnot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值