27#ifndef TMVA_DNN_BatchNormLayer
28#define TMVA_DNN_BatchNormLayer
63template <
typename Architecture_t>
67 using Scalar_t =
typename Architecture_t::Scalar_t;
68 using Matrix_t =
typename Architecture_t::Matrix_t;
69 using Tensor_t =
typename Architecture_t::Tensor_t;
101 TBatchNormLayer(
size_t batchSize,
size_t inputDepth,
size_t inputHeight,
size_t inputWidth,
102 const std::vector<size_t> & shape,
int axis = -1,
Scalar_t momentum = -1.,
Scalar_t epsilon = 0.0001);
186 std::vector<Matrix_t> params(2);
218template <
typename Architecture_t>
220 size_t inputWidth,
const std::vector<size_t> &shape,
int axis,
222 :
VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth,
223 inputDepth, inputHeight, inputWidth,
227 shape[2], shape[0], shape[1],
241template <
typename Architecture_t>
246 printf(
"Error - copy ctor not implemented\n");
250template <
typename Architecture_t>
254 printf(
"Error - copy ctor not implemented\n");
258template <
typename Architecture_t>
268template <
typename Architecture_t>
273 size_t bndim = gamma.GetNcols();
276 for (
size_t i = 0; i < bndim; ++i) {
290 Architecture_t::InitializeBNormDescriptors(
fDescriptors,
this);
294template <
typename Architecture_t>
310 Architecture_t::BatchNormLayerForwardTraining(
fNormAxis, x2, y2,
316 descr->HelperDescriptor);
336template <
typename Architecture_t>
338 const Tensor_t & activations_backward ) ->
void
344 if (activations_backward.GetLayout() !=
fReshapedData.GetLayout()) {
349 Architecture_t::BatchNormLayerBackward(
fNormAxis,
x, dy, dx,
357 Architecture_t::BatchNormLayerBackward(
fNormAxis, activations_backward,
368template <
typename Architecture_t>
371 std::cout <<
" BATCH NORM Layer: \t";
372 std::cout <<
" Input/Output = ( " ;
373 auto &shape = this->
GetOutput().GetShape();
374 for (
size_t i = 0; i < shape.size(); ++i) {
375 if (i > 0) std::cout <<
" , ";
376 std::cout << shape[i];
379 std::cout <<
"\t Norm dim =" << std::setw(6) << this->
GetWeightsAt(0).GetNcols();
380 std::cout <<
"\t axis = " <<
fNormAxis << std::endl;
381 std::cout << std::endl;
386template <
typename Architecture_t>
411template <
typename Architecture_t>
void ReadWeightsFromXML(void *parent) override
Read the information and the weights about the layer from XML node.
static size_t CalculateNormDim(int axis, size_t c, size_t h, size_t w)
const Matrix_t & GetMuVector() const
void Backward(Tensor_t &gradients_backward, const Tensor_t &activations_backward) override
Compute weight, bias and activation gradients.
typename TCpu< AReal >::Matrix_t Matrix_t
Scalar_t GetMomentum() const
TDescriptors * fDescriptors
int & GetNTrainedBatches()
Scalar_t GetEpsilon() const
Scalar_t GetNormAxis() const
void AddWeightsXMLTo(void *parent) override
Writes the information and the weights about the layer in an XML node.
TBatchNormLayer(const TBatchNormLayer &)
Copy Constructor.
typename TCpu< AReal >::Tensor_t Tensor_t
void Initialize() override
Initialize the weights and biases according to the given initialization method.
Matrix_t & GetVarVector()
const Matrix_t & GetVariance() const
void Print() const override
Printing the layer info.
Matrix_t & GetBatchMean()
const Matrix_t & GetReshapedData() const
void SetExtraLayerParameters(const std::vector< Matrix_t > ¶ms) override
const int & GetNTrainedBatches() const
TBatchNormLayer(TBatchNormLayer< Architecture_t > *layer)
Copy the dense layer provided as a pointer.
const Matrix_t & GetIVariance() const
typename TCpu< AReal >::Scalar_t Scalar_t
Matrix_t & GetReshapedData()
~TBatchNormLayer()
Destructor.
typename TCpu< AReal >::TensorDescriptor_t HelperDescriptor_t
std::vector< Matrix_t > GetExtraLayerParameters() const override
typename TCpu< AReal >::BNormDescriptors_t BNormDescriptors_t
const Matrix_t & GetVarVector() const
const Matrix_t & GetBatchMean() const
void ResetTraining() override
Reset some training flags after a loop on all batches Some layer (e.g.
TBatchNormLayer(size_t batchSize, size_t inputDepth, size_t inputHeight, size_t inputWidth, const std::vector< size_t > &shape, int axis=-1, Scalar_t momentum=-1., Scalar_t epsilon=0.0001)
Constructor.
void Forward(Tensor_t &input, bool inTraining=true) override
Compute activation of the layer for the given input.
Matrix_t & GetIVariance()
const Matrix_t & GetWeightsAt(size_t i) const
const Tensor_t & GetOutput() const
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
const Tensor_t & GetActivationGradients() const
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.
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
create variable transformations