Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBatchGenerator_TensorFlow.py File Reference

Detailed Description

View in nbviewer Open in SWAN

Example of getting batches of events from a ROOT dataset into a basic TensorFlow workflow.

import tensorflow as tf
import ROOT
tree_name = "sig_tree"
file_name = "http://root.cern/files/Higgs_data.root"
batch_size = 128
chunk_size = 5_000
target = "Type"
# Returns two TF.Dataset for training and validation batches.
ds_train, ds_valid = ROOT.TMVA.Experimental.CreateTFDatasets(
tree_name,
file_name,
batch_size,
chunk_size,
validation_split=0.3,
target=target,
)
# Get a list of the columns used for training
input_columns = ds_train.train_columns
num_features = len(input_columns)
##############################################################################
# AI example
##############################################################################
# Define TensorFlow model
model = tf.keras.Sequential(
[
tf.keras.layers.Dense(
300, activation=tf.nn.tanh, input_shape=(num_features,)
), # input shape required
tf.keras.layers.Dense(300, activation=tf.nn.tanh),
tf.keras.layers.Dense(300, activation=tf.nn.tanh),
tf.keras.layers.Dense(1, activation=tf.nn.sigmoid),
]
)
loss_fn = tf.keras.losses.BinaryCrossentropy()
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
# Train model
model.fit(ds_train, validation_data=ds_valid, epochs=2)
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
Epoch 1/2
1/Unknown - 8s 8s/step - loss: 0.7672 - accuracy: 0.3672␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
11/Unknown - 9s 5ms/step - loss: 0.0818 - accuracy: 0.9425␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
23/Unknown - 9s 5ms/step - loss: 0.0391 - accuracy: 0.9725␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
35/Unknown - 9s 5ms/step - loss: 0.0257 - accuracy: 0.9819␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
46/Unknown - 9s 5ms/step - loss: 0.0196 - accuracy: 0.9862␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
54/54 [==============================] - 9s 14ms/step - loss: 0.0167 - accuracy: 0.9883 - val_loss: 5.0032e-07 - val_accuracy: 1.0000
Epoch 2/2
1/54 [..............................] - ETA: 2s - loss: 4.4667e-07 - accuracy: 1.0000␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
15/54 [=======>......................] - ETA: 0s - loss: 5.1714e-07 - accuracy: 1.0000␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
28/54 [==============>...............] - ETA: 0s - loss: 5.0045e-07 - accuracy: 1.0000␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
41/54 [=====================>........] - ETA: 0s - loss: 4.9994e-07 - accuracy: 1.0000␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
53/54 [============================>.] - ETA: 0s - loss: 5.0212e-07 - accuracy: 1.0000␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈␈
54/54 [==============================] - 0s 5ms/step - loss: 5.0098e-07 - accuracy: 1.0000 - val_loss: 4.7474e-07 - val_accuracy: 1.0000
Author
Dante Niewenhuis

Definition in file RBatchGenerator_TensorFlow.py.