這篇文章主要講解了C++如何實(shí)現(xiàn)簡(jiǎn)單BP神經(jīng)網(wǎng)絡(luò),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
創(chuàng)新互聯(lián)公司專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站制作、岳陽(yáng)樓網(wǎng)絡(luò)推廣、成都微信小程序、岳陽(yáng)樓網(wǎng)絡(luò)營(yíng)銷(xiāo)、岳陽(yáng)樓企業(yè)策劃、岳陽(yáng)樓品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供岳陽(yáng)樓建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):028-86922220,官方網(wǎng)址:aaarwkj.com
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡(jiǎn)單BP神經(jīng)網(wǎng)絡(luò)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的BP神經(jīng)網(wǎng)絡(luò)
使用EasyX圖形化顯示訓(xùn)練過(guò)程和訓(xùn)練結(jié)果
使用了25個(gè)樣本,一共訓(xùn)練了1萬(wàn)次。
該神經(jīng)網(wǎng)絡(luò)有兩個(gè)輸入,一個(gè)輸出端
下圖是訓(xùn)練效果,data是訓(xùn)練的輸入數(shù)據(jù),temp代表所在層的輸出,target是訓(xùn)練目標(biāo),右邊的大圖是BP神經(jīng)網(wǎng)絡(luò)的測(cè)試結(jié)果。
以下是詳細(xì)的代碼實(shí)現(xiàn),主要還是基本的矩陣運(yùn)算。
#include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <time.h> #include <math.h> #define uint unsigned short #define real double #define threshold (real)(rand() % 99998 + 1) / 100000 // 神經(jīng)網(wǎng)絡(luò)的層 class layer{ private: char name[20]; uint row, col; uint x, y; real **data; real *bias; public: layer(){ strcpy_s(name, "temp"); row = 1; col = 3; x = y = 0; data = new real*[row]; bias = new real[row]; for (uint i = 0; i < row; i++){ data[i] = new real[col]; bias[i] = threshold; for (uint j = 0; j < col; j++){ data[i][j] = 1; } } } layer(FILE *fp){ fscanf_s(fp, "%d %d %d %d %s", &row, &col, &x, &y, name); data = new real*[row]; bias = new real[row]; for (uint i = 0; i < row; i++){ data[i] = new real[col]; bias[i] = threshold; for (uint j = 0; j < col; j++){ fscanf_s(fp, "%lf", &data[i][j]); } } } layer(uint row, uint col){ strcpy_s(name, "temp"); this->row = row; this->col = col; this->x = 0; this->y = 0; this->data = new real*[row]; this->bias = new real[row]; for (uint i = 0; i < row; i++){ data[i] = new real[col]; bias[i] = threshold; for (uint j = 0; j < col; j++){ data[i][j] = 1.0f; } } } layer(const layer &a){ strcpy_s(name, a.name); row = a.row, col = a.col; x = a.x, y = a.y; data = new real*[row]; bias = new real[row]; for (uint i = 0; i < row; i++){ data[i] = new real[col]; bias[i] = a.bias[i]; for (uint j = 0; j < col; j++){ data[i][j] = a.data[i][j]; } } } ~layer(){ // 刪除原有數(shù)據(jù) for (uint i = 0; i < row; i++){ delete[]data[i]; } delete[]data; } layer& operator =(const layer &a){ // 刪除原有數(shù)據(jù) for (uint i = 0; i < row; i++){ delete[]data[i]; } delete[]data; delete[]bias; // 重新分配空間 strcpy_s(name, a.name); row = a.row, col = a.col; x = a.x, y = a.y; data = new real*[row]; bias = new real[row]; for (uint i = 0; i < row; i++){ data[i] = new real[col]; bias[i] = a.bias[i]; for (uint j = 0; j < col; j++){ data[i][j] = a.data[i][j]; } } return *this; } layer Transpose() const { layer arr(col, row); arr.x = x, arr.y = y; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ arr.data[j][i] = data[i][j]; } } return arr; } layer sigmoid(){ layer arr(col, row); arr.x = x, arr.y = y; for (uint i = 0; i < x.row; i++){ for (uint j = 0; j < x.col; j++){ arr.data[i][j] = 1 / (1 + exp(-data[i][j]));// 1/(1+exp(-z)) } } return arr; } layer operator *(const layer &b){ layer arr(row, col); arr.x = x, arr.y = y; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ arr.data[i][j] = data[i][j] * b.data[i][j]; } } return arr; } layer operator *(const int b){ layer arr(row, col); arr.x = x, arr.y = y; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ arr.data[i][j] = b * data[i][j]; } } return arr; } layer matmul(const layer &b){ layer arr(row, b.col); arr.x = x, arr.y = y; for (uint k = 0; k < b.col; k++){ for (uint i = 0; i < row; i++){ arr.bias[i] = bias[i]; arr.data[i][k] = 0; for (uint j = 0; j < col; j++){ arr.data[i][k] += data[i][j] * b.data[j][k]; } } } return arr; } layer operator -(const layer &b){ layer arr(row, col); arr.x = x, arr.y = y; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ arr.data[i][j] = data[i][j] - b.data[i][j]; } } return arr; } layer operator +(const layer &b){ layer arr(row, col); arr.x = x, arr.y = y; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ arr.data[i][j] = data[i][j] + b.data[i][j]; } } return arr; } layer neg(){ layer arr(row, col); arr.x = x, arr.y = y; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ arr.data[i][j] = -data[i][j]; } } return arr; } bool operator ==(const layer &a){ bool result = true; for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ if (abs(data[i][j] - a.data[i][j]) > 10e-6){ result = false; break; } } } return result; } void randomize(){ for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ data[i][j] = threshold; } bias[i] = 0.3; } } void print(){ outtextxy(x, y - 20, name); for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ COLORREF color = HSVtoRGB(360 * data[i][j], 1, 1); putpixel(x + i, y + j, color); } } } void save(FILE *fp){ fprintf_s(fp, "%d %d %d %d %s\n", row, col, x, y, name); for (uint i = 0; i < row; i++){ for (uint j = 0; j < col; j++){ fprintf_s(fp, "%lf ", data[i][j]); } fprintf_s(fp, "\n"); } } friend class network; friend layer operator *(const double a, const layer &b); }; layer operator *(const double a, const layer &b){ layer arr(b.row, b.col); arr.x = b.x, arr.y = b.y; for (uint i = 0; i < arr.row; i++){ for (uint j = 0; j < arr.col; j++){ arr.data[i][j] = a * b.data[i][j]; } } return arr; } // 神經(jīng)網(wǎng)絡(luò) class network{ int iter; double learn; layer arr[3]; layer data, target, test; layer& unit(layer &x){ for (uint i = 0; i < x.row; i++){ for (uint j = 0; j < x.col; j++){ x.data[i][j] = i == j ? 1.0 : 0.0; } } return x; } layer grad_sigmoid(layer &x){ layer e(x.row, x.col); e = x*(e - x); return e; } public: network(FILE *fp){ fscanf_s(fp, "%d %lf", &iter, &learn); // 輸入數(shù)據(jù) data = layer(fp); for (uint i = 0; i < 3; i++){ arr[i] = layer(fp); //arr[i].randomize(); } target = layer(fp); // 測(cè)試數(shù)據(jù) test = layer(2, 40000); for (uint i = 0; i < test.col; i++){ test.data[0][i] = ((double)i / 200) / 200.0f; test.data[1][i] = (double)(i % 200) / 200.0f; } } void train(){ int i = 0; char str[20]; data.print(); target.print(); for (i = 0; i < iter; i++){ sprintf_s(str, "Iterate:%d", i); outtextxy(0, 0, str); // 正向傳播 layer l0 = data; layer l1 = arr[0].matmul(l0).sigmoid(); layer l2 = arr[1].matmul(l1).sigmoid(); layer l3 = arr[2].matmul(l2).sigmoid(); // 顯示輸出結(jié)果 l1.print(); l2.print(); l3.print(); if (l3 == target){ break; } // 反向傳播 layer l3_delta = (l3 - target ) * grad_sigmoid(l3); layer l2_delta = arr[2].Transpose().matmul(l3_delta) * grad_sigmoid(l2); layer l1_delta = arr[1].Transpose().matmul(l2_delta) * grad_sigmoid(l1); // 梯度下降法 arr[2] = arr[2] - learn * l3_delta.matmul(l2.Transpose()); arr[1] = arr[1] - learn * l2_delta.matmul(l1.Transpose()); arr[0] = arr[0] - learn * l1_delta.matmul(l0.Transpose()); } sprintf_s(str, "Iterate:%d", i); outtextxy(0, 0, str); // 測(cè)試輸出 // selftest(); } void selftest(){ // 測(cè)試 layer l0 = test; layer l1 = arr[0].matmul(l0).sigmoid(); layer l2 = arr[1].matmul(l1).sigmoid(); layer l3 = arr[2].matmul(l2).sigmoid(); setlinecolor(WHITE); // 測(cè)試?yán)? for (uint j = 0; j < test.col; j++){ COLORREF color = HSVtoRGB(360 * l3.data[0][j], 1, 1);// 輸出顏色 putpixel((int)(test.data[0][j] * 160) + 400, (int)(test.data[1][j] * 160) + 30, color); } // 標(biāo)準(zhǔn)例 for (uint j = 0; j < data.col; j++){ COLORREF color = HSVtoRGB(360 * target.data[0][j], 1, 1);// 輸出顏色 setfillcolor(color); fillcircle((int)(data.data[0][j] * 160) + 400, (int)(data.data[1][j] * 160) + 30, 3); } line(400, 30, 400, 230); line(400, 30, 600, 30); } void save(FILE *fp){ fprintf_s(fp, "%d %lf\n", iter, learn); data.save(fp); for (uint i = 0; i < 3; i++){ arr[i].save(fp); } target.save(fp); } };
#include "network.h" void main(){ FILE file; FILE *fp = &file; // 讀取狀態(tài) fopen_s(&fp, "Text.txt", "r"); network net(fp); fclose(fp); initgraph(600, 320); net.train(); // 保存狀態(tài) fopen_s(&fp, "Text.txt", "w"); net.save(fp); fclose(fp); getchar(); closegraph(); }
看完上述內(nèi)容,是不是對(duì)C++如何實(shí)現(xiàn)簡(jiǎn)單BP神經(jīng)網(wǎng)絡(luò)有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:C++如何實(shí)現(xiàn)簡(jiǎn)單BP神經(jīng)網(wǎng)絡(luò)
文章位置:http://aaarwkj.com/article40/pjcgeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、虛擬主機(jī)、移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、企業(yè)建站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)