danielegrattarola / spektral

Graph Neural Networks with Keras and Tensorflow 2.
https://graphneural.network
MIT License
2.36k stars 335 forks source link

Scipy Sparse or TF Sparse?? #379

Open WilliamKorcari opened 2 years ago

WilliamKorcari commented 2 years ago

Dear Devs, thank you for this library. I am currently writing my model using it, but I am a bit confused about the usage. I am working with sparse matrices but I find myself struggling with which type I should use. When I build my dataset according to your documentation, the spektral.data.graph.Graph containers expect Scipy matrices. However, when it comes the time to feed my data to the spektral.layers.GeneralConv layer, I get the following error:

  File "main_GAN.py", line 184, in <module>
    test = generator((active_node_features[0], sparse_edges[0]))
  File "/beegfs/desy/user/korcariw/conda/envs/tf29/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "main_GAN.py", line 120, in call
    x = self._gc0([x, a])
  File "/beegfs/desy/user/korcariw/conda/envs/tf29/lib/python3.8/site-packages/spektral/layers/convolutional/general_conv.py", line 137, in call
    x, a, _ = self.get_inputs(inputs)
  File "/beegfs/desy/user/korcariw/conda/envs/tf29/lib/python3.8/site-packages/spektral/layers/convolutional/message_passing.py", line 183, in get_inputs
    assert K.is_sparse(a), "A must be a SparseTensor"
AssertionError: Exception encountered when calling layer "general_conv" (type GeneralConv).

A must be a SparseTensor

Call arguments received by layer "general_conv" (type GeneralConv):
  • inputs=['tf.Tensor(shape=(10, 2), dtype=float32)', "<10x10 sparse matrix of type '<class 'numpy.int64'>'\n\twith 19 stored elements in Compressed Sparse Row format>"]
  • kwargs={'training': 'None'}

Am I missing something or there is some incompatibility between what the dataset construction requires and what the layer pretends as an input? I looked up the documentation but I could not find clear answers except that the adjacency matrices have to be sparse, without further specifications.

danielegrattarola commented 2 years ago

This is correct. A dataset is not expected to contain tensors, the data is converted to tensors automatically by loaders. Otherwise, you must do the conversion manually when feeding the data to a model.