Search code examples
pythonimportpytorchpytorch-geometric

ImportError: cannot import name 'dropout_edge' from 'torch_geometric.utils'


I am doing some preprocessing of a dataset with torch_geometric. The error occurred when trying to run the following lines where OneStepDataset is a subclass of the torch_geometric.data.Dataset class

dataset_sample = OneStepDataset(OUTPUT_DIR, "valid", return_pos=True)
graph, position = dataset_sample[0]

The compiler traces to these lines in C:\...\AppData\Local\Programs\Python\Python39\lib\site-packages\torch_geometric\data\dataset.py:197

 if (isinstance(idx, (int, np.integer))
    194         or (isinstance(idx, Tensor) and idx.dim() == 0)
    195         or (isinstance(idx, np.ndarray) and np.isscalar(idx))):
--> 197     data = self.get(self.indices()[idx])
    198     data = data if self.transform is None else self.transform(data)
    199     return data

but it doesn't show the root line that caused this error

ImportError: cannot import name 'dropout_edge' from 'torch_geometric.utils' (c:\Users\...\AppData\Local\Programs\Python\Python39\lib\site-packages\torch_geometric\utils\__init__.py)

Solution

  • What's going on?

    You mentioned in the comment that you use PyTorch Geometric in version 2.2.0 – the error you posted, however, is from a different one. You can identify by going to GitHub source with tag 2.2.0 – the line of the error is 239. The last version, in which the error was in line 197 is 2.1.0 (see GitHub source with tag 2.1.0). This version does not include the dropout_edge in torch_geometric.utils, as opposed to the 2.2.0, which does.

    What does it mean?

    Apparently, there is some kind of package version mismatch. It could have happened because of installing and re-installing in different ways (e.g., through pip and conda at the same time) or even tampering with the library files manually. To know for sure, one would need to know a little more about your environment (e.g., an exact list of packages with their respective versions), the installation process and all the steps since then. It's probably not worth it, though.

    Suggestions?

    A quick way would be to simply reinstall the package. That being said, if you have a problem of this nature, something is not right with the installation process, and it might not necessarily help.

    My suggestion would be to try to use a fresh environment, install packages one-by-one (ideally, using the same package manager) and see if the issue persists. I see that you have the library install globally – next time try to use virtual environments to mitigate such issues, or be able to recover quickly. You can find more on this topic in the Python documentation.