Closed ytgcljj closed 5 years ago
Thanks for opening the issue. That seems to be taken form the usage example:
import graph_nets as gn
import sonnet as snt
# Provide your own functions to generate graph-structured data.
input_graphs = get_graphs()
In that example get_graphs()
is assumed to be a function provided by the user (see comment) that returns a graphs.GraphsTuple
. You can find more information on how to build those in the demos.
As a newbie, I am confused about the data format of Graph Nets input. I don't understand how Graph Nets works. I hope you can provide some simple styles of data. Thank you
I create my own graphs with the code as
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
Hi, you are feeding numpy data into your graph. You should do
input_graphs = utils_tf.data_dicts_to_graphs_tuple([data_dic])
@vbapst yep, you are right. utils_tf.data_dicts_to_graphs_tuple([data_dic])
fix the no as_list
error well.
And for more information, I should also change my np dtype from int32 to float, for an example,
G.add_node(node_index, features=np.array([node_index]))
should change to
G.add_node(node_index, features=np.array([node_index], dtype=np.float))
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.
Hope this helps.
Leaving this here as a note to others who might end up here because googling the error has this as a top result. This same error happens if you mistakenly use utils_np.data_dicts_to_graphs_tuple instead of utils_tf.data_dicts_to_graphs_tuple. So ensure you are using the tf version if you are passing the resultant object into tensorflow.
NameError: name 'get_graphs' is not defined
launchx@launchx-System-Product-Name:~$ python Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information.