27#ifndef TMVA_DNN_GENERALLAYER
28#define TMVA_DNN_GENERALLAYER
48template <
typename Architecture_t>
51 using Tensor_t =
typename Architecture_t::Tensor_t;
52 using Matrix_t =
typename Architecture_t::Matrix_t;
53 using Scalar_t =
typename Architecture_t::Scalar_t;
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,
size_t WeightsNRows,
size_t WeightsNCols,
84 size_t BiasesNSlices,
size_t BiasesNRows,
size_t BiasesNCols,
size_t OutputNSlices,
size_t OutputNRows,
88 VGeneralLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
89 size_t Height,
size_t Width,
size_t WeightsNSlices, std::vector<size_t> WeightsNRows,
90 std::vector<size_t> WeightsNCols,
size_t BiasesNSlices, std::vector<size_t> BiasesNRows,
91 std::vector<size_t> BiasesNCols,
size_t OutputNSlices,
size_t OutputNRows,
size_t OutputNCols,
137 void CopyWeights(
const std::vector<Matrix_t> &otherWeights);
140 void CopyBiases(
const std::vector<Matrix_t> &otherBiases);
145 template <
typename Arch>
236template <
typename Architecture_t>
238 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
239 size_t weightsNRows,
size_t weightsNCols,
size_t biasesNSlices,
240 size_t biasesNRows,
size_t biasesNCols,
size_t outputNSlices,
242 : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
243 fHeight(height), fWidth(
width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
244 fOutput( outputNSlices, outputNRows, outputNCols ),
245 fActivationGradients( outputNSlices, outputNRows, outputNCols ),
249 for (
size_t i = 0; i < weightsNSlices; i++) {
250 fWeights.emplace_back(weightsNRows, weightsNCols);
254 for (
size_t i = 0; i < biasesNSlices; i++) {
255 fBiases.emplace_back(biasesNRows, biasesNCols);
261template <
typename Architecture_t>
263 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
264 std::vector<size_t> weightsNRows, std::vector<size_t> weightsNCols,
265 size_t biasesNSlices, std::vector<size_t> biasesNRows,
266 std::vector<size_t> biasesNCols,
size_t outputNSlices,
size_t outputNRows,
268 : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
269 fHeight(height), fWidth(
width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
270 fOutput( outputNSlices, outputNRows, outputNCols ),
271 fActivationGradients( outputNSlices, outputNRows, outputNCols ),
275 for (
size_t i = 0; i < weightsNSlices; i++) {
276 fWeights.emplace_back(weightsNRows[i], weightsNCols[i]);
280 for (
size_t i = 0; i < biasesNSlices; i++) {
281 fBiases.emplace_back(biasesNRows[i], biasesNCols[i]);
292template <
typename Architecture_t>
294 : fBatchSize(layer->GetBatchSize()), fInputDepth(layer->GetInputDepth()), fInputHeight(layer->GetInputHeight()),
295 fInputWidth(layer->GetInputWidth()), fDepth(layer->GetDepth()), fHeight(layer->GetHeight()),
296 fWidth(layer->GetWidth()), fIsTraining(layer->IsTraining()), fWeights(), fBiases(), fWeightGradients(),
298 fOutput( layer->GetOutput().GetShape() ),
299 fActivationGradients( layer->GetActivationGradients().GetShape() ),
300 fInit(layer->GetInitialization() )
303 size_t weightsNSlices = (layer->
GetWeights()).size();
304 size_t weightsNRows = 0;
305 size_t weightsNCols = 0;
307 for (
size_t i = 0; i < weightsNSlices; i++) {
311 fWeights.emplace_back(weightsNRows, weightsNCols);
317 size_t biasesNSlices = (layer->
GetBiases()).size();
318 size_t biasesNRows = 0;
319 size_t biasesNCols = 0;
321 for (
size_t i = 0; i < biasesNSlices; i++) {
325 fBiases.emplace_back(biasesNRows, biasesNCols);
333template <
typename Architecture_t>
335 : fBatchSize(layer.fBatchSize), fInputDepth(layer.fInputDepth), fInputHeight(layer.fInputHeight),
336 fInputWidth(layer.fInputWidth), fDepth(layer.fDepth), fHeight(layer.fHeight), fWidth(layer.fWidth),
337 fIsTraining(layer.fIsTraining), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
338 fOutput( layer.GetOutput() ),
339 fActivationGradients( layer.GetActivationGradients() ),
340 fInit( layer.GetInitialization())
343 size_t weightsNSlices = layer.
fWeights.size();
344 size_t weightsNRows = 0;
345 size_t weightsNCols = 0;
347 for (
size_t i = 0; i < weightsNSlices; i++) {
348 weightsNRows = (layer.
fWeights[i]).GetNrows();
349 weightsNCols = (layer.
fWeights[i]).GetNcols();
351 fWeights.emplace_back(weightsNRows, weightsNCols);
357 size_t biasesNSlices = layer.
fBiases.size();
358 size_t biasesNRows = 0;
359 size_t biasesNCols = 0;
361 for (
size_t i = 0; i < biasesNSlices; i++) {
362 biasesNRows = (layer.
fBiases[i]).GetNrows();
363 biasesNCols = (layer.
fBiases[i]).GetNcols();
365 fBiases.emplace_back(biasesNRows, biasesNCols);
371 size_t outputNSlices = layer.
fOutput.size();
372 size_t outputNRows = 0;
373 size_t outputNCols = 0;
375 for (
size_t i = 0; i < outputNSlices; i++) {
376 outputNRows = (layer.
fOutput[i]).GetNrows();
377 outputNCols = (layer.
fOutput[i]).GetNcols();
379 fOutput.emplace_back(outputNRows, outputNCols);
385template <
typename Architecture_t>
392template <
typename Architecture_t>
395 for (
size_t i = 0; i < fWeights.size(); i++) {
396 initialize<Architecture_t>(fWeights[i], this->GetInitialization());
400 for (
size_t i = 0; i < fBiases.size(); i++) {
407template <
typename Architecture_t>
410 this->UpdateWeights(fWeightGradients, learningRate);
411 this->UpdateBiases(fBiasGradients, learningRate);
415template <
typename Architecture_t>
417 const Scalar_t learningRate) ->
void
419 for (
size_t i = 0; i < fWeights.size(); i++) {
420 Architecture_t::ScaleAdd(fWeights[i], weightGradients[i], -learningRate);
425template <
typename Architecture_t>
427 const Scalar_t learningRate) ->
void
429 for (
size_t i = 0; i < fBiases.size(); i++) {
430 Architecture_t::ScaleAdd(fBiases[i], biasGradients[i], -learningRate);
435template <
typename Architecture_t>
437 const Scalar_t learningRate) ->
void
439 for (
size_t i = 0; i < fWeightGradients.size(); i++) {
440 Architecture_t::ScaleAdd(fWeightGradients[i], weightGradients[i], -learningRate);
445template <
typename Architecture_t>
447 const Scalar_t learningRate) ->
void
449 for (
size_t i = 0; i < fBiasGradients.size(); i++) {
450 Architecture_t::ScaleAdd(fBiasGradients[i], biasGradients[i], -learningRate);
455template <
typename Architecture_t>
459 for (
size_t i = 0; i < fWeights.size(); i++) {
465template <
typename Architecture_t>
468 for (
size_t i = 0; i < fBiases.size(); i++) {
474template <
typename Architecture_t>
475template <
typename Arch>
480 Architecture_t::CopyDiffArch(this->GetWeights(), layer.
GetWeights());
481 Architecture_t::CopyDiffArch(this->GetBiases(), layer.
GetBiases());
485 if (params.size() > 0) {
486 auto paramsToCopy = GetExtraLayerParameters();
487 Architecture_t::CopyDiffArch(paramsToCopy, params );
488 SetExtraLayerParameters(paramsToCopy);
493template <
typename Architecture_t>
498 if (tensor.size() == 0)
return;
499 xmlengine.NewAttr(matnode,0,
"Depth",
gTools().StringFromInt(tensor.size()) );
501 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(tensor[0].GetNrows()) );
502 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(tensor[0].GetNcols()) );
504 for (
size_t i = 0; i < tensor.size(); ++i) {
505 auto & mat = tensor[i];
506 for (
Int_t row = 0; row < mat.GetNrows(); row++) {
507 for (
Int_t col = 0; col < mat.GetNcols(); col++) {
510 s << std::scientific << mat(row, col) <<
" ";
514 xmlengine.AddRawLine( matnode,
s.str().c_str() );
518template <
typename Architecture_t>
524 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(matrix.GetNrows()) );
525 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(matrix.GetNcols()) );
527 s.precision( std::numeric_limits<Scalar_t>::digits10 );
528 size_t nrows = matrix.GetNrows();
529 size_t ncols = matrix.GetNcols();
530 for (
size_t row = 0; row < nrows; row++) {
531 for (
size_t col = 0; col < ncols; col++) {
533 s << std::scientific << matrix(row,col) <<
" ";
537 xmlengine.AddRawLine( matnode,
s.str().c_str() );
541template <
typename Architecture_t>
549 R__ASSERT((
size_t) matrix.GetNrows() == rows);
550 R__ASSERT((
size_t) matrix.GetNcols() == cols);
555 std::stringstream matrixStringStream(matrixString);
557 for (
size_t i = 0; i < rows; i++)
559 for (
size_t j = 0; j < cols; j++)
561#ifndef R__HAS_TMVAGPU
562 matrixStringStream >> tmatrix(i,j);
565 matrixStringStream >> value;
566 tmatrix(i,j) = value;
579template <
typename Architecture>
580auto debugTensor(
const typename Architecture::Tensor_t &
A,
const std::string
name =
"tensor") ->
void
582 Architecture::PrintTensor(
A,
name);
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.
Tensor_t fOutput
Activations of this layer.
const std::vector< Matrix_t > & GetWeightGradients() const
virtual void SetDropoutProbability(Scalar_t)
Set Dropout probability.
void CopyParameters(const VGeneralLayer< Arch > &layer)
Copy all trainable weight and biases from another equivalent layer but with different architecture Th...
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.
virtual 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 SetExtraLayerParameters(const std::vector< Matrix_t > &)
virtual void ReadWeightsFromXML(void *parent)=0
Read the information and the weights about the layer from XML node.
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.
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.
size_t fInputDepth
The depth of the previous layer or input.
void SetWidth(size_t width)
bool fIsTraining
Flag indicating the mode.
const Tensor_t & GetOutput() const
const std::vector< Matrix_t > & GetBiases() const
typename Architecture_t::Scalar_t Scalar_t
std::vector< Matrix_t > & GetBiasGradients()
Tensor_t & GetActivationGradients()
std::vector< Matrix_t > fWeights
The weights associated to the layer.
EInitialization GetInitialization() const
Tensor_t fActivationGradients
Gradients w.r.t. the activations of this layer.
Matrix_t & GetWeightsAt(size_t i)
Matrix_t & GetBiasGradientsAt(size_t i)
size_t GetInputDepth() const
const Matrix_t & GetActivationGradientsAt(size_t i) const
std::vector< Matrix_t > & GetBiases()
virtual std::vector< Matrix_t > GetExtraLayerParameters() const
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
Matrix_t GetActivationGradientsAt(size_t i)
std::vector< Matrix_t > & GetWeightGradients()
const Tensor_t & GetActivationGradients() const
size_t fInputHeight
The height of the previous layer or input.
size_t fDepth
The depth of the layer.
virtual void Backward(Tensor_t &gradients_backward, const Tensor_t &activations_backward)=0
Backpropagates the error.
void CopyBiases(const std::vector< Matrix_t > &otherBiases)
Copies the biases provided as an input.
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 ResetTraining()
Reset some training flags after a loop on all batches Some layer (e.g.
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)
virtual void Forward(Tensor_t &input, bool applyDropout=false)=0
Computes activation of the layer for the given input.
Matrix_t GetOutputAt(size_t i)
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.
typename Architecture_t::Tensor_t Tensor_t
virtual ~VGeneralLayer()
Virtual Destructor.
const Matrix_t & GetOutputAt(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
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
EvaluateInfo init(std::vector< RooRealProxy > parameters, std::vector< ArrayWrapper * > wrappers, std::vector< double * > arrays, size_t begin, size_t batchSize)
void Copy(void *source, void *dest)
void Init(TClassEdit::TInterpreterLookupHelper *helper)
static constexpr double s
auto debugTensor(const typename Architecture::Tensor_t &A, const std::string name="tensor") -> void
UInt_t Depth(const Node< T > *node)
create variable transformations