google-deepmind / graph_nets

Build Graph Nets in Tensorflow
https://arxiv.org/abs/1806.01261
Apache License 2.0
5.34k stars 783 forks source link

Can't plot GN output_graphs #69

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi, I have trouble to plot the GN output_graphs after training. Is the only way to show output graph figure --- to update node/edge/global through blocks.broadcast, and then run output_graphs=sess.run() ? Is it possible to print out all the updated attributes for node/edge/global? Current output only print the shape. Thank you.

tf.reset_default_graph()
OUTPUT_EDGE_SIZE = 19
OUTPUT_NODE_SIZE = 19
OUTPUT_GLOBAL_SIZE = 19

graph_network = modules.GraphNetwork(
    edge_model_fn = lambda:snt.Linear(output_size = OUTPUT_EDGE_SIZE),
    node_model_fn = lambda:snt.Linear(output_size = OUTPUT_NODE_SIZE),
    global_model_fn = lambda:snt.Linear(output_size = OUTPUT_GLOBAL_SIZE)
)
#
# ################# Feeding a GraphsTuple######
input_graphs = utils_tf.data_dicts_to_graphs_tuple(data_dict_list)

output_graphs= graph_network(input_graphs)

with tf.Session() as sess:
  output_graph_np = sess.run(output_graphs)

graphs_nx = utils_np.graphs_tuple_to_networkxs(output_graph_np)
_, axs = plt.subplots(ncols=2, figsize=(6, 3))
for iax, (graph_nx, ax) in enumerate(zip(graphs_nx, axs)):
  nx.draw(graph_nx, ax=ax)
  ax.set_title("Graph {}".format(iax))

The error message is :

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value graph_network/node_block/linear/b [[node graph_network/node_block/linear/b/read (defined at /home/zz/venv/lib/python3.5/site-packages/sonnet/python/modules/util.py:961) ]]

This model's output variables are:

Instructions for updating: Colocations handled automatically by placer. <tf.Variable 'graph_network/edge_block/linear/b:0' shape=(19,) dtype=float32_ref> <tf.Variable 'graph_network/edge_block/linear/w:0' shape=(11, 19) dtype=float32_ref> <tf.Variable 'graph_network/global_block/linear/b:0' shape=(19,) dtype=float32_ref> <tf.Variable 'graph_network/global_block/linear/w:0' shape=(41, 19) dtype=float32_ref> <tf.Variable 'graph_network/node_block/linear/b:0' shape=(19,) dtype=float32_ref> <tf.Variable 'graph_network/node_block/linear/w:0' shape=(25, 19) dtype=float32_ref>

alvarosg commented 5 years ago

If you want to use a tf.Session you need to run an initialization op to initialize any variables in the tensorflow graph. Alternatively you may use a tf.train.MonitoredSession, which does that for you automatically.

ghost commented 5 years ago

Appreciated for all the comments! It works!