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);
213template <
typename Architecture_t>
215 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
216 size_t weightsNRows,
size_t weightsNCols,
size_t biasesNSlices,
217 size_t biasesNRows,
size_t biasesNCols,
size_t outputNSlices,
219 : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
220 fHeight(height), fWidth(
width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
221 fOutput(), fActivationGradients(), fInit(
init)
224 for (
size_t i = 0; i < weightsNSlices; i++) {
225 fWeights.emplace_back(weightsNRows, weightsNCols);
229 for (
size_t i = 0; i < biasesNSlices; i++) {
230 fBiases.emplace_back(biasesNRows, biasesNCols);
234 for (
size_t i = 0; i < outputNSlices; i++) {
235 fOutput.emplace_back(outputNRows, outputNCols);
241template <
typename Architecture_t>
243 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
244 std::vector<size_t> weightsNRows, std::vector<size_t> weightsNCols,
245 size_t biasesNSlices, std::vector<size_t> biasesNRows,
246 std::vector<size_t> biasesNCols,
size_t outputNSlices,
size_t outputNRows,
248 : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
249 fHeight(height), fWidth(
width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
250 fOutput(), fActivationGradients(), fInit(
init)
253 for (
size_t i = 0; i < weightsNSlices; i++) {
254 fWeights.emplace_back(weightsNRows[i], weightsNCols[i]);
258 for (
size_t i = 0; i < biasesNSlices; i++) {
259 fBiases.emplace_back(biasesNRows[i], biasesNCols[i]);
263 for (
size_t i = 0; i < outputNSlices; i++) {
264 fOutput.emplace_back(outputNRows, outputNCols);
270template <
typename Architecture_t>
272 : fBatchSize(layer->GetBatchSize()), fInputDepth(layer->GetInputDepth()), fInputHeight(layer->GetInputHeight()),
273 fInputWidth(layer->GetInputWidth()), fDepth(layer->GetDepth()), fHeight(layer->GetHeight()),
274 fWidth(layer->GetWidth()), fIsTraining(layer->IsTraining()), fWeights(), fBiases(), fWeightGradients(),
275 fBiasGradients(), fOutput(), fActivationGradients(), fInit(layer->GetInitialization())
277 size_t weightsNSlices = (layer->
GetWeights()).size();
278 size_t weightsNRows = 0;
279 size_t weightsNCols = 0;
281 for (
size_t i = 0; i < weightsNSlices; i++) {
285 fWeights.emplace_back(weightsNRows, weightsNCols);
291 size_t biasesNSlices = (layer->
GetBiases()).size();
292 size_t biasesNRows = 0;
293 size_t biasesNCols = 0;
295 for (
size_t i = 0; i < biasesNSlices; i++) {
299 fBiases.emplace_back(biasesNRows, biasesNCols);
305 size_t outputNSlices = (layer->
GetOutput()).size();
306 size_t outputNRows = 0;
307 size_t outputNCols = 0;
309 for (
size_t i = 0; i < outputNSlices; i++) {
313 fOutput.emplace_back(outputNRows, outputNCols);
319template <
typename Architecture_t>
321 : fBatchSize(layer.fBatchSize), fInputDepth(layer.fInputDepth), fInputHeight(layer.fInputHeight),
322 fInputWidth(layer.fInputWidth), fDepth(layer.fDepth), fHeight(layer.fHeight), fWidth(layer.fWidth),
323 fIsTraining(layer.fIsTraining), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(), fOutput(),
324 fActivationGradients(), fInit(layer.fInit)
326 size_t weightsNSlices = layer.
fWeights.size();
327 size_t weightsNRows = 0;
328 size_t weightsNCols = 0;
330 for (
size_t i = 0; i < weightsNSlices; i++) {
331 weightsNRows = (layer.
fWeights[i]).GetNrows();
332 weightsNCols = (layer.
fWeights[i]).GetNcols();
334 fWeights.emplace_back(weightsNRows, weightsNCols);
340 size_t biasesNSlices = layer.
fBiases.size();
341 size_t biasesNRows = 0;
342 size_t biasesNCols = 0;
344 for (
size_t i = 0; i < biasesNSlices; i++) {
345 biasesNRows = (layer.
fBiases[i]).GetNrows();
346 biasesNCols = (layer.
fBiases[i]).GetNcols();
348 fBiases.emplace_back(biasesNRows, biasesNCols);
354 size_t outputNSlices = layer.
fOutput.size();
355 size_t outputNRows = 0;
356 size_t outputNCols = 0;
358 for (
size_t i = 0; i < outputNSlices; i++) {
359 outputNRows = (layer.
fOutput[i]).GetNrows();
360 outputNCols = (layer.
fOutput[i]).GetNcols();
362 fOutput.emplace_back(outputNRows, outputNCols);
368template <
typename Architecture_t>
375template <
typename Architecture_t>
378 for (
size_t i = 0; i < fWeights.size(); i++) {
379 initialize<Architecture_t>(fWeights[i], this->GetInitialization());
383 for (
size_t i = 0; i < fBiases.size(); i++) {
390template <
typename Architecture_t>
393 this->UpdateWeights(fWeightGradients, learningRate);
394 this->UpdateBiases(fBiasGradients, learningRate);
398template <
typename Architecture_t>
400 const Scalar_t learningRate) ->
void
402 for (
size_t i = 0; i < fWeights.size(); i++) {
403 Architecture_t::ScaleAdd(fWeights[i], weightGradients[i], -learningRate);
408template <
typename Architecture_t>
410 const Scalar_t learningRate) ->
void
412 for (
size_t i = 0; i < fBiases.size(); i++) {
413 Architecture_t::ScaleAdd(fBiases[i], biasGradients[i], -learningRate);
418template <
typename Architecture_t>
420 const Scalar_t learningRate) ->
void
422 for (
size_t i = 0; i < fWeightGradients.size(); i++) {
423 Architecture_t::ScaleAdd(fWeightGradients[i], weightGradients[i], -learningRate);
428template <
typename Architecture_t>
430 const Scalar_t learningRate) ->
void
432 for (
size_t i = 0; i < fBiasGradients.size(); i++) {
433 Architecture_t::ScaleAdd(fBiasGradients[i], biasGradients[i], -learningRate);
438template <
typename Architecture_t>
442 for (
size_t i = 0; i < fWeights.size(); i++) {
448template <
typename Architecture_t>
451 for (
size_t i = 0; i < fBiases.size(); i++) {
458template <
typename Architecture_t>
463 if (tensor.size() == 0)
return;
464 xmlengine.NewAttr(matnode,0,
"Depth",
gTools().StringFromInt(tensor.size()) );
466 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(tensor[0].GetNrows()) );
467 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(tensor[0].GetNcols()) );
469 for (
size_t i = 0; i < tensor.size(); ++i) {
470 auto & mat = tensor[i];
471 for (
Int_t row = 0; row < mat.GetNrows(); row++) {
472 for (
Int_t col = 0; col < mat.GetNcols(); col++) {
478 xmlengine.AddRawLine( matnode,
s.str().c_str() );
482template <
typename Architecture_t>
488 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(matrix.GetNrows()) );
489 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(matrix.GetNcols()) );
491 s.precision( std::numeric_limits<Scalar_t>::digits10 );
492 size_t nrows = matrix.GetNrows();
493 size_t ncols = matrix.GetNcols();
494 for (
size_t row = 0; row < nrows; row++) {
495 for (
size_t col = 0; col < ncols; col++) {
497 s << std::scientific << matrix(row,col) <<
" ";
501 xmlengine.AddRawLine( matnode,
s.str().c_str() );
505template <
typename Architecture_t>
513 R__ASSERT((
size_t) matrix.GetNrows() == rows);
514 R__ASSERT((
size_t) matrix.GetNcols() == cols);
517 std::stringstream matrixStringStream(matrixString);
519 for (
size_t i = 0; i < rows; i++)
521 for (
size_t j = 0; j < cols; j++)
523#ifndef R__HAS_TMVAGPU
524 matrixStringStream >> matrix(i,j);
527 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
virtual void SetDropoutProbability(Scalar_t)
Set Dropout probability.
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)
create variable transformations