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

首頁 > 學院 > 開發(fā)設計 > 正文

感知機介紹及實現(xiàn)

2019-11-14 10:14:07
字體:
來源:轉載
供稿:網(wǎng)友

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

感知機介紹及實現(xiàn)

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

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

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

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

圖 1

感知機工作方式

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

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

         感知機學習策略

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

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

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

         損失函數(shù)的選取

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

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

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

以下代碼根據(jù)上面的描述實現(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;  }  

運行結果如下圖:

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平塘县| 林口县| 左云县| 甘谷县| 阿瓦提县| 应城市| 柏乡县| 石首市| 津市市| 九江市| 女性| 浦县| 正镶白旗| 河北省| 阳西县| 都匀市| 晋宁县| 莲花县| 惠东县| 云霄县| 玛曲县| 临清市| 肇州县| 井研县| 延安市| 耿马| 沧州市| 喀喇沁旗| 崇礼县| 沧州市| 惠东县| 政和县| 札达县| 城步| 会东县| 环江| 绥芬河市| 金华市| 光泽县| 上犹县| 齐齐哈尔市|