27#ifndef TMVA_CNN_CONVLAYER
28#define TMVA_CNN_CONVLAYER
62 TConvParams(
size_t _batchSize,
size_t _inputDepth,
size_t _inputHeight,
size_t _inputWidth,
size_t _numberFilters,
63 size_t _filterHeight,
size_t _filterWidth,
size_t _strideRows,
size_t _strideCols,
64 size_t _paddingHeight,
size_t _paddingWidth)
74template <
typename Architecture_t>
77 using Tensor_t =
typename Architecture_t::Tensor_t;
78 using Matrix_t =
typename Architecture_t::Matrix_t;
79 using Scalar_t =
typename Architecture_t::Scalar_t;
100 static size_t calculateNLocalViews(
size_t inputHeight,
size_t filterHeight,
size_t paddingHeight,
size_t strideRows,
101 size_t inputWidth,
size_t filterWidth,
size_t paddingWidth,
size_t strideCols);
141 size_t FilterHeight,
size_t FilterWidth,
size_t StrideRows,
size_t StrideCols,
size_t PaddingHeight,
221template <
typename Architecture_t>
223 size_t depth,
EInitialization init,
size_t filterHeight,
size_t filterWidth,
224 size_t strideRows,
size_t strideCols,
size_t paddingHeight,
size_t paddingWidth,
227 :
VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth,
231 1, depth, 1, batchSize, depth,
233 inputWidth, filterWidth, paddingWidth, strideCols),
238 inputWidth, filterWidth, paddingWidth, strideCols)),
256template <
typename Architecture_t>
275template <
typename Architecture_t>
293template <
typename Architecture_t>
310template <
typename Architecture_t>
325template <
typename Architecture_t>
327 const Tensor_t &activations_backward) ->
void
331 Architecture_t::ConvLayerBackward(
346template <
typename Architecture_t>
349 std::cout <<
" CONV LAYER: \t";
350 std::cout <<
"( W = " << this->
GetWidth() <<
" , ";
351 std::cout <<
" H = " << this->
GetHeight() <<
" , ";
352 std::cout <<
" D = " << this->
GetDepth() <<
" ) ";
354 std::cout <<
"\t Filter ( W = " << this->
GetFilterWidth() <<
" , ";
358 std::cout <<
"\tOutput = ( " << this->
GetOutput().GetFirstSize() <<
" , "
362 std::vector<std::string> activationNames = {
"Identity",
"Relu",
"Sigmoid",
"Tanh",
"SymmRelu",
"SoftSign",
"Gauss" };
363 std::cout <<
"\t Activation Function = ";
364 std::cout << activationNames[ static_cast<int>(
fF) ] << std::endl;
368template <
typename Architecture_t>
392template <
typename Architecture_t>
401template <
typename Architecture_t>
404 size_t temp = imgDim - fltDim + 2 * padding;
405 if (temp % stride || temp + stride <= 0) {
406 Fatal(
"calculateDimension",
"Not compatible hyper parameters for layer - (imageDim, filterDim, padding, stride) "
407 "%zu, %zu, %zu, %zu", imgDim, fltDim, padding, stride);
409 return temp / stride + 1;
412template <
typename Architecture_t>
414 size_t strideRows,
size_t inputWidth,
size_t filterWidth,
415 size_t paddingWidth,
size_t strideCols)
417 int height =
calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows);
420 return height * width;
424template <
typename Architecture_t>
426 Architecture_t::InitializeConvDescriptors(
fDescriptors,
this);
429template <
typename Architecture_t>
435template <
typename Architecture_t>
444template <
typename Architecture_t>
446 Architecture_t::FreeConvWorkspace(
fWorkspace);
void Fatal(const char *location, const char *msgfmt,...)
Use this function in case of a fatal error. It will abort the program.
void Print() const override
Prints the info about the layer.
static size_t calculateNLocalViews(size_t inputHeight, size_t filterHeight, size_t paddingHeight, size_t strideRows, size_t inputWidth, size_t filterWidth, size_t paddingWidth, size_t strideCols)
const Tensor_t & GetForwardMatrices() const
size_t GetNLocalViewPixels() const
Tensor_t fInputActivation
const TDescriptors * GetDescriptors() const
typename TCpu< AReal >::ActivationDescriptor_t HelperDescriptor_t
size_t GetStrideRows() const
typename TCpu< AReal >::Tensor_t Tensor_t
Scalar_t GetWeightDecay() const
Tensor_t & GetInputActivation()
std::vector< int > fBackwardIndices
size_t GetFilterWidth() const
TConvLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth, EInitialization Init, size_t FilterHeight, size_t FilterWidth, size_t StrideRows, size_t StrideCols, size_t PaddingHeight, size_t PaddingWidth, Scalar_t DropoutProbability, EActivationFunction f, ERegularization Reg, Scalar_t WeightDecay)
Constructor.
static size_t calculateDimension(size_t imgDim, size_t fltDim, size_t padding, size_t stride)
TConvLayer(TConvLayer< Architecture_t > *layer)
Copy the conv layer provided as a pointer.
TDescriptors * GetDescriptors()
TConvLayer(const TConvLayer &)
Copy constructor.
void ReleaseDescriptors()
typename TCpu< AReal >::ConvolutionDescriptor_t LayerDescriptor_t
void Forward(Tensor_t &input, bool applyDropout=false) override
Computes activation of the layer for the given input.
size_t GetPaddingWidth() const
typename TCpu< AReal >::AlgorithmForward_t AlgorithmForward_t
Scalar_t fDropoutProbability
static size_t calculateNLocalViewPixels(size_t depth, size_t height, size_t width)
typename TCpu< AReal >::AlgorithmDataType_t AlgorithmDataType_t
virtual ~TConvLayer()
Destructor.
Tensor_t & GetForwardMatrices()
typename TCpu< AReal >::AlgorithmBackward_t AlgorithmBackward_t
const TWorkspace * GetWorkspace() const
const Matrix_t & GetInputActivationAt(size_t i) const
typename TCpu< AReal >::Matrix_t Matrix_t
EActivationFunction GetActivationFunction() const
TDescriptors * fDescriptors
Matrix_t & GetInputActivationAt(size_t i)
TWorkspace * GetWorkspace()
size_t GetStrideCols() const
typename TCpu< AReal >::FilterDescriptor_t WeightsDescriptor_t
size_t GetFilterDepth() const
Getters.
size_t GetPaddingHeight() const
size_t GetFilterHeight() const
const Tensor_t & GetInputActivation() const
void Backward(Tensor_t &gradients_backward, const Tensor_t &activations_backward) override
Compute weight, bias and activation gradients.
void InitializeWorkspace()
typename TCpu< AReal >::ReduceTensorDescriptor_t ReduceTensorDescriptor_t
size_t GetNLocalViews() const
typename TCpu< AReal >::AlgorithmHelper_t AlgorithmHelper_t
Scalar_t GetDropoutProbability() const
typename TCpu< AReal >::Scalar_t Scalar_t
void AddWeightsXMLTo(void *parent) override
Writes the information and the weights about the layer in an XML node.
ERegularization GetRegularization() const
void InitializeDescriptors()
void ReadWeightsFromXML(void *parent) override
Read the information and the weights about the layer from XML node.
const Matrix_t & GetWeightsAt(size_t i) const
const Tensor_t & GetOutput() const
size_t GetInputDepth() const
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
const Tensor_t & GetActivationGradients() const
const Matrix_t & GetBiasesAt(size_t i) const
size_t GetInputHeight() const
const Matrix_t & GetBiasGradientsAt(size_t i) const
size_t GetBatchSize() const
Getters.
void ReadMatrixXML(void *node, const char *name, Matrix_t &matrix)
const Matrix_t & GetWeightGradientsAt(size_t i) const
VGeneralLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth, size_t Height, size_t Width, size_t WeightsNSlices, size_t WeightsNRows, size_t WeightsNCols, size_t BiasesNSlices, size_t BiasesNRows, size_t BiasesNCols, size_t OutputNSlices, size_t OutputNRows, size_t OutputNCols, EInitialization Init)
Constructor.
size_t GetInputWidth() const
static TString Itoa(Int_t value, Int_t base)
Converts an Int_t to a TString with respect to the base specified (2-36).
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
void addRegularizationGradients(typename Architecture_t::Matrix_t &A, const typename Architecture_t::Matrix_t &W, typename Architecture_t::Scalar_t weightDecay, ERegularization R)
Add the regularization gradient corresponding to weight matrix W, to the matrix A.
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
ERegularization
Enum representing the regularization type applied for a given layer.
EActivationFunction
Enum that represents layer activation functions.
create variable transformations
size_t strideRows
The number of row pixels to slid the filter each step.
size_t filterHeight
The height of the filter.
size_t inputHeight
The height of the previous layer or input.
size_t batchSize
Batch size used for training and evaluation.
size_t paddingWidth
The number of zero layers left and right of the input.
size_t filterWidth
The width of the filter.
size_t paddingHeight
The number of zero layers added top and bottom of the input.
size_t inputWidth
The width of the previous layer or input.
TConvParams(size_t _batchSize, size_t _inputDepth, size_t _inputHeight, size_t _inputWidth, size_t _numberFilters, size_t _filterHeight, size_t _filterWidth, size_t _strideRows, size_t _strideCols, size_t _paddingHeight, size_t _paddingWidth)
size_t numberFilters
The number of the filters, which is equal to the output's depth.
size_t inputDepth
The depth of the previous layer or input.
size_t strideCols
The number of column pixels to slid the filter each step.