pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.01k stars 3.62k forks source link

I use benchmark in kernel(graph pooling),but report a bug #1397

Closed jiaruHithub closed 4 years ago

jiaruHithub commented 4 years ago

🐛 Bug

-----
FRANKENSTEIN - SAGPool
Traceback (most recent call last):
  File "main.py", line 65, in <module>      
    model = Net(dataset, num_layers, hidden)
  File "D:\muqian\code\pytorch_geometric\benchmark\kernel\sag_pool.py", line 11, in __init__
    self.conv1 = GraphConv(dataset.num_features, hidden, aggr='mean')
  File "D:\anna\Anaconda\envs\torch1.4.0\lib\site-packages\torch_geometric\nn\conv\graph_conv.py", line 37, in __init__
    self.lin = torch.nn.Linear(in_channels, out_channels, bias=bias)
  File "D:\anna\Anaconda\envs\torch1.4.0\lib\site-packages\torch\nn\modules\linear.py", line 77, in __init__
    self.reset_parameters()
  File "D:\anna\Anaconda\envs\torch1.4.0\lib\site-packages\torch\nn\modules\linear.py", line 80, in reset_parameters
    init.kaiming_uniform_(self.weight, a=math.sqrt(5))
  File "D:\anna\Anaconda\envs\torch1.4.0\lib\site-packages\torch\nn\init.py", line 312, in kaiming_uniform_
    std = gain / math.sqrt(fan)
ZeroDivisionError: float division by zero

To Reproduce

Steps to reproduce the behavior:

my code in main.pyis

from itertools import product

import argparse
from datasets import get_dataset
from train_eval import cross_validation_with_val_set

from gcn import GCN, GCNWithJK
from graph_sage import GraphSAGE, GraphSAGEWithJK
from gin import GIN0, GIN0WithJK, GIN, GINWithJK
from graclus import Graclus
from top_k import TopK
from sag_pool import SAGPool
from diff_pool import DiffPool
from edge_pool import EdgePool
from global_attention import GlobalAttentionNet
from set2set import Set2SetNet
from sort_pool import SortPool
from asap import ASAP

parser = argparse.ArgumentParser()
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--lr', type=float, default=0.01)
parser.add_argument('--lr_decay_factor', type=float, default=0.5)
parser.add_argument('--lr_decay_step_size', type=int, default=50)
args = parser.parse_args()

layers = [3]#[1, 2, 3, 4, 5]
hiddens = [128] #[16, 32, 64, 128]
datasets = ['FRANKENSTEIN']
nets = [SAGPool]

def logger(info):
    fold, epoch = info['fold'] + 1, info['epoch']
    val_loss, test_acc = info['val_loss'], info['test_acc']
    print('{:02d}/{:03d}: Val Loss: {:.4f}, Test Accuracy: {:.3f}'.format(
        fold, epoch, val_loss, test_acc))

results = []
for dataset_name, Net in product(datasets, nets):
    best_result = (float('inf'), 0, 0)  # (loss, acc, std)
    print('-----\n{} - {}'.format(dataset_name, Net.__name__))
    for num_layers, hidden in product(layers, hiddens):
        dataset = get_dataset(dataset_name, sparse=Net != DiffPool)
        model = Net(dataset, num_layers, hidden)
        loss, acc, std = cross_validation_with_val_set(
            dataset,
            model,
            folds=10,
            epochs=args.epochs,
            batch_size=args.batch_size,
            lr=args.lr,
            lr_decay_factor=args.lr_decay_factor,
            lr_decay_step_size=args.lr_decay_step_size,
            weight_decay=0,
            logger=logger,
        )
        if loss < best_result[0]:
            best_result = (loss, acc, std)

    desc = '{:.3f} ± {:.3f}'.format(best_result[1], best_result[2])
    print('Best result - {}'.format(desc))
    results += ['{} - {}: {}'.format(dataset_name, model, desc)]
print('-----\n{}'.format('\n'.join(results)))

Expected behavior

Environment

Additional context

Why it reported this bug?and how can I fix this issue? Thank you!

rusty1s commented 4 years ago

You need to add node features to your input graphs, e.g., via transforms.OneHotDegree.

jiaruHithub commented 4 years ago

transforms.OneHotDegree

I'm sorry,which line should I add this code?

rusty1s commented 4 years ago

Oh, that should be automatically done, see here. Can you confirm that node features exist?

jiaruHithub commented 4 years ago

Oh, that should be automatically done, see here. Can you confirm that node features exist?

I think it exist.this dataset is widely used in graph pooling.and I use TUDdataset to donwload it.it is so wired!( 0 x 0 ) I use this in my own code(not report this bug ),it accuracy never change,So I want to use benchmak to test then it report this bug.

rusty1s commented 4 years ago

Can you change this line to TUDataset(path, name, use_node_attr=True)?

jiaruHithub commented 4 years ago

Can you change this line to TUDataset(path, name, use_node_attr=True)?

It's working! Could you simply state why? Thank you!