Logo ROOT   6.16/01
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"
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
31class TRandom;
32
33namespace TMVA
34{
35namespace 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 */
45template<typename AFloat = Real_t>
46class TCuda
47{
48private:
50public:
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.*/
112 const TCudaMatrix<AFloat> & B,
113 Scalar_t beta = 1.0);
114 /** Copy the elements of matrix A into matrix 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 // Activation Functions
141 //____________________________________________________________________________
142
143 /** @name Activation Functions
144 * For each activation function, the low-level interface contains two routines.
145 * One that applies the acitvation function to a matrix and one that evaluate
146 * the derivatives of the activation function at the elements of a given matrix
147 * and writes the results into the result matrix.
148 */
149 ///@{
152 const TCudaMatrix<AFloat> & A);
153
154 static void Relu(TCudaMatrix<AFloat> & B);
156 const TCudaMatrix<AFloat> & A);
157
160 const TCudaMatrix<AFloat> & A);
161
162 static void Tanh(TCudaMatrix<AFloat> & B);
164 const TCudaMatrix<AFloat> & A);
165
168 const TCudaMatrix<AFloat> & A);
169
172 const TCudaMatrix<AFloat> & A);
173
176 const TCudaMatrix<AFloat> & A);
177 ///@}
178
179 //____________________________________________________________________________
180 //
181 // Loss Functions
182 //____________________________________________________________________________
183
184 /** @name Loss Functions
185 * Loss functions compute a scalar value given the \p output of the network
186 * for a given training input and the expected network prediction \p Y that
187 * quantifies the quality of the prediction. For each function also a routing
188 * that computes the gradients (suffixed by Gradients) must be provided for
189 * the starting of the backpropagation algorithm.
190 */
191 ///@{
192
194 const TCudaMatrix<AFloat> &weights);
196 const TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &weights);
197
198 /** Sigmoid transformation is implicitly applied, thus \p output should
199 * hold the linear activations of the last layer in the net. */
201 const TCudaMatrix<AFloat> &weights);
202
204 const TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &weights);
205
206 /** Softmax transformation is implicitly applied, thus \p output should
207 * hold the linear activations of the last layer in the net. */
209 const TCudaMatrix<AFloat> &weights);
211 const TCudaMatrix<AFloat> &output, const TCudaMatrix<AFloat> &weights);
212 ///@}
213
214 //____________________________________________________________________________
215 //
216 // Output Functions
217 //____________________________________________________________________________
218
219 /** @name Output Functions
220 * Output functions transform the activations \p output of the
221 * output layer in the network to a valid prediction \p YHat for
222 * the desired usage of the network, e.g. the identity function
223 * for regression or the sigmoid transformation for two-class
224 * classification.
225 */
226 ///@{
227 static void Sigmoid(TCudaMatrix<AFloat> &YHat,
228 const TCudaMatrix<AFloat> & );
229 static void Softmax(TCudaMatrix<AFloat> &YHat,
230 const TCudaMatrix<AFloat> & );
231 ///@}
232
233 //____________________________________________________________________________
234 //
235 // Regularization
236 //____________________________________________________________________________
237
238 /** @name Regularization
239 * For each regularization type two functions are required, one named
240 * <tt><Type>Regularization</tt> that evaluates the corresponding
241 * regularization functional for a given weight matrix and the
242 * <tt>Add<Type>RegularizationGradients</tt>, that adds the regularization
243 * component in the gradients to the provided matrix.
244 */
245 ///@{
246
247 static AFloat L1Regularization(const TCudaMatrix<AFloat> & W);
249 const TCudaMatrix<AFloat> & W,
250 AFloat weightDecay);
251
252 static AFloat L2Regularization(const TCudaMatrix<AFloat> & W);
254 const TCudaMatrix<AFloat> & W,
255 AFloat weightDecay);
256 ///@}
257
258 //____________________________________________________________________________
259 //
260 // Initialization
261 //____________________________________________________________________________
262
263 /** @name Initialization
264 * For each initialization method, one function in the low-level interface
265 * is provided. The naming scheme is <p>Initialize<Type></p> for a given
266 * initialization method Type.
267 */
268 ///@{
269
276 // return static instance of random generator used for initialization
277 // if generator does not exist it is created the first time with a random seed (e.g. seed = 0)
279 // set random seed for the static geenrator
280 // if the static geneerator does not exists it is created
281 static void SetRandomSeed(size_t seed);
282
283
284 ///@}
285
286 //____________________________________________________________________________
287 //
288 // Dropout
289 //____________________________________________________________________________
290
291 /** @name Dropout
292 */
293 ///@{
294
295 /** Apply dropout with activation probability \p p to the given
296 * matrix \p A and scale the result by reciprocal of \p p. */
297 static void Dropout(TCudaMatrix<AFloat> & A, AFloat p);
298
299 ///@}
300
301 //____________________________________________________________________________
302 //
303 // Convolutional Layer Propagation
304 //____________________________________________________________________________
305
306 /** @name Forward Propagation in Convolutional Layer
307 */
308 ///@{
309
310 /** Attaches a cuda stream to each matrix in order to accomodate parallel kernel launches. */
311 static void PrepareInternals(std::vector<TCudaMatrix<AFloat>> & inputPrime);
312
313 /** Calculate how many neurons "fit" in the output layer, given the input as well as the layer's hyperparameters. */
314 static size_t calculateDimension(size_t imgDim, size_t fltDim, size_t padding, size_t stride);
315
316 /** Transform the matrix \p B in local view format, suitable for
317 * convolution, and store it in matrix \p A. */
319 const TCudaMatrix<AFloat> &B,
320 size_t imgHeight,
321 size_t imgWidth,
322 size_t fltHeight,
323 size_t fltWidth,
324 size_t strideRows,
325 size_t strideCols,
326 size_t zeroPaddingHeight,
327 size_t zeroPaddingWidth);
328
329 static void Im2colIndices(std::vector<int> & /* V */, const TCudaMatrix<AFloat> & /* B */, size_t /* nLocalViews */,
330 size_t /* imgHeight */, size_t /* imgWidth */, size_t /* fltHeight */,
331 size_t /* fltWidth */, size_t /* strideRows */, size_t /* strideCols */,
332 size_t /* zeroPaddingHeight */, size_t /* zeroPaddingWidth */) {}
333 static void Im2colFast(TCudaMatrix<AFloat> & /* A */, const TCudaMatrix<AFloat> & /* B */,
334 const std::vector<int> & /* V */) {}
335
336
337 /** Rotates the matrix \p B, which is representing a weights,
338 * and stores them in the matrix \p A. */
339 static void RotateWeights(TCudaMatrix<AFloat> &A, const TCudaMatrix<AFloat> &B, size_t filterDepth,
340 size_t filterHeight, size_t filterWidth, size_t numFilters);
341
342 /** Add the biases in the Convolutional Layer. */
344
345 ///@}
346 /** Forward propagation in the Convolutional layer */
347 static void ConvLayerForward(std::vector<TCudaMatrix<AFloat>> & output,
348 std::vector<TCudaMatrix<AFloat>> & derivatives,
349 const std::vector<TCudaMatrix<AFloat>> &input,
350 const TCudaMatrix<AFloat> &weights, const TCudaMatrix<AFloat> & biases,
351 const DNN::CNN::TConvParams & params, EActivationFunction activFunc,
352 std::vector<TCudaMatrix<AFloat>> & inputPrime);
353
354 /** @name Backward Propagation in Convolutional Layer
355 */
356 ///@{
357
358 /** Perform the complete backward propagation step in a Convolutional Layer.
359 * If the provided \p activationGradientsBackward matrix is not empty, compute the
360 * gradients of the objective function with respect to the activations
361 * of the previous layer (backward direction).
362 * Also compute the weight and the bias gradients. Modifies the values
363 * in \p df and thus produces only a valid result, if it is applied the
364 * first time after the corresponding forward propagation has been per-
365 * formed. */
366 static void ConvLayerBackward(std::vector<TCudaMatrix<AFloat>> &activationGradientsBackward,
367 TCudaMatrix<AFloat> &weightGradients, TCudaMatrix<AFloat> &biasGradients,
368 std::vector<TCudaMatrix<AFloat>> &df,
369 const std::vector<TCudaMatrix<AFloat>> &activationGradients,
370 const TCudaMatrix<AFloat> &weights,
371 const std::vector<TCudaMatrix<AFloat>> &activationBackward, size_t batchSize,
372 size_t inputHeight, size_t inputWidth, size_t depth, size_t height, size_t width,
373 size_t filterDepth, size_t filterHeight, size_t filterWidth, size_t nLocalViews);
374
375 /** Utility function for calculating the activation gradients of the layer
376 * before the convolutional layer. */
377 static void CalculateConvActivationGradients(std::vector<TCudaMatrix<AFloat>> &activationGradientsBackward,
378 std::vector<TCudaMatrix<AFloat>> &df,
379 const TCudaMatrix<AFloat> &weights, size_t batchSize,
380 size_t inputHeight, size_t inputWidth, size_t depth, size_t height,
381 size_t width, size_t filterDepth, size_t filterHeight,
382 size_t filterWidth);
383
384 /** Utility function for calculating the weight gradients of the convolutional
385 * layer. */
386 static void CalculateConvWeightGradients(TCudaMatrix<AFloat> &weightGradients, std::vector<TCudaMatrix<AFloat>> &df,
387 const std::vector<TCudaMatrix<AFloat>> &activations_backward,
388 size_t batchSize, size_t inputHeight, size_t inputWidth, size_t depth,
389 size_t height, size_t width, size_t filterDepth, size_t filterHeight,
390 size_t filterWidth, size_t nLocalViews);
391
392 /** Utility function for calculating the bias gradients of the convolutional
393 * layer */
394 static void CalculateConvBiasGradients(TCudaMatrix<AFloat> &biasGradients, std::vector<TCudaMatrix<AFloat>> &df,
395 size_t batchSize, size_t depth, size_t nLocalViews);
396
397 ///@}
398
399 //____________________________________________________________________________
400 //
401 // Max Pooling Layer Propagation
402 //____________________________________________________________________________
403 /** @name Forward Propagation in Max Pooling Layer
404 */
405 ///@{
406
407 /** Downsample the matrix \p C to the matrix \p A, using max
408 * operation, such that the winning indices are stored in matrix
409 * \p B. */
411 size_t imgHeight, size_t imgWidth, size_t fltHeight, size_t fltWidth,
412 size_t strideRows, size_t strideCols);
413 ///@}
414
415 /** @name Backward Propagation in Max Pooling Layer
416 */
417 ///@{
418
419 /** Perform the complete backward propagation step in a Pooling Layer. Based on the
420 * winning idices stored in the index matrix, it just forwards the actiovation
421 * gradients to the previous layer. */
422 static void MaxPoolLayerBackward(TCudaMatrix<AFloat> &activationGradientsBackward,
423 const TCudaMatrix<AFloat> &activationGradients,
424 const TCudaMatrix<AFloat> &indexMatrix,
425 size_t imgHeight,
426 size_t imgWidth,
427 size_t fltHeight,
428 size_t fltWidth,
429 size_t strideRows,
430 size_t strideCols,
431 size_t nLocalViews);
432
433 ///@}
434
435 //____________________________________________________________________________
436 //
437 // Reshape Layer Propagation
438 //____________________________________________________________________________
439 /** @name Forward and Backward Propagation in Reshape Layer
440 */
441 ///@{
442
443 /** Transform the matrix \p B to a matrix with different dimensions \p A */
445
446 /** Flattens the tensor \p B, such that each matrix, is stretched in
447 * one row, resulting with a matrix \p A. */
448 static void Flatten(TCudaMatrix<AFloat> &A, const std::vector<TCudaMatrix<AFloat>> &B, size_t size, size_t nRows,
449 size_t nCols);
450
451 /** Transforms each row of \p B to a matrix and stores it in the tensor \p B. */
452 static void Deflatten(std::vector<TCudaMatrix<AFloat>> &A, const TCudaMatrix<AFloat> &B, size_t index, size_t nRows,
453 size_t nCols);
454 /** Rearrage data accoring to time fill B x T x D out with T x B x D matrix in*/
455 static void Rearrange(std::vector<TCudaMatrix<AFloat>> &out, const std::vector<TCudaMatrix<AFloat>> &in);
456
457 ///@}
458
459 //____________________________________________________________________________
460 //
461 // Additional Arithmetic Functions
462 //____________________________________________________________________________
463
464 /** @name Additional Arithmetic Functions
465 *
466 * Additional arithmetic on CUDA matrices used to implement the low-level
467 * interface.
468 */
469 ///@{
470
471 /** Standard multiplication of two matrices \p A and \p B with the result being
472 * written into C.
473 */
475 const TCudaMatrix<AFloat> & A,
476 const TCudaMatrix<AFloat> & B);
477 /** Matrix multiplication of two matrices \p A and \p B^T (transposed) with the
478 * result being written into C.
479 */
481 const TCudaMatrix<AFloat> & input,
482 const TCudaMatrix<AFloat> & Weights);
483 /** In-place Hadamard (element-wise) product of matrices \p A and \p B
484 * with the result being written into \p A.
485 */
487
488 /** Sum columns of (m x n) matrix \p A and write the results into the first
489 * m elements in \p B.
490 */
492
493 /** Sum rows of (m x n) matrix \p A and write the results into the first
494 * m elements in \p B.
495 */
497
498 /** Compute the sum of all elements in \p A */
499 static AFloat Sum(const TCudaMatrix<AFloat> &A);
500
501 /** Check two matrices for equality, taking floating point arithmetic errors into account. */
502 static bool AlmostEquals(const TCudaMatrix<AFloat> &A, const TCudaMatrix<AFloat> &B, double epsilon = 0.1);
503
504 /** Add the constant \p beta to all the elements of matrix \p A and write the
505 * result into \p A.
506 */
507 static void ConstAdd(TCudaMatrix<AFloat> &A, AFloat beta);
508
509 /** Multiply the constant \p beta to all the elements of matrix \p A and write the
510 * result into \p A.
511 */
512 static void ConstMult(TCudaMatrix<AFloat> &A, AFloat beta);
513
514 /** Reciprocal each element of the matrix \p A and write the result into
515 * \p A
516 */
518
519 /** Square each element of the matrix \p A and write the result into
520 * \p A
521 */
523
524 /** Square root each element of the matrix \p A and write the result into
525 * \p A
526 */
528
529 // optimizer functions
530 static void AdamUpdate(TCudaMatrix<AFloat> & A, const TCudaMatrix<AFloat> & M, const TCudaMatrix<AFloat> & V, AFloat alpha, AFloat eps);
533
534};
535
536//____________________________________________________________________________
537template <typename AFloat>
538template <typename AMatrix_t>
540 const AMatrix_t &A)
541{
542 // copy from another architecture using the reference one
543 // this is not very efficient since creates temporary objects
544 TMatrixT<AFloat> tmp = A;
545 Copy(B, TCudaMatrix<AFloat>(tmp) );
546}
547
548//____________________________________________________________________________
549template <typename AFloat>
550template <typename AMatrix_t>
552 const std::vector<AMatrix_t> &A)
553{
554 for (size_t i = 0; i < B.size(); ++i) {
555 CopyDiffArch(B[i], A[i]);
556 }
557}
558
559} // namespace DNN
560} // namespace TMVA
561
562#endif
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
TCudaDeviceBuffer.
Definition: CudaBuffers.h:98
TCudaHostBuffer.
Definition: CudaBuffers.h:43
TCudaMatrix Class.
Definition: CudaMatrix.h:99
The TCuda architecture class.
Definition: Cuda.h:47
static TRandom * fgRandomGen
Definition: Cuda.h:49
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 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...
static void MultiplyTranspose(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &input, const TCudaMatrix< AFloat > &weights)
Matrix-multiply input with the transpose of \pweights and write the results into output.
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 Softmax(TCudaMatrix< AFloat > &YHat, const TCudaMatrix< AFloat > &)
static void AddL2RegularizationGradients(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &W, AFloat weightDecay)
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 Copy(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
Copy the elements of matrix A into matrix B.
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 void Copy(std::vector< TCudaMatrix< Scalar_t > > &A, const std::vector< TCudaMatrix< Scalar_t > > &B)
static void InitializeGlorotNormal(TCudaMatrix< AFloat > &A)
static void InitializeGlorotUniform(TCudaMatrix< AFloat > &A)
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 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 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...
static void GaussDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void Relu(TCudaMatrix< AFloat > &B)
static void PrepareInternals(std::vector< TCudaMatrix< AFloat > > &inputPrime)
Attaches a cuda stream to each matrix in order to accomodate parallel kernel launches.
static void ConstAdd(TCudaMatrix< AFloat > &A, AFloat beta)
Add the constant beta to all the elements of matrix A and write the result into A.
static void SumColumns(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
Sum columns of (m x n) matrix A and write the results into the first m elements in B.
static void InitializeZero(TCudaMatrix< AFloat > &A)
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 void SoftSign(TCudaMatrix< AFloat > &B)
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 SymmetricReluDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void MaxPoolLayerBackward(TCudaMatrix< AFloat > &activationGradientsBackward, const TCudaMatrix< AFloat > &activationGradients, const TCudaMatrix< AFloat > &indexMatrix, size_t imgHeight, size_t imgWidth, size_t fltHeight, size_t fltWidth, size_t strideRows, size_t strideCols, size_t nLocalViews)
Perform the complete backward propagation step in a Pooling Layer.
static size_t calculateDimension(size_t imgDim, size_t fltDim, size_t padding, size_t stride)
Calculate how many neurons "fit" in the output layer, given the input as well as the layer's hyperpar...
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.
static AFloat L2Regularization(const TCudaMatrix< AFloat > &W)
static void Im2colIndices(std::vector< int > &, const TCudaMatrix< AFloat > &, size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t)
Definition: Cuda.h:329
static void MeanSquaredErrorGradients(TCudaMatrix< AFloat > &dY, const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
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 AdamUpdateFirstMom(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, AFloat beta)
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...
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 CopyDiffArch(TCudaMatrix< Scalar_t > &B, const AMatrix_t &A)
Definition: Cuda.h:539
static bool AlmostEquals(const TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, double epsilon=0.1)
Check two matrices for equality, taking floating point arithmetic errors into account.
AFloat Scalar_t
Definition: Cuda.h:52
static void SquareElementWise(TCudaMatrix< AFloat > &A)
Square each element of the matrix A and write the result into A.
static void IdentityDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static AFloat MeanSquaredError(const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
static void SumRows(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
Sum rows of (m x n) matrix A and write the results into the first m elements in B.
static void Tanh(TCudaMatrix< AFloat > &B)
static void SigmoidDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void ConvLayerForward(std::vector< TCudaMatrix< AFloat > > &output, std::vector< TCudaMatrix< AFloat > > &derivatives, const std::vector< TCudaMatrix< AFloat > > &input, const TCudaMatrix< AFloat > &weights, const TCudaMatrix< AFloat > &biases, const DNN::CNN::TConvParams &params, EActivationFunction activFunc, std::vector< TCudaMatrix< AFloat > > &inputPrime)
Forward propagation in the Convolutional layer.
static void AdamUpdate(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &M, const TCudaMatrix< AFloat > &V, AFloat alpha, AFloat eps)
static void ConstMult(TCudaMatrix< AFloat > &A, AFloat beta)
Multiply the constant beta to all the elements of matrix A and write the result into A.
static void ReciprocalElementWise(TCudaMatrix< AFloat > &A)
Reciprocal each element of the matrix A and write the result into A.
static void Identity(TCudaMatrix< AFloat > &B)
static void SoftSignDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static AFloat Sum(const TCudaMatrix< AFloat > &A)
Compute the sum of all elements in A.
static void SqrtElementWise(TCudaMatrix< AFloat > &A)
Square root each element of the matrix A and write the result into A.
static void AddL1RegularizationGradients(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &W, AFloat weightDecay)
static void Reshape(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B)
Transform the matrix B to a matrix with different dimensions A.
static void TanhDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
static void SetRandomSeed(size_t seed)
static void AdamUpdateSecondMom(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, AFloat beta)
static void Im2colFast(TCudaMatrix< AFloat > &, const TCudaMatrix< AFloat > &, const std::vector< int > &)
Definition: Cuda.h:333
static AFloat L1Regularization(const TCudaMatrix< AFloat > &W)
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 ...
static void AddRowWise(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &biases)
Add the vectors biases row-wise to the matrix output.
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 InitializeGauss(TCudaMatrix< AFloat > &A)
static void InitializeIdentity(TCudaMatrix< AFloat > &A)
static TRandom & GetRandomGenerator()
static void ScaleAdd(std::vector< TCudaMatrix< Scalar_t > > &A, const std::vector< TCudaMatrix< Scalar_t > > &B, Scalar_t beta=1.0)
Above functions extended to vectors.
static void Sigmoid(TCudaMatrix< AFloat > &B)
static void Gauss(TCudaMatrix< AFloat > &B)
static void ReluDerivative(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
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 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 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 SymmetricRelu(TCudaMatrix< AFloat > &B)
static void SoftmaxCrossEntropyGradients(TCudaMatrix< AFloat > &dY, const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
static void Sigmoid(TCudaMatrix< AFloat > &YHat, const TCudaMatrix< AFloat > &)
static void AddConvBiases(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &biases)
Add the biases in the Convolutional Layer.
static void CrossEntropyGradients(TCudaMatrix< AFloat > &dY, const TCudaMatrix< AFloat > &Y, const TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &weights)
TMatrixT.
Definition: TMatrixT.h:39
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
double beta(double x, double y)
Calculates the beta function.
static double B[]
static double A[]
static double C[]
void Copy(void *source, void *dest)
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
EActivationFunction
Enum that represents layer activation functions.
Definition: Functions.h:32
Abstract ClassifierFactory template that handles arbitrary types.
REAL epsilon
Definition: triangle.c:617