Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBatchGenerator_PyTorch.py
Go to the documentation of this file.
1### \file
2### \ingroup tutorial_tmva
3### \notebook -nodraw
4###
5### Example of getting batches of events from a ROOT dataset into a basic
6### PyTorch workflow.
7###
8### \macro_code
9### \macro_output
10### \author Dante Niewenhuis
11
12import torch
13import ROOT
14
15tree_name = "sig_tree"
16file_name = str(ROOT.gROOT.GetTutorialDir()) + "/tmva/data/Higgs_data.root"
17
18batch_size = 128
19chunk_size = 5_000
20
21target = "Type"
22
23# Returns two generators that return training and validation batches
24# as PyTorch tensors.
25gen_train, gen_validation = ROOT.TMVA.Experimental.CreatePyTorchGenerators(
26 tree_name,
27 file_name,
28 batch_size,
29 chunk_size,
30 target=target,
31 validation_split=0.3,
32)
33
34# Get a list of the columns used for training
35input_columns = gen_train.train_columns
36num_features = len(input_columns)
37
38
39def calc_accuracy(targets, pred):
40 return torch.sum(targets == pred.round()) / pred.size(0)
41
42
43# Initialize PyTorch model
45 torch.nn.Linear(num_features, 300),
47 torch.nn.Linear(300, 300),
49 torch.nn.Linear(300, 300),
51 torch.nn.Linear(300, 1),
53)
54loss_fn = torch.nn.MSELoss(reduction="mean")
55optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
56
57
58# Loop through the training set and train model
59for i, (x_train, y_train) in enumerate(gen_train):
60 # Make prediction and calculate loss
61 pred = model(x_train).view(-1)
62 loss = loss_fn(pred, y_train)
63
64 # improve model
68
69 # Calculate accuracy
70 accuracy = calc_accuracy(y_train, pred)
71
72 print(f"Training => accuracy: {accuracy}")
73
74#################################################################
75# Validation
76#################################################################
77
78# Evaluate the model on the validation set
79for i, (x_train, y_train) in enumerate(gen_validation):
80 # Make prediction and calculate accuracy
81 pred = model(x_train).view(-1)
82 accuracy = calc_accuracy(y_train, pred)
83
84 print(f"Validation => accuracy: {accuracy}")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 UChar_t len