最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)神經(jīng)BP神經(jīng)網(wǎng)絡(luò)

 更新時間:2020年05月25日 14:15:33   作者:悟名堂  
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)神經(jīng)BP神經(jīng)網(wǎng)絡(luò),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)神經(jīng)BP神經(jīng)網(wǎng)絡(luò)的具體代碼,供大家參考,具體內(nèi)容如下

BP.h

#pragma once
#include<vector>
#include<stdlib.h>
#include<time.h>
#include<cmath>
#include<iostream>
using std::vector;
using std::exp;
using std::cout;
using std::endl;
class BP
{
private:
 int studyNum;//允許學(xué)習(xí)次數(shù)
 double h;//學(xué)習(xí)率
 double allowError;//允許誤差
 vector<int> layerNum;//每層的節(jié)點數(shù),不包括常量節(jié)點1
 vector<vector<vector<double>>> w;//權(quán)重
 vector<vector<vector<double>>> dw;//權(quán)重增量
 vector<vector<double>> b;//偏置
 vector<vector<double>> db;//偏置增量
 vector<vector<vector<double>>> a;//節(jié)點值
 vector<vector<double>> x;//輸入
 vector<vector<double>> y;//期望輸出

 void iniwb();//初始化w與b
 void inidwdb();//初始化dw與db
 double sigmoid(double z);//激活函數(shù)
 void forward();//前向傳播
 void backward();//后向傳播
 double Error();//計算誤差
public:
 BP(vector<int>const& layer_num, vector<vector<double>>const & input_a0,
 vector<vector<double>> const & output_y, double hh = 0.5, double allerror = 0.001, int studynum = 1000);
 BP();
 void setLayerNumInput(vector<int>const& layer_num, vector<vector<double>> const & input);
 void setOutputy(vector<vector<double>> const & output_y);
 void setHErrorStudyNum(double hh, double allerror,int studynum);
 void run();//運行BP神經(jīng)網(wǎng)絡(luò)
 vector<double> predict(vector<double>& input);//使用已經(jīng)學(xué)習(xí)好的神經(jīng)網(wǎng)絡(luò)進(jìn)行預(yù)測
 ~BP();
};

BP.cpp

#include "BP.h"
BP::BP(vector<int>const& layer_num, vector<vector<double>>const & input,
 vector<vector<double>> const & output_y, double hh, double allerror,int studynum)
{
 layerNum = layer_num;
 x = input;//輸入多少個節(jié)點的數(shù)據(jù),每個節(jié)點有多少份數(shù)據(jù)
 y = output_y;
 h = hh;
 allowError = allerror;
 a.resize(layerNum.size());//有這么多層網(wǎng)絡(luò)節(jié)點
 for (int i = 0; i < layerNum.size(); i++)
 {
 a[i].resize(layerNum[i]);//每層網(wǎng)絡(luò)節(jié)點有這么多個節(jié)點
 for (int j = 0; j < layerNum[i]; j++)
  a[i][j].resize(input[0].size());
 }
 a[0] = input;
 studyNum = studynum;
}

BP::BP()
{
 layerNum = {};
 a = {};
 y = {};
 h = 0;
 allowError = 0;
}

BP::~BP()
{
}

void BP::setLayerNumInput(vector<int>const& layer_num, vector<vector<double>> const & input)
{
 layerNum = layer_num;
 x = input;
 a.resize(layerNum.size());//有這么多層網(wǎng)絡(luò)節(jié)點
 for (int i = 0; i < layerNum.size(); i++)
 {
 a[i].resize(layerNum[i]);//每層網(wǎng)絡(luò)節(jié)點有這么多個節(jié)點
 for (int j = 0; j < layerNum[i]; j++)
  a[i][j].resize(input[0].size());
 }
 a[0] = input;
}


void BP::setOutputy(vector<vector<double>> const & output_y)
{
 y = output_y;
}

void BP::setHErrorStudyNum(double hh, double allerror,int studynum)
{
 h = hh;
 allowError = allerror;
 studyNum = studynum;
}

//初始化權(quán)重矩陣
void BP::iniwb()
{
 w.resize(layerNum.size() - 1);
 b.resize(layerNum.size() - 1);
 srand((unsigned)time(NULL));
 //節(jié)點層數(shù)層數(shù)
 for (int l = 0; l < layerNum.size() - 1; l++)
 {
 w[l].resize(layerNum[l + 1]);
 b[l].resize(layerNum[l + 1]);
 //對應(yīng)后層的節(jié)點
 for (int j = 0; j < layerNum[l + 1]; j++)
 {
  w[l][j].resize(layerNum[l]);
  b[l][j] = -1 + 2 * (rand() / RAND_MAX);
  //對應(yīng)前層的節(jié)點
  for (int k = 0; k < layerNum[l]; k++)
  w[l][j][k] = -1 + 2 * (rand() / RAND_MAX);
 }
 }
}


void BP::inidwdb()
{
 dw.resize(layerNum.size() - 1);
 db.resize(layerNum.size() - 1);
 //節(jié)點層數(shù)層數(shù)
 for (int l = 0; l < layerNum.size() - 1; l++)
 {
 dw[l].resize(layerNum[l + 1]);
 db[l].resize(layerNum[l + 1]);
 //對應(yīng)后層的節(jié)點
 for (int j = 0; j < layerNum[l + 1]; j++)
 {
  dw[l][j].resize(layerNum[l]);
  db[l][j] = 0;
  //對應(yīng)前層的節(jié)點
  for (int k = 0; k < layerNum[l]; k++)
  w[l][j][k] = 0;
 }
 }
}

//激活函數(shù)
double BP::sigmoid(double z)
{
 return 1.0 / (1 + exp(-z));
}

void BP::forward()
{
 for (int l = 1; l < layerNum.size(); l++)
 {
 for (int i = 0; i < layerNum[l]; i++)
 {
  for (int j = 0; j < x[0].size(); j++)
  {

  a[l][i][j] = 0;//第l層第i個節(jié)點第j個數(shù)據(jù)樣本
  //計算變量節(jié)點乘權(quán)值的和
  for (int k = 0; k < layerNum[l - 1]; k++)
   a[l][i][j] += a[l - 1][k][j] * w[l - 1][i][k];
  //加上節(jié)點偏置
  a[l][i][j] += b[l - 1][i];
  a[l][i][j] = sigmoid(a[l][i][j]);
  }
 }
 }
}

void BP::backward()
{
 int xNum = x[0].size();//樣本個數(shù)
 //daP第l層da,daB第l+1層da
 vector<double> daP, daB;
 

 for (int j = 0; j < xNum; j++)
 {
 //處理最后一層的dw
 daP.clear();
 daP.resize(layerNum[layerNum.size() - 1]);
 for (int i = 0, l = layerNum.size() - 1; i < layerNum[l]; i++)
 {
  daP[i] = a[l][i][j] - y[i][j];
  for (int k = 0; k < layerNum[l - 1]; k++)
  dw[l - 1][i][k] += daP[i] * a[l][i][j] * (1 - a[l][i][j])*a[l - 1][k][j];
  db[l - 1][i] += daP[i] * a[l][i][j] * (1 - a[l][i][j]);
 }

 //處理剩下層的權(quán)重w的增量Dw
 for (int l = layerNum.size() - 2; l > 0; l--)
 {
  daB = daP;
  daP.clear();
  daP.resize(layerNum[l]);
  for (int k = 0; k < layerNum[l]; k++)
  {
  daP[k] = 0;
  for (int i = 0; i < layerNum[l + 1]; i++)
   daP[k] += daB[i] * a[l + 1][i][j] * (1 - a[l + 1][i][j])*w[l][i][k];
  //dw
  for (int i = 0; i < layerNum[l - 1]; i++)
   dw[l - 1][k][i] += daP[k] * a[l][k][j] * (1 - a[l][k][j])*a[l - 1][i][j];
  //db
  db[l-1][k] += daP[k] * a[l][k][j] * (1 - a[l][k][j]);
  }
 }

 }
 
 //計算dw與db平均值
 for (int l = 0; l < layerNum.size() - 1; l++)
 {
 //對應(yīng)后層的節(jié)點
 for (int j = 0; j < layerNum[l + 1]; j++)
 {
  db[l][j] = db[l][j] / xNum;
  //對應(yīng)前層的節(jié)點
  for (int k = 0; k < layerNum[l]; k++)
  w[l][j][k] = w[l][j][k] / xNum;
 }
 }

 //更新參數(shù)w與b
 for (int l = 0; l < layerNum.size() - 1; l++)
 {
 for (int j = 0; j < layerNum[l + 1]; j++)
 {
  b[l][j] = b[l][j] - h * db[l][j];
  //對應(yīng)前層的節(jié)點
  for (int k = 0; k < layerNum[l]; k++)
  w[l][j][k] = w[l][j][k] - h * dw[l][j][k];
 }
 }
}

double BP::Error()
{
 int l = layerNum.size() - 1;
 double temp = 0, error = 0;
 for (int i = 0; i < layerNum[l]; i++)
 for (int j = 0; j < x[0].size(); j++)
 {
  temp = a[l][i][j] - y[i][j];
  error += temp * temp;
 }
 error = error / x[0].size();//求對每一組樣本的誤差平均
 error = error / 2;
 cout << error << endl;
 return error;
}

//運行神經(jīng)網(wǎng)絡(luò)
void BP::run()
{
 iniwb();
 inidwdb();
 int i = 0;
 for (; i < studyNum; i++)
 {
 forward();
 if (Error() <= allowError)
 {
  cout << "Study Success!" << endl;
  break;
 }
 backward();
 }
 if (i == 10000)
 cout << "Study Failed!" << endl;
}

vector<double> BP::predict(vector<double>& input)
{
 vector<vector<double>> a1;
 a1.resize(layerNum.size());
 for (int l = 0; l < layerNum.size(); l++)
 a1[l].resize(layerNum[l]);
 a1[0] = input;
 for (int l = 1; l < layerNum.size(); l++)
 for (int i = 0; i < layerNum[l]; i++)
 {
  a1[l][i] = 0;//第l層第i個節(jié)點第j個數(shù)據(jù)樣本
  //計算變量節(jié)點乘權(quán)值的和
  for (int k = 0; k < layerNum[l - 1]; k++)
  a1[l][i] += a1[l - 1][k] * w[l - 1][i][k];
  //加上節(jié)點偏置
  a1[l][i] += b[l - 1][i];
  a1[l][i] = sigmoid(a1[l][i]);
 }
 return a1[layerNum.size() - 1];
}

驗證程序:

#include"BP.h"

int main()
{
 vector<int> layer_num = { 1, 10, 1 };
 vector<vector<double>> input_a0 = { { 1,2,3,4,5,6,7,8,9,10 } };
 vector<vector<double>> output_y = { {0,0,0,0,1,1,1,1,1,1} };

 BP bp(layer_num, input_a0,output_y,0.6,0.001, 2000);
 bp.run();
 for (int j = 0; j < 30; j++)
 {
 vector<double> input = { 0.5*j };
 vector<double> output = bp.predict(input);
 for (auto i : output)
  cout << "j:" << 0.5*j <<" pridict:" << i << " ";
 cout << endl;
 }
 system("pause");
 return 0;
}

輸出:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于C程序啟動代碼的深入分析

    基于C程序啟動代碼的深入分析

    本篇文章是對C程序啟動的代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C++使用JsonCpp庫操作json格式數(shù)據(jù)示例

    C++使用JsonCpp庫操作json格式數(shù)據(jù)示例

    這篇文章主要介紹了C++使用JsonCpp庫操作json格式數(shù)據(jù),結(jié)合實例形式詳細(xì)分析了JsonCpp庫的下載及C++使用JsonCpp庫對json格式數(shù)據(jù)序列化相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • C++實現(xiàn)單例模式的自動釋放

    C++實現(xiàn)單例模式的自動釋放

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)單例模式的自動釋放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C語言修煉之路函數(shù)篇真題訓(xùn)練下

    C語言修煉之路函數(shù)篇真題訓(xùn)練下

    函數(shù)是一組一起執(zhí)行一個任務(wù)的語句。每個 C 程序都至少有一個函數(shù),即主函數(shù) main() ,所有簡單的程序都可以定義其他額外的函數(shù)
    2022-03-03
  • C++中的函數(shù)知識點大全

    C++中的函數(shù)知識點大全

    這篇文章介紹了C++中的函數(shù)知識點,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C++中Digraphs、Trigraphs和Tokens的深入講解

    C++中Digraphs、Trigraphs和Tokens的深入講解

    這篇文章主要給大家介紹了關(guān)于C++中Digraphs、Trigraphs和Tokens的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Ubuntu系統(tǒng)下如何在VScode配置OpenCV(C++)環(huán)境(.json文件)

    Ubuntu系統(tǒng)下如何在VScode配置OpenCV(C++)環(huán)境(.json文件)

    這篇文章主要介紹了如何在VSCode中配置和運行C++程序,包括創(chuàng)建test.cpp文件、配置launch.json、tasks.json和c_cpp_properties.json文件,以及重啟VSCode以解決可能的報錯問題,需要的朋友可以參考下
    2025-02-02
  • C++ stringstream類用法詳解

    C++ stringstream類用法詳解

    這篇文章主要介紹了C++ stringstream類用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 詳解C++編程中運算符的使用

    詳解C++編程中運算符的使用

    這篇文章主要介紹了詳解C++編程中運算符的使用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • C++ 關(guān)于MFC List Control 控件的總結(jié)

    C++ 關(guān)于MFC List Control 控件的總結(jié)

    這篇文章主要介紹了C++ 關(guān)于MFC List Control 控件的總結(jié)的相關(guān)資料,十分的詳細(xì),有需要的朋友可以參考下
    2015-06-06

最新評論

镇平县| 凤山市| 宜黄县| 五莲县| 鹤庆县| 台东市| 建湖县| 新蔡县| 沂南县| 固阳县| 汉阴县| 德钦县| 郑州市| 荥经县| 郯城县| 抚顺县| 富蕴县| 谷城县| 长海县| 台江县| 沅陵县| 洞头县| 新营市| 壤塘县| 静安区| 甘肃省| 霍林郭勒市| 红原县| 本溪市| 桂林市| 宜昌市| 隆回县| 通许县| 营山县| 巴里| 双江| 潜山县| 仁化县| 永德县| 银川市| 昌江|