Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Random.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Random
2#define TMVA_SOFIE_ROPERATOR_Random
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14 // Random operator for RandomUniform, RandomUniformLike,
15 // RandomNormal, RandomNormalLike
17
19{
20public:
21
22 bool fUseROOT = true; // use ROOT or std for random number generation
23private:
24
27 std::string fNX;
28 std::string fNY;
29 int fSeed;
30 std::vector<size_t> fShapeY;
31 std::map<std::string,float> fParams; // parameter for random generator (e.g. low,high or mean and scale)
32
33
34
35public:
36
38 ROperator_Random(RandomOpMode mode, ETensorType type, const std::string & nameX, const std::string & nameY, const std::vector<size_t> & shape, const std::map<std::string, float> & params, float seed) :
39 fMode(mode),
40 fType(type),
41 fNX(UTILITY::Clean_name(nameX)),
42 fNY(UTILITY::Clean_name(nameY)),
43 fSeed(seed),
44 fShapeY(shape),
45 fParams(params)
46 {
49 }
50
51
52 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
53 return input;
54 }
55
56 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
57 auto ret = input; //suggest copy to compiler
58 return ret;
59 }
60
61 void Initialize(RModel& model) override {
62
64
65 if (fUseROOT) {
66 model.AddNeededCustomHeader("TRandom3.h");
67 }
68
69 // use default values
70 if (fMode == kNormal) {
71 if (fParams.count("mean") == 0 )
72 fParams["mean"] = 0;
73 if (fParams.count("scale") == 0)
74 fParams["scale"] = 1;
75 }
76 if (fMode == kUniform) {
77 if (fParams.count("low") == 0)
78 fParams["low"] = 0;
79 if (fParams.count("high") == 0)
80 fParams["high"] = 1;
81 }
82
83 if (model.Verbose()) {
84 std::cout << "Random";
85 if (fMode == kNormal) std::cout << "Normal";
86 else if (fMode == kUniform) std::cout << "Uniform";
87 std::cout << " op -> " << fNY << " : " << ConvertShapeToString(fShapeY) << std::endl;
88 for (auto & p : fParams)
89 std::cout << p.first << " : " << p.second << std::endl;
90 }
91 }
92 // generate declaration code for random number generators
93 std::string GenerateDeclCode() override {
94 std::stringstream out;
95 out << "std::unique_ptr<TRandom> fRndmEngine; // random number engine\n";
96 return out.str();
97 }
98 // generate initialization code for random number generators
99 std::string GenerateInitCode() override {
100 std::stringstream out;
101 out << "//--- creating random number generator ----\n";
102 if (fUseROOT) {
103 // generate initialization code for creating random number generator
104 out << SP << "fRndmEngine.reset(new TRandom3(" << fSeed << "));\n";
105 }
106 else {
107 // not supported
108 }
109 return out.str();
110 }
111 std::string Generate(std::string OpName) override {
112 OpName = "op_" + OpName;
113
114 std::stringstream out;
115 out << "\n//------ Random";
116 if (fMode == kNormal) out << "Normal\n";
117 else if (fMode == kUniform) out << "Uniform\n";
118
119 // generate the random array
121 out << SP << "for (int i = 0; i < " << length << "; i++) {\n";
122 if (fUseROOT) {
123 if (fMode == kNormal) {
124 if (fParams.count("mean") == 0 || fParams.count("scale") == 0)
125 throw std::runtime_error("TMVA SOFIE RandomNormal op : no mean or scale are defined");
126 float mean = fParams["mean"];
127 float scale = fParams["scale"];
128 out << SP << SP << "tensor_" << fNY << "[i] = fRndmEngine->Gaus(" << mean << "," << scale << ");\n";
129 } else if (fMode == kUniform) {
130 if (fParams.count("high") == 0 || fParams.count("low") == 0)
131 throw std::runtime_error("TMVA SOFIE RandomUniform op : no low or high are defined");
132 float high = fParams["high"];
133 float low = fParams["low"];
134 out << SP << SP << "tensor_" << fNY << "[i] = fRndmEngine->Uniform(" << low << "," << high << ");\n";
135 }
136 }
137 out << SP << "}\n";
138
139 return out.str();
140 }
141
142 std::vector<std::string> GetStdLibs() override {
143 std::vector<std::string> ret = {"memory"}; // for unique ptr
144 return ret;
145 }
146
147};
148
149}//SOFIE
150}//Experimental
151}//TMVA
152
153
154#endif //TMVA_SOFIE_ROPERATOR_Swish
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
void AddNeededCustomHeader(std::string filename)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:200
ROperator_Random(RandomOpMode mode, ETensorType type, const std::string &nameX, const std::string &nameY, const std::vector< size_t > &shape, const std::map< std::string, float > &params, float seed)
std::vector< std::string > GetStdLibs() override
std::string Generate(std::string OpName) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:42
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:47
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations