dmlc / dgl

Python package built to ease deep learning on graph, on top of existing DL frameworks.
http://dgl.ai
Apache License 2.0
13.53k stars 3.02k forks source link

There have big bug if I use DGL with tensorflow #6474

Closed qq492947833 closed 11 months ago

qq492947833 commented 1 year ago

I notice that your function support tensorflow know,but there have big problem now. When I use code:

g=dgl.graph((start_point,end_point),num_nodes=vx.shape[1]) 
g.edata['w']=eweight

start_point and end_point and vx and eweight are all np.narray type.

it will raise error:

AttributeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_410264\883440279.py in <cell line: 1>() ----> 1 model,predicty,testy,r,p,heatmap,weights=Auto_GCN(vy,vx[:,::2,::2,:],lat1[::2],lon1[::2],[['gc',16],['batchnormalization'],['activation','tanh'],['gc',32],['batchnormalization'],['activation','tanh'],['sortpooling',5],['batchnormalization'],['activation','tanh'],['flatten'],['fc',2],['activation','tanh']],r_number=9,test_size=0.25,ifrandom_split='yes',if_weight_initialize='no',weight_initialize_method='TruncatedNormal',weight_initialize_parameter1=0.00,weight_initialize_parameter2=0.05,if_print_model='yes',loss_function='Pearsonr',optimizer='Adam',metrics='Pearsonr',learning_rate=0.001,epochs=200,batch_size=20,ifheatmap='yes',ifweight='shap',device='gpu')

~\AppData\Local\Temp\ipykernel_410264\2527131151.py in Auto_GCN(vy, vx, lat, lon, model_list, r_number, test_size, task_mode, if_best_mode, modelpath, ifrandom_split, if_weight_initialize, weight_initialize_method, weight_initialize_parameter1, weight_initialize_parameter2, if_print_model, loss_function, optimizer, metrics, learning_rate, epochs, batch_size, ifheatmap, ifweight, ifmute, ifsave, savepath, device) 208 eweight=np.array(eweight,dtype='int32').reshape(-1) 209 g=dgl.graph((start_point,end_point),num_nodes=vx.shape[1]) --> 210 g.edata['w']=eweight 211 #g=dgl.graph((tf.convert_to_tensor(start_point),tf.convert_to_tensor(end_point)),num_nodes=vx.shape[1]) 212 #g.edata['w']=tf.convert_to_tensor(eweight)

D:\anaconda\lib\site-packages\dgl\view.py in setitem(self, key, val) 225 "please pass a tensor directly" 226 ) --> 227 self._graph._set_e_repr(self._etid, self._edges, {key: val}) 228 229 def delitem(self, key):

D:\anaconda\lib\site-packages\dgl\heterograph.py in _set_e_repr(self, etid, edges, data) 4457 " Got %d and %d instead." % (nfeats, num_edges) 4458 ) -> 4459 if F.context(val) != self.device: 4460 raise DGLError( 4461 'Cannot assign edge feature "{}" on device {} to a graph on'

D:\anaconda\lib\site-packages\dgl\backend\tensorflow\tensor.py in context(input) 115 116 def context(input): --> 117 spec = tf.DeviceSpec.from_string(input.device) 118 return "/{}:{}".format(spec.device_type.lower(), spec.device_index) 119

AttributeError: 'numpy.ndarray' object has no attribute 'device'

but if I make start_point,end_point and eweight to tf.Tensor type like this:

g=dgl.graph((tf.convert_to_tensor(start_point),tf.convert_to_tensor(end_point)),num_nodes=vx.shape[1])
        g.edata['w']=tf.convert_to_tensor(eweight)

It will raise a error:

AttributeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_410264\883440279.py in <cell line: 1>() ----> 1 model,predicty,testy,r,p,heatmap,weights=Auto_GCN(vy,vx[:,::2,::2,:],lat1[::2],lon1[::2],[['gc',16],['batchnormalization'],['activation','tanh'],['gc',32],['batchnormalization'],['activation','tanh'],['sortpooling',5],['batchnormalization'],['activation','tanh'],['flatten'],['fc',2],['activation','tanh']],r_number=9,test_size=0.25,ifrandom_split='yes',if_weight_initialize='no',weight_initialize_method='TruncatedNormal',weight_initialize_parameter1=0.00,weight_initialize_parameter2=0.05,if_print_model='yes',loss_function='Pearsonr',optimizer='Adam',metrics='Pearsonr',learning_rate=0.001,epochs=200,batch_size=20,ifheatmap='yes',ifweight='shap',device='gpu')

~\AppData\Local\Temp\ipykernel_410264\3693158481.py in Auto_GCN(vy, vx, lat, lon, model_list, r_number, test_size, task_mode, if_best_mode, modelpath, ifrandom_split, if_weight_initialize, weight_initialize_method, weight_initialize_parameter1, weight_initialize_parameter2, if_print_model, loss_function, optimizer, metrics, learning_rate, epochs, batch_size, ifheatmap, ifweight, ifmute, ifsave, savepath, device) 247 if model_list[i][0] == 'gc': 248 if i==0: --> 249 exec('gc'+str(i+1)+'=GraphConv(trainx.shape[2],model_list[i][1],norm="right")(g,inputs)') 250 else: 251 if model_list[i-1][0]=='gc':

in D:\anaconda\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.__traceback__) ---> 67 raise e.with_traceback(filtered_tb) from None 68 finally: 69 del filtered_tb D:\anaconda\lib\site-packages\tensorflow\python\autograph\impl\api.py in wrapper(*args, **kwargs) 697 except Exception as e: # pylint:disable=broad-except 698 if hasattr(e, 'ag_error_metadata'): --> 699 raise e.ag_error_metadata.to_exception(e) 700 else: 701 raise AttributeError: Exception encountered when calling layer "graph_conv_2" (type GraphConv). in user code: File "D:\anaconda\lib\site-packages\dgl\nn\tensorflow\conv\graphconv.py", line 231, in call * if tf.math.count_nonzero(graph.in_degrees() == 0) > 0: File "D:\anaconda\lib\site-packages\dgl\heterograph.py", line 3670, in in_degrees * v_tensor = utils.prepare_tensor(self, v, "v") File "D:\anaconda\lib\site-packages\dgl\utils\checks.py", line 62, in prepare_tensor * if F.ndim(ret) == 0: File "D:\anaconda\lib\site-packages\dgl\backend\tensorflow\tensor.py", line 113, in ndim * return input.ndim AttributeError: 'Tensor' object has no attribute 'ndim' Call arguments received: • graph=Graph(num_nodes=943, num_edges=8487, ndata_schemes={} edata_schemes={'w': Scheme(shape=(), dtype=tf.float32)}) • feat=tf.Tensor(shape=(None, 943, 12), dtype=float32) • weight=None

It look like contradiction,becase if start_point and end_point and eweight is np.narray type,it will raise error one.And if start_point and end_point and eweight is tf.Tensor type,it will raise error two.You can run any example include my code,and it will raise this error. I dont know the DGL library is support tensorflow or not.

qq492947833 commented 1 year ago

The error two looks like because input is tf.Tensor type,but if I want to use tensorflow.keras.models.Model to create a neural network,I must be use keras.layers.Input as model's input.So how can I add DGL's GraphConv layer to tensorflow model?

github-actions[bot] commented 11 months ago

This issue has been automatically marked as stale due to lack of activity. It will be closed if no further activity occurs. Thank you

frozenbugs commented 11 months ago

As we announced in our slack channel: After DGL 2.0 (ETA: 2023Q4), we'll be prioritizing PyTorch backend due to resource constraints, saying goodbye to MXNet and TensorFlow support. If you're interested in investigating this further and fixing it, feel free to reopen the issue and modify the related DGL-TF code. We're happy to help you get your code merged.

qq492947833 commented 11 months ago

OK,I will use other function in my work,thanks again