国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

感知機(jī)介紹及實(shí)現(xiàn)

2019-11-14 10:02:32
字體:
供稿:網(wǎng)友

http://blog.csdn.net/fengbingchun/article/details/50097723

感知機(jī)介紹及實(shí)現(xiàn)

2015-11-29 17:32 2104人閱讀 評論(1) 收藏 舉報(bào) 分類:

感知機(jī)(perceptron)由Rosenblatt于1957年提出,是神經(jīng)網(wǎng)絡(luò)與支持向量機(jī)的基礎(chǔ)。

感知機(jī)是最早被設(shè)計(jì)并被實(shí)現(xiàn)的人工神經(jīng)網(wǎng)絡(luò)。感知機(jī)是一種非常特殊的神經(jīng)網(wǎng)絡(luò),它在人工神經(jīng)網(wǎng)絡(luò)的發(fā)展史上有著非常重要的地位,盡管它的能力非常有限,主要用于線性分類。

感知機(jī)還包括多層感知機(jī),簡單的線性感知機(jī)用于線性分類器,多層感知機(jī)(含有隱層的網(wǎng)絡(luò))可用于非線性分類器。本文中介紹的均是簡單的線性感知機(jī)。

圖 1

感知機(jī)工作方式

         (1)、學(xué)習(xí)階段:修改權(quán)值和偏置,根據(jù)”已知的樣本”對權(quán)值和偏置不斷修改----有監(jiān)督學(xué)習(xí)。當(dāng)給定某個樣本的輸入/輸出模式對時,感知機(jī)輸出單元會產(chǎn)生一個實(shí)際輸出向量,用期望輸出(樣本輸出)與實(shí)際輸出之差來修正網(wǎng)絡(luò)連接權(quán)值和偏置。

         (2)、工作階段:計(jì)算單元變化,由響應(yīng)函數(shù)給出新輸入下的輸出。

         感知機(jī)學(xué)習(xí)策略

感知機(jī)學(xué)習(xí)的目標(biāo)就是求得一個能夠?qū)⒂?xùn)練數(shù)據(jù)集中正負(fù)實(shí)例完全分開的分類超平面,為了找到分類超平面,即確定感知機(jī)模型中的參數(shù)w和b,需要定義一個基于誤分類的損失函數(shù),并通過將損失函數(shù)最小化來求w和b。

         (1)、數(shù)據(jù)集線性可分性:在二維平面中,可以用一條直線將+1類和-1類完美分開,那么這個樣本空間就是線性可分的。因此,感知機(jī)都基于一個前提,即問題空間線性可分;

         (2)、定義損失函數(shù),找到參數(shù)w和b,使得損失函數(shù)最小。

         損失函數(shù)的選取

         (1)、損失函數(shù)的一個自然選擇就是誤分類點(diǎn)的總數(shù),但是這樣的點(diǎn)不是參數(shù)w,b的連續(xù)可導(dǎo)函數(shù),不易優(yōu)化;

         (2)、損失函數(shù)的另一個選擇就是誤分類點(diǎn)到劃分超平面S(w*x+b=0)的總距離。

以上理論部分主要來自: http://staff.ustc.edu.cn/~qiliuql/files/DM2013/2013SVM.pdf

以下代碼根據(jù)上面的描述實(shí)現(xiàn):

perceptron.hpp:

[cpp] view plain copy 在CODE上查看代碼片#ifndef _PERCEPTRON_HPP_  #define _PERCEPTRON_HPP_    #include <vector>    namespace ANN {    typedef std::vector<float> feature;  typedef int label;    class Perceptron {  #include "perceptron.hpp"  #include <assert.h>  #include <time.h>  #include <iostream>    namespace ANN {    void Perceptron::updateWeight(const feature feature_, int label_)  {      for (int i = 0; i < size_weight; i++) {          weight[i] += learn_rate * feature_[i] * label_; // formula 5      }        bias += learn_rate * label_; // formula 5  }    float Perceptron::calDotProduct(const feature feature_, const std::vector<float> weight_)  {      assert(feature_.size() == weight_.size());      float ret = 0.;        for (int i = 0; i < feature_.size(); i++) {          ret += feature_[i] * weight_[i];      }        return ret;  }    void Perceptron::initWeight()  {      srand(time(0));      float range = 100.0;      for (int i = 0; i < size_weight; i++) {          float tmp = range * rand() / (RAND_MAX + 1.0);          weight.push_back(tmp);      }  }    Perceptron::Perceptron(int iterates_, float learn_rate_, int size_weight_, float bias_)  {      iterates = iterates_;      learn_rate = learn_rate_;      size_weight = size_weight_;      bias = bias_;      weight.resize(0);      feature_set.resize(0);      label_set.resize(0);  }    void Perceptron::getDataset(const std::vector<feature> feature_set_, const std::vector<label> label_set_)  {      assert(feature_set_.size() == label_set_.size());        feature_set.resize(0);      label_set.resize(0);        for (int i = 0; i < feature_set_.size(); i++) {          feature_set.push_back(feature_set_[i]);          label_set.push_back(label_set_[i]);      }  }    bool Perceptron::train()  {      initWeight();        for (int i = 0; i < iterates; i++) {          bool flag = true;            for (int j = 0; j < feature_set.size(); j++) {              float tmp = calDotProduct(feature_set[j], weight) + bias;              if (tmp * label_set[j] <= 0) {                  updateWeight(feature_set[j], label_set[j]);                  flag = false;              }          }            if (flag) {              std::cout << "iterate: " << i << std::endl;              std::cout << "weight: ";              for (int m = 0; m < size_weight; m++) {                  std::cout << weight[m] << "    ";              }              std::cout << std::endl;              std::cout << "bias: " << bias << std::endl;                return true;          }      }        return false;  }    label Perceptron::predict(const feature feature_)  {      assert(feature_.size() == size_weight);        return calDotProduct(feature_, weight) + bias >= 0 ? 1 : -1; //formula 2  }    }  test_NN.cpp:

[cpp] view%20plain copy #include <iostream>  #include "perceptron.hpp"    int test_perceptron();    int main()  {      test_perceptron();      std::cout << "ok!" << std::endl;  }    int test_perceptron()  {      // prepare data      const int len_data = 20;      const int feature_dimension = 2;      float data[len_data][feature_dimension] = { { 10.3, 10.7 }, { 20.1, 100.8 }, { 44.9, 8.0 }, { -2.2, 15.3 }, { -33.3, 77.7 },      { -10.4, 111.1 }, { 99.3, -2.2 }, { 222.2, -5.5 }, { 10.1, 10.1 }, { 66.6, 30.2 },      { 0.1, 0.2 }, { 1.2, 0.03 }, { 0.5, 4.6 }, { -22.3, -11.1 }, { -88.9, -12.3 },      { -333.3, -444.4 }, { -111.2, 0.5 }, { -6.6, 2.9 }, { 3.3, -100.2 }, { 5.6, -88.8 } };      int label_[len_data] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,          -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };        std::vector<ANN::feature> set_feature;      std::vector<ANN::label> set_label;        for (int i = 0; i < len_data; i++) {          ANN::feature feature_single;          for (int j = 0; j < feature_dimension; j++) {              feature_single.push_back(data[i][j]);          }            set_feature.push_back(feature_single);          set_label.push_back(label_[i]);            feature_single.resize(0);      }        // train      int iterates = 1000;      float learn_rate = 0.5;      int size_weight = feature_dimension;      float bias = 2.5;      ANN::Perceptron perceptron(iterates, learn_rate, size_weight, bias);      perceptron.getDataset(set_feature, set_label);      bool flag = perceptron.train();      if (flag) {          std::cout << "data set is linearly separable" << std::endl;      }      else {          std::cout << "data set is linearly inseparable" << std::endl;          return -1;      }        // predict      ANN::feature feature1;      feature1.push_back(636.6);      feature1.push_back(881.8);      std::cout << "the correct result label is 1, " << "the real result label is: " << perceptron.predict(feature1) << std::endl;        ANN::feature feature2;      feature2.push_back(-26.32);      feature2.push_back(-255.95);      std::cout << "the correct result label is -1, " << "the real result label is: " << perceptron.predict(feature2) << std::endl;        return 0;  }  

運(yùn)行結(jié)果如下圖:

GitHub:https://github.com/fengbingchun/NN


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 余姚市| 五原县| 靖边县| 洛扎县| 平遥县| 青冈县| 临江市| 松江区| 伊宁县| 通山县| 晋中市| 呈贡县| 夏津县| 临夏市| 黄骅市| 富顺县| 大余县| 麻江县| 安阳县| 中山市| 盐亭县| 德庆县| 洛宁县| 牙克石市| 济阳县| 建昌县| 马公市| 湘乡市| 甘洛县| 昭平县| 措勤县| 太谷县| 上栗县| 钦州市| 涪陵区| 寿光市| 江北区| 武宣县| 德庆县| 岳普湖县| 武邑县|