ARCS6 AR6-REV.24062600
読み取り中…
検索中…
一致する文字列を見つけられません
SimplePerceptron.hh
[詳解]
1
8//
9// Copyright (C) 2011-2020 Yuki YOKOKURA
10// This program is free software;
11// you can redistribute it and/or modify it under the terms of the FreeBSD License.
12// For details, see the License.txt file.
13
14#ifndef SIMPLEPERCEPTRON
15#define SIMPLEPERCEPTRON
16
17#include <cassert>
18#include <array>
19#include "Matrix.hh"
21
22// ARCS組込み用マクロ
23#ifdef ARCS_IN
24 // ARCSに組み込まれる場合
25 #include "ARCSassert.hh"
26 #include "ARCSeventlog.hh"
27#else
28 // ARCSに組み込まれない場合
29 #define arcs_assert(a) (assert(a))
30 #define PassedLog()
31 #define EventLog(a)
32 #define EventLogVar(a)
33#endif
34
35namespace ARCS { // ARCS名前空間
38 template <unsigned int N, unsigned int M>
40 public:
43 : w(), eta(0.5)
44 {
45
46 }
47
51 : w(r.w), eta(r.eta)
52 {
53
54 }
55
60
66 void Train(const std::array<Matrix<1,N+1>, M>& x, const Matrix<1,M>& t, double Gain, unsigned int Epoch){
67 double Error = 0; // 誤差
68 eta = Gain; // 学習ゲインをセット
69 // エポック数だけ回す
70 for(unsigned int i = 0; i < Epoch; ++i){
71 printf("Epoch: %u, ", i);
72 // データ数だけ回す
73 for(unsigned int j = 0; j < M; ++j){
74 Error = WeightCalculation(x[j], t.GetElement(1,j+1)); // 重み修正計算
75 printf("%f, ", Error);
76 }
77 printf("\n");
78 }
79 }
80
84 Matrix<1,1> u = tp(w)*x; // 重み乗算加算
85 return ActivationFunctions::Step(u.GetElement(1,1)); // 活性化関数
86 }
87
88 private:
89 SimplePerceptron(const SimplePerceptron&) = delete;
90 const SimplePerceptron& operator=(const SimplePerceptron&) = delete;
92 double eta;
93
98 double WeightCalculation(const Matrix<1,N+1>& x, double t){
99 double z = ForwardCalculation(x); // 前進計算
100 double K = (t - z)*eta; // 教師データとの差分に学習ゲインを乗算
101 w += K*x; // 重み修正計算
102 return K; // 誤差を返す
103 }
104 };
105}
106
107#endif
108
ARCS イベントログクラス
ARCS用ASSERTクラス
行列/ベクトル計算クラス(テンプレート版)
活性化関数
static double Step(double u)
ステップ活性化関数
Definition ActivationFunctions.hh:50
行列/ベクトル計算クラス(テンプレート版)
Definition Matrix.hh:44
constexpr TT GetElement(size_t n, size_t m) const
指定した要素番号の値を返す関数
Definition Matrix.hh:489
単純パーセプトロンクラス
Definition SimplePerceptron.hh:39
void Train(const std::array< Matrix< 1, N+1 >, M > &x, const Matrix< 1, M > &t, double Gain, unsigned int Epoch)
学習をする関数
Definition SimplePerceptron.hh:66
double ForwardCalculation(const Matrix< 1, N+1 > &x)
前進計算
Definition SimplePerceptron.hh:83
SimplePerceptron(SimplePerceptron &&r)
ムーブコンストラクタ
Definition SimplePerceptron.hh:50
~SimplePerceptron()
デストラクタ
Definition SimplePerceptron.hh:57
SimplePerceptron()
コンストラクタ
Definition SimplePerceptron.hh:42