Logo ROOT   6.14/05
Reference Guide
Cuda.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 05/07/16
3 
4 /*************************************************************************
5  * Copyright (C) 2016, Simon Pfreundschuh *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 ///////////////////////////////////////////////////////////////////
13 // Definition of the TCuda architecture class, which provides an //
14 // implementation of the low-level functionality for neural //
15 // networks for the CUDA computing architectures. //
16 ///////////////////////////////////////////////////////////////////
17 
18 #ifndef TMVA_DNN_ARCHITECTURES_CUDA
19 #define TMVA_DNN_ARCHITECTURES_CUDA
20 
21 #include "TMVA/DNN/Functions.h"
22 
23 
24 #include "cuda.h"
25 #include "Cuda/CudaBuffers.h"
26 #include "Cuda/CudaMatrix.h"
27 #include "TMVA/DNN/DataLoader.h"
28 #include <utility>
29 #include <vector>
30 
31 class TRandom;
32 
33 namespace TMVA
34 {
35 namespace DNN
36 {
37 
38 /** The TCuda architecture class.
39  *
40  * Low-level interface class for CUDA computing architectures. Contains as
41  * public types the declaration of the scalar, matrix and buffer types
42  * for this architecture as well as the remaining functions in the low-level
43  * interface in the form of static members.
44  */
45 template<typename AFloat = Real_t>
46 class TCuda
47 {
48 private:
49  static TRandom * fgRandomGen;
50 public:
51 
52  using Scalar_t = AFloat;
56 
57  //____________________________________________________________________________
58  //
59  // Propagation
60  //____________________________________________________________________________
61 
62  /** @name Forward Propagation
63  * Low-level functions required for the forward propagation of activations
64  * through the network.
65  */
66  ///@{
67  /** Matrix-multiply \p input with the transpose of \pweights and
68  * write the results into \p output. */
70  const TCudaMatrix<AFloat> &input,
71  const TCudaMatrix<AFloat> &weights);
72  /** Add the vectors biases row-wise to the matrix output */
74  const TCudaMatrix<AFloat> &biases);
75  ///@}
76 
77  /** @name Backward Propagation
78  * Low-level functions required for the forward propagation of activations
79  * through the network.
80  */
81  ///@{
82  /** Perform the complete backward propagation step. If the provided
83  * \p activationGradientsBackward matrix is not empty, compute the
84  * gradients of the objective function with respect to the activations
85  * of the previous layer (backward direction).
86  * Also compute the weight and the bias gradients. Modifies the values
87  * in \p df and thus produces only a valid result, if it is applied the
88  * first time after the corresponding forward propagation has been per-
89  * formed. */
90  static void Backward(TCudaMatrix<AFloat> & activationGradientsBackward,
91  TCudaMatrix<AFloat> & weightGradients,
92  TCudaMatrix<AFloat> & biasGradients,
94  const TCudaMatrix<AFloat> & activationGradients,
95  const TCudaMatrix<AFloat> & weights,
96  const TCudaMatrix<AFloat> & activationBackward);
97  /** Backward pass for Recurrent Networks */
98  static Matrix_t & RecurrentLayerBackward(TCudaMatrix<AFloat> & state_gradients_backward, // BxH
99  TCudaMatrix<AFloat> & input_weight_gradients,
100  TCudaMatrix<AFloat> & state_weight_gradients,
101  TCudaMatrix<AFloat> & bias_gradients,
102  TCudaMatrix<AFloat> & df, //DxH
103  const TCudaMatrix<AFloat> & state, // BxH
104  const TCudaMatrix<AFloat> & weights_input, // HxD
105  const TCudaMatrix<AFloat> & weights_state, // HxH
106  const TCudaMatrix<AFloat> & input, // BxD
107  TCudaMatrix<AFloat> & input_gradient);
108  /** Adds a the elements in matrix B scaled by c to the elements in
109  * the matrix A. This is required for the weight update in the gradient
110  * descent step.*/
111  static void ScaleAdd(TCudaMatrix<AFloat> & A,
112  const TCudaMatrix<AFloat> & B,
113  Scalar_t beta = 1.0);
114  /** Copy the elements of matrix A into matrix B. */
115  static void Copy(TCudaMatrix<AFloat> & B,
116  const TCudaMatrix<AFloat> & A);
117 
118  // copy from another type of matrix
119  template<typename AMatrix_t>
120  static void CopyDiffArch(TCudaMatrix<Scalar_t> & B, const AMatrix_t & A);
121 
122 
123  /** Above functions extended to vectors */
124  static void ScaleAdd(std::vector<TCudaMatrix<Scalar_t>> & A,
125  const std::vector<TCudaMatrix<Scalar_t>> & B,
126  Scalar_t beta = 1.0);
127 
128  static void Copy(std::vector<TCudaMatrix<Scalar_t>> & A,
129  const std::vector<TCudaMatrix<Scalar_t>> & B);
130 
131  // copy from another architecture
132  template<typename AMatrix_t>
133  static void CopyDiffArch(std::vector<TCudaMatrix<Scalar_t>> & A,
134  const std::vector<AMatrix_t> & B);
135 
136 
137  ///@}
138 
139  //____________________________________________________________________________
140  //
141  // Activation Functions
142  //____________________________________________________________________________
143 
144  /** @name Activation Functions
145  * For each activation function, the low-level interface contains two routines.
146  * One that applies the acitvation function to a matrix and one that evaluate
147  * the derivatives of the activation function at the elements of a given matrix
148  * and writes the results into the result matrix.
149  */
150  ///@{
151  static void Identity(TCudaMatrix<AFloat> & B);
153  const TCudaMatrix<AFloat> & A);
154 
155  static void Relu(TCudaMatrix<AFloat> & B);
156  static void ReluDerivative(TCudaMatrix<AFloat> & B,
157  const TCudaMatrix<AFloat> & A);
158 
159  static void Sigmoid(TCudaMatrix<AFloat> & B);
161  const TCudaMatrix<AFloat> & A);
162 
163  static void Tanh(TCudaMatrix<AFloat> & B);
164  static void TanhDerivative(TCudaMatrix<AFloat> & B,
165  const TCudaMatrix<AFloat> & A);
166 
167  static void SymmetricRelu(TCudaMatrix<AFloat> & B);
169  const TCudaMatrix<AFloat> & A);
170 
171  static void SoftSign(TCudaMatrix<AFloat> & B);
173  const TCudaMatrix<AFloat> & A);
174 
175  static void Gauss(TCudaMatrix<AFloat> & B);
176  static void GaussDerivative(TCudaMatrix<AFloat> & B,
177  const TCudaMatrix<AFloat> & A);
178  ///@}
179 
180  //____________________________________________________________________________
181  //
182  // Loss Functions
183  //____________________________________________________________________________
184 
185  /** @name Loss Functions
186  * Loss functions compute a scalar value given the \p output of the network
187  * for a given training input and the expected network prediction \p Y that
188  * quantifies the quality of the prediction. For each function also a routing
189  * that computes the gradients (suffixed by Gradients) must be provided for
190  * the starting of the backpropagation algorithm.
191  */
192  ///@{
193 
194  static AFloat MeanSquaredError(const TCudaMatrix<AFloat> &Y, const TCudaMatrix<AFloat> &output,
195  const TCudaMatrix<AFloat> &weights);
197  const TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &weights);
198 
199  /** Sigmoid transformation is implicitly applied, thus \p output should
200  * hold the linear activations of the last layer in the net. */
201  static AFloat CrossEntropy(const TCudaMatrix<AFloat> &Y, const TCudaMatrix<AFloat> &output,
202  const TCudaMatrix<AFloat> &weights);
203 
205  const TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &weights);
206 
207  /** Softmax transformation is implicitly applied, thus \p output should
208  * hold the linear activations of the last layer in the net. */
209  static AFloat SoftmaxCrossEntropy(const TCudaMatrix<AFloat> &Y, const TCudaMatrix<AFloat> &output,
210  const TCudaMatrix<AFloat> &weights);
212  const TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &weights);
213  ///@}
214 
215  //____________________________________________________________________________
216  //
217  // Output Functions
218  //____________________________________________________________________________
219 
220  /** @name Output Functions
221  * Output functions transform the activations \p output of the
222  * output layer in the network to a valid prediction \p YHat for
223  * the desired usage of the network, e.g. the identity function
224  * for regression or the sigmoid transformation for two-class
225  * classification.
226  */
227  ///@{
228  static void Sigmoid(TCudaMatrix<AFloat> &YHat,
229  const TCudaMatrix<AFloat> & );
230  static void Softmax(TCudaMatrix<AFloat> &YHat,
231  const TCudaMatrix<AFloat> & );
232  ///@}
233 
234  //____________________________________________________________________________
235  //
236  // Regularization
237  //____________________________________________________________________________
238 
239  /** @name Regularization
240  * For each regularization type two functions are required, one named
241  * <tt><Type>Regularization</tt> that evaluates the corresponding
242  * regularization functional for a given weight matrix and the
243  * <tt>Add<Type>RegularizationGradients</tt>, that adds the regularization
244  * component in the gradients to the provided matrix.
245  */
246  ///@{
247 
248  static AFloat L1Regularization(const TCudaMatrix<AFloat> & W);
250  const TCudaMatrix<AFloat> & W,
251  AFloat weightDecay);
252 
253  static AFloat L2Regularization(const TCudaMatrix<AFloat> & W);
255  const TCudaMatrix<AFloat> & W,
256  AFloat weightDecay);
257  ///@}
258 
259  //____________________________________________________________________________
260  //
261  // Initialization
262  //____________________________________________________________________________
263 
264  /** @name Initialization
265  * For each initialization method, one function in the low-level interface
266  * is provided. The naming scheme is <p>Initialize<Type></p> for a given
267  * initialization method Type.
268  */
269  ///@{
270 
271  static void InitializeGauss(TCudaMatrix<AFloat> & A);
272  static void InitializeUniform(TCudaMatrix<AFloat> & A);
274  static void InitializeZero(TCudaMatrix<AFloat> & A);
277  // return static instance of random generator used for initialization
278  // if generator does not exist it is created the first time with a random seed (e.g. seed = 0)
279  static TRandom & GetRandomGenerator();
280  // set random seed for the static geenrator
281  // if the static geneerator does not exists it is created
282  static void SetRandomSeed(size_t seed);
283 
284 
285  ///@}
286 
287  //____________________________________________________________________________
288  //
289  // Dropout
290  //____________________________________________________________________________
291 
292  /** @name Dropout
293  */
294  ///@{
295 
296  /** Apply dropout with activation probability \p p to the given
297  * matrix \p A and scale the result by reciprocal of \p p. */
298  static void Dropout(TCudaMatrix<AFloat> & A, AFloat p);
299 
300  ///@}
301 
302  //____________________________________________________________________________
303  //
304  // Convolutional Layer Propagation
305  //____________________________________________________________________________
306 
307  /** @name Forward Propagation in Convolutional Layer
308  */
309  ///@{
310 
311  /** Transform the matrix \p B in local view format, suitable for
312  * convolution, and store it in matrix \p A. */
313  static void Im2col(TCudaMatrix<AFloat> &A, const TCudaMatrix<AFloat> &B, size_t imgHeight, size_t imgWidth,
314  size_t fltHeight, size_t fltWidth, size_t strideRows, size_t strideCols, size_t zeroPaddingHeight,
315  size_t zeroPaddingWidth);
316 
317  static void Im2colIndices(std::vector<int> &V, const TCudaMatrix<AFloat> &B, size_t nLocalViews, size_t imgHeight, size_t imgWidth, size_t fltHeight,
318  size_t fltWidth, size_t strideRows, size_t strideCols, size_t zeroPaddingHeight,
319  size_t zeroPaddingWidth) {}
320  static void Im2colFast(TCudaMatrix<AFloat> &A, const TCudaMatrix<AFloat> &B, const std::vector<int> & V) {}
321 
322 
323  /** Rotates the matrix \p B, which is representing a weights,
324  * and stores them in the matrix \p A. */
325  static void RotateWeights(TCudaMatrix<AFloat> &A, const TCudaMatrix<AFloat> &B, size_t filterDepth,
326  size_t filterHeight, size_t filterWidth, size_t numFilters);
327 
328  /** Add the biases in the Convolutional Layer. */
329  static void AddConvBiases(TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &biases);
330 
331  ///@}
332  /** Forward propagation in the Convolutional layer */
333  static void ConvLayerForward(std::vector<TCudaMatrix<AFloat>> & output, std::vector<TCudaMatrix<AFloat>> & derivatives,
334  const std::vector<TCudaMatrix<AFloat>> &input,
335  const TCudaMatrix<Scalar_t> & weights, const TCudaMatrix<Scalar_t> & biases,
336  EActivationFunction func, const std::vector<int> & vIndices,
337  size_t nlocalViews, size_t nlocalViewPixels,
338  Scalar_t dropoutProbability, bool applyDropout) {}
339 
340  /** @name Backward Propagation in Convolutional Layer
341  */
342  ///@{
343 
344  /** Perform the complete backward propagation step in a Convolutional Layer.
345  * If the provided \p activationGradientsBackward matrix is not empty, compute the
346  * gradients of the objective function with respect to the activations
347  * of the previous layer (backward direction).
348  * Also compute the weight and the bias gradients. Modifies the values
349  * in \p df and thus produces only a valid result, if it is applied the
350  * first time after the corresponding forward propagation has been per-
351  * formed. */
352  static void ConvLayerBackward(std::vector<TCudaMatrix<AFloat>> &activationGradientsBackward,
353  TCudaMatrix<AFloat> &weightGradients, TCudaMatrix<AFloat> &biasGradients,
354  std::vector<TCudaMatrix<AFloat>> &df,
355  const std::vector<TCudaMatrix<AFloat>> &activationGradients,
356  const TCudaMatrix<AFloat> &weights,
357  const std::vector<TCudaMatrix<AFloat>> &activationBackward, size_t batchSize,
358  size_t inputHeight, size_t inputWidth, size_t depth, size_t height, size_t width,
359  size_t filterDepth, size_t filterHeight, size_t filterWidth, size_t nLocalViews);
360 
361  /** Utility function for calculating the activation gradients of the layer
362  * before the convolutional layer. */
363  static void CalculateConvActivationGradients(std::vector<TCudaMatrix<AFloat>> &activationGradientsBackward,
364  std::vector<TCudaMatrix<AFloat>> &df,
365  const TCudaMatrix<AFloat> &weights, size_t batchSize,
366  size_t inputHeight, size_t inputWidth, size_t depth, size_t height,
367  size_t width, size_t filterDepth, size_t filterHeight,
368  size_t filterWidth);
369 
370  /** Utility function for calculating the weight gradients of the convolutional
371  * layer. */
372  static void CalculateConvWeightGradients(TCudaMatrix<AFloat> &weightGradients, std::vector<TCudaMatrix<AFloat>> &df,
373  const std::vector<TCudaMatrix<AFloat>> &activations_backward,
374  size_t batchSize, size_t inputHeight, size_t inputWidth, size_t depth,
375  size_t height, size_t width, size_t filterDepth, size_t filterHeight,
376  size_t filterWidth, size_t nLocalViews);
377 
378  /** Utility function for calculating the bias gradients of the convolutional
379  * layer */
380  static void CalculateConvBiasGradients(TCudaMatrix<AFloat> &biasGradients, std::vector<TCudaMatrix<AFloat>> &df,
381  size_t batchSize, size_t depth, size_t nLocalViews);
382 
383  ///@}
384 
385  //____________________________________________________________________________
386  //
387  // Max Pooling Layer Propagation
388  //____________________________________________________________________________
389  /** @name Forward Propagation in Max Pooling Layer
390  */
391  ///@{
392 
393  /** Downsample the matrix \p C to the matrix \p A, using max
394  * operation, such that the winning indices are stored in matrix
395  * \p B. */
397  size_t imgHeight, size_t imgWidth, size_t fltHeight, size_t fltWidth,
398  size_t strideRows, size_t strideCols);
399  ///@}
400 
401  /** @name Backward Propagation in Max Pooling Layer
402  */
403  ///@{
404 
405  /** Perform the complete backward propagation step in a Pooling Layer. Based on the
406  * winning idices stored in the index matrix, it just forwards the actiovation
407  * gradients to the previous layer. */
408  static void MaxPoolLayerBackward(std::vector<TCudaMatrix<AFloat>> &activationGradientsBackward,
409  const std::vector<TCudaMatrix<AFloat>> &activationGradients,
410  const std::vector<TCudaMatrix<AFloat>> &indexMatrix, size_t batchSize, size_t depth,
411  size_t nLocalViews);
412 
413  ///@}
414 
415  //____________________________________________________________________________
416  //
417  // Reshape Layer Propagation
418  //____________________________________________________________________________
419  /** @name Forward and Backward Propagation in Reshape Layer
420  */
421  ///@{
422 
423  /** Transform the matrix \p B to a matrix with different dimensions \p A */
424  static void Reshape(TCudaMatrix<AFloat> &A, const TCudaMatrix<AFloat> &B);
425 
426  /** Flattens the tensor \p B, such that each matrix, is stretched in
427  * one row, resulting with a matrix \p A. */
428  static void Flatten(TCudaMatrix<AFloat> &A, const std::vector<TCudaMatrix<AFloat>> &B, size_t size, size_t nRows,
429  size_t nCols);
430 
431  /** Transforms each row of \p B to a matrix and stores it in the tensor \p B. */
432  static void Deflatten(std::vector<TCudaMatrix<AFloat>> &A, const TCudaMatrix<AFloat> &B, size_t index, size_t nRows,
433  size_t nCols);
434  /** Rearrage data accoring to time fill B x T x D out with T x B x D matrix in*/
435  static void Rearrange(std::vector<TCudaMatrix<AFloat>> &out, const std::vector<TCudaMatrix<AFloat>> &in);
436 
437  ///@}
438 
439  //____________________________________________________________________________
440  //
441  // Additional Arithmetic Functions
442  //____________________________________________________________________________
443 
444  /** @name Additional Arithmetic Functions
445  *
446  * Additional arithmetic on CUDA matrices used to implement the low-level
447  * interface.
448  */
449  ///@{
450 
451  /** Standard multiplication of two matrices \p A and \p B with the result being
452  * written into C.
453  */
454  static void Multiply(TCudaMatrix<AFloat> & C,
455  const TCudaMatrix<AFloat> & A,
456  const TCudaMatrix<AFloat> & B);
457  /** Matrix multiplication of two matrices \p A and \p B^T (transposed) with the
458  * result being written into C.
459  */
460  static void TransposeMultiply(TCudaMatrix<AFloat> & output,
461  const TCudaMatrix<AFloat> & input,
462  const TCudaMatrix<AFloat> & Weights);
463  /** In-place Hadamard (element-wise) product of matrices \p A and \p B
464  * with the result being written into \p A.
465  */
466  static void Hadamard(TCudaMatrix<AFloat> & A, const TCudaMatrix<AFloat> & B);
467 
468  /** Sum columns of (m x n) matrixx \p A and write the results into the first
469  * m elements in \p A.
470  */
471  static void SumColumns(TCudaMatrix<AFloat> & B, const TCudaMatrix<AFloat> & A);
472 
473  /** Compute the sum of all elements in \p A */
474  static AFloat Sum(const TCudaMatrix<AFloat> &A);
475 };
476 
477 //____________________________________________________________________________
478 template <typename AFloat>
479 template <typename AMatrix_t>
481  const AMatrix_t &A)
482 {
483  // copy from another architecture using the reference one
484  // this is not very efficient since creates temporary objects
485  TMatrixT<AFloat> tmp = A;
486  Copy(B, TCudaMatrix<AFloat>(tmp) );
487 }
488 
489 //____________________________________________________________________________
490 template <typename AFloat>
491 template <typename AMatrix_t>
493  const std::vector<AMatrix_t> &A)
494 {
495  for (size_t i = 0; i < B.size(); ++i) {
496  CopyDiffArch(B[i], A[i]);
497  }
498 }
499 
500 } // namespace DNN
501 } // namespace TMVA
502 
503 #endif
static double B[]
static Matrix_t & RecurrentLayerBackward(TCudaMatrix< AFloat > &state_gradients_backward, TCudaMatrix< AFloat > &input_weight_gradients, TCudaMatrix< AFloat > &state_weight_gradients, TCudaMatrix< AFloat > &bias_gradients, TCudaMatrix< AFloat > &df, const TCudaMatrix< AFloat > &state, const TCudaMatrix< AFloat > &weights_input, const TCudaMatrix< AFloat > &weights_state, const TCudaMatrix< AFloat > &input, TCudaMatrix< AFloat > &input_gradient)
Backward pass for Recurrent Networks.
static void ConvLayerForward(std::vector< TCudaMatrix< AFloat >> &output, std::vector< TCudaMatrix< AFloat >> &derivatives, const std::vector< TCudaMatrix< AFloat >> &input, const TCudaMatrix< Scalar_t > &weights, const TCudaMatrix< Scalar_t > &biases, EActivationFunction func, const std::vector< int > &vIndices, size_t nlocalViews, size_t nlocalViewPixels, Scalar_t dropoutProbability, bool applyDropout)
Forward propagation in the Convolutional layer.
Definition: Cuda.h:333
static void SymmetricReluDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
image html pict1_TGaxis_012 png width
Define new text attributes for the label number "labNum".
Definition: TGaxis.cxx:2551
static void RotateWeights(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, size_t filterDepth, size_t filterHeight, size_t filterWidth, size_t numFilters)
Rotates the matrix B, which is representing a weights, and stores them in the matrix A...
static void Im2colFast(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, const std::vector< int > &V)
Definition: Cuda.h:320
static void MaxPoolLayerBackward(std::vector< TCudaMatrix< AFloat >> &activationGradientsBackward, const std::vector< TCudaMatrix< AFloat >> &activationGradients, const std::vector< TCudaMatrix< AFloat >> &indexMatrix, size_t batchSize, size_t depth, size_t nLocalViews)
Perform the complete backward propagation step in a Pooling Layer.
static void Im2colIndices(std::vector< int > &V, const TCudaMatrix< AFloat > &B, size_t nLocalViews, size_t imgHeight, size_t imgWidth, size_t fltHeight, size_t fltWidth, size_t strideRows, size_t strideCols, size_t zeroPaddingHeight, size_t zeroPaddingWidth)
Definition: Cuda.h:317
static void GaussDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
TCudaDeviceBuffer.
Definition: CudaBuffers.h:28
static void MultiplyTranspose(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &input, const TCudaMatrix< AFloat > &weights)
Matrix-multiply input with the transpose of and write the results into output.
static void Softmax(TCudaMatrix< AFloat > &YHat, const TCudaMatrix< AFloat > &)
The TCuda architecture class.
Definition: Cuda.h:46
static void ScaleAdd(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, Scalar_t beta=1.0)
Adds a the elements in matrix B scaled by c to the elements in the matrix A.
static double A[]
static void Hadamard(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B)
In-place Hadamard (element-wise) product of matrices A and B with the result being written into A...
double beta(double x, double y)
Calculates the beta function.
AFloat Scalar_t
Definition: Cuda.h:52
TMatrixT.
Definition: TMatrixDfwd.h:22
static void IdentityDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void Deflatten(std::vector< TCudaMatrix< AFloat >> &A, const TCudaMatrix< AFloat > &B, size_t index, size_t nRows, size_t nCols)
Transforms each row of B to a matrix and stores it in the tensor B.
static void Dropout(TCudaMatrix< AFloat > &A, AFloat p)
Apply dropout with activation probability p to the given matrix A and scale the result by reciprocal ...
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
Definition: NeuralNet.icc:496
static void Reshape(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B)
Transform the matrix B to a matrix with different dimensions A.
static void SoftmaxCrossEntropyGradients(TCudaMatrix< AFloat > &dY, const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
static void CalculateConvActivationGradients(std::vector< TCudaMatrix< AFloat >> &activationGradientsBackward, std::vector< TCudaMatrix< AFloat >> &df, const TCudaMatrix< AFloat > &weights, size_t batchSize, size_t inputHeight, size_t inputWidth, size_t depth, size_t height, size_t width, size_t filterDepth, size_t filterHeight, size_t filterWidth)
Utility function for calculating the activation gradients of the layer before the convolutional layer...
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
static void Relu(TCudaMatrix< AFloat > &B)
static void Flatten(TCudaMatrix< AFloat > &A, const std::vector< TCudaMatrix< AFloat >> &B, size_t size, size_t nRows, size_t nCols)
Flattens the tensor B, such that each matrix, is stretched in one row, resulting with a matrix A...
static void AddRowWise(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &biases)
Add the vectors biases row-wise to the matrix output.
static AFloat SoftmaxCrossEntropy(const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
Softmax transformation is implicitly applied, thus output should hold the linear activations of the l...
TCudaHostBuffer.
Definition: CudaBuffers.h:42
static void SoftSignDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void Gauss(TCudaMatrix< AFloat > &B)
static void SymmetricRelu(TCudaMatrix< AFloat > &B)
static double C[]
static void TanhDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void Tanh(TCudaMatrix< AFloat > &B)
static void CalculateConvBiasGradients(TCudaMatrix< AFloat > &biasGradients, std::vector< TCudaMatrix< AFloat >> &df, size_t batchSize, size_t depth, size_t nLocalViews)
Utility function for calculating the bias gradients of the convolutional layer.
static void Multiply(TCudaMatrix< AFloat > &C, const TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B)
Standard multiplication of two matrices A and B with the result being written into C...
static void CalculateConvWeightGradients(TCudaMatrix< AFloat > &weightGradients, std::vector< TCudaMatrix< AFloat >> &df, const std::vector< TCudaMatrix< AFloat >> &activations_backward, size_t batchSize, size_t inputHeight, size_t inputWidth, size_t depth, size_t height, size_t width, size_t filterDepth, size_t filterHeight, size_t filterWidth, size_t nLocalViews)
Utility function for calculating the weight gradients of the convolutional layer. ...
static void InitializeUniform(TCudaMatrix< AFloat > &A)
static void SetRandomSeed(size_t seed)
static void AddL2RegularizationGradients(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &W, AFloat weightDecay)
static TRandom * fgRandomGen
Definition: Cuda.h:49
static void ConvLayerBackward(std::vector< TCudaMatrix< AFloat >> &activationGradientsBackward, TCudaMatrix< AFloat > &weightGradients, TCudaMatrix< AFloat > &biasGradients, std::vector< TCudaMatrix< AFloat >> &df, const std::vector< TCudaMatrix< AFloat >> &activationGradients, const TCudaMatrix< AFloat > &weights, const std::vector< TCudaMatrix< AFloat >> &activationBackward, size_t batchSize, size_t inputHeight, size_t inputWidth, size_t depth, size_t height, size_t width, size_t filterDepth, size_t filterHeight, size_t filterWidth, size_t nLocalViews)
Perform the complete backward propagation step in a Convolutional Layer.
static void InitializeGlorotNormal(TCudaMatrix< AFloat > &A)
static void Sigmoid(TCudaMatrix< AFloat > &B)
static AFloat L2Regularization(const TCudaMatrix< AFloat > &W)
static void Im2col(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, size_t imgHeight, size_t imgWidth, size_t fltHeight, size_t fltWidth, size_t strideRows, size_t strideCols, size_t zeroPaddingHeight, size_t zeroPaddingWidth)
Transform the matrix B in local view format, suitable for convolution, and store it in matrix A...
static void InitializeGlorotUniform(TCudaMatrix< AFloat > &A)
static void CrossEntropyGradients(TCudaMatrix< AFloat > &dY, const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
static void InitializeIdentity(TCudaMatrix< AFloat > &A)
static void ReluDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void MeanSquaredErrorGradients(TCudaMatrix< AFloat > &dY, const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
static TRandom & GetRandomGenerator()
static void CopyDiffArch(TCudaMatrix< Scalar_t > &B, const AMatrix_t &A)
Definition: Cuda.h:480
static void Downsample(TCudaMatrix< AFloat > &A, TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &C, size_t imgHeight, size_t imgWidth, size_t fltHeight, size_t fltWidth, size_t strideRows, size_t strideCols)
Downsample the matrix C to the matrix A, using max operation, such that the winning indices are store...
static AFloat Sum(const TCudaMatrix< AFloat > &A)
Compute the sum of all elements in A.
static AFloat MeanSquaredError(const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
static void Backward(TCudaMatrix< AFloat > &activationGradientsBackward, TCudaMatrix< AFloat > &weightGradients, TCudaMatrix< AFloat > &biasGradients, TCudaMatrix< AFloat > &df, const TCudaMatrix< AFloat > &activationGradients, const TCudaMatrix< AFloat > &weights, const TCudaMatrix< AFloat > &activationBackward)
Perform the complete backward propagation step.
static void AddL1RegularizationGradients(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &W, AFloat weightDecay)
static void AddConvBiases(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &biases)
Add the biases in the Convolutional Layer.
static void SigmoidDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void InitializeZero(TCudaMatrix< AFloat > &A)
static void SoftSign(TCudaMatrix< AFloat > &B)
Abstract ClassifierFactory template that handles arbitrary types.
static void TransposeMultiply(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &input, const TCudaMatrix< AFloat > &Weights)
Matrix multiplication of two matrices A and B^T (transposed) with the result being written into C...
static void Identity(TCudaMatrix< AFloat > &B)
static void SumColumns(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
Sum columns of (m x n) matrixx A and write the results into the first m elements in A...
EActivationFunction
Enum that represents layer activation functions.
Definition: Functions.h:31
static void Rearrange(std::vector< TCudaMatrix< AFloat >> &out, const std::vector< TCudaMatrix< AFloat >> &in)
Rearrage data accoring to time fill B x T x D out with T x B x D matrix in.
static AFloat L1Regularization(const TCudaMatrix< AFloat > &W)
static void Copy(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
Copy the elements of matrix A into matrix B.
static AFloat CrossEntropy(const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
Sigmoid transformation is implicitly applied, thus output should hold the linear activations of the l...
TCudaMatrix Class.
Definition: CudaMatrix.h:98
static void InitializeGauss(TCudaMatrix< AFloat > &A)