17from ROOT
import TMVA, TFile, TString, gROOT
18from array
import array
19from subprocess
import call
20from os.path
import isfile
30data =
TFile.Open(str(gROOT.GetTutorialDir()) +
"/machine_learning/data/tmva_reg_example.root")
31tree = data.Get(
'TreeR')
34for branch
in tree.GetListOfBranches():
35 branchName = branch.GetName()
36 branches[branchName] = array(
'f', [-999])
37 tree.SetBranchAddress(branchName, branches[branchName])
38 if branchName !=
'fvalue':
39 reader.AddVariable(branchName, branches[branchName])
43reader.BookMVA(
'PyTorch',
TString(
'dataset/weights/TMVARegression_PyTorch.weights.xml'))
47def predict(model, test_X, batch_size=32):
51 test_dataset = torch.utils.data.TensorDataset(torch.Tensor(test_X))
52 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=
False)
56 for i, data
in enumerate(test_loader):
59 predictions.append(outputs)
60 preds = torch.cat(predictions)
64load_model_custom_objects = {
"optimizer":
None,
"criterion":
None,
"train_func":
None,
"predict_func": predict}
68print(
'Some example regressions:')
71 print(
'True/MVA value: {}/{}'.format(branches[
'fvalue'][0],reader.EvaluateMVA(
'PyTorch')))
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
static void PyInitialize()
Initialize Python interpreter.
The Reader class serves to use the MVAs in a specific analysis context.