13from ROOT
import TMVA, TFile, TString
14from array
import array
15from subprocess
import call
16from os.path
import isfile
26if not isfile(
'tmva_reg_example.root'):
27 call([
'curl',
'-L',
'-O',
'http://root.cern.ch/files/tmva_reg_example.root'])
30tree = data.Get(
'TreeR')
33for branch
in tree.GetListOfBranches():
34 branchName = branch.GetName()
35 branches[branchName] = array(
'f', [-999])
36 tree.SetBranchAddress(branchName, branches[branchName])
37 if branchName !=
'fvalue':
38 reader.AddVariable(branchName, branches[branchName])
42reader.BookMVA(
'PyTorch',
TString(
'dataset/weights/TMVARegression_PyTorch.weights.xml'))
46def predict(model, test_X, batch_size=32):
50 test_dataset = torch.utils.data.TensorDataset(torch.Tensor(test_X))
51 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=
False)
55 for i, data
in enumerate(test_loader):
58 predictions.append(outputs)
59 preds = torch.cat(predictions)
63load_model_custom_objects = {
"optimizer":
None,
"criterion":
None,
"train_func":
None,
"predict_func": predict}
67print(
'Some example regressions:')
70 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.