Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GRULayer.h
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn/gru:$Id$
2// Author: Surya S Dwivedi 03/07/19
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : BasicGRULayer *
8 * *
9 * Description: *
10 * NeuralNetwork *
11 * *
12 * Authors (alphabetical): *
13 * Surya S Dwivedi <surya2191997@gmail.com> - IIT Kharagpur, India *
14 * *
15 * Copyright (c) 2005-2019: *
16 * All rights reserved. *
17 * CERN, Switzerland *
18 * *
19 * For the licensing terms see $ROOTSYS/LICENSE. *
20 * For the list of contributors see $ROOTSYS/README/CREDITS. *
21 **********************************************************************************/
22
23//#pragma once
24
25//////////////////////////////////////////////////////////////////////
26// This class implements the GRU layer. GRU is a variant of vanilla
27// RNN which is capable of learning long range dependencies.
28//////////////////////////////////////////////////////////////////////
29
30#ifndef TMVA_DNN_GRU_LAYER
31#define TMVA_DNN_GRU_LAYER
32
33#include <cmath>
34#include <iostream>
35#include <vector>
36
37#include "TMatrix.h"
39#include "TMVA/DNN/Functions.h"
41
42namespace TMVA
43{
44namespace DNN
45{
46namespace RNN
47{
48
49//______________________________________________________________________________
50//
51// Basic GRU Layer
52//______________________________________________________________________________
53
54/** \class BasicGRULayer
55 Generic implementation
56*/
57template<typename Architecture_t>
58 class TBasicGRULayer : public VGeneralLayer<Architecture_t>
59{
60
61public:
62
63 using Matrix_t = typename Architecture_t::Matrix_t;
64 using Scalar_t = typename Architecture_t::Scalar_t;
65 using Tensor_t = typename Architecture_t::Tensor_t;
66
67 using LayerDescriptor_t = typename Architecture_t::RecurrentDescriptor_t;
68 using WeightsDescriptor_t = typename Architecture_t::FilterDescriptor_t;
69 using TensorDescriptor_t = typename Architecture_t::TensorDescriptor_t;
70 using HelperDescriptor_t = typename Architecture_t::DropoutDescriptor_t;
71
72 using RNNWorkspace_t = typename Architecture_t::RNNWorkspace_t;
73 using RNNDescriptors_t = typename Architecture_t::RNNDescriptors_t;
74
75private:
76
77 size_t fStateSize; ///< Hidden state size for GRU
78 size_t fTimeSteps; ///< Timesteps for GRU
79
80 bool fRememberState; ///< Remember state in next pass
81 bool fReturnSequence = false; ///< Return in output full sequence or just last element
82 bool fResetGateAfter = false; ///< GRU variant to Apply the reset gate multiplication afterwards (used by cuDNN)
83
84 DNN::EActivationFunction fF1; ///< Activation function: sigmoid
85 DNN::EActivationFunction fF2; ///< Activation function: tanh
86
87 Matrix_t fResetValue; ///< Computed reset gate values
88 Matrix_t fUpdateValue; ///< Computed forget gate values
89 Matrix_t fCandidateValue; ///< Computed candidate values
90 Matrix_t fState; ///< Hidden state of GRU
91
92
93 Matrix_t &fWeightsResetGate; ///< Reset Gate weights for input, fWeights[0]
94 Matrix_t &fWeightsResetGateState; ///< Input Gate weights for prev state, fWeights[1]
95 Matrix_t &fResetGateBias; ///< Input Gate bias
96
97 Matrix_t &fWeightsUpdateGate; ///< Update Gate weights for input, fWeights[2]
98 Matrix_t &fWeightsUpdateGateState; ///< Update Gate weights for prev state, fWeights[3]
99 Matrix_t &fUpdateGateBias; ///< Update Gate bias
100
101 Matrix_t &fWeightsCandidate; ///< Candidate Gate weights for input, fWeights[4]
102 Matrix_t &fWeightsCandidateState; ///< Candidate Gate weights for prev state, fWeights[5]
103 Matrix_t &fCandidateBias; ///< Candidate Gate bias
104
105
106 std::vector<Matrix_t> reset_gate_value; ///< Reset gate value for every time step
107 std::vector<Matrix_t> update_gate_value; ///< Update gate value for every time step
108 std::vector<Matrix_t> candidate_gate_value; ///< Candidate gate value for every time step
109
110 std::vector<Matrix_t> fDerivativesReset; ///< First fDerivatives of the activations reset gate
111 std::vector<Matrix_t> fDerivativesUpdate; ///< First fDerivatives of the activations update gate
112 std::vector<Matrix_t> fDerivativesCandidate; ///< First fDerivatives of the activations candidate gate
113
114 Matrix_t &fWeightsResetGradients; ///< Gradients w.r.t the reset gate - input weights
115 Matrix_t &fWeightsResetStateGradients; ///< Gradients w.r.t the reset gate - hidden state weights
116 Matrix_t &fResetBiasGradients; ///< Gradients w.r.t the reset gate - bias weights
117 Matrix_t &fWeightsUpdateGradients; ///< Gradients w.r.t the update gate - input weights
118 Matrix_t &fWeightsUpdateStateGradients; ///< Gradients w.r.t the update gate - hidden state weights
119 Matrix_t &fUpdateBiasGradients; ///< Gradients w.r.t the update gate - bias weights
120 Matrix_t &fWeightsCandidateGradients; ///< Gradients w.r.t the candidate gate - input weights
121 Matrix_t &fWeightsCandidateStateGradients; ///< Gradients w.r.t the candidate gate - hidden state weights
122 Matrix_t &fCandidateBiasGradients; ///< Gradients w.r.t the candidate gate - bias weights
123
124 Matrix_t fCell; ///< Empty matrix for GRU
125
126 // Tensor representing all weights (used by cuDNN)
127 Tensor_t fWeightsTensor; ///< Tensor for all weights
128 Tensor_t fWeightGradientsTensor; ///< Tensor for all weight gradients
129
130 // tensors used internally for the forward and backward pass
131 Tensor_t fX; ///< cached input tensor as T x B x I
132 Tensor_t fY; ///< cached output tensor as T x B x S
133 Tensor_t fDx; ///< cached gradient on the input (output of backward) as T x B x I
134 Tensor_t fDy; ///< cached activation gradient (input of backward) as T x B x S
135
136 TDescriptors *fDescriptors = nullptr; ///< Keeps all the RNN descriptors
137 TWorkspace *fWorkspace = nullptr; // workspace needed for GPU computation (CudNN)
138
139public:
140
141 /*! Constructor */
142 TBasicGRULayer(size_t batchSize, size_t stateSize, size_t inputSize,
143 size_t timeSteps, bool rememberState = false, bool returnSequence = false,
144 bool resetGateAfter = false,
148
149 /*! Copy Constructor */
151
152 /*! Initialize the weights according to the given initialization
153 ** method. */
154 void Initialize() override;
155
156 /*! Initialize the hidden state and cell state method. */
158
159 /*! Computes the next hidden state
160 * and next cell state with given input matrix. */
161 void Forward(Tensor_t &input, bool isTraining = true) override;
162
163 /*! Forward for a single cell (time unit) */
165
166 /*! Backpropagates the error. Must only be called directly at the corresponding
167 * call to Forward(...). */
169 const Tensor_t &activations_backward) override;
170
171 /* Updates weights and biases, given the learning rate */
172 void Update(const Scalar_t learningRate);
173
174 /*! Backward for a single time unit
175 * a the corresponding call to Forward(...). */
178 const Matrix_t & reset_gate, const Matrix_t & update_gate,
179 const Matrix_t & candidate_gate,
182
183 /*! Decides the values we'll update (NN with Sigmoid) */
184 void ResetGate(const Matrix_t &input, Matrix_t &di);
185
186 /*! Forgets the past values (NN with Sigmoid) */
187 void UpdateGate(const Matrix_t &input, Matrix_t &df);
188
189 /*! Decides the new candidate values (NN with Tanh) */
190 void CandidateValue(const Matrix_t &input, Matrix_t &dc);
191
192 /*! Prints the info about the layer */
193 void Print() const override;
194
195 /*! Writes the information and the weights about the layer in an XML node. */
196 void AddWeightsXMLTo(void *parent) override;
197
198 /*! Read the information and the weights about the layer from XML node. */
199 void ReadWeightsFromXML(void *parent) override;
200
201 /*! Getters */
202 size_t GetInputSize() const { return this->GetInputWidth(); }
203 size_t GetTimeSteps() const { return fTimeSteps; }
204 size_t GetStateSize() const { return fStateSize; }
205
206 inline bool DoesRememberState() const { return fRememberState; }
207 inline bool DoesReturnSequence() const { return fReturnSequence; }
208
211
212 const Matrix_t & GetResetGateValue() const { return fResetValue; }
214 const Matrix_t & GetCandidateValue() const { return fCandidateValue; }
216 const Matrix_t & GetUpdateGateValue() const { return fUpdateValue; }
218
219 const Matrix_t & GetState() const { return fState; }
220 Matrix_t & GetState() { return fState; }
221 const Matrix_t &GetCell() const { return fCell; }
222 Matrix_t & GetCell() { return fCell; }
223
230
237
238 const std::vector<Matrix_t> & GetDerivativesReset() const { return fDerivativesReset; }
239 std::vector<Matrix_t> & GetDerivativesReset() { return fDerivativesReset; }
240 const Matrix_t & GetResetDerivativesAt(size_t i) const { return fDerivativesReset[i]; }
242 const std::vector<Matrix_t> & GetDerivativesUpdate() const { return fDerivativesUpdate; }
243 std::vector<Matrix_t> & GetDerivativesUpdate() { return fDerivativesUpdate; }
244 const Matrix_t & GetUpdateDerivativesAt(size_t i) const { return fDerivativesUpdate[i]; }
246 const std::vector<Matrix_t> & GetDerivativesCandidate() const { return fDerivativesCandidate; }
247 std::vector<Matrix_t> & GetDerivativesCandidate() { return fDerivativesCandidate; }
248 const Matrix_t & GetCandidateDerivativesAt(size_t i) const { return fDerivativesCandidate[i]; }
250
251 const std::vector<Matrix_t> & GetResetGateTensor() const { return reset_gate_value; }
252 std::vector<Matrix_t> & GetResetGateTensor() { return reset_gate_value; }
253 const Matrix_t & GetResetGateTensorAt(size_t i) const { return reset_gate_value[i]; }
255 const std::vector<Matrix_t> & GetUpdateGateTensor() const { return update_gate_value; }
256 std::vector<Matrix_t> & GetUpdateGateTensor() { return update_gate_value; }
257 const Matrix_t & GetUpdateGateTensorAt(size_t i) const { return update_gate_value[i]; }
259 const std::vector<Matrix_t> & GetCandidateGateTensor() const { return candidate_gate_value; }
260 std::vector<Matrix_t> & GetCandidateGateTensor() { return candidate_gate_value; }
261 const Matrix_t & GetCandidateGateTensorAt(size_t i) const { return candidate_gate_value[i]; }
263
264
265
266 const Matrix_t & GetResetGateBias() const { return fResetGateBias; }
268 const Matrix_t & GetUpdateGateBias() const { return fUpdateGateBias; }
270 const Matrix_t & GetCandidateBias() const { return fCandidateBias; }
272
291
293 const Tensor_t &GetWeightsTensor() const { return fWeightsTensor; }
296
297 Tensor_t &GetX() { return fX; }
298 Tensor_t &GetY() { return fY; }
299 Tensor_t &GetDX() { return fDx; }
300 Tensor_t &GetDY() { return fDy; }
301};
302
303
304//______________________________________________________________________________
305//
306// Basic GRU-Layer Implementation
307//______________________________________________________________________________
308
309template <typename Architecture_t>
310TBasicGRULayer<Architecture_t>::TBasicGRULayer(size_t batchSize, size_t stateSize, size_t inputSize, size_t timeSteps,
312 DNN::EActivationFunction f2, bool /* training */,
314 : VGeneralLayer<Architecture_t>(batchSize, 1, timeSteps, inputSize, 1, (returnSequence) ? timeSteps : 1, stateSize,
316 {inputSize, inputSize, inputSize, stateSize, stateSize, stateSize}, 3,
317 {stateSize, stateSize, stateSize}, {1, 1, 1}, batchSize,
318 (returnSequence) ? timeSteps : 1, stateSize, fA),
319 fStateSize(stateSize), fTimeSteps(timeSteps), fRememberState(rememberState), fReturnSequence(returnSequence), fResetGateAfter(resetGateAfter),
320 fF1(f1), fF2(f2), fResetValue(batchSize, stateSize), fUpdateValue(batchSize, stateSize),
321 fCandidateValue(batchSize, stateSize), fState(batchSize, stateSize), fWeightsResetGate(this->GetWeightsAt(0)),
322 fWeightsResetGateState(this->GetWeightsAt(3)), fResetGateBias(this->GetBiasesAt(0)),
323 fWeightsUpdateGate(this->GetWeightsAt(1)), fWeightsUpdateGateState(this->GetWeightsAt(4)),
324 fUpdateGateBias(this->GetBiasesAt(1)), fWeightsCandidate(this->GetWeightsAt(2)),
325 fWeightsCandidateState(this->GetWeightsAt(5)), fCandidateBias(this->GetBiasesAt(2)),
326 fWeightsResetGradients(this->GetWeightGradientsAt(0)), fWeightsResetStateGradients(this->GetWeightGradientsAt(3)),
327 fResetBiasGradients(this->GetBiasGradientsAt(0)), fWeightsUpdateGradients(this->GetWeightGradientsAt(1)),
328 fWeightsUpdateStateGradients(this->GetWeightGradientsAt(4)), fUpdateBiasGradients(this->GetBiasGradientsAt(1)),
329 fWeightsCandidateGradients(this->GetWeightGradientsAt(2)),
330 fWeightsCandidateStateGradients(this->GetWeightGradientsAt(5)),
331 fCandidateBiasGradients(this->GetBiasGradientsAt(2))
332{
333 for (size_t i = 0; i < timeSteps; ++i) {
334 fDerivativesReset.emplace_back(batchSize, stateSize);
335 fDerivativesUpdate.emplace_back(batchSize, stateSize);
336 fDerivativesCandidate.emplace_back(batchSize, stateSize);
337 reset_gate_value.emplace_back(batchSize, stateSize);
338 update_gate_value.emplace_back(batchSize, stateSize);
339 candidate_gate_value.emplace_back(batchSize, stateSize);
340 }
341 Architecture_t::InitializeGRUTensors(this);
342}
343
344 //______________________________________________________________________________
345template <typename Architecture_t>
347 : VGeneralLayer<Architecture_t>(layer),
348 fStateSize(layer.fStateSize),
349 fTimeSteps(layer.fTimeSteps),
350 fRememberState(layer.fRememberState),
351 fReturnSequence(layer.fReturnSequence),
352 fResetGateAfter(layer.fResetGateAfter),
353 fF1(layer.GetActivationFunctionF1()),
354 fF2(layer.GetActivationFunctionF2()),
355 fResetValue(layer.GetBatchSize(), layer.GetStateSize()),
356 fUpdateValue(layer.GetBatchSize(), layer.GetStateSize()),
357 fCandidateValue(layer.GetBatchSize(), layer.GetStateSize()),
358 fState(layer.GetBatchSize(), layer.GetStateSize()),
359 fWeightsResetGate(this->GetWeightsAt(0)),
360 fWeightsResetGateState(this->GetWeightsAt(3)),
361 fResetGateBias(this->GetBiasesAt(0)),
362 fWeightsUpdateGate(this->GetWeightsAt(1)),
363 fWeightsUpdateGateState(this->GetWeightsAt(4)),
364 fUpdateGateBias(this->GetBiasesAt(1)),
365 fWeightsCandidate(this->GetWeightsAt(2)),
366 fWeightsCandidateState(this->GetWeightsAt(5)),
367 fCandidateBias(this->GetBiasesAt(2)),
368 fWeightsResetGradients(this->GetWeightGradientsAt(0)),
369 fWeightsResetStateGradients(this->GetWeightGradientsAt(3)),
370 fResetBiasGradients(this->GetBiasGradientsAt(0)),
371 fWeightsUpdateGradients(this->GetWeightGradientsAt(1)),
372 fWeightsUpdateStateGradients(this->GetWeightGradientsAt(4)),
373 fUpdateBiasGradients(this->GetBiasGradientsAt(1)),
374 fWeightsCandidateGradients(this->GetWeightGradientsAt(2)),
375 fWeightsCandidateStateGradients(this->GetWeightGradientsAt(5)),
376 fCandidateBiasGradients(this->GetBiasGradientsAt(2))
377{
378 for (size_t i = 0; i < fTimeSteps; ++i) {
379 fDerivativesReset.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
380 Architecture_t::Copy(fDerivativesReset[i], layer.GetResetDerivativesAt(i));
381
382 fDerivativesUpdate.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
383 Architecture_t::Copy(fDerivativesUpdate[i], layer.GetUpdateDerivativesAt(i));
384
385 fDerivativesCandidate.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
386 Architecture_t::Copy(fDerivativesCandidate[i], layer.GetCandidateDerivativesAt(i));
387
388 reset_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
389 Architecture_t::Copy(reset_gate_value[i], layer.GetResetGateTensorAt(i));
390
391 update_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
392 Architecture_t::Copy(update_gate_value[i], layer.GetUpdateGateTensorAt(i));
393
394 candidate_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
395 Architecture_t::Copy(candidate_gate_value[i], layer.GetCandidateGateTensorAt(i));
396 }
397
398 // Gradient matrices not copied
399 Architecture_t::Copy(fState, layer.GetState());
400
401 // Copy each gate values.
402 Architecture_t::Copy(fResetValue, layer.GetResetGateValue());
403 Architecture_t::Copy(fCandidateValue, layer.GetCandidateValue());
404 Architecture_t::Copy(fUpdateValue, layer.GetUpdateGateValue());
405
406 Architecture_t::InitializeGRUTensors(this);
407}
408
409//______________________________________________________________________________
410template <typename Architecture_t>
412{
414
415 Architecture_t::InitializeGRUDescriptors(fDescriptors, this);
416 Architecture_t::InitializeGRUWorkspace(fWorkspace, fDescriptors, this);
417
418 //cuDNN only supports resetGate after
419 if (Architecture_t::IsCudnn())
420 fResetGateAfter = true;
421}
422
423//______________________________________________________________________________
424template <typename Architecture_t>
426-> void
427{
428 /*! Computes reset gate values according to equation:
429 * input = act(W_input . input + W_state . state + bias)
430 * activation function: sigmoid. */
431 const DNN::EActivationFunction fRst = this->GetActivationFunctionF1();
432 Matrix_t tmpState(fResetValue.GetNrows(), fResetValue.GetNcols());
433 Architecture_t::MultiplyTranspose(tmpState, fState, fWeightsResetGateState);
434 Architecture_t::MultiplyTranspose(fResetValue, input, fWeightsResetGate);
435 Architecture_t::ScaleAdd(fResetValue, tmpState);
436 Architecture_t::AddRowWise(fResetValue, fResetGateBias);
437 DNN::evaluateDerivativeMatrix<Architecture_t>(dr, fRst, fResetValue);
438 DNN::evaluateMatrix<Architecture_t>(fResetValue, fRst);
439}
440
441 //______________________________________________________________________________
442template <typename Architecture_t>
444-> void
445{
446 /*! Computes update gate values according to equation:
447 * forget = act(W_input . input + W_state . state + bias)
448 * activation function: sigmoid. */
449 const DNN::EActivationFunction fUpd = this->GetActivationFunctionF1();
450 Matrix_t tmpState(fUpdateValue.GetNrows(), fUpdateValue.GetNcols());
451 Architecture_t::MultiplyTranspose(tmpState, fState, fWeightsUpdateGateState);
452 Architecture_t::MultiplyTranspose(fUpdateValue, input, fWeightsUpdateGate);
453 Architecture_t::ScaleAdd(fUpdateValue, tmpState);
454 Architecture_t::AddRowWise(fUpdateValue, fUpdateGateBias);
455 DNN::evaluateDerivativeMatrix<Architecture_t>(du, fUpd, fUpdateValue);
456 DNN::evaluateMatrix<Architecture_t>(fUpdateValue, fUpd);
457}
458
459 //______________________________________________________________________________
460template <typename Architecture_t>
462-> void
463{
464 /*!
465 vanilla GRU:
466 candidate_value = act(W_input . input + W_state . (reset*state) + bias)
467
468 but CuDNN uses reset_after variant that is faster (with bias mode = input)
469 (apply reset gate multiplication after matrix multiplication)
470 candidate_value = act(W_input . input + reset * (W_state . state) + bias
471
472 activation function = tanh.
473
474 */
475
476 const DNN::EActivationFunction fCan = this->GetActivationFunctionF2();
477 Matrix_t tmp(fCandidateValue.GetNrows(), fCandidateValue.GetNcols());
478 if (!fResetGateAfter) {
479 Matrix_t tmpState(fResetValue); // I think here tmpState uses fResetValue buffer
480 Architecture_t::Hadamard(tmpState, fState);
481 Architecture_t::MultiplyTranspose(tmp, tmpState, fWeightsCandidateState);
482 } else {
483 // variant GRU used in cuDNN slightly faster
484 Architecture_t::MultiplyTranspose(tmp, fState, fWeightsCandidateState);
485 Architecture_t::Hadamard(tmp, fResetValue);
486 }
487 Architecture_t::MultiplyTranspose(fCandidateValue, input, fWeightsCandidate);
488 Architecture_t::ScaleAdd(fCandidateValue, tmp);
489 Architecture_t::AddRowWise(fCandidateValue, fCandidateBias);
490 DNN::evaluateDerivativeMatrix<Architecture_t>(dc, fCan, fCandidateValue);
491 DNN::evaluateMatrix<Architecture_t>(fCandidateValue, fCan);
492}
493
494 //______________________________________________________________________________
495template <typename Architecture_t>
497-> void
498{
499 // for Cudnn
500 if (Architecture_t::IsCudnn()) {
501
502 // input size is stride[1] of input tensor that is B x T x inputSize
503 assert(input.GetStrides()[1] == this->GetInputSize());
504
505 Tensor_t &x = this->fX;
506 Tensor_t &y = this->fY;
507 Architecture_t::Rearrange(x, input);
508
509 //const auto &weights = this->GetWeightsAt(0);
510 const auto &weights = this->GetWeightsTensor();
511
512 auto &hx = this->fState;
513 auto &cx = this->fCell;
514 // use same for hy and cy
515 auto &hy = this->fState;
516 auto &cy = this->fCell;
517
518 auto & rnnDesc = static_cast<RNNDescriptors_t &>(*fDescriptors);
519 auto & rnnWork = static_cast<RNNWorkspace_t &>(*fWorkspace);
520
521 Architecture_t::RNNForward(x, hx, cx, weights, y, hy, cy, rnnDesc, rnnWork, isTraining);
522
523 if (fReturnSequence) {
524 Architecture_t::Rearrange(this->GetOutput(), y); // swap B and T from y to Output
525 } else {
526 // tmp is a reference to y (full cudnn output)
527 Tensor_t tmp = (y.At(y.GetShape()[0] - 1)).Reshape({y.GetShape()[1], 1, y.GetShape()[2]});
528 Architecture_t::Copy(this->GetOutput(), tmp);
529 }
530
531 return;
532 }
533
534 // D : input size
535 // H : state size
536 // T : time size
537 // B : batch size
538
539 Tensor_t arrInput ( fTimeSteps, this->GetBatchSize(), this->GetInputWidth());
540 // for (size_t t = 0; t < fTimeSteps; ++t) {
541 // arrInput.emplace_back(this->GetBatchSize(), this->GetInputWidth()); // T x B x D
542 // }
543 Architecture_t::Rearrange(arrInput, input); // B x T x D
544
545 Tensor_t arrOutput ( fTimeSteps, this->GetBatchSize(), fStateSize );
546 // for (size_t t = 0; t < fTimeSteps;++t) {
547 // arrOutput.emplace_back(this->GetBatchSize(), fStateSize); // T x B x H
548 // }
549
550 if (!this->fRememberState) {
552 }
553
554 /*! Pass each gate values to CellForward() to calculate
555 * next hidden state and next cell state. */
556 for (size_t t = 0; t < fTimeSteps; ++t) {
557 /* Feed forward network: value of each gate being computed at each timestep t. */
558 ResetGate(arrInput[t], fDerivativesReset[t]);
559 Architecture_t::Copy(this->GetResetGateTensorAt(t), fResetValue);
560 UpdateGate(arrInput[t], fDerivativesUpdate[t]);
561 Architecture_t::Copy(this->GetUpdateGateTensorAt(t), fUpdateValue);
562
563 CandidateValue(arrInput[t], fDerivativesCandidate[t]);
564 Architecture_t::Copy(this->GetCandidateGateTensorAt(t), fCandidateValue);
565
566
567 CellForward(fUpdateValue, fCandidateValue);
568
569 // Architecture_t::PrintTensor(Tensor_t(fState), "state output");
570
572 Architecture_t::Copy(arrOutputMt, fState);
573 }
574
575 if (fReturnSequence)
576 Architecture_t::Rearrange(this->GetOutput(), arrOutput); // B x T x D
577 else {
578 // get T[end[]]
579 Tensor_t tmp = arrOutput.At(fTimeSteps - 1); // take last time step
580 // shape of tmp is for CPU (column wise) B x D , need to reshape to make a B x D x 1
581 // and transpose it to 1 x D x B (this is how output is expected in columnmajor format)
582 tmp = tmp.Reshape({tmp.GetShape()[0], tmp.GetShape()[1], 1});
583 assert(tmp.GetSize() == this->GetOutput().GetSize());
584 assert(tmp.GetShape()[0] == this->GetOutput().GetShape()[2]); // B is last dim in output and first in tmp
585 Architecture_t::Rearrange(this->GetOutput(), tmp);
586 // keep array output
587 fY = arrOutput;
588 }
589}
590
591//______________________________________________________________________________
592template <typename Architecture_t>
594-> void
595{
596 Architecture_t::Hadamard(fState, updateGateValues);
597
598 // this will reuse content of updateGateValues
599 Matrix_t tmp(updateGateValues); // H X 1
600 for (size_t j = 0; j < (size_t) tmp.GetNcols(); j++) {
601 for (size_t i = 0; i < (size_t) tmp.GetNrows(); i++) {
602 tmp(i,j) = 1 - tmp(i,j);
603 }
604 }
605
606 // Update state
607 Architecture_t::Hadamard(candidateValues, tmp);
608 Architecture_t::ScaleAdd(fState, candidateValues);
609}
610
611//____________________________________________________________________________
612template <typename Architecture_t>
614 const Tensor_t &activations_backward) // B x T x D
615-> void
616{
617 // BACKWARD for CUDNN
618 if (Architecture_t::IsCudnn()) {
619
620 Tensor_t &x = this->fX;
621 Tensor_t &y = this->fY;
622 Tensor_t &dx = this->fDx;
623 Tensor_t &dy = this->fDy;
624
625 // input size is stride[1] of input tensor that is B x T x inputSize
626 assert(activations_backward.GetStrides()[1] == this->GetInputSize());
627
628
629 Architecture_t::Rearrange(x, activations_backward);
630
631 if (!fReturnSequence) {
632
633 // Architecture_t::InitializeZero(dy);
634 Architecture_t::InitializeZero(dy);
635
636 // Tensor_t tmp1 = y.At(y.GetShape()[0] - 1).Reshape({y.GetShape()[1], 1, y.GetShape()[2]});
637 Tensor_t tmp2 = dy.At(dy.GetShape()[0] - 1).Reshape({dy.GetShape()[1], 1, dy.GetShape()[2]});
638
639 // Architecture_t::Copy(tmp1, this->GetOutput());
640 Architecture_t::Copy(tmp2, this->GetActivationGradients());
641 } else {
642 Architecture_t::Rearrange(y, this->GetOutput());
643 Architecture_t::Rearrange(dy, this->GetActivationGradients());
644 }
645
646 // Architecture_t::PrintTensor(this->GetOutput(), "output before bwd");
647
648 // for cudnn Matrix_t and Tensor_t are same type
649 const auto &weights = this->GetWeightsTensor();
650 auto &weightGradients = this->GetWeightGradientsTensor();
651
652 // note that cudnnRNNBackwardWeights accumulate the weight gradients.
653 // We need then to initialize the tensor to zero every time
654 Architecture_t::InitializeZero(weightGradients);
655
656 // hx is fState
657 auto &hx = this->GetState();
658 auto &cx = this->GetCell();
659 // use same for hy and cy
660 auto &dhy = hx;
661 auto &dcy = cx;
662 auto &dhx = hx;
663 auto &dcx = cx;
664
665 auto & rnnDesc = static_cast<RNNDescriptors_t &>(*fDescriptors);
666 auto & rnnWork = static_cast<RNNWorkspace_t &>(*fWorkspace);
667
668 Architecture_t::RNNBackward(x, hx, cx, y, dy, dhy, dcy, weights, dx, dhx, dcx, weightGradients, rnnDesc, rnnWork);
669
670 // Architecture_t::PrintTensor(this->GetOutput(), "output after bwd");
671
672 if (gradients_backward.GetSize() != 0)
673 Architecture_t::Rearrange(gradients_backward, dx);
674
675 return;
676 }
677
678 // gradients_backward is activationGradients of layer before it, which is input layer.
679 // Currently, gradients_backward is for input(x) and not for state.
680 // For the state it can be:
681 Matrix_t state_gradients_backward(this->GetBatchSize(), fStateSize); // B x H
682 DNN::initialize<Architecture_t>(state_gradients_backward, DNN::EInitialization::kZero); // B x H
683
684 // if dummy is false gradients_backward will be written back on the matrix
685 bool dummy = false;
686 if (gradients_backward.GetSize() == 0 || gradients_backward[0].GetNrows() == 0 || gradients_backward[0].GetNcols() == 0) {
687 dummy = true;
688 }
689
690 Tensor_t arr_gradients_backward ( fTimeSteps, this->GetBatchSize(), this->GetInputSize());
691
692
693 //Architecture_t::Rearrange(arr_gradients_backward, gradients_backward); // B x T x D
694 // activations_backward is input.
695 Tensor_t arr_activations_backward ( fTimeSteps, this->GetBatchSize(), this->GetInputSize());
696
697 Architecture_t::Rearrange(arr_activations_backward, activations_backward); // B x T x D
698
699 /*! For backpropagation, we need to calculate loss. For loss, output must be known.
700 * We obtain outputs during forward propagation and place the results in arr_output tensor. */
701 Tensor_t arr_output ( fTimeSteps, this->GetBatchSize(), fStateSize);
702
703 Matrix_t initState(this->GetBatchSize(), fStateSize); // B x H
704 DNN::initialize<Architecture_t>(initState, DNN::EInitialization::kZero); // B x H
705
706 // This will take partial derivative of state[t] w.r.t state[t-1]
707 Tensor_t arr_actgradients ( fTimeSteps, this->GetBatchSize(), fStateSize);
708
709 if (fReturnSequence) {
710 Architecture_t::Rearrange(arr_output, this->GetOutput());
711 Architecture_t::Rearrange(arr_actgradients, this->GetActivationGradients());
712 } else {
713 //
714 arr_output = fY;
715 Architecture_t::InitializeZero(arr_actgradients);
716 // need to reshape to pad a time dimension = 1 (note here is columnmajor tensors)
717 Tensor_t tmp_grad = arr_actgradients.At(fTimeSteps - 1).Reshape({this->GetBatchSize(), fStateSize, 1});
718 assert(tmp_grad.GetSize() == this->GetActivationGradients().GetSize());
719 assert(tmp_grad.GetShape()[0] ==
720 this->GetActivationGradients().GetShape()[2]); // B in tmp is [0] and [2] in input act. gradients
721
722 Architecture_t::Rearrange(tmp_grad, this->GetActivationGradients());
723 }
724
725 /*! There are total 8 different weight matrices and 4 bias vectors.
726 * Re-initialize them with zero because it should have some value. (can't be garbage values) */
727
728 // Reset Gate.
729 fWeightsResetGradients.Zero();
730 fWeightsResetStateGradients.Zero();
731 fResetBiasGradients.Zero();
732
733 // Update Gate.
734 fWeightsUpdateGradients.Zero();
735 fWeightsUpdateStateGradients.Zero();
736 fUpdateBiasGradients.Zero();
737
738 // Candidate Gate.
739 fWeightsCandidateGradients.Zero();
740 fWeightsCandidateStateGradients.Zero();
741 fCandidateBiasGradients.Zero();
742
743
744 for (size_t t = fTimeSteps; t > 0; t--) {
745 // Store the sum of gradients obtained at each timestep during backward pass.
746 Architecture_t::ScaleAdd(state_gradients_backward, arr_actgradients[t-1]);
747 if (t > 1) {
750 // During forward propagation, each gate value calculates their gradients.
752 this->GetResetGateTensorAt(t-1), this->GetUpdateGateTensorAt(t-1),
753 this->GetCandidateGateTensorAt(t-1),
755 fDerivativesReset[t-1], fDerivativesUpdate[t-1],
756 fDerivativesCandidate[t-1]);
757 } else {
761 this->GetResetGateTensorAt(t-1), this->GetUpdateGateTensorAt(t-1),
762 this->GetCandidateGateTensorAt(t-1),
764 fDerivativesReset[t-1], fDerivativesUpdate[t-1],
765 fDerivativesCandidate[t-1]);
766 }
767 }
768
769 if (!dummy) {
770 Architecture_t::Rearrange(gradients_backward, arr_gradients_backward );
771 }
772
773}
774
775
776//______________________________________________________________________________
777template <typename Architecture_t>
780 const Matrix_t & reset_gate, const Matrix_t & update_gate,
781 const Matrix_t & candidate_gate,
784-> Matrix_t &
785{
786 /*! Call here GRULayerBackward() to pass parameters i.e. gradient
787 * values obtained from each gate during forward propagation. */
788 return Architecture_t::GRULayerBackward(state_gradients_backward,
789 fWeightsResetGradients, fWeightsUpdateGradients, fWeightsCandidateGradients,
790 fWeightsResetStateGradients, fWeightsUpdateStateGradients,
791 fWeightsCandidateStateGradients, fResetBiasGradients, fUpdateBiasGradients,
792 fCandidateBiasGradients, dr, du, dc,
795 fWeightsResetGate, fWeightsUpdateGate, fWeightsCandidate,
796 fWeightsResetGateState, fWeightsUpdateGateState, fWeightsCandidateState,
797 input, input_gradient, fResetGateAfter);
798}
799
800
801//______________________________________________________________________________
802template <typename Architecture_t>
804-> void
805{
806 DNN::initialize<Architecture_t>(this->GetState(), DNN::EInitialization::kZero);
807}
808
809 //______________________________________________________________________________
810template<typename Architecture_t>
812-> void
813{
814 std::cout << " GRU Layer: \t ";
815 std::cout << " (NInput = " << this->GetInputSize(); // input size
816 std::cout << ", NState = " << this->GetStateSize(); // hidden state size
817 std::cout << ", NTime = " << this->GetTimeSteps() << " )"; // time size
818 std::cout << "\tOutput = ( " << this->GetOutput().GetFirstSize() << " , " << this->GetOutput()[0].GetNrows() << " , " << this->GetOutput()[0].GetNcols() << " )\n";
819}
820
821//______________________________________________________________________________
822template <typename Architecture_t>
824-> void
825{
826 auto layerxml = gTools().xmlengine().NewChild(parent, nullptr, "GRULayer");
827
828 // Write all other info like outputSize, cellSize, inputSize, timeSteps, rememberState
829 gTools().xmlengine().NewAttr(layerxml, nullptr, "StateSize", gTools().StringFromInt(this->GetStateSize()));
830 gTools().xmlengine().NewAttr(layerxml, nullptr, "InputSize", gTools().StringFromInt(this->GetInputSize()));
831 gTools().xmlengine().NewAttr(layerxml, nullptr, "TimeSteps", gTools().StringFromInt(this->GetTimeSteps()));
832 gTools().xmlengine().NewAttr(layerxml, nullptr, "RememberState", gTools().StringFromInt(this->DoesRememberState()));
833 gTools().xmlengine().NewAttr(layerxml, nullptr, "ReturnSequence", gTools().StringFromInt(this->DoesReturnSequence()));
834 gTools().xmlengine().NewAttr(layerxml, nullptr, "ResetGateAfter", gTools().StringFromInt(this->fResetGateAfter));
835
836 // write weights and bias matrices
837 this->WriteMatrixToXML(layerxml, "ResetWeights", this->GetWeightsAt(0));
838 this->WriteMatrixToXML(layerxml, "ResetStateWeights", this->GetWeightsAt(1));
839 this->WriteMatrixToXML(layerxml, "ResetBiases", this->GetBiasesAt(0));
840 this->WriteMatrixToXML(layerxml, "UpdateWeights", this->GetWeightsAt(2));
841 this->WriteMatrixToXML(layerxml, "UpdateStateWeights", this->GetWeightsAt(3));
842 this->WriteMatrixToXML(layerxml, "UpdateBiases", this->GetBiasesAt(1));
843 this->WriteMatrixToXML(layerxml, "CandidateWeights", this->GetWeightsAt(4));
844 this->WriteMatrixToXML(layerxml, "CandidateStateWeights", this->GetWeightsAt(5));
845 this->WriteMatrixToXML(layerxml, "CandidateBiases", this->GetBiasesAt(2));
846}
847
848 //______________________________________________________________________________
849template <typename Architecture_t>
851-> void
852{
853 // Read weights and biases
854 this->ReadMatrixXML(parent, "ResetWeights", this->GetWeightsAt(0));
855 this->ReadMatrixXML(parent, "ResetStateWeights", this->GetWeightsAt(1));
856 this->ReadMatrixXML(parent, "ResetBiases", this->GetBiasesAt(0));
857 this->ReadMatrixXML(parent, "UpdateWeights", this->GetWeightsAt(2));
858 this->ReadMatrixXML(parent, "UpdateStateWeights", this->GetWeightsAt(3));
859 this->ReadMatrixXML(parent, "UpdateBiases", this->GetBiasesAt(1));
860 this->ReadMatrixXML(parent, "CandidateWeights", this->GetWeightsAt(4));
861 this->ReadMatrixXML(parent, "CandidateStateWeights", this->GetWeightsAt(5));
862 this->ReadMatrixXML(parent, "CandidateBiases", this->GetBiasesAt(2));
863}
864
865} // namespace GRU
866} // namespace DNN
867} // namespace TMVA
868
869#endif // GRU_LAYER_H
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
const Matrix_t & GetWeightsCandidate() const
Definition GRULayer.h:226
Matrix_t & GetWeightsCandidateStateGradients()
Definition GRULayer.h:288
typename Architecture_t::RecurrentDescriptor_t LayerDescriptor_t
Definition GRULayer.h:67
void Forward(Tensor_t &input, bool isTraining=true) override
Computes the next hidden state and next cell state with given input matrix.
Definition GRULayer.h:496
Matrix_t & fResetBiasGradients
Gradients w.r.t the reset gate - bias weights.
Definition GRULayer.h:116
std::vector< Matrix_t > & GetUpdateGateTensor()
Definition GRULayer.h:256
typename Architecture_t::Tensor_t Tensor_t
Definition GRULayer.h:65
std::vector< Matrix_t > reset_gate_value
Reset gate value for every time step.
Definition GRULayer.h:106
Matrix_t & CellBackward(Matrix_t &state_gradients_backward, const Matrix_t &precStateActivations, const Matrix_t &reset_gate, const Matrix_t &update_gate, const Matrix_t &candidate_gate, const Matrix_t &input, Matrix_t &input_gradient, Matrix_t &dr, Matrix_t &du, Matrix_t &dc)
Backward for a single time unit a the corresponding call to Forward(...).
Definition GRULayer.h:778
size_t fStateSize
Hidden state size for GRU.
Definition GRULayer.h:77
const Matrix_t & GetWeightsResetGradients() const
Definition GRULayer.h:273
const Matrix_t & GetUpdateBiasGradients() const
Definition GRULayer.h:283
bool fReturnSequence
Return in output full sequence or just last element.
Definition GRULayer.h:81
const Matrix_t & GetWeightsResetStateGradients() const
Definition GRULayer.h:275
std::vector< Matrix_t > fDerivativesReset
First fDerivatives of the activations reset gate.
Definition GRULayer.h:110
const Tensor_t & GetWeightsTensor() const
Definition GRULayer.h:293
std::vector< Matrix_t > & GetResetGateTensor()
Definition GRULayer.h:252
Matrix_t & GetWeightsUpdateGateState()
Definition GRULayer.h:234
const std::vector< Matrix_t > & GetCandidateGateTensor() const
Definition GRULayer.h:259
const Matrix_t & GetUpdateDerivativesAt(size_t i) const
Definition GRULayer.h:244
Matrix_t & GetWeightsUpdateStateGradients()
Definition GRULayer.h:282
void Print() const override
Prints the info about the layer.
Definition GRULayer.h:811
size_t GetInputSize() const
Getters.
Definition GRULayer.h:202
Matrix_t fState
Hidden state of GRU.
Definition GRULayer.h:90
Matrix_t & GetWeightsResetGradients()
Definition GRULayer.h:274
Tensor_t & GetWeightGradientsTensor()
Definition GRULayer.h:294
const Matrix_t & GetCandidateBias() const
Definition GRULayer.h:270
std::vector< Matrix_t > update_gate_value
Update gate value for every time step.
Definition GRULayer.h:107
Tensor_t fX
cached input tensor as T x B x I
Definition GRULayer.h:131
Matrix_t & GetCandidateGateTensorAt(size_t i)
Definition GRULayer.h:262
Matrix_t & GetResetBiasGradients()
Definition GRULayer.h:278
void AddWeightsXMLTo(void *parent) override
Writes the information and the weights about the layer in an XML node.
Definition GRULayer.h:823
Matrix_t & GetWeightsResetGateState()
Definition GRULayer.h:232
DNN::EActivationFunction fF1
Activation function: sigmoid.
Definition GRULayer.h:84
const Matrix_t & GetWeightsUpdateGate() const
Definition GRULayer.h:228
const std::vector< Matrix_t > & GetDerivativesReset() const
Definition GRULayer.h:238
const Matrix_t & GetUpdateGateBias() const
Definition GRULayer.h:268
Matrix_t & fWeightsResetGradients
Gradients w.r.t the reset gate - input weights.
Definition GRULayer.h:114
std::vector< Matrix_t > & GetDerivativesUpdate()
Definition GRULayer.h:243
Matrix_t & fCandidateBiasGradients
Gradients w.r.t the candidate gate - bias weights.
Definition GRULayer.h:122
Matrix_t & fCandidateBias
Candidate Gate bias.
Definition GRULayer.h:103
Matrix_t & GetUpdateGateTensorAt(size_t i)
Definition GRULayer.h:258
DNN::EActivationFunction fF2
Activation function: tanh.
Definition GRULayer.h:85
const Matrix_t & GetWeightsUpdateGradients() const
Definition GRULayer.h:279
Matrix_t & GetWeightsCandidateGradients()
Definition GRULayer.h:286
Matrix_t & fWeightsUpdateStateGradients
Gradients w.r.t the update gate - hidden state weights.
Definition GRULayer.h:118
Matrix_t & fWeightsUpdateGradients
Gradients w.r.t the update gate - input weights.
Definition GRULayer.h:117
size_t fTimeSteps
Timesteps for GRU.
Definition GRULayer.h:78
std::vector< Matrix_t > fDerivativesCandidate
First fDerivatives of the activations candidate gate.
Definition GRULayer.h:112
const Tensor_t & GetWeightGradientsTensor() const
Definition GRULayer.h:295
typename Architecture_t::FilterDescriptor_t WeightsDescriptor_t
Definition GRULayer.h:68
Tensor_t fWeightGradientsTensor
Tensor for all weight gradients.
Definition GRULayer.h:128
Matrix_t & fUpdateBiasGradients
Gradients w.r.t the update gate - bias weights.
Definition GRULayer.h:119
Matrix_t & GetWeightsResetStateGradients()
Definition GRULayer.h:276
std::vector< Matrix_t > & GetCandidateGateTensor()
Definition GRULayer.h:260
Matrix_t & fWeightsResetGate
Reset Gate weights for input, fWeights[0].
Definition GRULayer.h:93
const Matrix_t & GetResetDerivativesAt(size_t i) const
Definition GRULayer.h:240
Matrix_t & GetWeightsUpdateGate()
Definition GRULayer.h:229
typename Architecture_t::Matrix_t Matrix_t
Definition GRULayer.h:63
const Matrix_t & GetCandidateGateTensorAt(size_t i) const
Definition GRULayer.h:261
Matrix_t & GetWeightsCandidateState()
Definition GRULayer.h:236
const Matrix_t & GetCandidateBiasGradients() const
Definition GRULayer.h:289
Matrix_t & GetResetGateTensorAt(size_t i)
Definition GRULayer.h:254
Matrix_t & fResetGateBias
Input Gate bias.
Definition GRULayer.h:95
const std::vector< Matrix_t > & GetResetGateTensor() const
Definition GRULayer.h:251
Matrix_t fCell
Empty matrix for GRU.
Definition GRULayer.h:124
std::vector< Matrix_t > candidate_gate_value
Candidate gate value for every time step.
Definition GRULayer.h:108
typename Architecture_t::Scalar_t Scalar_t
Definition GRULayer.h:64
const Matrix_t & GetWeigthsUpdateStateGradients() const
Definition GRULayer.h:281
const Matrix_t & GetCandidateValue() const
Definition GRULayer.h:214
Matrix_t & GetCandidateBiasGradients()
Definition GRULayer.h:290
Matrix_t & fWeightsCandidateStateGradients
Gradients w.r.t the candidate gate - hidden state weights.
Definition GRULayer.h:121
const std::vector< Matrix_t > & GetDerivativesUpdate() const
Definition GRULayer.h:242
const Matrix_t & GetCell() const
Definition GRULayer.h:221
void Initialize() override
Initialize the weights according to the given initialization method.
Definition GRULayer.h:411
void UpdateGate(const Matrix_t &input, Matrix_t &df)
Forgets the past values (NN with Sigmoid)
Definition GRULayer.h:443
const Matrix_t & GetCandidateDerivativesAt(size_t i) const
Definition GRULayer.h:248
Matrix_t fResetValue
Computed reset gate values.
Definition GRULayer.h:87
DNN::EActivationFunction GetActivationFunctionF2() const
Definition GRULayer.h:210
typename Architecture_t::RNNWorkspace_t RNNWorkspace_t
Definition GRULayer.h:72
Matrix_t fUpdateValue
Computed forget gate values.
Definition GRULayer.h:88
const Matrix_t & GetResetBiasGradients() const
Definition GRULayer.h:277
bool fResetGateAfter
GRU variant to Apply the reset gate multiplication afterwards (used by cuDNN)
Definition GRULayer.h:82
const Matrix_t & GetWeightsCandidateGradients() const
Definition GRULayer.h:285
DNN::EActivationFunction GetActivationFunctionF1() const
Definition GRULayer.h:209
Matrix_t & GetUpdateBiasGradients()
Definition GRULayer.h:284
const Matrix_t & GetUpdateGateTensorAt(size_t i) const
Definition GRULayer.h:257
Matrix_t & fWeightsResetGateState
Input Gate weights for prev state, fWeights[1].
Definition GRULayer.h:94
Matrix_t & fWeightsUpdateGateState
Update Gate weights for prev state, fWeights[3].
Definition GRULayer.h:98
const std::vector< Matrix_t > & GetDerivativesCandidate() const
Definition GRULayer.h:246
Tensor_t fWeightsTensor
Tensor for all weights.
Definition GRULayer.h:127
typename Architecture_t::RNNDescriptors_t RNNDescriptors_t
Definition GRULayer.h:73
const Matrix_t & GetResetGateBias() const
Definition GRULayer.h:266
Matrix_t & GetResetDerivativesAt(size_t i)
Definition GRULayer.h:241
const Matrix_t & GetUpdateGateValue() const
Definition GRULayer.h:216
const Matrix_t & GetResetGateTensorAt(size_t i) const
Definition GRULayer.h:253
TDescriptors * fDescriptors
Keeps all the RNN descriptors.
Definition GRULayer.h:136
void CellForward(Matrix_t &updateGateValues, Matrix_t &candidateValues)
Forward for a single cell (time unit)
Definition GRULayer.h:593
Matrix_t & GetWeightsUpdateGradients()
Definition GRULayer.h:280
Matrix_t & fWeightsResetStateGradients
Gradients w.r.t the reset gate - hidden state weights.
Definition GRULayer.h:115
Matrix_t & fWeightsCandidateState
Candidate Gate weights for prev state, fWeights[5].
Definition GRULayer.h:102
std::vector< Matrix_t > & GetDerivativesReset()
Definition GRULayer.h:239
Matrix_t & fUpdateGateBias
Update Gate bias.
Definition GRULayer.h:99
void Backward(Tensor_t &gradients_backward, const Tensor_t &activations_backward) override
Backpropagates the error.
Definition GRULayer.h:613
const Matrix_t & GetWeightsCandidateStateGradients() const
Definition GRULayer.h:287
void ResetGate(const Matrix_t &input, Matrix_t &di)
Decides the values we'll update (NN with Sigmoid)
Definition GRULayer.h:425
const Matrix_t & GetWeightsResetGate() const
Definition GRULayer.h:224
Tensor_t fDx
cached gradient on the input (output of backward) as T x B x I
Definition GRULayer.h:133
typename Architecture_t::TensorDescriptor_t TensorDescriptor_t
Definition GRULayer.h:69
bool fRememberState
Remember state in next pass.
Definition GRULayer.h:80
Matrix_t & fWeightsCandidate
Candidate Gate weights for input, fWeights[4].
Definition GRULayer.h:101
Matrix_t & fWeightsCandidateGradients
Gradients w.r.t the candidate gate - input weights.
Definition GRULayer.h:120
const Matrix_t & GetWeightsCandidateState() const
Definition GRULayer.h:235
void ReadWeightsFromXML(void *parent) override
Read the information and the weights about the layer from XML node.
Definition GRULayer.h:850
const std::vector< Matrix_t > & GetUpdateGateTensor() const
Definition GRULayer.h:255
const Matrix_t & GetResetGateValue() const
Definition GRULayer.h:212
void Update(const Scalar_t learningRate)
Tensor_t fY
cached output tensor as T x B x S
Definition GRULayer.h:132
Matrix_t fCandidateValue
Computed candidate values.
Definition GRULayer.h:89
const Matrix_t & GetState() const
Definition GRULayer.h:219
void InitState(DNN::EInitialization m=DNN::EInitialization::kZero)
Initialize the hidden state and cell state method.
Definition GRULayer.h:803
Tensor_t fDy
cached activation gradient (input of backward) as T x B x S
Definition GRULayer.h:134
Matrix_t & GetCandidateDerivativesAt(size_t i)
Definition GRULayer.h:249
std::vector< Matrix_t > fDerivativesUpdate
First fDerivatives of the activations update gate.
Definition GRULayer.h:111
const Matrix_t & GetWeightsUpdateGateState() const
Definition GRULayer.h:233
std::vector< Matrix_t > & GetDerivativesCandidate()
Definition GRULayer.h:247
const Matrix_t & GetWeightsResetGateState() const
Definition GRULayer.h:231
void CandidateValue(const Matrix_t &input, Matrix_t &dc)
Decides the new candidate values (NN with Tanh)
Definition GRULayer.h:461
TBasicGRULayer(size_t batchSize, size_t stateSize, size_t inputSize, size_t timeSteps, bool rememberState=false, bool returnSequence=false, bool resetGateAfter=false, DNN::EActivationFunction f1=DNN::EActivationFunction::kSigmoid, DNN::EActivationFunction f2=DNN::EActivationFunction::kTanh, bool training=true, DNN::EInitialization fA=DNN::EInitialization::kZero)
Constructor.
Definition GRULayer.h:310
Matrix_t & GetUpdateDerivativesAt(size_t i)
Definition GRULayer.h:245
Matrix_t & fWeightsUpdateGate
Update Gate weights for input, fWeights[2].
Definition GRULayer.h:97
typename Architecture_t::DropoutDescriptor_t HelperDescriptor_t
Definition GRULayer.h:70
Generic General Layer class.
virtual void Initialize()
Initialize the weights and biases according to the given initialization method.
size_t GetInputWidth() const
TXMLEngine & xmlengine()
Definition Tools.h:262
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TF1 * f1
Definition legend1.C:11
EActivationFunction
Enum that represents layer activation functions.
Definition Functions.h:32
create variable transformations
Tools & gTools()
TMarker m
Definition textangle.C:8