7from graph_nets
import utils_tf
31 python_process = psutil.Process(pid)
32 memoryUse = python_process.memory_info()[0]/(1024.*1024.)
33 print(s,
"memory:",memoryUse,
"(MB)")
38 num_nodes = np.random.randint(num_max_nodes-2, size=1)[0] + 2
39 num_edges = np.random.randint(num_max_edges-1, size=1)[0] + 1
41 "globals": 10*np.random.rand(GLOBAL_FEATURE_SIZE).astype(np.float32)-5.,
42 "nodes": 10*np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32)-5.,
43 "edges": 10*np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32)-5.,
44 "senders": np.random.randint(num_nodes, size=num_edges, dtype=np.int32),
45 "receivers": np.random.randint(num_nodes, size=num_edges, dtype=np.int32)
51 "globals": np.ones((GLOBAL_FEATURE_SIZE),dtype=np.float32),
52 "nodes": np.ones((num_nodes, NODE_FEATURE_SIZE), dtype = np.float32),
53 "edges": np.ones((num_edges, EDGE_FEATURE_SIZE), dtype = np.float32),
54 "senders": np.random.randint(num_nodes, size=num_edges, dtype=np.int32),
55 "receivers": np.random.randint(num_nodes, size=num_edges, dtype=np.int32)
63 return snt.Sequential([
64 snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
65 snt.LayerNorm(axis=-1, create_offset=
True, create_scale=
True)
70 def __init__(self, name="MLPGraphIndependent"):
71 super(MLPGraphIndependent, self).
__init__(name=name)
73 edge_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
74 node_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
75 global_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True))
83 super(MLPGraphNetwork, self).
__init__(name=name)
85 edge_model_fn=make_mlp_model,
86 node_model_fn=make_mlp_model,
87 global_model_fn=make_mlp_model)
96 name="EncodeProcessDecode"):
97 super(EncodeProcessDecode, self).
__init__(name=name)
103 def __call__(self, input_op, num_processing_steps):
107 for _
in range(num_processing_steps):
108 core_input = utils_tf.concat([latent0, latent], axis=1)
109 latent = self.
_core(core_input)
127input_graph_data = utils_tf.data_dicts_to_graphs_tuple([GraphData])
132input_core_graph_data = utils_tf.data_dicts_to_graphs_tuple([CoreGraphData])
139output_gn =
ep_model(input_graph_data, processing_steps)
147encoder = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._encoder._network, GraphData, filename =
"encoder")
149encoder.OutputGenerated()
151core = ROOT.TMVA.Experimental.SOFIE.RModel_GNN.ParseFromMemory(ep_model._core._network, CoreGraphData, filename =
"core")
153core.OutputGenerated()
155decoder = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._decoder._network, DecodeGraphData, filename =
"decoder")
157decoder.OutputGenerated()
159output_transform = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._output_transform._network, DecodeGraphData, filename =
"output_transform")
160output_transform.Generate()
161output_transform.OutputGenerated()
168fileOut = ROOT.TFile.Open(
"graph_data.root",
"RECREATE")
169tree = ROOT.TTree(
"gdata",
"GNN data")
172node_data = ROOT.std.vector[
'float'](num_max_nodes*node_size)
173edge_data = ROOT.std.vector[
'float'](num_max_edges*edge_size)
174global_data = ROOT.std.vector[
'float'](global_size)
175receivers = ROOT.std.vector[
'int'](num_max_edges)
176senders = ROOT.std.vector[
'int'](num_max_edges)
177outgnn = ROOT.std.vector[
'float'](3)
179tree.Branch(
"node_data",
"std::vector<float>" , node_data)
180tree.Branch(
"edge_data",
"std::vector<float>" , edge_data)
181tree.Branch(
"global_data",
"std::vector<float>" , global_data)
182tree.Branch(
"receivers",
"std::vector<int>" , receivers)
183tree.Branch(
"senders",
"std::vector<int>" , senders)
186print(
"\n\nSaving data in a ROOT File:")
187h1 = ROOT.TH1D(
"h1",
"GraphNet nodes output",40,1,0)
188h2 = ROOT.TH1D(
"h2",
"GraphNet edges output",40,1,0)
189h3 = ROOT.TH1D(
"h3",
"GraphNet global output",40,1,0)
191for i
in range(0,numevts):
193 s_nodes = graphData[
'nodes'].size
194 s_edges = graphData[
'edges'].size
195 num_edges = graphData[
'edges'].shape[0]
196 tmp = ROOT.std.vector[
'float'](graphData[
'nodes'].reshape((graphData[
'nodes'].size)))
197 node_data.assign(tmp.begin(),tmp.end())
198 tmp = ROOT.std.vector[
'float'](graphData[
'edges'].reshape((graphData[
'edges'].size)))
199 edge_data.assign(tmp.begin(),tmp.end())
200 tmp = ROOT.std.vector[
'float'](graphData[
'globals'].reshape((graphData[
'globals'].size)))
201 global_data.assign(tmp.begin(),tmp.end())
203 tmp = ROOT.std.vector[
'int'](graphData[
'receivers'])
204 receivers.assign(tmp.begin(),tmp.end())
205 tmp = ROOT.std.vector[
'int'](graphData[
'senders'])
206 senders.assign(tmp.begin(),tmp.end())
207 if (i < 1
and verbose) :
208 print(
"Nodes - shape:",
int(node_data.size()/node_size),node_size,
"data: ",node_data)
209 print(
"Edges - shape:",num_edges, edge_size,
"data: ", edge_data)
210 print(
"Globals : ",global_data)
211 print(
"Receivers : ",receivers)
212 print(
"Senders : ",senders)
217 tf_graph_data = utils_tf.data_dicts_to_graphs_tuple([graphData])
218 dataset.append(tf_graph_data)
229for tf_graph_data
in dataset:
230 output_gnn =
ep_model(tf_graph_data, processing_steps)
231 output_nodes = output_gnn[-1].nodes.numpy()
232 output_edges = output_gnn[-1].edges.numpy()
233 output_globals = output_gnn[-1].globals.numpy()
234 outgnn[0] = np.mean(output_nodes)
235 outgnn[1] = np.mean(output_edges)
236 outgnn[2] = np.mean(output_globals)
240 if (firstEvent
and verbose) :
241 print(
"Output of first event")
242 print(
"nodes data", output_gnn[-1].nodes.numpy())
243 print(
"edge data", output_gnn[-1].edges.numpy())
244 print(
"global data", output_gnn[-1].globals.numpy())
250print(
"time to evaluate events",end-start)
__init__(self, name="EncodeProcessDecode")
__call__(self, input_op, num_processing_steps)
__init__(self, name="MLPGraphIndependent")
__init__(self, name="MLPGraphNetwork")
get_dynamic_graph_data_dict(NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1)
get_fix_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1)