27 #ifndef TMVA_DNN_GENERALLAYER 28 #define TMVA_DNN_GENERALLAYER 44 template <
typename Architecture_t>
46 using Matrix_t =
typename Architecture_t::Matrix_t;
47 using Scalar_t =
typename Architecture_t::Scalar_t;
75 VGeneralLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
76 size_t Height,
size_t Width,
size_t WeightsNSlices,
size_t WeightsNRows,
size_t WeightsNCols,
77 size_t BiasesNSlices,
size_t BiasesNRows,
size_t BiasesNCols,
size_t OutputNSlices,
size_t OutputNRows,
81 VGeneralLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
82 size_t Height,
size_t Width,
size_t WeightsNSlices, std::vector<size_t> WeightsNRows,
83 std::vector<size_t> WeightsNCols,
size_t BiasesNSlices, std::vector<size_t> BiasesNRows,
84 std::vector<size_t> BiasesNCols,
size_t OutputNSlices,
size_t OutputNRows,
size_t OutputNCols,
102 virtual void Forward(std::vector<Matrix_t> &input,
bool applyDropout =
false) = 0;
106 virtual void Backward(std::vector<Matrix_t> &gradients_backward,
const std::vector<Matrix_t> &activations_backward,
107 std::vector<Matrix_t> &inp1, std::vector<Matrix_t> &inp2) = 0;
125 void CopyWeights(
const std::vector<Matrix_t> &otherWeights);
128 void CopyBiases(
const std::vector<Matrix_t> &otherBiases);
131 virtual void Print()
const = 0;
209 template <
typename Architecture_t>
211 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
212 size_t weightsNRows,
size_t weightsNCols,
size_t biasesNSlices,
213 size_t biasesNRows,
size_t biasesNCols,
size_t outputNSlices,
220 for (
size_t i = 0; i < weightsNSlices; i++) {
221 fWeights.emplace_back(weightsNRows, weightsNCols);
225 for (
size_t i = 0; i < biasesNSlices; i++) {
226 fBiases.emplace_back(biasesNRows, biasesNCols);
230 for (
size_t i = 0; i < outputNSlices; i++) {
231 fOutput.emplace_back(outputNRows, outputNCols);
237 template <
typename Architecture_t>
239 size_t depth,
size_t height,
size_t width,
size_t weightsNSlices,
240 std::vector<size_t> weightsNRows, std::vector<size_t> weightsNCols,
241 size_t biasesNSlices, std::vector<size_t> biasesNRows,
242 std::vector<size_t> biasesNCols,
size_t outputNSlices,
size_t outputNRows,
249 for (
size_t i = 0; i < weightsNSlices; i++) {
250 fWeights.emplace_back(weightsNRows[i], weightsNCols[i]);
254 for (
size_t i = 0; i < biasesNSlices; i++) {
255 fBiases.emplace_back(biasesNRows[i], biasesNCols[i]);
259 for (
size_t i = 0; i < outputNSlices; i++) {
260 fOutput.emplace_back(outputNRows, outputNCols);
266 template <
typename Architecture_t>
273 size_t weightsNSlices = (layer->
GetWeights()).size();
274 size_t weightsNRows = 0;
275 size_t weightsNCols = 0;
277 for (
size_t i = 0; i < weightsNSlices; i++) {
281 fWeights.emplace_back(weightsNRows, weightsNCols);
287 size_t biasesNSlices = (layer->
GetBiases()).size();
288 size_t biasesNRows = 0;
289 size_t biasesNCols = 0;
291 for (
size_t i = 0; i < biasesNSlices; i++) {
295 fBiases.emplace_back(biasesNRows, biasesNCols);
301 size_t outputNSlices = (layer->
GetOutput()).size();
302 size_t outputNRows = 0;
303 size_t outputNCols = 0;
305 for (
size_t i = 0; i < outputNSlices; i++) {
309 fOutput.emplace_back(outputNRows, outputNCols);
315 template <
typename Architecture_t>
322 size_t weightsNSlices = layer.
fWeights.size();
323 size_t weightsNRows = 0;
324 size_t weightsNCols = 0;
326 for (
size_t i = 0; i < weightsNSlices; i++) {
327 weightsNRows = (layer.
fWeights[i]).GetNrows();
328 weightsNCols = (layer.
fWeights[i]).GetNcols();
330 fWeights.emplace_back(weightsNRows, weightsNCols);
336 size_t biasesNSlices = layer.
fBiases.size();
337 size_t biasesNRows = 0;
338 size_t biasesNCols = 0;
340 for (
size_t i = 0; i < biasesNSlices; i++) {
341 biasesNRows = (layer.
fBiases[i]).GetNrows();
342 biasesNCols = (layer.
fBiases[i]).GetNcols();
344 fBiases.emplace_back(biasesNRows, biasesNCols);
350 size_t outputNSlices = layer.
fOutput.size();
351 size_t outputNRows = 0;
352 size_t outputNCols = 0;
354 for (
size_t i = 0; i < outputNSlices; i++) {
355 outputNRows = (layer.
fOutput[i]).GetNrows();
356 outputNCols = (layer.
fOutput[i]).GetNcols();
358 fOutput.emplace_back(outputNRows, outputNCols);
364 template <
typename Architecture_t>
371 template <
typename Architecture_t>
374 for (
size_t i = 0; i <
fWeights.size(); i++) {
379 for (
size_t i = 0; i <
fBiases.size(); i++) {
386 template <
typename Architecture_t>
394 template <
typename Architecture_t>
396 const Scalar_t learningRate) ->
void 398 for (
size_t i = 0; i <
fWeights.size(); i++) {
399 Architecture_t::ScaleAdd(
fWeights[i], weightGradients[i], -learningRate);
404 template <
typename Architecture_t>
406 const Scalar_t learningRate) ->
void 408 for (
size_t i = 0; i <
fBiases.size(); i++) {
409 Architecture_t::ScaleAdd(
fBiases[i], biasGradients[i], -learningRate);
414 template <
typename Architecture_t>
416 const Scalar_t learningRate) ->
void 419 Architecture_t::ScaleAdd(
fWeightGradients[i], weightGradients[i], -learningRate);
424 template <
typename Architecture_t>
426 const Scalar_t learningRate) ->
void 429 Architecture_t::ScaleAdd(
fBiasGradients[i], biasGradients[i], -learningRate);
434 template <
typename Architecture_t>
438 for (
size_t i = 0; i <
fWeights.size(); i++) {
444 template <
typename Architecture_t>
447 for (
size_t i = 0; i <
fBiases.size(); i++) {
454 template <
typename Architecture_t>
459 if (tensor.size() == 0)
return;
460 xmlengine.NewAttr(matnode,0,
"Depth",
gTools().StringFromInt(tensor.size()) );
462 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(tensor[0].GetNrows()) );
463 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(tensor[0].GetNcols()) );
465 for (
size_t i = 0; i < tensor.size(); ++i) {
466 auto & mat = tensor[i];
467 for (
Int_t row = 0; row < mat.GetNrows(); row++) {
468 for (
Int_t col = 0; col < mat.GetNcols(); col++) {
474 xmlengine.AddRawLine( matnode, s.str().c_str() );
478 template <
typename Architecture_t>
484 xmlengine.NewAttr(matnode,0,
"Rows",
gTools().StringFromInt(matrix.GetNrows()) );
485 xmlengine.NewAttr(matnode,0,
"Columns",
gTools().StringFromInt(matrix.GetNcols()) );
488 size_t nrows = matrix.GetNrows();
489 size_t ncols = matrix.GetNcols();
490 for (
size_t row = 0; row < nrows; row++) {
491 for (
size_t col = 0; col < ncols; col++) {
493 s << std::scientific << matrix(row,col) <<
" ";
497 xmlengine.AddRawLine( matnode, s.str().c_str() );
501 template <
typename Architecture_t>
509 R__ASSERT((
size_t) matrix.GetNrows() == rows);
510 R__ASSERT((
size_t) matrix.GetNcols() == cols);
513 std::stringstream matrixStringStream(matrixString);
515 for (
size_t i = 0; i < rows; i++)
517 for (
size_t j = 0; j < cols; j++)
519 #ifndef R__HAS_TMVAGPU 520 matrixStringStream >> matrix(i,j);
523 matrixStringStream >> value;
void SetWidth(size_t width)
size_t GetInputWidth() const
Generic General Layer class.
size_t fDepth
The depth of the layer.
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 std::vector< Matrix_t > & GetWeightGradients() const
void SetBatchSize(size_t batchSize)
Setters.
size_t fHeight
The height of the layer.
size_t fWidth
The width of this layer.
void ReadMatrixXML(void *node, const char *name, Matrix_t &matrix)
const Matrix_t & GetBiasesAt(size_t i) const
void SetDepth(size_t depth)
image html pict1_TGaxis_012 png width
Define new text attributes for the label number "labNum".
EInitialization GetInitialization() const
Matrix_t & GetWeightsAt(size_t i)
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.
std::vector< Matrix_t > fWeights
The weights associated to the layer.
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.
virtual void Forward(std::vector< Matrix_t > &input, bool applyDropout=false)=0
Computes activation of the layer for the given input.
EInitialization fInit
The initialization method.
const Matrix_t & GetOutputAt(size_t i) const
std::vector< Matrix_t > fWeightGradients
Gradients w.r.t. the weights of the layer.
virtual void Print() const =0
Prints the info about the layer.
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
UInt_t Depth(const Node< T > *node)
const std::vector< Matrix_t > & GetWeights() const
Matrix_t & GetOutputAt(size_t i)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
size_t fInputDepth
The depth of the previous layer or input.
virtual void ReadWeightsFromXML(void *parent)=0
Read the information and the weights about the layer from XML node.
const std::vector< Matrix_t > & GetBiasGradients() const
void Init(TClassEdit::TInterpreterLookupHelper *helper)
size_t GetBatchSize() const
Getters.
Matrix_t & GetBiasesAt(size_t i)
std::vector< Matrix_t > fOutput
Activations of this layer.
const std::vector< Matrix_t > & GetActivationGradients() const
std::vector< Matrix_t > & GetWeightGradients()
void UpdateBiasGradients(const std::vector< Matrix_t > &biasGradients, const Scalar_t learningRate)
Updates the bias gradients, given some other weight gradients and learning rate.
std::vector< Matrix_t > & GetBiases()
std::vector< Matrix_t > fBiasGradients
Gradients w.r.t. the bias values of the layer.
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 ~VGeneralLayer()
Virtual Destructor.
void UpdateWeights(const std::vector< Matrix_t > &weightGradients, const Scalar_t learningRate)
Updates the weights, given the gradients and the learning rate,.
void Initialize()
Initialize the weights and biases according to the given initialization method.
const Matrix_t & GetBiasGradientsAt(size_t i) const
typename Architecture_t::Matrix_t Matrix_t
const std::vector< Matrix_t > & GetBiases() const
void SetInputWidth(size_t inputWidth)
size_t GetInputHeight() const
const Matrix_t & GetActivationGradientsAt(size_t i) const
size_t GetInputDepth() const
Matrix_t & GetBiasGradientsAt(size_t i)
void SetInputHeight(size_t inputHeight)
std::vector< Matrix_t > & GetActivationGradients()
std::vector< Matrix_t > & GetBiasGradients()
const Matrix_t & GetWeightsAt(size_t i) const
Matrix_t & GetActivationGradientsAt(size_t i)
void Copy(void *source, void *dest)
std::vector< Matrix_t > fActivationGradients
Gradients w.r.t. the activations of this layer.
std::vector< Matrix_t > fBiases
The biases associated to the layer.
size_t fBatchSize
Batch size used for training and evaluation.
std::vector< Matrix_t > & GetWeights()
static constexpr double s
void UpdateBiases(const std::vector< Matrix_t > &biasGradients, const Scalar_t learningRate)
Updates the biases, given the gradients and the learning rate.
Matrix_t & GetWeightGradientsAt(size_t i)
void SetInputDepth(size_t inputDepth)
std::vector< Matrix_t > & GetOutput()
void SetHeight(size_t height)
Abstract ClassifierFactory template that handles arbitrary types.
void WriteTensorToXML(void *node, const char *name, const std::vector< Matrix_t > &tensor)
helper functions for XML
const Matrix_t & GetWeightGradientsAt(size_t i) const
void SetIsTraining(bool isTraining)
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
typename Architecture_t::Scalar_t Scalar_t
const std::vector< Matrix_t > & GetOutput() const
size_t fInputHeight
The height of the previous layer or input.
void CopyWeights(const std::vector< Matrix_t > &otherWeights)
Copies the weights provided as an input.
size_t fInputWidth
The width of the previous layer or input.
virtual void AddWeightsXMLTo(void *parent)=0
Writes the information and the weights about the layer in an XML node.
bool fIsTraining
Flag indicatig the mode.
const char * Data() const