Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
LSTMLayer.h
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn/lstm:$Id$
2// Author: Surya S Dwivedi 27/05/19
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : BasicLSTMLayer *
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 LSTM layer. LSTM is a variant of vanilla
27// RNN which is capable of learning long range dependencies.
28//////////////////////////////////////////////////////////////////////
29
30#ifndef TMVA_DNN_LSTM_LAYER
31#define TMVA_DNN_LSTM_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 LSTM Layer
52//______________________________________________________________________________
53
54/** \class BasicLSTMLayer
55 Generic implementation
56*/
57template<typename Architecture_t>
58 class TBasicLSTMLayer : 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 LSTM
78 size_t fCellSize; ///< Cell state size of LSTM
79 size_t fTimeSteps; ///< Timesteps for LSTM
80
81 bool fRememberState; ///< Remember state in next pass
82 bool fReturnSequence = false; ///< Return in output full sequence or just last element
83
84 DNN::EActivationFunction fF1; ///< Activation function: sigmoid
85 DNN::EActivationFunction fF2; ///< Activation function: tanh
86
87 Matrix_t fInputValue; ///< Computed input gate values
88 Matrix_t fCandidateValue; ///< Computed candidate values
89 Matrix_t fForgetValue; ///< Computed forget gate values
90 Matrix_t fOutputValue; ///< Computed output gate values
91 Matrix_t fState; ///< Hidden state of LSTM
92 Matrix_t fCell; ///< Cell state of LSTM
93
94 Matrix_t &fWeightsInputGate; ///< Input Gate weights for input, fWeights[0]
95 Matrix_t &fWeightsInputGateState; ///< Input Gate weights for prev state, fWeights[1]
96 Matrix_t &fInputGateBias; ///< Input Gate bias
97
98 Matrix_t &fWeightsForgetGate; ///< Forget Gate weights for input, fWeights[2]
99 Matrix_t &fWeightsForgetGateState; ///< Forget Gate weights for prev state, fWeights[3]
100 Matrix_t &fForgetGateBias; ///< Forget Gate bias
101
102 Matrix_t &fWeightsCandidate; ///< Candidate Gate weights for input, fWeights[4]
103 Matrix_t &fWeightsCandidateState; ///< Candidate Gate weights for prev state, fWeights[5]
104 Matrix_t &fCandidateBias; ///< Candidate Gate bias
105
106 Matrix_t &fWeightsOutputGate; ///< Output Gate weights for input, fWeights[6]
107 Matrix_t &fWeightsOutputGateState; ///< Output Gate weights for prev state, fWeights[7]
108 Matrix_t &fOutputGateBias; ///< Output Gate bias
109
110 std::vector<Matrix_t> input_gate_value; ///< input gate value for every time step
111 std::vector<Matrix_t> forget_gate_value; ///< forget gate value for every time step
112 std::vector<Matrix_t> candidate_gate_value; ///< candidate gate value for every time step
113 std::vector<Matrix_t> output_gate_value; ///< output gate value for every time step
114 std::vector<Matrix_t> cell_value; ///< cell value for every time step
115 std::vector<Matrix_t> fDerivativesInput; ///< First fDerivatives of the activations input gate
116 std::vector<Matrix_t> fDerivativesForget; ///< First fDerivatives of the activations forget gate
117 std::vector<Matrix_t> fDerivativesCandidate; ///< First fDerivatives of the activations candidate gate
118 std::vector<Matrix_t> fDerivativesOutput; ///< First fDerivatives of the activations output gate
119
120 Matrix_t &fWeightsInputGradients; ///< Gradients w.r.t the input gate - input weights
121 Matrix_t &fWeightsInputStateGradients; ///< Gradients w.r.t the input gate - hidden state weights
122 Matrix_t &fInputBiasGradients; ///< Gradients w.r.t the input gate - bias weights
123 Matrix_t &fWeightsForgetGradients; ///< Gradients w.r.t the forget gate - input weights
124 Matrix_t &fWeightsForgetStateGradients; ///< Gradients w.r.t the forget gate - hidden state weights
125 Matrix_t &fForgetBiasGradients; ///< Gradients w.r.t the forget gate - bias weights
126 Matrix_t &fWeightsCandidateGradients; ///< Gradients w.r.t the candidate gate - input weights
127 Matrix_t &fWeightsCandidateStateGradients; ///< Gradients w.r.t the candidate gate - hidden state weights
128 Matrix_t &fCandidateBiasGradients; ///< Gradients w.r.t the candidate gate - bias weights
129 Matrix_t &fWeightsOutputGradients; ///< Gradients w.r.t the output gate - input weights
130 Matrix_t &fWeightsOutputStateGradients; ///< Gradients w.r.t the output gate - hidden state weights
131 Matrix_t &fOutputBiasGradients; ///< Gradients w.r.t the output gate - bias weights
132
133 // Tensor representing all weights (used by cuDNN)
134 Tensor_t fWeightsTensor; ///< Tensor for all weights
135 Tensor_t fWeightGradientsTensor; ///< Tensor for all weight gradients
136
137 // tensors used internally for the forward and backward pass
138 Tensor_t fX; ///< cached input tensor as T x B x I
139 Tensor_t fY; ///< cached output tensor as T x B x S
140 Tensor_t fDx; ///< cached gradient on the input (output of backward) as T x B x I
141 Tensor_t fDy; ///< cached activation gradient (input of backward) as T x B x S
142
143 TDescriptors *fDescriptors = nullptr; ///< Keeps all the RNN descriptors
144 TWorkspace *fWorkspace = nullptr; // workspace needed for GPU computation (CudNN)
145
146public:
147
148 /*! Constructor */
149 TBasicLSTMLayer(size_t batchSize, size_t stateSize, size_t inputSize, size_t timeSteps, bool rememberState = false,
150 bool returnSequence = false,
154
155 /*! Copy Constructor */
157
158 /*! Initialize the weights according to the given initialization
159 ** method. */
160 void Initialize() override;
161
162 /*! Initialize the hidden state and cell state method. */
164
165 /*! Computes the next hidden state
166 * and next cell state with given input matrix. */
167 void Forward(Tensor_t &input, bool isTraining = true) override;
168
169 /*! Forward for a single cell (time unit) */
172
173 /*! Backpropagates the error. Must only be called directly at the corresponding
174 * call to Forward(...). */
176 const Tensor_t &activations_backward) override;
177
178 /* Updates weights and biases, given the learning rate */
179 void Update(const Scalar_t learningRate);
180
181 /*! Backward for a single time unit
182 * a the corresponding call to Forward(...). */
186 const Matrix_t & input_gate, const Matrix_t & forget_gate,
189 Matrix_t &di, Matrix_t &df, Matrix_t &dc, Matrix_t &dout, size_t t);
190
191 /*! Decides the values we'll update (NN with Sigmoid) */
192 void InputGate(const Matrix_t &input, Matrix_t &di);
193
194 /*! Forgets the past values (NN with Sigmoid) */
195 void ForgetGate(const Matrix_t &input, Matrix_t &df);
196
197 /*! Decides the new candidate values (NN with Tanh) */
198 void CandidateValue(const Matrix_t &input, Matrix_t &dc);
199
200 /*! Computes output values (NN with Sigmoid) */
201 void OutputGate(const Matrix_t &input, Matrix_t &dout);
202
203 /*! Prints the info about the layer */
204 void Print() const override;
205
206 /*! Writes the information and the weights about the layer in an XML node. */
207 void AddWeightsXMLTo(void *parent) override;
208
209 /*! Read the information and the weights about the layer from XML node. */
210 void ReadWeightsFromXML(void *parent) override;
211
212 /*! Getters */
213 size_t GetInputSize() const { return this->GetInputWidth(); }
214 size_t GetTimeSteps() const { return fTimeSteps; }
215 size_t GetStateSize() const { return fStateSize; }
216 size_t GetCellSize() const { return fCellSize; }
217
218 inline bool DoesRememberState() const { return fRememberState; }
219 inline bool DoesReturnSequence() const { return fReturnSequence; }
220
223
224 const Matrix_t & GetInputGateValue() const { return fInputValue; }
226 const Matrix_t & GetCandidateValue() const { return fCandidateValue; }
228 const Matrix_t & GetForgetGateValue() const { return fForgetValue; }
230 const Matrix_t & GetOutputGateValue() const { return fOutputValue; }
232
233 const Matrix_t & GetState() const { return fState; }
234 Matrix_t & GetState() { return fState; }
235 const Matrix_t & GetCell() const { return fCell; }
236 Matrix_t & GetCell() { return fCell; }
237
254
255 const std::vector<Matrix_t> & GetDerivativesInput() const { return fDerivativesInput; }
256 std::vector<Matrix_t> & GetDerivativesInput() { return fDerivativesInput; }
257 const Matrix_t & GetInputDerivativesAt(size_t i) const { return fDerivativesInput[i]; }
259 const std::vector<Matrix_t> & GetDerivativesForget() const { return fDerivativesForget; }
260 std::vector<Matrix_t> & GetDerivativesForget() { return fDerivativesForget; }
261 const Matrix_t & GetForgetDerivativesAt(size_t i) const { return fDerivativesForget[i]; }
263 const std::vector<Matrix_t> & GetDerivativesCandidate() const { return fDerivativesCandidate; }
264 std::vector<Matrix_t> & GetDerivativesCandidate() { return fDerivativesCandidate; }
265 const Matrix_t & GetCandidateDerivativesAt(size_t i) const { return fDerivativesCandidate[i]; }
267 const std::vector<Matrix_t> & GetDerivativesOutput() const { return fDerivativesOutput; }
268 std::vector<Matrix_t> & GetDerivativesOutput() { return fDerivativesOutput; }
269 const Matrix_t & GetOutputDerivativesAt(size_t i) const { return fDerivativesOutput[i]; }
271
272 const std::vector<Matrix_t> & GetInputGateTensor() const { return input_gate_value; }
273 std::vector<Matrix_t> & GetInputGateTensor() { return input_gate_value; }
274 const Matrix_t & GetInputGateTensorAt(size_t i) const { return input_gate_value[i]; }
276 const std::vector<Matrix_t> & GetForgetGateTensor() const { return forget_gate_value; }
277 std::vector<Matrix_t> & GetForgetGateTensor() { return forget_gate_value; }
278 const Matrix_t & GetForgetGateTensorAt(size_t i) const { return forget_gate_value[i]; }
280 const std::vector<Matrix_t> & GetCandidateGateTensor() const { return candidate_gate_value; }
281 std::vector<Matrix_t> & GetCandidateGateTensor() { return candidate_gate_value; }
282 const Matrix_t & GetCandidateGateTensorAt(size_t i) const { return candidate_gate_value[i]; }
284 const std::vector<Matrix_t> & GetOutputGateTensor() const { return output_gate_value; }
285 std::vector<Matrix_t> & GetOutputGateTensor() { return output_gate_value; }
286 const Matrix_t & GetOutputGateTensorAt(size_t i) const { return output_gate_value[i]; }
288 const std::vector<Matrix_t> & GetCellTensor() const { return cell_value; }
289 std::vector<Matrix_t> & GetCellTensor() { return cell_value; }
290 const Matrix_t & GetCellTensorAt(size_t i) const { return cell_value[i]; }
291 Matrix_t & GetCellTensorAt(size_t i) { return cell_value[i]; }
292
293 const Matrix_t & GetInputGateBias() const { return fInputGateBias; }
295 const Matrix_t & GetForgetGateBias() const { return fForgetGateBias; }
297 const Matrix_t & GetCandidateBias() const { return fCandidateBias; }
299 const Matrix_t & GetOutputGateBias() const { return fOutputGateBias; }
325
327 const Tensor_t &GetWeightsTensor() const { return fWeightsTensor; }
330
331 Tensor_t &GetX() { return fX; }
332 Tensor_t &GetY() { return fY; }
333 Tensor_t &GetDX() { return fDx; }
334 Tensor_t &GetDY() { return fDy; }
335};
336
337//______________________________________________________________________________
338//
339// Basic LSTM-Layer Implementation
340//______________________________________________________________________________
341
342template <typename Architecture_t>
343TBasicLSTMLayer<Architecture_t>::TBasicLSTMLayer(size_t batchSize, size_t stateSize, size_t inputSize, size_t timeSteps,
345 DNN::EActivationFunction f2, bool /* training */,
347 : VGeneralLayer<Architecture_t>(
348 batchSize, 1, timeSteps, inputSize, 1, (returnSequence) ? timeSteps : 1, stateSize, 8,
350 {inputSize, inputSize, inputSize, inputSize, stateSize, stateSize, stateSize, stateSize}, 4,
351 {stateSize, stateSize, stateSize, stateSize}, {1, 1, 1, 1}, batchSize, (returnSequence) ? timeSteps : 1,
352 stateSize, fA),
353 fStateSize(stateSize), fCellSize(stateSize), fTimeSteps(timeSteps), fRememberState(rememberState),
354 fReturnSequence(returnSequence), fF1(f1), fF2(f2), fInputValue(batchSize, stateSize),
355 fCandidateValue(batchSize, stateSize), fForgetValue(batchSize, stateSize), fOutputValue(batchSize, stateSize),
356 fState(batchSize, stateSize), fCell(batchSize, stateSize), fWeightsInputGate(this->GetWeightsAt(0)),
357 fWeightsInputGateState(this->GetWeightsAt(4)), fInputGateBias(this->GetBiasesAt(0)),
358 fWeightsForgetGate(this->GetWeightsAt(1)), fWeightsForgetGateState(this->GetWeightsAt(5)),
359 fForgetGateBias(this->GetBiasesAt(1)), fWeightsCandidate(this->GetWeightsAt(2)),
360 fWeightsCandidateState(this->GetWeightsAt(6)), fCandidateBias(this->GetBiasesAt(2)),
361 fWeightsOutputGate(this->GetWeightsAt(3)), fWeightsOutputGateState(this->GetWeightsAt(7)),
362 fOutputGateBias(this->GetBiasesAt(3)), fWeightsInputGradients(this->GetWeightGradientsAt(0)),
363 fWeightsInputStateGradients(this->GetWeightGradientsAt(4)), fInputBiasGradients(this->GetBiasGradientsAt(0)),
364 fWeightsForgetGradients(this->GetWeightGradientsAt(1)),
365 fWeightsForgetStateGradients(this->GetWeightGradientsAt(5)), fForgetBiasGradients(this->GetBiasGradientsAt(1)),
366 fWeightsCandidateGradients(this->GetWeightGradientsAt(2)),
367 fWeightsCandidateStateGradients(this->GetWeightGradientsAt(6)),
368 fCandidateBiasGradients(this->GetBiasGradientsAt(2)), fWeightsOutputGradients(this->GetWeightGradientsAt(3)),
369 fWeightsOutputStateGradients(this->GetWeightGradientsAt(7)), fOutputBiasGradients(this->GetBiasGradientsAt(3))
370{
371 for (size_t i = 0; i < timeSteps; ++i) {
372 fDerivativesInput.emplace_back(batchSize, stateSize);
373 fDerivativesForget.emplace_back(batchSize, stateSize);
374 fDerivativesCandidate.emplace_back(batchSize, stateSize);
375 fDerivativesOutput.emplace_back(batchSize, stateSize);
376 input_gate_value.emplace_back(batchSize, stateSize);
377 forget_gate_value.emplace_back(batchSize, stateSize);
378 candidate_gate_value.emplace_back(batchSize, stateSize);
379 output_gate_value.emplace_back(batchSize, stateSize);
380 cell_value.emplace_back(batchSize, stateSize);
381 }
382 Architecture_t::InitializeLSTMTensors(this);
383}
384
385 //______________________________________________________________________________
386template <typename Architecture_t>
388 : VGeneralLayer<Architecture_t>(layer),
389 fStateSize(layer.fStateSize),
390 fCellSize(layer.fCellSize),
391 fTimeSteps(layer.fTimeSteps),
392 fRememberState(layer.fRememberState),
393 fReturnSequence(layer.fReturnSequence),
394 fF1(layer.GetActivationFunctionF1()),
395 fF2(layer.GetActivationFunctionF2()),
396 fInputValue(layer.GetBatchSize(), layer.GetStateSize()),
397 fCandidateValue(layer.GetBatchSize(), layer.GetStateSize()),
398 fForgetValue(layer.GetBatchSize(), layer.GetStateSize()),
399 fOutputValue(layer.GetBatchSize(), layer.GetStateSize()),
400 fState(layer.GetBatchSize(), layer.GetStateSize()),
401 fCell(layer.GetBatchSize(), layer.GetCellSize()),
402 fWeightsInputGate(this->GetWeightsAt(0)),
403 fWeightsInputGateState(this->GetWeightsAt(4)),
404 fInputGateBias(this->GetBiasesAt(0)),
405 fWeightsForgetGate(this->GetWeightsAt(1)),
406 fWeightsForgetGateState(this->GetWeightsAt(5)),
407 fForgetGateBias(this->GetBiasesAt(1)),
408 fWeightsCandidate(this->GetWeightsAt(2)),
409 fWeightsCandidateState(this->GetWeightsAt(6)),
410 fCandidateBias(this->GetBiasesAt(2)),
411 fWeightsOutputGate(this->GetWeightsAt(3)),
412 fWeightsOutputGateState(this->GetWeightsAt(7)),
413 fOutputGateBias(this->GetBiasesAt(3)),
414 fWeightsInputGradients(this->GetWeightGradientsAt(0)),
415 fWeightsInputStateGradients(this->GetWeightGradientsAt(4)),
416 fInputBiasGradients(this->GetBiasGradientsAt(0)),
417 fWeightsForgetGradients(this->GetWeightGradientsAt(1)),
418 fWeightsForgetStateGradients(this->GetWeightGradientsAt(5)),
419 fForgetBiasGradients(this->GetBiasGradientsAt(1)),
420 fWeightsCandidateGradients(this->GetWeightGradientsAt(2)),
421 fWeightsCandidateStateGradients(this->GetWeightGradientsAt(6)),
422 fCandidateBiasGradients(this->GetBiasGradientsAt(2)),
423 fWeightsOutputGradients(this->GetWeightGradientsAt(3)),
424 fWeightsOutputStateGradients(this->GetWeightGradientsAt(7)),
425 fOutputBiasGradients(this->GetBiasGradientsAt(3))
426{
427 for (size_t i = 0; i < fTimeSteps; ++i) {
428 fDerivativesInput.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
429 Architecture_t::Copy(fDerivativesInput[i], layer.GetInputDerivativesAt(i));
430
431 fDerivativesForget.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
432 Architecture_t::Copy(fDerivativesForget[i], layer.GetForgetDerivativesAt(i));
433
434 fDerivativesCandidate.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
435 Architecture_t::Copy(fDerivativesCandidate[i], layer.GetCandidateDerivativesAt(i));
436
437 fDerivativesOutput.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
438 Architecture_t::Copy(fDerivativesOutput[i], layer.GetOutputDerivativesAt(i));
439
440 input_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
441 Architecture_t::Copy(input_gate_value[i], layer.GetInputGateTensorAt(i));
442
443 forget_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
444 Architecture_t::Copy(forget_gate_value[i], layer.GetForgetGateTensorAt(i));
445
446 candidate_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
447 Architecture_t::Copy(candidate_gate_value[i], layer.GetCandidateGateTensorAt(i));
448
449 output_gate_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
450 Architecture_t::Copy(output_gate_value[i], layer.GetOutputGateTensorAt(i));
451
452 cell_value.emplace_back(layer.GetBatchSize(), layer.GetStateSize());
453 Architecture_t::Copy(cell_value[i], layer.GetCellTensorAt(i));
454 }
455
456 // Gradient matrices not copied
457 Architecture_t::Copy(fState, layer.GetState());
458 Architecture_t::Copy(fCell, layer.GetCell());
459
460 // Copy each gate values.
461 Architecture_t::Copy(fInputValue, layer.GetInputGateValue());
462 Architecture_t::Copy(fCandidateValue, layer.GetCandidateValue());
463 Architecture_t::Copy(fForgetValue, layer.GetForgetGateValue());
464 Architecture_t::Copy(fOutputValue, layer.GetOutputGateValue());
465
466 Architecture_t::InitializeLSTMTensors(this);
467}
468
469//______________________________________________________________________________
470template <typename Architecture_t>
472{
474
475 Architecture_t::InitializeLSTMDescriptors(fDescriptors, this);
476 Architecture_t::InitializeLSTMWorkspace(fWorkspace, fDescriptors, this);
477}
478
479//______________________________________________________________________________
480template <typename Architecture_t>
482-> void
483{
484 /*! Computes input gate values according to equation:
485 * input = act(W_input . input + W_state . state + bias)
486 * activation function: sigmoid. */
487 const DNN::EActivationFunction fInp = this->GetActivationFunctionF1();
488 Matrix_t tmpState(fInputValue.GetNrows(), fInputValue.GetNcols());
489 Architecture_t::MultiplyTranspose(tmpState, fState, fWeightsInputGateState);
490 Architecture_t::MultiplyTranspose(fInputValue, input, fWeightsInputGate);
491 Architecture_t::ScaleAdd(fInputValue, tmpState);
492 Architecture_t::AddRowWise(fInputValue, fInputGateBias);
493 DNN::evaluateDerivativeMatrix<Architecture_t>(di, fInp, fInputValue);
494 DNN::evaluateMatrix<Architecture_t>(fInputValue, fInp);
495}
496
497 //______________________________________________________________________________
498template <typename Architecture_t>
500-> void
501{
502 /*! Computes forget gate values according to equation:
503 * forget = act(W_input . input + W_state . state + bias)
504 * activation function: sigmoid. */
505 const DNN::EActivationFunction fFor = this->GetActivationFunctionF1();
506 Matrix_t tmpState(fForgetValue.GetNrows(), fForgetValue.GetNcols());
507 Architecture_t::MultiplyTranspose(tmpState, fState, fWeightsForgetGateState);
508 Architecture_t::MultiplyTranspose(fForgetValue, input, fWeightsForgetGate);
509 Architecture_t::ScaleAdd(fForgetValue, tmpState);
510 Architecture_t::AddRowWise(fForgetValue, fForgetGateBias);
511 DNN::evaluateDerivativeMatrix<Architecture_t>(df, fFor, fForgetValue);
512 DNN::evaluateMatrix<Architecture_t>(fForgetValue, fFor);
513}
514
515 //______________________________________________________________________________
516template <typename Architecture_t>
518-> void
519{
520 /*! Candidate value will be used to scale input gate values followed by Hadamard product.
521 * candidate_value = act(W_input . input + W_state . state + bias)
522 * activation function = tanh. */
523 const DNN::EActivationFunction fCan = this->GetActivationFunctionF2();
524 Matrix_t tmpState(fCandidateValue.GetNrows(), fCandidateValue.GetNcols());
525 Architecture_t::MultiplyTranspose(tmpState, fState, fWeightsCandidateState);
526 Architecture_t::MultiplyTranspose(fCandidateValue, input, fWeightsCandidate);
527 Architecture_t::ScaleAdd(fCandidateValue, tmpState);
528 Architecture_t::AddRowWise(fCandidateValue, fCandidateBias);
529 DNN::evaluateDerivativeMatrix<Architecture_t>(dc, fCan, fCandidateValue);
530 DNN::evaluateMatrix<Architecture_t>(fCandidateValue, fCan);
531}
532
533 //______________________________________________________________________________
534template <typename Architecture_t>
536-> void
537{
538 /*! Output gate values will be used to calculate next hidden state and output values.
539 * output = act(W_input . input + W_state . state + bias)
540 * activation function = sigmoid. */
541 const DNN::EActivationFunction fOut = this->GetActivationFunctionF1();
542 Matrix_t tmpState(fOutputValue.GetNrows(), fOutputValue.GetNcols());
543 Architecture_t::MultiplyTranspose(tmpState, fState, fWeightsOutputGateState);
544 Architecture_t::MultiplyTranspose(fOutputValue, input, fWeightsOutputGate);
545 Architecture_t::ScaleAdd(fOutputValue, tmpState);
546 Architecture_t::AddRowWise(fOutputValue, fOutputGateBias);
547 DNN::evaluateDerivativeMatrix<Architecture_t>(dout, fOut, fOutputValue);
548 DNN::evaluateMatrix<Architecture_t>(fOutputValue, fOut);
549}
550
551
552
553 //______________________________________________________________________________
554template <typename Architecture_t>
556-> void
557{
558
559 // for Cudnn
560 if (Architecture_t::IsCudnn()) {
561
562 // input size is stride[1] of input tensor that is B x T x inputSize
563 assert(input.GetStrides()[1] == this->GetInputSize());
564
565 Tensor_t &x = this->fX;
566 Tensor_t &y = this->fY;
567 Architecture_t::Rearrange(x, input);
568
569 //const auto &weights = this->GetWeightsAt(0);
570 const auto &weights = this->GetWeightsTensor();
571 // Tensor_t cx({1}); // not used for normal RNN
572 // Tensor_t cy({1}); // not used for normal RNN
573
574 // hx is fState - tensor are of right shape
575 auto &hx = this->fState;
576 //auto &cx = this->fCell;
577 auto &cx = this->fCell; // pass an empty cell state
578 // use same for hy and cy
579 auto &hy = this->fState;
580 auto &cy = this->fCell;
581
582 auto & rnnDesc = static_cast<RNNDescriptors_t &>(*fDescriptors);
583 auto & rnnWork = static_cast<RNNWorkspace_t &>(*fWorkspace);
584
585 Architecture_t::RNNForward(x, hx, cx, weights, y, hy, cy, rnnDesc, rnnWork, isTraining);
586
587 if (fReturnSequence) {
588 Architecture_t::Rearrange(this->GetOutput(), y); // swap B and T from y to Output
589 } else {
590 // tmp is a reference to y (full cudnn output)
591 Tensor_t tmp = (y.At(y.GetShape()[0] - 1)).Reshape({y.GetShape()[1], 1, y.GetShape()[2]});
592 Architecture_t::Copy(this->GetOutput(), tmp);
593 }
594
595 return;
596 }
597
598 // Standard CPU implementation
599
600 // D : input size
601 // H : state size
602 // T : time size
603 // B : batch size
604
605 Tensor_t arrInput( fTimeSteps, this->GetBatchSize(), this->GetInputWidth());
606 //Tensor_t &arrInput = this->GetX();
607
608 Architecture_t::Rearrange(arrInput, input); // B x T x D
609
610 Tensor_t arrOutput ( fTimeSteps, this->GetBatchSize(), fStateSize);
611
612
613 if (!this->fRememberState) {
615 }
616
617 /*! Pass each gate values to CellForward() to calculate
618 * next hidden state and next cell state. */
619 for (size_t t = 0; t < fTimeSteps; ++t) {
620 /* Feed forward network: value of each gate being computed at each timestep t. */
622 InputGate(arrInputMt, fDerivativesInput[t]);
623 ForgetGate(arrInputMt, fDerivativesForget[t]);
624 CandidateValue(arrInputMt, fDerivativesCandidate[t]);
625 OutputGate(arrInputMt, fDerivativesOutput[t]);
626
627 Architecture_t::Copy(this->GetInputGateTensorAt(t), fInputValue);
628 Architecture_t::Copy(this->GetForgetGateTensorAt(t), fForgetValue);
629 Architecture_t::Copy(this->GetCandidateGateTensorAt(t), fCandidateValue);
630 Architecture_t::Copy(this->GetOutputGateTensorAt(t), fOutputValue);
631
632 CellForward(fInputValue, fForgetValue, fCandidateValue, fOutputValue);
634 Architecture_t::Copy(arrOutputMt, fState);
635 Architecture_t::Copy(this->GetCellTensorAt(t), fCell);
636 }
637
638 // check if full output needs to be returned
639 if (fReturnSequence)
640 Architecture_t::Rearrange(this->GetOutput(), arrOutput); // B x T x D
641 else {
642 // get T[end[]]
643 Tensor_t tmp = arrOutput.At(fTimeSteps - 1); // take last time step
644 // shape of tmp is for CPU (columnwise) B x D , need to reshape to make a B x D x 1
645 // and transpose it to 1 x D x B (this is how output is expected in columnmajor format)
646 tmp = tmp.Reshape( {tmp.GetShape()[0], tmp.GetShape()[1], 1});
647 assert(tmp.GetSize() == this->GetOutput().GetSize());
648 assert( tmp.GetShape()[0] == this->GetOutput().GetShape()[2]); // B is last dim in output and first in tmp
649 Architecture_t::Rearrange(this->GetOutput(), tmp);
650 // keep array output
651 fY = arrOutput;
652 }
653}
654
655 //______________________________________________________________________________
656template <typename Architecture_t>
659-> void
660{
661
662 // Update cell state.
663 Architecture_t::Hadamard(fCell, forgetGateValues);
664 Architecture_t::Hadamard(inputGateValues, candidateValues);
665 Architecture_t::ScaleAdd(fCell, inputGateValues);
666
667 Matrix_t cache(fCell.GetNrows(), fCell.GetNcols());
668 Architecture_t::Copy(cache, fCell);
669
670 // Update hidden state.
671 const DNN::EActivationFunction fAT = this->GetActivationFunctionF2();
672 DNN::evaluateMatrix<Architecture_t>(cache, fAT);
673
674 /*! The Hadamard product of output_gate_value . tanh(cell_state)
675 * will be copied to next hidden state (passed to next LSTM cell)
676 * and we will update our outputGateValues also. */
677 Architecture_t::Copy(fState, cache);
678 Architecture_t::Hadamard(fState, outputGateValues);
679}
680
681 //____________________________________________________________________________
682template <typename Architecture_t>
684 const Tensor_t &activations_backward) // B x T x D
685-> void
686{
687
688 // BACKWARD for CUDNN
689 if (Architecture_t::IsCudnn()) {
690
691 Tensor_t &x = this->fX;
692 Tensor_t &y = this->fY;
693 Tensor_t &dx = this->fDx;
694 Tensor_t &dy = this->fDy;
695
696 // input size is stride[1] of input tensor that is B x T x inputSize
697 assert(activations_backward.GetStrides()[1] == this->GetInputSize());
698
699 Architecture_t::Rearrange(x, activations_backward);
700
701 if (!fReturnSequence) {
702
703 // Architecture_t::InitializeZero(dy);
704 Architecture_t::InitializeZero(dy);
705
706 // Tensor_t tmp1 = y.At(y.GetShape()[0] - 1).Reshape({y.GetShape()[1], 1, y.GetShape()[2]});
707 // dy is a tensor of shape (rowmajor for Cudnn): T x B x S
708 // and this->ActivationGradients is B x (T=1) x S
709 Tensor_t tmp2 = dy.At(dy.GetShape()[0] - 1).Reshape({dy.GetShape()[1], 1, dy.GetShape()[2]});
710
711 // Architecture_t::Copy(tmp1, this->GetOutput());
712 Architecture_t::Copy(tmp2, this->GetActivationGradients());
713 } else {
714 Architecture_t::Rearrange(y, this->GetOutput());
715 Architecture_t::Rearrange(dy, this->GetActivationGradients());
716 }
717
718 // Architecture_t::PrintTensor(this->GetOutput(), "output before bwd");
719
720 // for cudnn Matrix_t and Tensor_t are same type
721 const auto &weights = this->GetWeightsTensor();
722 auto &weightGradients = this->GetWeightGradientsTensor();
723 // note that cudnnRNNBackwardWeights accumulate the weight gradients.
724 // We need then to initialize the tensor to zero every time
725 Architecture_t::InitializeZero(weightGradients);
726
727 // hx is fState
728 auto &hx = this->GetState();
729 auto &cx = this->GetCell();
730 //auto &cx = this->GetCell();
731 // use same for hy and cy
732 auto &dhy = hx;
733 auto &dcy = cx;
734 auto &dhx = hx;
735 auto &dcx = cx;
736
737 auto & rnnDesc = static_cast<RNNDescriptors_t &>(*fDescriptors);
738 auto & rnnWork = static_cast<RNNWorkspace_t &>(*fWorkspace);
739
740 Architecture_t::RNNBackward(x, hx, cx, y, dy, dhy, dcy, weights, dx, dhx, dcx, weightGradients, rnnDesc, rnnWork);
741
742 // Architecture_t::PrintTensor(this->GetOutput(), "output after bwd");
743
744 if (gradients_backward.GetSize() != 0)
745 Architecture_t::Rearrange(gradients_backward, dx);
746
747 return;
748 }
749 // CPU implementation
750
751 // gradients_backward is activationGradients of layer before it, which is input layer.
752 // Currently, gradients_backward is for input(x) and not for state.
753 // For the state it can be:
754 Matrix_t state_gradients_backward(this->GetBatchSize(), fStateSize); // B x H
755 DNN::initialize<Architecture_t>(state_gradients_backward, DNN::EInitialization::kZero); // B x H
756
757
758 Matrix_t cell_gradients_backward(this->GetBatchSize(), fStateSize); // B x H
759 DNN::initialize<Architecture_t>(cell_gradients_backward, DNN::EInitialization::kZero); // B x H
760
761 // if dummy is false gradients_backward will be written back on the matrix
762 bool dummy = false;
763 if (gradients_backward.GetSize() == 0 || gradients_backward[0].GetNrows() == 0 || gradients_backward[0].GetNcols() == 0) {
764 dummy = true;
765 }
766
767
768 Tensor_t arr_gradients_backward ( fTimeSteps, this->GetBatchSize(), this->GetInputSize());
769
770
771 //Architecture_t::Rearrange(arr_gradients_backward, gradients_backward); // B x T x D
772 // activations_backward is input.
773 Tensor_t arr_activations_backward ( fTimeSteps, this->GetBatchSize(), this->GetInputSize());
774
775 Architecture_t::Rearrange(arr_activations_backward, activations_backward); // B x T x D
776
777 /*! For backpropagation, we need to calculate loss. For loss, output must be known.
778 * We obtain outputs during forward propagation and place the results in arr_output tensor. */
779 Tensor_t arr_output ( fTimeSteps, this->GetBatchSize(), fStateSize);
780
781 Matrix_t initState(this->GetBatchSize(), fCellSize); // B x H
782 DNN::initialize<Architecture_t>(initState, DNN::EInitialization::kZero); // B x H
783
784 // This will take partial derivative of state[t] w.r.t state[t-1]
785
786 Tensor_t arr_actgradients(fTimeSteps, this->GetBatchSize(), fStateSize);
787
788 if (fReturnSequence) {
789 Architecture_t::Rearrange(arr_output, this->GetOutput());
790 Architecture_t::Rearrange(arr_actgradients, this->GetActivationGradients());
791 } else {
792 // here for CPU need to transpose the input activation gradients into the right format
793 arr_output = fY;
794 Architecture_t::InitializeZero(arr_actgradients);
795 // need to reshape to pad a time dimension = 1 (note here is columnmajor tensors)
796 Tensor_t tmp_grad = arr_actgradients.At(fTimeSteps - 1).Reshape( {this->GetBatchSize(), fStateSize, 1});
797 assert(tmp_grad.GetSize() == this->GetActivationGradients().GetSize());
798 assert(tmp_grad.GetShape()[0] == this->GetActivationGradients().GetShape()[2]); // B in tmp is [0] and [2] in input act. gradients
799
800 Architecture_t::Rearrange(tmp_grad, this->GetActivationGradients());
801 }
802
803 /*! There are total 8 different weight matrices and 4 bias vectors.
804 * Re-initialize them with zero because it should have some value. (can't be garbage values) */
805
806 // Input Gate.
807 fWeightsInputGradients.Zero();
808 fWeightsInputStateGradients.Zero();
809 fInputBiasGradients.Zero();
810
811 // Forget Gate.
812 fWeightsForgetGradients.Zero();
813 fWeightsForgetStateGradients.Zero();
814 fForgetBiasGradients.Zero();
815
816 // Candidate Gate.
817 fWeightsCandidateGradients.Zero();
818 fWeightsCandidateStateGradients.Zero();
819 fCandidateBiasGradients.Zero();
820
821 // Output Gate.
822 fWeightsOutputGradients.Zero();
823 fWeightsOutputStateGradients.Zero();
824 fOutputBiasGradients.Zero();
825
826
827 for (size_t t = fTimeSteps; t > 0; t--) {
828 // Store the sum of gradients obtained at each timestep during backward pass.
829 Architecture_t::ScaleAdd(state_gradients_backward, arr_actgradients[t-1]);
830 if (t > 1) {
832 const Matrix_t &prevCellActivations = this->GetCellTensorAt(t-2);
833 // During forward propagation, each gate value calculates their gradients.
837 this->GetInputGateTensorAt(t-1), this->GetForgetGateTensorAt(t-1),
838 this->GetCandidateGateTensorAt(t-1), this->GetOutputGateTensorAt(t-1),
840 fDerivativesInput[t-1], fDerivativesForget[t-1],
841 fDerivativesCandidate[t-1], fDerivativesOutput[t-1], t-1);
842 } else {
848 this->GetInputGateTensorAt(t-1), this->GetForgetGateTensorAt(t-1),
849 this->GetCandidateGateTensorAt(t-1), this->GetOutputGateTensorAt(t-1),
851 fDerivativesInput[t-1], fDerivativesForget[t-1],
852 fDerivativesCandidate[t-1], fDerivativesOutput[t-1], t-1);
853 }
854 }
855
856 if (!dummy) {
857 Architecture_t::Rearrange(gradients_backward, arr_gradients_backward );
858 }
859
860}
861
862
863 //______________________________________________________________________________
864template <typename Architecture_t>
868 const Matrix_t & input_gate, const Matrix_t & forget_gate,
872 size_t t)
873-> Matrix_t &
874{
875 /*! Call here LSTMLayerBackward() to pass parameters i.e. gradient
876 * values obtained from each gate during forward propagation. */
877
878
879 // cell gradient for current time step
880 const DNN::EActivationFunction fAT = this->GetActivationFunctionF2();
881 Matrix_t cell_gradient(this->GetCellTensorAt(t).GetNrows(), this->GetCellTensorAt(t).GetNcols());
882 DNN::evaluateDerivativeMatrix<Architecture_t>(cell_gradient, fAT, this->GetCellTensorAt(t));
883
884 // cell tanh value for current time step
885 Matrix_t cell_tanh(this->GetCellTensorAt(t).GetNrows(), this->GetCellTensorAt(t).GetNcols());
886 Architecture_t::Copy(cell_tanh, this->GetCellTensorAt(t));
887 DNN::evaluateMatrix<Architecture_t>(cell_tanh, fAT);
888
889 return Architecture_t::LSTMLayerBackward(state_gradients_backward, cell_gradients_backward,
890 fWeightsInputGradients, fWeightsForgetGradients, fWeightsCandidateGradients,
891 fWeightsOutputGradients, fWeightsInputStateGradients, fWeightsForgetStateGradients,
892 fWeightsCandidateStateGradients, fWeightsOutputStateGradients, fInputBiasGradients, fForgetBiasGradients,
893 fCandidateBiasGradients, fOutputBiasGradients, di, df, dc, dout,
896 fWeightsInputGate, fWeightsForgetGate, fWeightsCandidate, fWeightsOutputGate,
897 fWeightsInputGateState, fWeightsForgetGateState, fWeightsCandidateState,
898 fWeightsOutputGateState, input, input_gradient,
900}
901
902 //______________________________________________________________________________
903template <typename Architecture_t>
905-> void
906{
907 DNN::initialize<Architecture_t>(this->GetState(), DNN::EInitialization::kZero);
908 DNN::initialize<Architecture_t>(this->GetCell(), DNN::EInitialization::kZero);
909}
910
911 //______________________________________________________________________________
912template<typename Architecture_t>
914-> void
915{
916 std::cout << " LSTM Layer: \t ";
917 std::cout << " (NInput = " << this->GetInputSize(); // input size
918 std::cout << ", NState = " << this->GetStateSize(); // hidden state size
919 std::cout << ", NTime = " << this->GetTimeSteps() << " )"; // time size
920 std::cout << "\tOutput = ( " << this->GetOutput().GetFirstSize() << " , " << this->GetOutput()[0].GetNrows() << " , " << this->GetOutput()[0].GetNcols() << " )\n";
921}
922
923 //______________________________________________________________________________
924template <typename Architecture_t>
926-> void
927{
928 auto layerxml = gTools().xmlengine().NewChild(parent, nullptr, "LSTMLayer");
929
930 // Write all other info like outputSize, cellSize, inputSize, timeSteps, rememberState
931 gTools().xmlengine().NewAttr(layerxml, nullptr, "StateSize", gTools().StringFromInt(this->GetStateSize()));
932 gTools().xmlengine().NewAttr(layerxml, nullptr, "CellSize", gTools().StringFromInt(this->GetCellSize()));
933 gTools().xmlengine().NewAttr(layerxml, nullptr, "InputSize", gTools().StringFromInt(this->GetInputSize()));
934 gTools().xmlengine().NewAttr(layerxml, nullptr, "TimeSteps", gTools().StringFromInt(this->GetTimeSteps()));
935 gTools().xmlengine().NewAttr(layerxml, nullptr, "RememberState", gTools().StringFromInt(this->DoesRememberState()));
936 gTools().xmlengine().NewAttr(layerxml, nullptr, "ReturnSequence", gTools().StringFromInt(this->DoesReturnSequence()));
937
938 // write weights and bias matrices
939 this->WriteMatrixToXML(layerxml, "InputWeights", this->GetWeightsAt(0));
940 this->WriteMatrixToXML(layerxml, "InputStateWeights", this->GetWeightsAt(1));
941 this->WriteMatrixToXML(layerxml, "InputBiases", this->GetBiasesAt(0));
942 this->WriteMatrixToXML(layerxml, "ForgetWeights", this->GetWeightsAt(2));
943 this->WriteMatrixToXML(layerxml, "ForgetStateWeights", this->GetWeightsAt(3));
944 this->WriteMatrixToXML(layerxml, "ForgetBiases", this->GetBiasesAt(1));
945 this->WriteMatrixToXML(layerxml, "CandidateWeights", this->GetWeightsAt(4));
946 this->WriteMatrixToXML(layerxml, "CandidateStateWeights", this->GetWeightsAt(5));
947 this->WriteMatrixToXML(layerxml, "CandidateBiases", this->GetBiasesAt(2));
948 this->WriteMatrixToXML(layerxml, "OuputWeights", this->GetWeightsAt(6));
949 this->WriteMatrixToXML(layerxml, "OutputStateWeights", this->GetWeightsAt(7));
950 this->WriteMatrixToXML(layerxml, "OutputBiases", this->GetBiasesAt(3));
951}
952
953 //______________________________________________________________________________
954template <typename Architecture_t>
956-> void
957{
958 // Read weights and biases
959 this->ReadMatrixXML(parent, "InputWeights", this->GetWeightsAt(0));
960 this->ReadMatrixXML(parent, "InputStateWeights", this->GetWeightsAt(1));
961 this->ReadMatrixXML(parent, "InputBiases", this->GetBiasesAt(0));
962 this->ReadMatrixXML(parent, "ForgetWeights", this->GetWeightsAt(2));
963 this->ReadMatrixXML(parent, "ForgetStateWeights", this->GetWeightsAt(3));
964 this->ReadMatrixXML(parent, "ForgetBiases", this->GetBiasesAt(1));
965 this->ReadMatrixXML(parent, "CandidateWeights", this->GetWeightsAt(4));
966 this->ReadMatrixXML(parent, "CandidateStateWeights", this->GetWeightsAt(5));
967 this->ReadMatrixXML(parent, "CandidateBiases", this->GetBiasesAt(2));
968 this->ReadMatrixXML(parent, "OuputWeights", this->GetWeightsAt(6));
969 this->ReadMatrixXML(parent, "OutputStateWeights", this->GetWeightsAt(7));
970 this->ReadMatrixXML(parent, "OutputBiases", this->GetBiasesAt(3));
971}
972
973} // namespace LSTM
974} // namespace DNN
975} // namespace TMVA
976
977#endif // LSTM_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
void InputGate(const Matrix_t &input, Matrix_t &di)
Decides the values we'll update (NN with Sigmoid)
Definition LSTMLayer.h:481
const Matrix_t & GetForgetGateTensorAt(size_t i) const
Definition LSTMLayer.h:278
Matrix_t & GetWeightsOutputGateState()
Definition LSTMLayer.h:253
const std::vector< Matrix_t > & GetOutputGateTensor() const
Definition LSTMLayer.h:284
Tensor_t fWeightsTensor
Tensor for all weights.
Definition LSTMLayer.h:134
const std::vector< Matrix_t > & GetInputGateTensor() const
Definition LSTMLayer.h:272
std::vector< Matrix_t > & GetDerivativesOutput()
Definition LSTMLayer.h:268
const Matrix_t & GetWeigthsForgetStateGradients() const
Definition LSTMLayer.h:309
typename Architecture_t::Matrix_t Matrix_t
Definition LSTMLayer.h:63
Matrix_t & GetCandidateGateTensorAt(size_t i)
Definition LSTMLayer.h:283
void InitState(DNN::EInitialization m=DNN::EInitialization::kZero)
Initialize the hidden state and cell state method.
Definition LSTMLayer.h:904
Matrix_t & fWeightsCandidateGradients
Gradients w.r.t the candidate gate - input weights.
Definition LSTMLayer.h:126
const Matrix_t & GetOutputGateBias() const
Definition LSTMLayer.h:299
Matrix_t & GetWeightsCandidateStateGradients()
Definition LSTMLayer.h:316
Matrix_t & GetWeightsInputGateState()
Definition LSTMLayer.h:247
const std::vector< Matrix_t > & GetCandidateGateTensor() const
Definition LSTMLayer.h:280
const Matrix_t & GetInputGateTensorAt(size_t i) const
Definition LSTMLayer.h:274
std::vector< Matrix_t > & GetForgetGateTensor()
Definition LSTMLayer.h:277
std::vector< Matrix_t > cell_value
cell value for every time step
Definition LSTMLayer.h:114
void Backward(Tensor_t &gradients_backward, const Tensor_t &activations_backward) override
Backpropagates the error.
Definition LSTMLayer.h:683
Matrix_t & fWeightsOutputGradients
Gradients w.r.t the output gate - input weights.
Definition LSTMLayer.h:129
Matrix_t & fOutputBiasGradients
Gradients w.r.t the output gate - bias weights.
Definition LSTMLayer.h:131
void Initialize() override
Initialize the weights according to the given initialization method.
Definition LSTMLayer.h:471
DNN::EActivationFunction fF1
Activation function: sigmoid.
Definition LSTMLayer.h:84
Tensor_t fDy
cached activation gradient (input of backward) as T x B x S
Definition LSTMLayer.h:141
Matrix_t & fWeightsOutputGate
Output Gate weights for input, fWeights[6].
Definition LSTMLayer.h:106
Matrix_t & fWeightsCandidateStateGradients
Gradients w.r.t the candidate gate - hidden state weights.
Definition LSTMLayer.h:127
const Matrix_t & GetInputGateBias() const
Definition LSTMLayer.h:293
typename Architecture_t::Scalar_t Scalar_t
Definition LSTMLayer.h:64
size_t GetInputSize() const
Getters.
Definition LSTMLayer.h:213
Matrix_t & GetForgetGateTensorAt(size_t i)
Definition LSTMLayer.h:279
const Matrix_t & GetOutputGateTensorAt(size_t i) const
Definition LSTMLayer.h:286
const Matrix_t & GetCellTensorAt(size_t i) const
Definition LSTMLayer.h:290
Tensor_t fX
cached input tensor as T x B x I
Definition LSTMLayer.h:138
DNN::EActivationFunction GetActivationFunctionF2() const
Definition LSTMLayer.h:222
Matrix_t & GetCellTensorAt(size_t i)
Definition LSTMLayer.h:291
Matrix_t & fWeightsInputStateGradients
Gradients w.r.t the input gate - hidden state weights.
Definition LSTMLayer.h:121
void CellForward(Matrix_t &inputGateValues, const Matrix_t &forgetGateValues, const Matrix_t &candidateValues, const Matrix_t &outputGateValues)
Forward for a single cell (time unit)
Definition LSTMLayer.h:657
Matrix_t & CellBackward(Matrix_t &state_gradients_backward, Matrix_t &cell_gradients_backward, const Matrix_t &precStateActivations, const Matrix_t &precCellActivations, const Matrix_t &input_gate, const Matrix_t &forget_gate, const Matrix_t &candidate_gate, const Matrix_t &output_gate, const Matrix_t &input, Matrix_t &input_gradient, Matrix_t &di, Matrix_t &df, Matrix_t &dc, Matrix_t &dout, size_t t)
Backward for a single time unit a the corresponding call to Forward(...).
Definition LSTMLayer.h:865
const Matrix_t & GetWeightsInputStateGradients() const
Definition LSTMLayer.h:303
std::vector< Matrix_t > fDerivativesOutput
First fDerivatives of the activations output gate.
Definition LSTMLayer.h:118
void ReadWeightsFromXML(void *parent) override
Read the information and the weights about the layer from XML node.
Definition LSTMLayer.h:955
Matrix_t & fWeightsForgetGateState
Forget Gate weights for prev state, fWeights[3].
Definition LSTMLayer.h:99
Matrix_t & fOutputGateBias
Output Gate bias.
Definition LSTMLayer.h:108
std::vector< Matrix_t > fDerivativesCandidate
First fDerivatives of the activations candidate gate.
Definition LSTMLayer.h:117
const Matrix_t & GetInputDerivativesAt(size_t i) const
Definition LSTMLayer.h:257
Matrix_t & fWeightsForgetGate
Forget Gate weights for input, fWeights[2].
Definition LSTMLayer.h:98
Matrix_t & fWeightsInputGradients
Gradients w.r.t the input gate - input weights.
Definition LSTMLayer.h:120
typename Architecture_t::Tensor_t Tensor_t
Definition LSTMLayer.h:65
const std::vector< Matrix_t > & GetDerivativesInput() const
Definition LSTMLayer.h:255
Matrix_t & fForgetGateBias
Forget Gate bias.
Definition LSTMLayer.h:100
Matrix_t & GetWeightsInputGradients()
Definition LSTMLayer.h:302
Matrix_t & GetCandidateBiasGradients()
Definition LSTMLayer.h:318
Matrix_t & GetWeightsOutputGradients()
Definition LSTMLayer.h:320
Matrix_t & fCandidateBias
Candidate Gate bias.
Definition LSTMLayer.h:104
Matrix_t fCandidateValue
Computed candidate values.
Definition LSTMLayer.h:88
Tensor_t & GetWeightGradientsTensor()
Definition LSTMLayer.h:328
const Matrix_t & GetWeightsOutputGradients() const
Definition LSTMLayer.h:319
typename Architecture_t::RecurrentDescriptor_t LayerDescriptor_t
Definition LSTMLayer.h:67
const Matrix_t & GetWeightsInputGradients() const
Definition LSTMLayer.h:301
Matrix_t & GetWeightsCandidateState()
Definition LSTMLayer.h:251
const Matrix_t & GetInputBiasGradients() const
Definition LSTMLayer.h:305
DNN::EActivationFunction fF2
Activation function: tanh.
Definition LSTMLayer.h:85
void AddWeightsXMLTo(void *parent) override
Writes the information and the weights about the layer in an XML node.
Definition LSTMLayer.h:925
Matrix_t & fInputBiasGradients
Gradients w.r.t the input gate - bias weights.
Definition LSTMLayer.h:122
Matrix_t & GetWeightsOutputStateGradients()
Definition LSTMLayer.h:322
Matrix_t & fWeightsCandidateState
Candidate Gate weights for prev state, fWeights[5].
Definition LSTMLayer.h:103
std::vector< Matrix_t > fDerivativesForget
First fDerivatives of the activations forget gate.
Definition LSTMLayer.h:116
const Tensor_t & GetWeightGradientsTensor() const
Definition LSTMLayer.h:329
Matrix_t & GetForgetDerivativesAt(size_t i)
Definition LSTMLayer.h:262
const Matrix_t & GetWeightsInputGateState() const
Definition LSTMLayer.h:246
Matrix_t & GetWeightsInputStateGradients()
Definition LSTMLayer.h:304
typename Architecture_t::DropoutDescriptor_t HelperDescriptor_t
Definition LSTMLayer.h:70
Matrix_t & fForgetBiasGradients
Gradients w.r.t the forget gate - bias weights.
Definition LSTMLayer.h:125
const Matrix_t & GetCandidateBias() const
Definition LSTMLayer.h:297
std::vector< Matrix_t > output_gate_value
output gate value for every time step
Definition LSTMLayer.h:113
const std::vector< Matrix_t > & GetDerivativesCandidate() const
Definition LSTMLayer.h:263
size_t fStateSize
Hidden state size for LSTM.
Definition LSTMLayer.h:77
void CandidateValue(const Matrix_t &input, Matrix_t &dc)
Decides the new candidate values (NN with Tanh)
Definition LSTMLayer.h:517
std::vector< Matrix_t > fDerivativesInput
First fDerivatives of the activations input gate.
Definition LSTMLayer.h:115
const Matrix_t & GetWeightsForgetGateState() const
Definition LSTMLayer.h:248
Matrix_t & GetWeightsForgetGateState()
Definition LSTMLayer.h:249
const Matrix_t & GetWeightsInputGate() const
Definition LSTMLayer.h:238
const Matrix_t & GetInputGateValue() const
Definition LSTMLayer.h:224
void Update(const Scalar_t learningRate)
Tensor_t fDx
cached gradient on the input (output of backward) as T x B x I
Definition LSTMLayer.h:140
typename Architecture_t::RNNWorkspace_t RNNWorkspace_t
Definition LSTMLayer.h:72
TBasicLSTMLayer(size_t batchSize, size_t stateSize, size_t inputSize, size_t timeSteps, bool rememberState=false, bool returnSequence=false, DNN::EActivationFunction f1=DNN::EActivationFunction::kSigmoid, DNN::EActivationFunction f2=DNN::EActivationFunction::kTanh, bool training=true, DNN::EInitialization fA=DNN::EInitialization::kZero)
Constructor.
Definition LSTMLayer.h:343
Matrix_t & GetWeightsForgetStateGradients()
Definition LSTMLayer.h:310
const Matrix_t & GetOutputBiasGradients() const
Definition LSTMLayer.h:323
typename Architecture_t::TensorDescriptor_t TensorDescriptor_t
Definition LSTMLayer.h:69
const Matrix_t & GetWeightsOutputStateGradients() const
Definition LSTMLayer.h:321
Matrix_t & fWeightsOutputStateGradients
Gradients w.r.t the output gate - hidden state weights.
Definition LSTMLayer.h:130
bool fReturnSequence
Return in output full sequence or just last element.
Definition LSTMLayer.h:82
Matrix_t & GetWeightsForgetGradients()
Definition LSTMLayer.h:308
Matrix_t & GetWeightsCandidateGradients()
Definition LSTMLayer.h:314
void Forward(Tensor_t &input, bool isTraining=true) override
Computes the next hidden state and next cell state with given input matrix.
Definition LSTMLayer.h:555
const Matrix_t & GetWeightsForgetGradients() const
Definition LSTMLayer.h:307
Matrix_t fCell
Cell state of LSTM.
Definition LSTMLayer.h:92
std::vector< Matrix_t > & GetDerivativesCandidate()
Definition LSTMLayer.h:264
const Matrix_t & GetForgetBiasGradients() const
Definition LSTMLayer.h:311
std::vector< Matrix_t > & GetOutputGateTensor()
Definition LSTMLayer.h:285
const Matrix_t & GetForgetDerivativesAt(size_t i) const
Definition LSTMLayer.h:261
Matrix_t fState
Hidden state of LSTM.
Definition LSTMLayer.h:91
void OutputGate(const Matrix_t &input, Matrix_t &dout)
Computes output values (NN with Sigmoid)
Definition LSTMLayer.h:535
const Matrix_t & GetForgetGateValue() const
Definition LSTMLayer.h:228
std::vector< Matrix_t > candidate_gate_value
candidate gate value for every time step
Definition LSTMLayer.h:112
const Matrix_t & GetState() const
Definition LSTMLayer.h:233
const Matrix_t & GetWeightsCandidateState() const
Definition LSTMLayer.h:250
const std::vector< Matrix_t > & GetForgetGateTensor() const
Definition LSTMLayer.h:276
const std::vector< Matrix_t > & GetDerivativesOutput() const
Definition LSTMLayer.h:267
const std::vector< Matrix_t > & GetCellTensor() const
Definition LSTMLayer.h:288
const Tensor_t & GetWeightsTensor() const
Definition LSTMLayer.h:327
Matrix_t & fWeightsInputGate
Input Gate weights for input, fWeights[0].
Definition LSTMLayer.h:94
std::vector< Matrix_t > & GetCandidateGateTensor()
Definition LSTMLayer.h:281
const Matrix_t & GetOutputDerivativesAt(size_t i) const
Definition LSTMLayer.h:269
const Matrix_t & GetCell() const
Definition LSTMLayer.h:235
Matrix_t & fWeightsForgetStateGradients
Gradients w.r.t the forget gate - hidden state weights.
Definition LSTMLayer.h:124
const Matrix_t & GetCandidateGateTensorAt(size_t i) const
Definition LSTMLayer.h:282
Matrix_t fOutputValue
Computed output gate values.
Definition LSTMLayer.h:90
size_t fCellSize
Cell state size of LSTM.
Definition LSTMLayer.h:78
Matrix_t & GetOutputDerivativesAt(size_t i)
Definition LSTMLayer.h:270
Matrix_t & GetInputGateTensorAt(size_t i)
Definition LSTMLayer.h:275
std::vector< Matrix_t > & GetDerivativesInput()
Definition LSTMLayer.h:256
Matrix_t & fWeightsOutputGateState
Output Gate weights for prev state, fWeights[7].
Definition LSTMLayer.h:107
const std::vector< Matrix_t > & GetDerivativesForget() const
Definition LSTMLayer.h:259
const Matrix_t & GetForgetGateBias() const
Definition LSTMLayer.h:295
const Matrix_t & GetCandidateDerivativesAt(size_t i) const
Definition LSTMLayer.h:265
Matrix_t & GetOutputGateTensorAt(size_t i)
Definition LSTMLayer.h:287
size_t fTimeSteps
Timesteps for LSTM.
Definition LSTMLayer.h:79
const Matrix_t & GetCandidateBiasGradients() const
Definition LSTMLayer.h:317
const Matrix_t & GetCandidateValue() const
Definition LSTMLayer.h:226
typename Architecture_t::FilterDescriptor_t WeightsDescriptor_t
Definition LSTMLayer.h:68
Matrix_t & fInputGateBias
Input Gate bias.
Definition LSTMLayer.h:96
const Matrix_t & GetWeightsForgetGate() const
Definition LSTMLayer.h:242
std::vector< Matrix_t > input_gate_value
input gate value for every time step
Definition LSTMLayer.h:110
const Matrix_t & GetWeightsCandidateStateGradients() const
Definition LSTMLayer.h:315
Matrix_t & fWeightsForgetGradients
Gradients w.r.t the forget gate - input weights.
Definition LSTMLayer.h:123
std::vector< Matrix_t > & GetDerivativesForget()
Definition LSTMLayer.h:260
const Matrix_t & GetWeightsOutputGate() const
Definition LSTMLayer.h:244
void ForgetGate(const Matrix_t &input, Matrix_t &df)
Forgets the past values (NN with Sigmoid)
Definition LSTMLayer.h:499
std::vector< Matrix_t > & GetInputGateTensor()
Definition LSTMLayer.h:273
void Print() const override
Prints the info about the layer.
Definition LSTMLayer.h:913
const Matrix_t & GetOutputGateValue() const
Definition LSTMLayer.h:230
const Matrix_t & GetWeightsOutputGateState() const
Definition LSTMLayer.h:252
Matrix_t & GetCandidateDerivativesAt(size_t i)
Definition LSTMLayer.h:266
Matrix_t fInputValue
Computed input gate values.
Definition LSTMLayer.h:87
const Matrix_t & GetWeightsCandidate() const
Definition LSTMLayer.h:240
const Matrix_t & GetWeightsCandidateGradients() const
Definition LSTMLayer.h:313
Tensor_t fWeightGradientsTensor
Tensor for all weight gradients.
Definition LSTMLayer.h:135
Matrix_t & GetInputDerivativesAt(size_t i)
Definition LSTMLayer.h:258
typename Architecture_t::RNNDescriptors_t RNNDescriptors_t
Definition LSTMLayer.h:73
DNN::EActivationFunction GetActivationFunctionF1() const
Definition LSTMLayer.h:221
Tensor_t fY
cached output tensor as T x B x S
Definition LSTMLayer.h:139
std::vector< Matrix_t > forget_gate_value
forget gate value for every time step
Definition LSTMLayer.h:111
Matrix_t & fWeightsCandidate
Candidate Gate weights for input, fWeights[4].
Definition LSTMLayer.h:102
bool fRememberState
Remember state in next pass.
Definition LSTMLayer.h:81
Matrix_t & fWeightsInputGateState
Input Gate weights for prev state, fWeights[1].
Definition LSTMLayer.h:95
TDescriptors * fDescriptors
Keeps all the RNN descriptors.
Definition LSTMLayer.h:143
std::vector< Matrix_t > & GetCellTensor()
Definition LSTMLayer.h:289
Matrix_t & fCandidateBiasGradients
Gradients w.r.t the candidate gate - bias weights.
Definition LSTMLayer.h:128
Matrix_t fForgetValue
Computed forget gate values.
Definition LSTMLayer.h:89
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