5from graph_nets
import utils_tf
12snd = np.array([1,2,3,4,2,3,4,3,4,4,0,0,0,0,1,1,1,2,2,3], dtype=
'int32')
13rec = np.array([0,0,0,0,1,1,1,2,2,3,1,2,3,4,2,3,4,3,4,4], dtype=
'int32')
22def get_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1):
24 "globals": 10*np.random.rand(GLOBAL_FEATURE_SIZE).astype(np.float32)-5.,
25 "nodes": 10*np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32)-5.,
26 "edges": 10*np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32)-5.,
33 return snt.Sequential([
34 snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
35 snt.LayerNorm(axis=-1, create_offset=
True, create_scale=
True)
40 def __init__(self, name="MLPGraphIndependent"):
41 super(MLPGraphIndependent, self).
__init__(name=name)
43 edge_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
44 node_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
45 global_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True))
53 super(MLPGraphNetwork, self).
__init__(name=name)
55 edge_model_fn=make_mlp_model,
56 node_model_fn=make_mlp_model,
57 global_model_fn=make_mlp_model)
66 name="EncodeProcessDecode"):
67 super(EncodeProcessDecode, self).
__init__(name=name)
73 def __call__(self, input_op, num_processing_steps):
77 for _
in range(num_processing_steps):
78 core_input = utils_tf.concat([latent0, latent], axis=1)
79 latent = self.
_core(core_input)
92input_graph_data = utils_tf.data_dicts_to_graphs_tuple([GraphData])
97input_core_graph_data = utils_tf.data_dicts_to_graphs_tuple([CoreGraphData])
103output_gn =
ep_model(input_graph_data, processing_steps)
104print(
"---> Input:\n",input_graph_data)
105print(
"\n\n------> Input core data:\n",input_core_graph_data)
106print(
"\n\n---> Output:\n",output_gn)
109encoder = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._encoder._network, GraphData, filename =
"encoder")
111encoder.OutputGenerated()
113core = ROOT.TMVA.Experimental.SOFIE.RModel_GNN.ParseFromMemory(ep_model._core._network, CoreGraphData, filename =
"core")
115core.OutputGenerated()
117decoder = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._decoder._network, DecodeGraphData, filename =
"decoder")
119decoder.OutputGenerated()
121output_transform = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._output_transform._network, DecodeGraphData, filename =
"output_transform")
122output_transform.Generate()
123output_transform.OutputGenerated()
126ROOT.gInterpreter.Declare(
'#pragma cling optimize(2)')
127ROOT.gInterpreter.Declare(
'#include "encoder.hxx"')
128ROOT.gInterpreter.Declare(
'#include "core.hxx"')
129ROOT.gInterpreter.Declare(
'#include "decoder.hxx"')
130ROOT.gInterpreter.Declare(
'#include "output_transform.hxx"')
134 n = np.asarray(output.node_data)
135 e = np.asarray(output.edge_data)
136 g = np.asarray(output.global_data)
138 print(
"SOFIE data ... shapes",n.shape,e.shape,g.shape)
139 print(
" node data", n.reshape(n.size,))
140 print(
" edge data", e.reshape(e.size,))
141 print(
" global data",g.reshape(g.size,))
144 output_data = ROOT.TMVA.Experimental.SOFIE.Copy(input_data)
164 for _
in range(processing_steps):
165 core_input = ROOT.TMVA.Experimental.SOFIE.Concatenate(latent0, latent, axis=1)
171 output_ops.append(output)
182for i
in range(0,numevts):
189for i
in range(0,numevts):
190 graphData = dataSet[i]
191 gnet_data_i = utils_tf.data_dicts_to_graphs_tuple([graphData])
192 gnetData.append(gnet_data_i)
196 output_gn =
ep_model(inputGraphData, processing_steps)
200hG = ROOT.TH1D(
"hG",
"Result from graphnet",100,1,0)
201for i
in range(0,numevts):
203 g = out[1].globals.numpy()
207print(
"elapsed time for ",numevts,
"events = ",end-start)
211for i
in range(0,numevts):
212 graphData = dataSet[i]
213 input_data = ROOT.TMVA.Experimental.SOFIE.GNN_Data()
214 input_data.node_data = ROOT.TMVA.Experimental.AsRTensor(graphData[
'nodes'])
215 input_data.edge_data = ROOT.TMVA.Experimental.AsRTensor(graphData[
'edges'])
216 input_data.global_data = ROOT.TMVA.Experimental.AsRTensor(graphData[
'globals'])
218 input_data.receivers = graphData[
'receivers']
219 input_data.senders = graphData[
'senders']
220 sofieData.append(input_data)
222print(
"SOFIE Data: first event")
223print(
"receivers",sofieData[0].receivers)
224print(
"senders",sofieData[0].senders)
227print(
"time to convert data to SOFIE format",endSC-end)
229hS = ROOT.TH1D(
"hS",
"Result from SOFIE",100,1,0)
233print(
"time to create SOFIE GNN class", start-start0)
234for i
in range(0,numevts):
236 out = gnn.infer(sofieData[i])
237 g = np.asarray(out[1].global_data)
241print(
"elapsed time for ",numevts,
"events = ",end-start)
252hDe = ROOT.TH1D(
"hDe",
"Difference for edge data",100,1,0)
253hDn = ROOT.TH1D(
"hDn",
"Difference for node data",100,1,0)
254hDg = ROOT.TH1D(
"hDg",
"Difference for global data",100,1,0)
256for i
in range(0,numevts):
257 outSofie = gnn.infer(sofieData[i])
259 edgesG = outGnet[1].edges.numpy()
260 edgesS = np.asarray(outSofie[1].edge_data)
261 if (i == 0) : print(edgesG.shape)
262 for j
in range(0,edgesG.shape[0]) :
263 for k
in range(0,edgesG.shape[1]) :
264 hDe.Fill(edgesG[j,k]-edgesS[j,k])
266 nodesG = outGnet[1].nodes.numpy()
267 nodesS = np.asarray(outSofie[1].node_data)
268 for j
in range(0,nodesG.shape[0]) :
269 for k
in range(0,nodesG.shape[1]) :
270 hDn.Fill(nodesG[j,k]-nodesS[j,k])
272 globG = outGnet[1].globals.numpy()
273 globS = np.asarray(outSofie[1].global_data)
274 for j
in range(0,globG.shape[1]) :
275 hDg.Fill(globG[0,j]-globS[j])
__call__(self, input_op, num_processing_steps)
__init__(self, name="EncodeProcessDecode")
__init__(self, name="MLPGraphIndependent")
__init__(self, name="MLPGraphNetwork")
get_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1)
PrintSofie(output, printShape=False)