美文网首页
感知器 (c++描述)

感知器 (c++描述)

作者: gibyeng | 来源:发表于2017-05-04 21:49 被阅读0次

    根据《统计学习方法》用C++实现的感知器模型。

    #include <iostream>
    using namespace std;
    const int N = 3;/*样本数量*/
    const int M = 2;/*输入数据的维数*/
    double W[M]{};
    double b = 0;
    struct slp {
        double X[M];
        double Y;
    };
    slp trainData[N] = {
        { { 3,3 },1 },
        { { 4,3 },1 },
        { { 1,1 },-1 }
    };
    
    /*根据第i项数据更新一次*/
    void update(int i) {
        for (int j = 0; j < M; j++) {
            W[j] += trainData[i].X[j] * trainData[i].Y;
        }
        b += trainData[i].Y;
        
    }
    
    /*判断当前解是否可行*/
    int check() {
        for (int i = 0; i < N; i++) {
            double res = 0;
            for (int j = 0; j < M; j++) {
                res += trainData[i].X[j] * W[j];
            }
            res += b;
            if (res*trainData[i].Y<=0) {
                update(i);
                return -1;
            }
        }
        return 1;
    }
    void print_ans() {
        for (int j = 0; j < M; j++) {
            cout << W[j] << endl;
        }
        cout << b << endl;
    
    }
    int main()
    {
        while (check() < 0) {
            
        }
        print_ans();
        system("pause");
    }

    相关文章

      网友评论

          本文标题:感知器 (c++描述)

          本文链接:https://www.haomeiwen.com/subject/hewdtxtx.html