14#ifndef RECURRENTNEURALLAYER
15#define RECURRENTNEURALLAYER
34 #define arcs_assert(a) (assert(a))
37 #define EventLogVar(a)
65 std::array<Matrix<1,P>, W + 2>
z;
66 std::array<Matrix<1,N>, W + 2>
dLdz;
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)
94 u.at(t) = Wl*zprev.at(t) + Wt*
z.at(t - 1) + b;
103 d.at(t) = fpu & (dLdznext.at(t) + tp(Wt)*d.at(t + 1));
105 dLdz.at(t) = tp(Wl)*d.at(t);
113 d.at(t) = fpu & (dLdenext + tp(Wt)*d.at(t + 1));
115 dLdz.at(t) = tp(Wl)*d.at(t);
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));
146 GetUpdatedValue(dWl, dWt, db);
157 for(
size_t t = 1; t <= W; ++t){
158 dWl += e*tp(zprev.at(t));
162 GetUpdatedValue(dWl, dWt, db);
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();
177 if constexpr(IT == NnInitTypes::XAVIER){
179 InitWeightByRandom( 1.0/sqrt((
double)Nprev) );
181 if constexpr(IT == NnInitTypes::HE){
183 InitWeightByRandom( sqrt(2.0/(
double)Nprev) );
194 void DispError(
void){
200 if constexpr(DD == NnDropout::ENABLE){
205 for(
size_t i = 0; i < P; ++i){
217 std::array<Matrix<1,P>, W + 2> u;
218 std::array<Matrix<1,P>, W + 2> d;
230 static constexpr double DropRate = 0.5;
236 void InitWeightByRandom(
const double sigma){
239 RandWl.GetGaussianRandomMatrix(Wl);
249 void GetUpdatedValue(
const Matrix<N,P>& DiffWl,
const Matrix<P,P>& DiffWt,
const Matrix<1,P>& diffb){
251 Wl += (-epsilon*DiffWl);
252 Wt += (-epsilon*DiffWt);
253 b += (-epsilon*diffb);
#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