Hanjun-Dai / pytorch_structure2vec

pytorch implementation of structure2vec (https://arxiv.org/abs/1603.05629)
MIT License
305 stars 76 forks source link

graph_classification/main.py fails when options mode="gpu" is provided #5

Closed arshad512 closed 6 years ago

arshad512 commented 6 years ago

Hello, Default mode is set to "cpu". When mode is passed as "gpu" graph_classification/main.py fails as it incorrectly calls scatter method with "torch.FloatTensor". This patch fixes the error by correctly calling scatter method with "torch.cuda.FloatTensor" when "gpu" is set.

Below is the fix.

From 808205995523058acb971d97d4d656e7d5d0c1d3 Mon Sep 17 00:00:00 2001
From: Arshad Hussain <arshad.super@gmail.com>
Date: Wed, 14 Feb 2018 09:32:16 +0000
Subject: [PATCH] graph_classification/main.py fails when options mode="gpu" is
 provided

Default mode is set to "cpu". When mode is passed as "gpu"
graph_classification/main.py fails as it incorrectly calls
scatter method with "torch.FloatTensor". This patch fixes
the error by correctly calling scatter method with
"torch.cuda.FloatTensor" when "gpu" is set.

Signed-off-by: Arshad Hussain <arshad.super@gmail.com>
---
 graph_classification/main.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/graph_classification/main.py b/graph_classification/main.py
index 92bbada..faafbe8 100644
--- a/graph_classification/main.py
+++ b/graph_classification/main.py
@@ -50,8 +50,9 @@ class Classifier(nn.Module):
         node_feat = torch.zeros(n_nodes, cmd_args.feat_dim)
         if cmd_args.mode == 'gpu':
             node_feat = node_feat.cuda()
-
-        node_feat.scatter_(1, concat_feat, 1)
+            node_feat.scatter_(1, concat_feat.cuda(), 1)
+        else:
+            node_feat.scatter_(1, concat_feat, 1)

         return node_feat, labels
-
2.7.4

Error message Without the Fix.

====== begin of s2v configuration ======
| msg_average = 0
======   end of s2v configuration ======
Namespace(batch_size=50, data='NCI109', feat_dim=0, fold=1, gm='mean_field', hidden=64, latent_dim=64, learning_rate=0.001, max_lv=3, mode='gpu', num_class=0, num_epochs=100, out_dim=0, seed=1)
loading data
# classes: 2
# node features: 38
# train: 3715, # test: 412
  0%|                                                                                                                                          | 0/74 [00:00<?, ?batch/s]Traceback (most recent call last):
  File "main.py", line 117, in <module>
    avg_loss = loop_dataset(train_graphs, classifier, train_idxes, optimizer=optimizer)
  File "main.py", line 79, in loop_dataset
    _, loss, acc = classifier(batch_graph)
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 357, in call
    result = self.forward(*input, **kwargs)
  File "main.py", line 61, in forward
    node_feat, labels = self.PrepareFeatureLabel(batch_graph)
  File "main.py", line 54, in PrepareFeatureLabel
    node_feat.scatter_(1, concat_feat, 1)
TypeError: scatter_ received an invalid combination of arguments - got (int, torch.LongTensor, int), but expected one of:
 * (int dim, torch.cuda.LongTensor index, float value)
      didn't match because some of the arguments have invalid types: (int, torch.LongTensor, int)
 * (int dim, torch.cuda.LongTensor index, torch.cuda.FloatTensor src)
      didn't match because some of the arguments have invalid types: (int, torch.LongTensor, int)
Hanjun-Dai commented 6 years ago

Thank you very much for pointing out this!

I've just fixed this.