27 #ifndef TMVA_CNN_CONVLAYER 28 #define TMVA_CNN_CONVLAYER 42 template <
typename Architecture_t>
45 using Matrix_t =
typename Architecture_t::Matrix_t;
46 using Scalar_t =
typename Architecture_t::Scalar_t;
76 TConvLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
size_t Height,
77 size_t Width,
size_t WeightsNRows,
size_t WeightsNCols,
size_t BiasesNRows,
size_t BiasesNCols,
78 size_t OutputNSlices,
size_t OutputNRows,
size_t OutputNCols,
EInitialization Init,
size_t FilterDepth,
79 size_t FilterHeight,
size_t FilterWidth,
size_t StrideRows,
size_t StrideCols,
size_t PaddingHeight,
97 void Forward(std::vector<Matrix_t> &input,
bool applyDropout =
false);
103 void Backward(std::vector<Matrix_t> &gradients_backward,
const std::vector<Matrix_t> &activations_backward,
104 std::vector<Matrix_t> &inp1, std::vector<Matrix_t> &inp2);
146 template <
typename Architecture_t>
148 size_t depth,
size_t height,
size_t width,
size_t weightsNRows,
149 size_t weightsNCols,
size_t biasesNRows,
size_t biasesNCols,
150 size_t outputNSlices,
size_t outputNRows,
size_t outputNCols,
152 size_t filterWidth,
size_t strideRows,
size_t strideCols,
size_t paddingHeight,
155 :
VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth, height, width, 1,
156 weightsNRows, weightsNCols, 1, biasesNRows, biasesNCols, outputNSlices, outputNRows,
163 for (
size_t i = 0; i < outputNSlices; i++) {
169 template <
typename Architecture_t>
180 size_t outputNRows = 0;
181 size_t outputNCols = 0;
183 for (
size_t i = 0; i < outputNSlices; i++) {
192 template <
typename Architecture_t>
205 for (
size_t i = 0; i < outputNSlices; i++) {
211 template <
typename Architecture_t>
217 template <
typename Architecture_t>
237 std::vector<int> vIndices(inputTr.GetNrows() * inputTr.GetNcols() );
254 Architecture_t::Im2colFast(inputTr, input[i], vIndices);
283 template <
typename Architecture_t>
285 const std::vector<Matrix_t> &activations_backward,
286 std::vector<Matrix_t> & , std::vector<Matrix_t> &
289 Architecture_t::ConvLayerBackward(
300 template <
typename Architecture_t>
303 std::cout <<
" CONV LAYER: \t";
304 std::cout <<
"( W = " << this->
GetWidth() <<
" , ";
305 std::cout <<
" H = " << this->
GetHeight() <<
" , ";
306 std::cout <<
" D = " << this->
GetDepth() <<
" ) ";
308 std::cout <<
"\t Filter ( W = " << this->
GetFilterWidth() <<
" , ";
312 std::cout <<
"\tOutput = ( " << this->
GetOutput().size() <<
" , " << this->
GetOutput()[0].GetNrows() <<
" , " << this->
GetOutput()[0].GetNcols() <<
" ) ";
314 std::vector<std::string> activationNames = {
"Identity",
"Relu",
"Sigmoid",
"Tanh",
"SymmRelu",
"SoftSign",
"Gauss" };
315 std::cout <<
"\t Activation Function = ";
316 std::cout << activationNames[ static_cast<int>(
fF) ] << std::endl;
320 template <
typename Architecture_t>
345 template <
typename Architecture_t>
size_t GetInputWidth() const
size_t GetFilterWidth() const
Generic General Layer class.
std::vector< Matrix_t > fDerivatives
First fDerivatives of the activations of this layer.
size_t fStrideRows
The number of row pixels to slid the filter each step.
static TString Itoa(Int_t value, Int_t base)
Converts an Int_t to a TString with respect to the base specified (2-36).
size_t GetNLocalViewPixels() const
size_t GetStrideRows() const
size_t fFilterHeight
The height of the filter.
void ReadMatrixXML(void *node, const char *name, Matrix_t &matrix)
const Matrix_t & GetBiasesAt(size_t i) const
image html pict1_TGaxis_012 png width
Define new text attributes for the label number "labNum".
Scalar_t GetDropoutProbability() const
std::vector< Matrix_t > & GetDerivatives()
const Matrix_t & GetDerivativesAt(size_t i) const
size_t fPaddingWidth
The number of zero layers left and right of the input.
size_t GetFilterDepth() const
Getters.
size_t fNLocalViews
The number of local views in one image.
Scalar_t GetWeightDecay() const
size_t fFilterWidth
The width of the filter.
UInt_t Depth(const Node< T > *node)
size_t GetStrideCols() const
Matrix_t & GetOutputAt(size_t i)
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
Scalar_t fDropoutProbability
Probability that an input is active.
ERegularization GetRegularization() const
void Init(TClassEdit::TInterpreterLookupHelper *helper)
size_t GetBatchSize() const
Getters.
const std::vector< Matrix_t > & GetActivationGradients() const
Scalar_t fWeightDecay
The weight decay.
void Forward(std::vector< Matrix_t > &input, bool applyDropout=false)
Computes activation of the layer for the given input.
const Matrix_t & GetBiasGradientsAt(size_t i) const
void Print() const
Prints the info about the layer.
void Backward(std::vector< Matrix_t > &gradients_backward, const std::vector< Matrix_t > &activations_backward, std::vector< Matrix_t > &inp1, std::vector< Matrix_t > &inp2)
Compute weight, bias and activation gradients.
size_t GetInputHeight() const
size_t fNLocalViewPixels
The number of pixels in one local image view.
size_t fPaddingHeight
The number of zero layers added top and bottom of the input.
size_t GetFilterHeight() const
virtual void ReadWeightsFromXML(void *parent)
Read the information and the weights about the layer from XML node.
Matrix_t & GetDerivativesAt(size_t i)
TConvLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth, size_t Height, size_t Width, size_t WeightsNRows, size_t WeightsNCols, size_t BiasesNRows, size_t BiasesNCols, size_t OutputNSlices, size_t OutputNRows, size_t OutputNCols, EInitialization Init, size_t FilterDepth, 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.
size_t GetPaddingHeight() const
const Matrix_t & GetWeightsAt(size_t i) const
size_t fStrideCols
The number of column pixels to slid the filter each step.
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
EActivationFunction GetActivationFunction() const
EActivationFunction fF
Activation function of the layer.
virtual void AddWeightsXMLTo(void *parent)
Writes the information and the weights about the layer in an XML node.
typename Architecture_t::Scalar_t Scalar_t
Abstract ClassifierFactory template that handles arbitrary types.
const std::vector< Matrix_t > & GetDerivatives() const
const Matrix_t & GetWeightGradientsAt(size_t i) const
size_t GetPaddingWidth() const
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
size_t fFilterDepth
The depth of the filter.
size_t GetNLocalViews() const
ERegularization
Enum representing the regularization type applied for a given layer.
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
EActivationFunction
Enum that represents layer activation functions.
const std::vector< Matrix_t > & GetOutput() const
ERegularization fReg
The regularization method.
typename Architecture_t::Matrix_t Matrix_t
std::vector< int > fForwardIndices
Vector of indices used for a fast Im2Col in forward pass.
std::vector< int > fBackwardIndices
Vector of indices used for a fast Im2Col in backward pass.