#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;
}
BP神经网络 C++实现
最新推荐文章于 2024-04-28 20:12:43 发布