27#ifndef TMVA_DNN_GENERALLAYER
28#define TMVA_DNN_GENERALLAYER
45template <
typename Architecture_t>
47 using Matrix_t =
typename Architecture_t::Matrix_t;
48 using Scalar_t =
typename Architecture_t::Scalar_t;
76 VGeneralLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
77 size_t Height,
size_t Width,
size_t WeightsNSlices,
size_t WeightsNRows,
size_t WeightsNCols,
78 size_t BiasesNSlices,
size_t BiasesNRows,
size_t BiasesNCols,
size_t OutputNSlices,
size_t OutputNRows,
82 VGeneralLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
83 size_t Height,
size_t Width,
size_t WeightsNSlices, std::vector<size_t> WeightsNRows,
84 std::vector<size_t> WeightsNCols,
size_t BiasesNSlices, std::vector<size_t> BiasesNRows,
85 std::vector<size_t> BiasesNCols,
size_t OutputNSlices,
size_t OutputNRows,
size_t OutputNCols,
103 virtual void Forward(std::vector<Matrix_t> &input,
bool applyDropout =
false) = 0;
107 virtual void Backward(std::vector<Matrix_t> &gradients_backward,
const std::vector<Matrix_t> &activations_backward,
108 std::vector<Matrix_t> &inp1, std::vector<Matrix_t> &inp2) = 0;
126 void CopyWeights(
const std::vector<Matrix_t> &otherWeights);
129 void CopyBiases(
const std::vector<Matrix_t> &otherBiases);
210template <
typename Architecture_t>
212 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
213 size_t weightsNRows,
size_t weightsNCols,
size_t biasesNSlices,
214 size_t biasesNRows,
size_t biasesNCols,
size_t outputNSlices,
216 : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
217 fHeight(height), fWidth(
width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
218 fOutput(), fActivationGradients(), fInit(
init)
221 for (
size_t i = 0; i < weightsNSlices; i++) {
222 fWeights.emplace_back(weightsNRows, weightsNCols);
226 for (
size_t i = 0; i < biasesNSlices; i++) {
227 fBiases.emplace_back(biasesNRows, biasesNCols);
231 for (
size_t i = 0; i < outputNSlices; i++) {
232 fOutput.emplace_back(outputNRows, outputNCols);
238template <
typename Architecture_t>
240 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
241 std::vector<size_t> weightsNRows, std::vector<size_t> weightsNCols,
242 size_t biasesNSlices, std::vector<size_t> biasesNRows,
243 std::vector<size_t> biasesNCols,
size_t outputNSlices,
size_t outputNRows,
245 : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
246 fHeight(height), fWidth(
width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
247 fOutput(), fActivationGradients(), fInit(
init)
250 for (
size_t i = 0; i < weightsNSlices; i++) {
251 fWeights.emplace_back(weightsNRows[i], weightsNCols[i]);
255 for (
size_t i = 0; i < biasesNSlices; i++) {
256 fBiases.emplace_back(biasesNRows[i], biasesNCols[i]);
260 for (
size_t i = 0; i < outputNSlices; i++) {
261 fOutput.emplace_back(outputNRows, outputNCols);
267template <
typename Architecture_t>
269 : fBatchSize(layer->GetBatchSize()), fInputDepth(layer->GetInputDepth()), fInputHeight(layer->GetInputHeight()),
270 fInputWidth(layer->GetInputWidth()), fDepth(layer->GetDepth()), fHeight(layer->GetHeight()),
271 fWidth(layer->GetWidth()), fIsTraining(layer->IsTraining()), fWeights(), fBiases(), fWeightGradients(),
272 fBiasGradients(), fOutput(), fActivationGradients(), fInit(layer->GetInitialization())
274 size_t weightsNSlices = (layer->
GetWeights()).size();
275 size_t weightsNRows = 0;
276 size_t weightsNCols = 0;
278 for (
size_t i = 0; i < weightsNSlices; i++) {
282 fWeights.emplace_back(weightsNRows, weightsNCols);
288 size_t biasesNSlices = (layer->
GetBiases()).size();
289 size_t biasesNRows = 0;
290 size_t biasesNCols = 0;
292 for (
size_t i = 0; i < biasesNSlices; i++) {
296 fBiases.emplace_back(biasesNRows, biasesNCols);
302 size_t outputNSlices = (layer->
GetOutput()).size();
303 size_t outputNRows = 0;
304 size_t outputNCols = 0;
306 for (
size_t i = 0; i < outputNSlices; i++) {
310 fOutput.emplace_back(outputNRows, outputNCols);
316template <
typename Architecture_t>
318 : fBatchSize(layer.fBatchSize), fInputDepth(layer.fInputDepth), fInputHeight(layer.fInputHeight),
319 fInputWidth(layer.fInputWidth), fDepth(layer.fDepth), fHeight(layer.fHeight), fWidth(layer.fWidth),
320 fIsTraining(layer.fIsTraining), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(), fOutput(),
321 fActivationGradients(), fInit(layer.fInit)
323 size_t weightsNSlices = layer.
fWeights.size();
324 size_t weightsNRows = 0;
325 size_t weightsNCols = 0;
327 for (
size_t i = 0; i < weightsNSlices; i++) {
328 weightsNRows = (layer.
fWeights[i]).GetNrows();
329 weightsNCols = (layer.
fWeights[i]).GetNcols();
331 fWeights.emplace_back(weightsNRows, weightsNCols);
337 size_t biasesNSlices = layer.
fBiases.size();
338 size_t biasesNRows = 0;
339 size_t biasesNCols = 0;
341 for (
size_t i = 0; i < biasesNSlices; i++) {
342 biasesNRows = (layer.
fBiases[i]).GetNrows();
343 biasesNCols = (layer.
fBiases[i]).GetNcols();
345 fBiases.emplace_back(biasesNRows, biasesNCols);
351 size_t outputNSlices = layer.
fOutput.size();
352 size_t outputNRows = 0;
353 size_t outputNCols = 0;
355 for (
size_t i = 0; i < outputNSlices; i++) {
356 outputNRows = (layer.
fOutput[i]).GetNrows();
357 outputNCols = (layer.
fOutput[i]).GetNcols();
359 fOutput.emplace_back(outputNRows, outputNCols);
365template <
typename Architecture_t>
372template <
typename Architecture_t>
375 for (
size_t i = 0; i < fWeights.size(); i++) {
376 initialize<Architecture_t>(fWeights[i], this->GetInitialization());
380 for (
size_t i = 0; i < fBiases.size(); i++) {
387template <
typename Architecture_t>
390 this->UpdateWeights(fWeightGradients, learningRate);
391 this->UpdateBiases(fBiasGradients, learningRate);
395template <
typename Architecture_t>
397 const Scalar_t learningRate) ->
void
399 for (
size_t i = 0; i < fWeights.size(); i++) {
400 Architecture_t::ScaleAdd(fWeights[i], weightGradients[i], -learningRate);
405template <
typename Architecture_t>
407 const Scalar_t learningRate) ->
void
409 for (
size_t i = 0; i < fBiases.size(); i++) {
410 Architecture_t::ScaleAdd(fBiases[i], biasGradients[i], -learningRate);
415template <
typename Architecture_t>
417 const Scalar_t learningRate) ->
void
419 for (
size_t i = 0; i < fWeightGradients.size(); i++) {
420 Architecture_t::ScaleAdd(fWeightGradients[i], weightGradients[i], -learningRate);
425template <
typename Architecture_t>
427 const Scalar_t learningRate) ->
void
429 for (
size_t i = 0; i < fBiasGradients.size(); i++) {
430 Architecture_t::ScaleAdd(fBiasGradients[i], biasGradients[i], -learningRate);
435template <
typename Architecture_t>
439 for (
size_t i = 0; i < fWeights.size(); i++) {
445template <
typename Architecture_t>
448 for (
size_t i = 0; i < fBiases.size(); i++) {
455template <
typename Architecture_t>
460 if (tensor.size() == 0)
return;
461 xmlengine.NewAttr(matnode,0,
"Depth",
gTools().StringFromInt(tensor.size()) );
463 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(tensor[0].GetNrows()) );
464 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(tensor[0].GetNcols()) );
466 for (
size_t i = 0; i < tensor.size(); ++i) {
467 auto & mat = tensor[i];
468 for (
Int_t row = 0; row < mat.GetNrows(); row++) {
469 for (
Int_t col = 0; col < mat.GetNcols(); col++) {
475 xmlengine.AddRawLine( matnode,
s.str().c_str() );
479template <
typename Architecture_t>
485 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(matrix.GetNrows()) );
486 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(matrix.GetNcols()) );
488 s.precision( std::numeric_limits<Scalar_t>::digits10 );
489 size_t nrows = matrix.GetNrows();
490 size_t ncols = matrix.GetNcols();
491 for (
size_t row = 0; row < nrows; row++) {
492 for (
size_t col = 0; col < ncols; col++) {
494 s << std::scientific << matrix(row,col) <<
" ";
498 xmlengine.AddRawLine( matnode,
s.str().c_str() );
502template <
typename Architecture_t>
510 R__ASSERT((
size_t) matrix.GetNrows() == rows);
511 R__ASSERT((
size_t) matrix.GetNcols() == cols);
514 std::stringstream matrixStringStream(matrixString);
516 for (
size_t i = 0; i < rows; i++)
518 for (
size_t j = 0; j < cols; j++)
520#ifndef R__HAS_TMVAGPU
521 matrixStringStream >> matrix(i,j);
524 matrixStringStream >> value;
include TDocParser_001 C image html pict1_TDocParser_001 png width
Generic General Layer class.
std::vector< Matrix_t > fWeightGradients
Gradients w.r.t. the weights of the layer.
const std::vector< Matrix_t > & GetWeightGradients() const
const Matrix_t & GetWeightsAt(size_t i) const
void SetHeight(size_t height)
void UpdateWeightGradients(const std::vector< Matrix_t > &weightGradients, const Scalar_t learningRate)
Updates the weight gradients, given some other weight gradients and learning rate.
void Initialize()
Initialize the weights and biases according to the given initialization method.
Matrix_t & GetBiasesAt(size_t i)
void SetInputHeight(size_t inputHeight)
std::vector< Matrix_t > fBiasGradients
Gradients w.r.t. the bias values of the layer.
void SetDepth(size_t depth)
virtual void ReadWeightsFromXML(void *parent)=0
Read the information and the weights about the layer from XML node.
virtual 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)=0
Backpropagates the error.
void UpdateBiasGradients(const std::vector< Matrix_t > &biasGradients, const Scalar_t learningRate)
Updates the bias gradients, given some other weight gradients and learning rate.
void SetBatchSize(size_t batchSize)
Setters.
void CopyWeights(const std::vector< Matrix_t > &otherWeights)
Copies the weights provided as an input.
size_t fBatchSize
Batch size used for training and evaluation.
virtual void AddWeightsXMLTo(void *parent)=0
Writes the information and the weights about the layer in an XML node.
std::vector< Matrix_t > fActivationGradients
Gradients w.r.t. the activations of this layer.
void UpdateWeights(const std::vector< Matrix_t > &weightGradients, const Scalar_t learningRate)
Updates the weights, given the gradients and the learning rate,.
typename Architecture_t::Matrix_t Matrix_t
const std::vector< Matrix_t > & GetBiasGradients() const
void SetInputDepth(size_t inputDepth)
const std::vector< Matrix_t > & GetWeights() const
std::vector< Matrix_t > & GetWeights()
size_t fWidth
The width of this layer.
EInitialization fInit
The initialization method.
std::vector< Matrix_t > fBiases
The biases associated to the layer.
void SetIsTraining(bool isTraining)
size_t fInputWidth
The width of the previous layer or input.
size_t fHeight
The height of the layer.
virtual void Print() const =0
Prints the info about the layer.
std::vector< Matrix_t > fOutput
Activations of this layer.
size_t fInputDepth
The depth of the previous layer or input.
void SetWidth(size_t width)
bool fIsTraining
Flag indicatig the mode.
Matrix_t & GetOutputAt(size_t i)
const std::vector< Matrix_t > & GetBiases() const
typename Architecture_t::Scalar_t Scalar_t
std::vector< Matrix_t > & GetBiasGradients()
std::vector< Matrix_t > fWeights
The weights associated to the layer.
EInitialization GetInitialization() const
Matrix_t & GetWeightsAt(size_t i)
Matrix_t & GetBiasGradientsAt(size_t i)
std::vector< Matrix_t > & GetActivationGradients()
size_t GetInputDepth() const
const Matrix_t & GetActivationGradientsAt(size_t i) const
std::vector< Matrix_t > & GetBiases()
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
std::vector< Matrix_t > & GetWeightGradients()
const std::vector< Matrix_t > & GetActivationGradients() const
size_t fInputHeight
The height of the previous layer or input.
size_t fDepth
The depth of the layer.
const std::vector< Matrix_t > & GetOutput() const
void CopyBiases(const std::vector< Matrix_t > &otherBiases)
Copies the biases provided as an input.
std::vector< Matrix_t > & GetOutput()
void Update(const Scalar_t learningRate)
Updates the weights and biases, given the learning rate.
const Matrix_t & GetBiasesAt(size_t i) const
virtual void Forward(std::vector< Matrix_t > &input, bool applyDropout=false)=0
Computes activation of the layer for the given input.
size_t GetInputHeight() const
void SetInputWidth(size_t inputWidth)
const Matrix_t & GetBiasGradientsAt(size_t i) const
void WriteTensorToXML(void *node, const char *name, const std::vector< Matrix_t > &tensor)
helper functions for XML
size_t GetBatchSize() const
Getters.
Matrix_t & GetWeightGradientsAt(size_t i)
void ReadMatrixXML(void *node, const char *name, Matrix_t &matrix)
const Matrix_t & GetWeightGradientsAt(size_t i) const
void UpdateBiases(const std::vector< Matrix_t > &biasGradients, const Scalar_t learningRate)
Updates the biases, given the gradients and the learning rate.
virtual ~VGeneralLayer()
Virtual Destructor.
const Matrix_t & GetOutputAt(size_t i) const
Matrix_t & GetActivationGradientsAt(size_t i)
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
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
void Copy(void *source, void *dest)
void Init(TClassEdit::TInterpreterLookupHelper *helper)
static constexpr double s
UInt_t Depth(const Node< T > *node)
Abstract ClassifierFactory template that handles arbitrary types.