"ImportError: cannot import name 'BatchSampler'"

Hello guys,
anybody else using Ubuntu on Windows? In my laptop I’m using arch linux and everything is working fine. However, since the laptop has no GPU I tried to configure jupyter notebook with all the libraries necessary, in Ubuntu for Windows 10, but I wasn’t able to run the notebook from the first lesson.

The error I’m getting is “ImportError: cannot import name ‘BatchSampler’” – I have searched on google/on the forums, but can’t find anybody with the same problem. Maybe I have a wrong library version installed? If someone has had the same issue, can you give me some tip?

Below is a screenshot of the traceback in jupyter notebook.

Thanks in advance!!

1 Like

Did you check if you have the following source code somewhere in your local libraries? http://pytorch.org/docs/master/_modules/torch/utils/data/sampler.html
I had issues installing Pytorch env on my ubuntu Win10 laptop, and decided against it because of no GPU support.

1 Like

Thank you for your response! I had no idea that it would have no GPU support… In that case, it makes no sense to configure it in my windows desktop. I’m going to try to setup AWS using the free credits.

Thanks again! :wink:

Can you please elaborate, I didn’t understand. I’ve same problem in my machine. I do have Nvidia GeForce 820M GPU. In python3, I can’t run “from torch.utils.data.sampler import BatchSampler”.
Thanks in advance.

Same thing happened for me today, running native Ubuntu 16.04 / Anaconda on my own desktop. I never understood why, so I just copied the code from

I suspect this has to do with pytorch versions. Installing through conda gave me version 0.1.12_1, so I’ll try installing the current version 0.3 from source. Not only because of this, but I had other problems when I tried running lesson1 on my desktop - memory filled up and crashed.

1 Like

I just ran into this. I had started with the correct version of pytorch 0.3 installed using “conda install pytorch -c pytorch”. In the course of installing some other files to get past errors, pytorch 0.1.12 was installed I believe as a dependency(?) which doesn’t include BatchSampler. Running “conda install pytorch -c pytorch” installed 0.3 again and I was able to run.

2 Likes

Using
conda install -y --name fastai$PYTHON_VERSION -c soumith pytorch=0.3.0
soloved my problem where fastai is your conda env.

I copied https://github.com/pytorch/pytorch/blob/master/torch/_six.py and https://github.com/pytorch/pytorch/blob/master/torch/utils/data/sampler.py and it worked. My os was debian without GPU.

I am also having this problem in Paperspace. Any ideas would be greatly appreciated. I’ve updated my conda and did a git pull

3 Likes

I also have the same problem with a newly created Paperspace machine (after running “git pull” and “conda env update”).

(fastai) paperspace@psetuga1v:~/fastai$ python -c “from fastai import *”
Traceback (most recent call last):
File “<string>”, line 1, in <module>
File “/home/paperspace/fastai/fastai/init.py”, line 1, in <module> from .basic_train import *
File “/home/paperspace/fastai/fastai/basic_train.py”, line 2, in <module> from .torch_core import *
File “/home/paperspace/fastai/fastai/torch_core.py”, line 2, in <module> from .imports.torch import *
File “/home/paperspace/fastai/fastai/imports/torch.py”, line 4, in <module> from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset
ImportError: cannot import name 'BatchSampler

Ran into the same error. I think it’s got to do with the move to the rewritten fastai library.
What fixed it for me was just reverting to an earlier, working commit by issuing:
git checkout 2e1ccb58121dc648751e2109fc0fbf6925aa8887

That puts the repo back to an earlier stage which gets everything working for me again. Note that this isn’t a resilient fix: if you intend to work on the fastai library code, this definitely wouldn’t be the way to go. Then again, if that’s the case, you probably know enough to fix the underlying root cause.

2 Likes

I reset the symbolic link to point to …/fastai/old/fastai/ (old library). Then, it works again. I also have the latest library for the upcoming course.

3 Likes

I am using Google colab for setting up Fastai, how can i resolve this issue in colab…

You can pip install from github repository, choosing your preferred branch or commit.

!pip install git+GITHUBADDRESS@NAMEOFTHEBRANCH

or

!pip install git+GITHUBADDRESS@COMMIT

So for example, in Laurenth case it should be

!pip install git+https://github.com/fastai/fastai@2e1ccb58121dc648751e2109fc0fbf6925aa8887

Personally I used the following, and solved that error, but a new one raised

!pip install git+https://github.com/fastai/fastai@ulmfit_cleanup

It returned:

AttributeError: module 'torch.nn' has no attribute 'functional'

but that’s another story…

Thank you,

It seems to be the best option for now as they changed a lot to the fastai repo to use pytorch 0.4.1 features, but the setup process still using 0.3.1. Installing 0.4.1 is an option but in the future who knows what might break.

1 Like

@jakelong Can you please help me in installing fastai library. I have followed all the steps mentioned in the forum

But when I import fastai module I am getting the error.

Thanks,
Ritika

It seems like you already had your fastai conda env set up. In the step 8 of the howto you can see the instruction as:
cd courses\dl1
del fastai
mklink /d fastai ..\..\fastai
cd ..\..

If you already did the above things, open an anaconda prompt, cd into fastai folder, activate fastai env and run these
cd courses\dl1
rmdir fastai
mklink /d fastai ..\..\old\fastai
cd ..\..

Don’t try to import fastai from python started in the command line, run jupyter notebook and open lession1.ipynb in courses/dl1 and run the first 6 blocks of code. It should run without problem and torch.cuda.is_available() and torch.backends.cudnn.enabled should both return True.

Hope this help

Hoping to be helpful here I shared a Google Colab notebook ready to go, in order to make fast.ai course ready to go to anyone.
It has comments about common issues I solved too.
You are free to comment or fork it and let me/us know if/how you’ll improve it.

Hi Jake,

Thank you for the quick response. I tried to followed the instructions shared by you.
I got the error.Please suggest how to solve this error.


Thanks,
Ritika