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

I create my own graphs with the code as #27

Closed luyifanlu closed 5 years ago

luyifanlu commented 5 years ago
def get_ordered_multi_digraph():
  G = nx.OrderedMultiDiGraph()
  # 定义图的节点
  for node_index in range(10):    
    # add_node 接受attr_dict=None, **attr. 
    # graph_nets的networkx_to_data_dict方法需要graph具有一个feature属性,
    # feature属性表示的是节点的特征
    G.add_node(node_index, features=np.array([node_index]))

  # 定义 receivers和senders 
  senders = [1,1,2,2,3,4,5,3]
  receivers = [2,3,4,5,6,8,8,7]
  # 定义边
  for edge_index, (receiver, sender) in enumerate(zip(receivers, senders)):
    # Removing the "index" key makes this test fail 100%.
    edge_data = {"features": np.array([edge_index]), "index": edge_index}
    G.add_edge(sender, receiver, **edge_data)
  G.graph["features"] = np.array([0.])
  #H = nx.path_graph(10)  # 生成10个节点,由0-9表示,由10-1条边线性连接
  #G.add_nodes_from(H) # 把10个节点加入到图里
  #G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(4,8),(5,8),(3,7)], weight=3) # 把指定的节点连接起来
  print(list(G.nodes(data=True)))
  return G  

When I run the sample as

nxs = get_ordered_multi_digraph()
show_graph(nxs)
input_graphs = utils_np.networkxs_to_graphs_tuple([nxs])
# input_graphs = utils_np.data_dicts_to_graphs_tuple([data_dic])
# Create the graph network module.
graph_net_module = gn.modules.GraphNetwork(
    edge_model_fn=lambda: snt.nets.MLP([32, 32]),
    node_model_fn=lambda: snt.nets.MLP([32, 32]),
    global_model_fn=lambda: snt.nets.MLP([32, 32]))

# Pass the input graphs to the graph network, and return the output graphs.
output_graphs = graph_net_module(input_graphs)
res = utils_np.graphs_tuple_to_networkxs(output_graphs)

I got an error as

/usr/local/lib/python3.6/dist-packages/graph_nets/utils_tf.py in repeat(tensor, repeats, axis, name)
    535     repeated_tensor = _inside_to_axis(repeated_shifted_tensor, axis)
    536 
--> 537     shape = tensor.shape.as_list()
    538     shape[axis] = None
    539     repeated_tensor.set_shape(shape)

AttributeError: 'tuple' object has no attribute 'as_list'

originally defined at:
  File "<ipython-input-8-a4b8bd9ccc0d>", line 11, in <module>
    global_model_fn=lambda: snt.nets.MLP([32, 32]))
  File "/usr/local/lib/python3.6/dist-packages/graph_nets/modules.py", line 282, in __init__
    edge_model_fn=edge_model_fn, **edge_block_opt)
  File "/usr/local/lib/python3.6/dist-packages/graph_nets/blocks.py", line 403, in __init__
    super(EdgeBlock, self).__init__(name=name)
  File "/usr/local/lib/python3.6/dist-packages/sonnet/python/modules/base.py", line 215, in __init__
    custom_getter_=self._custom_getter)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/template.py", line 154, in make_template
    **kwargs)

I use tfp-nightly-0.6.0 and dm-sonnet-1.23, graph-nets-1.0.2

_Originally posted by @jcsh4326 in https://github.com/deepmind/graph_nets/issues/6#issuecomment-440900296_

luyifanlu commented 5 years ago

There are seem no global-level attributes in the graphs. dose it work?why?

jcsh4326 commented 5 years ago

There is a global feature with

G.graph["features"] = np.array([0.])

I also update my comment in #6(comment) where you can see more information.

mmv commented 5 years ago

You seem to be trying to run the network directly against a GraphTuple with numpy arrays (hence .shape being a tuple instead of TensorShape).

In order to run the network you must feed it tf.placeholder objects which are filled with the actual values through session.run(). There are some helper functions to create tf.placeholder objects based on the GraphTuple. Check the example notebooks for now to run the network.

alvarosg commented 5 years ago

Closing due to inactivity, since the last answer seems to have worked for the OP.

Run "graph nets basics demo" in browser

The new "graph nets basics demo" is a tutorial containing step by step examples about how to create and manipulate graphs, how to feed them into graph networks and how to build custom graph network modules. It should contain most of the answers to these questions.