ARCS6 AR6-REV.24062600
読み取り中…
検索中…
一致する文字列を見つけられません
RecurrentNeuralLayer.hh
[詳解]
1
8//
9// Copyright (C) 2011-2021 Yokokura, Yuki
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 RECURRENTNEURALLAYER
15#define RECURRENTNEURALLAYER
16
17#include <cassert>
18#include <array>
19#include <cmath>
20#include <string>
21#include <array>
22#include "Matrix.hh"
23#include "NeuralNetParamDef.hh"
25#include "RandomGenerator.hh"
26
27// ARCS組込み用マクロ
28#ifdef ARCS_IN
29 // ARCSに組み込まれる場合
30 #include "ARCSassert.hh"
31 #include "ARCSeventlog.hh"
32#else
33 // ARCSに組み込まれない場合
34 #define arcs_assert(a) (assert(a))
35 #define PassedLog()
36 #define EventLog(a)
37 #define EventLogVar(a)
38#endif
39
40namespace ARCS { // ARCS名前空間
51template <
52 size_t N,
53 size_t P,
54 size_t T,
55 size_t W,
56 size_t M,
57 ActvFunc AF,
58 NnInitTypes IT = NnInitTypes::XAVIER,
59 NnDescentTypes GD = NnDescentTypes::MOMENTUM,
60 NnDropout DD = NnDropout::DISABLE
61>
63 public:
64 // レイヤーの入出力変数
65 std::array<Matrix<1,P>, W + 2> z;
66 std::array<Matrix<1,N>, W + 2> dLdz;
68
71 : z(), dLdz(), dLde(), u(), d(), y(), e(), Wl(), Wt(), b(), dWl(), dWt(), db(), fpu(),
72 DropRand(0, 1), DropMask(),
73 epsilon(0.001), alpha(0.00001)
74 {
75
76 }
77
84
89
93 void PropagateForward(const std::array<Matrix<1,N>, W + 2>& zprev, const size_t t){
94 u.at(t) = Wl*zprev.at(t) + Wt*z.at(t - 1) + b; // 重み乗算加算とバイアス加算
95 ActivationFunctions::f<AF,1,P>(u.at(t), z.at(t)); // 活性化関数
96 }
97
101 void PropagateBackward(const std::array<Matrix<1,P>, W + 2>& dLdznext, const size_t t){
102 ActivationFunctions::fp<AF,1,P>(u.at(t), fpu); // 活性化関数の微分を通す計算
103 d.at(t) = fpu & (dLdznext.at(t) + tp(Wt)*d.at(t + 1)); // 誤差ベクトルの計算 ←ここのdLdznextのtの関数はどうするの?
104
105 dLdz.at(t) = tp(Wl)*d.at(t); // 前の層に渡すための勾配計算
106 }
107
111 void PropagateBackward(const Matrix<1,P>& dLdenext, const size_t t){
112 ActivationFunctions::fp<AF,1,P>(z.at(t), fpu); // 活性化関数の微分を通す計算
113 d.at(t) = fpu & (dLdenext + tp(Wt)*d.at(t + 1)); // 誤差ベクトルの計算 ←ここのdLdznextのtの関数はどうするの?
114
115 dLdz.at(t) = tp(Wl)*d.at(t); // 前の層に渡すための勾配計算
116 }
117
119 void PropagateForwardForOutput(const std::array<Matrix<1,N>, W + 2>& zprev){
120 ActivationFunctions::f<AF,1,P>(Wl*zprev.at(W) + b, y); // 重み乗算加算とバイアス加算と活性化関数
121 }
122
127 e = y - r; // 誤差ベクトルの計算
128 dLde = tp(Wl)*e; // 前の層に渡すための勾配計算
129 }
130
133 void UpdateWeightAndBias(const std::array<Matrix<1,N>, W + 2>& zprev){
134 // 重み・バイアス勾配の初期化
135 dWl.FillAllZero();
136 dWt.FillAllZero();
137 db.FillAllZero();
138
139 // 重み・バイアス勾配の計算
140 for(size_t t = 1; t <= W; ++t){
141 dWl += d.at(t)*tp(zprev.at(t)); // 階層方向の重み勾配
142 dWt += d.at(t)*tp(z.at(t - 1)); // 時刻方向の重み勾配
143 db += d.at(t); // バイアス勾配
144 }
145
146 GetUpdatedValue(dWl, dWt, db); // 確率的勾配降下法
147 }
148
151 void UpdateWeightAndBiasForOutput(const std::array<Matrix<1,N>, W + 2>& zprev){
152 // 重み・バイアス勾配の初期化
153 dWl.FillAllZero();
154 db.FillAllZero();
155
156 // 重み・バイアス勾配の計算
157 for(size_t t = 1; t <= W; ++t){
158 dWl += e*tp(zprev.at(t)); // 階層方向の重み勾配
159 db += e; // バイアス勾配
160 }
161
162 GetUpdatedValue(dWl, dWt, db); // 確率的勾配降下法
163 }
164
165 void ClearStateVars(void){
166 for(size_t t = 0; t < W + 2; ++t){
167 z.at(t).FillAllZero();
168 dLdz.at(t).FillAllZero();
169 u.at(t).FillAllZero();
170 d.at(t).FillAllZero();
171 }
172 }
173
176 void InitWeight(const size_t Nprev){
177 if constexpr(IT == NnInitTypes::XAVIER){
178 // Xavierの初期化
179 InitWeightByRandom( 1.0/sqrt((double)Nprev) );
180 }
181 if constexpr(IT == NnInitTypes::HE){
182 // Heの初期化
183 InitWeightByRandom( sqrt(2.0/(double)Nprev) );
184 }
185 }
186
189 PrintMat(Wl);
190 PrintMat(Wt);
191 PrintMat(b);
192 }
193
194 void DispError(void){
195 PrintMatrix(e, "% 16.8f");
196 }
197
200 if constexpr(DD == NnDropout::ENABLE){
201 // ドロップアウトするときのみ下記を計算
202 DropRand.GetRandomMatrix(DropMask); // 乱数生成
203
204 // ドロップアウト率よりも大きければマスクを0にする
205 for(size_t i = 0; i < P; ++i){
206 if(DropRate < DropMask.GetElement(1,i+1)){
207 DropMask.SetElement(1,i+1, 0);
208 }else{
209 DropMask.SetElement(1,i+1, 1);
210 }
211 }
212 }
213 }
214
215 private:
216 static constexpr Matrix<1,M> l = Matrix<1,M>::ones();
217 std::array<Matrix<1,P>, W + 2> u;
218 std::array<Matrix<1,P>, W + 2> d;
219 Matrix<1,P> y, e;
220 Matrix<N,P> Wl;
221 Matrix<P,P> Wt;
222 Matrix<1,P> b;
223 Matrix<N,P> dWl;
224 Matrix<P,P> dWt;
225 Matrix<1,P> db;
226 Matrix<1,P> fpu;
227 RandomGenerator DropRand;
228 Matrix<1,P> DropMask;
229
230 static constexpr double DropRate = 0.5;
231 double epsilon;
232 double alpha;
233
236 void InitWeightByRandom(const double sigma){
237 RandomGenerator RandWl(0, sigma); // メルセンヌ・ツイスタの生成
238 RandomGenerator RandWt(0, sigma); // メルセンヌ・ツイスタの生成
239 RandWl.GetGaussianRandomMatrix(Wl); // 平均0,標準偏差σのガウシアン乱数行列の取得
240 //RandWt.GetGaussianRandomMatrix(Wt); // 平均0,標準偏差σのガウシアン乱数行列の取得
241 //Wl = Wl * 0.01;
242 //Wt = Wt * 0.01;
243 }
244
249 void GetUpdatedValue(const Matrix<N,P>& DiffWl, const Matrix<P,P>& DiffWt, const Matrix<1,P>& diffb){
250
251 Wl += (-epsilon*DiffWl); // 重み行列の更新
252 Wt += (-epsilon*DiffWt); // 重み行列の更新
253 b += (-epsilon*diffb); // バイアスベクトルの更新
254
255 //dWl = alpha*dWl - epsilon*DiffWl; // 更新ゲイン乗算後の重み更新差分値
256 //dWt = alpha*dWt - epsilon*DiffWt; // 更新ゲイン乗算後の重み更新差分値
257 //db = alpha*db - epsilon*diffb; // 更新ゲイン乗算後のバイアス更新差分値
258 //Wl += dWl; // 重み行列の更新
259 //Wt += dWt; // 重み行列の更新
260 //b += db; // バイアスベクトルの更新
261
262 }
263
264};
265}
266
267#endif
268
ARCS イベントログクラス
ARCS用ASSERTクラス
乱数生成器
行列/ベクトル計算クラス(テンプレート版)
#define PrintMat(a)
行列要素表示マクロ(フォーマット指定なし版)
Definition Matrix.hh:36
#define PrintMatrix(a, b)
行列要素表示マクロ(フォーマット指定あり版)
Definition Matrix.hh:35
活性化関数
ActvFunc
活性化関数のタイプの定義
Definition ActivationFunctions.hh:35
ニューラルネットワークパラメータ定義ファイル
NnInitTypes
重み初期化のタイプの定義
Definition NeuralNetParamDef.hh:19
NnDropout
ドロップアウトの定義
Definition NeuralNetParamDef.hh:35
NnDescentTypes
勾配降下法のタイプの定義
Definition NeuralNetParamDef.hh:25
static void f(const Matrix< N, M > &U, Matrix< N, M > &Y)
活性化関数
Definition ActivationFunctions.hh:118
static void fp(const Matrix< N, M > &U, Matrix< N, M > &Y)
活性化関数の微分
Definition ActivationFunctions.hh:177
行列/ベクトル計算クラス(テンプレート版)
Definition Matrix.hh:44
constexpr TT GetElement(size_t n, size_t m) const
指定した要素番号の値を返す関数
Definition Matrix.hh:489
static constexpr Matrix ones(void)
m行n列の要素がすべて1の行列を返す関数
Definition Matrix.hh:655
constexpr void SetElement(size_t n, size_t m, TT val)
指定した要素番号に値を設定する関数
Definition Matrix.hh:480
constexpr void FillAllZero(void)
すべての要素を指定したゼロで埋める関数
Definition Matrix.hh:555
乱数生成器
Definition RandomGenerator.hh:22
void GetRandomMatrix(Matrix< N, M > &Y)
乱数行列を生成する関数
Definition RandomGenerator.hh:77
再帰ニューラルレイヤクラス
Definition RecurrentNeuralLayer.hh:62
std::array< Matrix< 1, N >, W+2 > dLdz
勾配ベクトルの時系列配列(範囲 t = 1 … T, t = 0 と T + 1 の分も確保)
Definition RecurrentNeuralLayer.hh:66
void PropagateForward(const std::array< Matrix< 1, N >, W+2 > &zprev, const size_t t)
順伝播計算(ベクトル入出力版)
Definition RecurrentNeuralLayer.hh:93
std::array< Matrix< 1, P >, W+2 > z
活性化関数通過後の状態ベクトルの時系列配列(範囲 t = 1 … T, t = 0 と T + 1 の分も確保)
Definition RecurrentNeuralLayer.hh:65
void PropagateBackwardForOutput(const Matrix< 1, P > &r)
出力の重み誤差ベクトル計算(出力層用,ベクトル入出力訓練版)
Definition RecurrentNeuralLayer.hh:126
RecurrentNeuralLayer(RecurrentNeuralLayer &&r)
ムーブコンストラクタ
Definition RecurrentNeuralLayer.hh:80
RecurrentNeuralLayer()
コンストラクタ
Definition RecurrentNeuralLayer.hh:70
void DispWeightAndBias(void)
重み行列とバイアスベクトルの表示
Definition RecurrentNeuralLayer.hh:188
Matrix< 1, N > dLde
勾配ベクトルの時系列配列(出力層用)
Definition RecurrentNeuralLayer.hh:67
void PropagateBackward(const Matrix< 1, P > &dLdenext, const size_t t)
逆伝播計算(-)
Definition RecurrentNeuralLayer.hh:111
void UpdateWeightAndBias(const std::array< Matrix< 1, N >, W+2 > &zprev)
重み行列とバイアスベクトルの更新
Definition RecurrentNeuralLayer.hh:133
~RecurrentNeuralLayer()
デストラクタ
Definition RecurrentNeuralLayer.hh:86
void PropagateBackward(const std::array< Matrix< 1, P >, W+2 > &dLdznext, const size_t t)
逆伝播計算(入力層と内部層用,ベクトル入出力版)
Definition RecurrentNeuralLayer.hh:101
void InitWeight(const size_t Nprev)
重み行列の初期化
Definition RecurrentNeuralLayer.hh:176
void GenerateDropMask(void)
ドロップアウトマスクの生成
Definition RecurrentNeuralLayer.hh:199
void UpdateWeightAndBiasForOutput(const std::array< Matrix< 1, N >, W+2 > &zprev)
重み行列とバイアスベクトルの更新(出力層用)
Definition RecurrentNeuralLayer.hh:151
void PropagateForwardForOutput(const std::array< Matrix< 1, N >, W+2 > &zprev)
順伝播計算(出力層用,ベクトル入出力版)
Definition RecurrentNeuralLayer.hh:119