Repository: dmlc/dgl

Issue:
Error when using `dgl.data.TUDataset` in the latest versions
## 🐛 Bug

I get an error when using `dgl.data.TUDataset` in dgl versions `0.5.0` and `0.5.1`. I also get the same error if I build from source. 

## To Reproduce

I've used `ENZYMES` in this example but it seems to be an issue across all the datasets.

Steps to reproduce the behaviour:

1. (Optional) Delete cached data `rm -rf ~/.dgl/ENZYMES ~/.dgl/ENZYMES.zip`
2. Inside a python 3.7 virtualenv `pip install dgl torch torchvision`
3. Import TUDataset `from dgl.data import TUDataset`
4. Try and create an object `TUDataset('ENZYMES')`

The stack trace is as follows

```
(venv) kenlay@Henry-2:~/Dropbox/DPhil/research/graph-attack$ python
Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from dgl.data import TUDataset
Using backend: pytorch
>>> TUDataset('ENZYMES')
/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/utils.py:31: UserWarning: Pandas is not installed, now using numpy.loadtxt to load data, which could be extremely slow. Accelerate by installing pandas
  warnings.warn("Pandas is not installed, now using numpy.loadtxt to load data, "
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/tu.py", line 288, in __init__
    verbose=verbose)
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/dgl_dataset.py", line 284, in __init__
    verbose=verbose)
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/dgl_dataset.py", line 91, in __init__
    self._load()
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/dgl_dataset.py", line 175, in _load
    self.process()
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/tu.py", line 326, in process
    getattr(g, field_name[0])[field_name[1]] = data
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/view.py", line 81, in __setitem__
    self._graph._set_n_repr(self._ntid, self._nodes, {key : val})
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/heterograph.py", line 3753, in _set_n_repr
    if F.context(val) != self.device:
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/backend/pytorch/tensor.py", line 76, in context
    return input.device
AttributeError: 'numpy.ndarray' object has no attribute 'device'
``` 

Note that installing pandas doesn't fix the issue.

## Expected behaviour

Have an object returned: `<dgl.data.tu.TUDataset object at 0x7fddfc527750>`

## Environment

 - DGL Version (e.g., 1.0): 0.5.0, 0.5.1, installed from source
 - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3): Pytorch 1.6.0
 - OS (e.g., Linux): MacOS Catalina 10.15.5
 - How you installed DGL (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source): MacOS instructions given [here](https://docs.dgl.ai/install/index.html#install-from-source)
 - Python version: 3.7.6
 - CUDA/cuDNN version (if applicable): N/A (cpu version)
 - GPU models and configuration (e.g. V100): N/A
 - Any other relevant information: N/A

## Comment

This class works for me if I downgrade to `0.4.3`. Note that if I use the class in `0.4.3` and then try and load the cached data in `0.5.1` I get the same error. 

I can try to dig into the source code to identify the issue if this is a bug that others can reproduce. 

Error when using `dgl.data.TUDataset` in the latest versions
## 🐛 Bug

I get an error when using `dgl.data.TUDataset` in dgl versions `0.5.0` and `0.5.1`. I also get the same error if I build from source. 

## To Reproduce

I've used `ENZYMES` in this example but it seems to be an issue across all the datasets.

Steps to reproduce the behaviour:

1. (Optional) Delete cached data `rm -rf ~/.dgl/ENZYMES ~/.dgl/ENZYMES.zip`
2. Inside a python 3.7 virtualenv `pip install dgl torch torchvision`
3. Import TUDataset `from dgl.data import TUDataset`
4. Try and create an object `TUDataset('ENZYMES')`

The stack trace is as follows

```
(venv) kenlay@Henry-2:~/Dropbox/DPhil/research/graph-attack$ python
Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from dgl.data import TUDataset
Using backend: pytorch
>>> TUDataset('ENZYMES')
/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/utils.py:31: UserWarning: Pandas is not installed, now using numpy.loadtxt to load data, which could be extremely slow. Accelerate by installing pandas
  warnings.warn("Pandas is not installed, now using numpy.loadtxt to load data, "
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/tu.py", line 288, in __init__
    verbose=verbose)
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/dgl_dataset.py", line 284, in __init__
    verbose=verbose)
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/dgl_dataset.py", line 91, in __init__
    self._load()
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/dgl_dataset.py", line 175, in _load
    self.process()
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/data/tu.py", line 326, in process
    getattr(g, field_name[0])[field_name[1]] = data
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/view.py", line 81, in __setitem__
    self._graph._set_n_repr(self._ntid, self._nodes, {key : val})
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/heterograph.py", line 3753, in _set_n_repr
    if F.context(val) != self.device:
  File "/Users/kenlay/Dropbox/DPhil/research/graph-attack/venv/lib/python3.7/site-packages/dgl/backend/pytorch/tensor.py", line 76, in context
    return input.device
AttributeError: 'numpy.ndarray' object has no attribute 'device'
``` 

Note that installing pandas doesn't fix the issue.

## Expected behaviour

Have an object returned: `<dgl.data.tu.TUDataset object at 0x7fddfc527750>`

## Environment

 - DGL Version (e.g., 1.0): 0.5.0, 0.5.1, installed from source
 - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3): Pytorch 1.6.0
 - OS (e.g., Linux): MacOS Catalina 10.15.5
 - How you installed DGL (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source): MacOS instructions given [here](https://docs.dgl.ai/install/index.html#install-from-source)
 - Python version: 3.7.6
 - CUDA/cuDNN version (if applicable): N/A (cpu version)
 - GPU models and configuration (e.g. V100): N/A
 - Any other relevant information: N/A

## Comment

This class works for me if I downgrade to `0.4.3`. Note that if I use the class in `0.4.3` and then try and load the cached data in `0.5.1` I get the same error. 

I can try to dig into the source code to identify the issue if this is a bug that others can reproduce. 



Discussion:

Resolving patch:
diff --git a/python/dgl/data/tu.py b/python/dgl/data/tu.py
--- a/python/dgl/data/tu.py
+++ b/python/dgl/data/tu.py
@@ -323,6 +323,8 @@ def process(self):
                                delimiter=',').astype(int)
                 if 'label' in filename:
                     data = F.tensor(self._idx_from_zero(data))
+                else:
+                    data = F.tensor(data)
                 getattr(g, field_name[0])[field_name[1]] = data
             except IOError:
                 pass


Test patch: