Lesson 4 IMDB: can't run the training code

I’m running the notebook for lesson 4, without any changes: https://github.com/fastai/fastai/blob/master/courses/dl1/lesson4-imdb.ipynb

But this code:
learner.fit(3e-3, 4, wds=1e-6, cycle_len=1, cycle_mult=2)
from the section on training the model produces a RuntimeError:

RuntimeError: shape ‘[1000000, 1]’ is invalid for input of size 2000

How can I fix this ?

Apparently there can be some problems related to the torch and fastai versions: Lesson 4 IMDB Test Part Fails

I just used the installation commands on fastai’s github.
Conda says that the following are my installed packages:

fastai 1.0.28 py_1 fastai
pytorch-nightly 1.0.0.dev20181121 py3.6_cuda9.0.176_cudnn7.4.1_0 pytorch
torchvision-nightly 0.2.1 py_0 fastai
torchtext 0.3.1

Yup, it’s the packages.

On the fastai github, you’re instructed to install nightly versions. But that brings torch 1.
Fastai expects torch 0.4

As far as I can see, the correct way of installing fastai is now:
conda install pytorch torchvision -c pytorch
conda install -c fastai
pip install torchtext

This will give you
pytorch 0.4.1
torchvision 0.2.1
torchtext 0.3.1
fastai 1.0.28

I’ll test my setup a bit more (by running more sample code).
But as far as I can see, this is the working combination.

Le me start saying I have the same problems as you: dimensions don’t match.

Fastai v1 expects pytorch v1. The lesson about IMDB is taught with fastai v1 / torch 1.0

Besides, I find the same problem with fastai 0.7, pytorch 0.4. Note that nobody who asked on the forums about that issued received an answer whatsoever.

Really hope somebody can give an answer!

Looks like beginners just got to fend for themselves! Here’s the solution I’ve got: create an additional environment so that both versions of fast.ai and both versions of dl1 can run on the same instance. To switch, I just need to select from Jupyter’s pull-down menu, no need to quit an instance and start another. Step-by-step instructions I’m pasting here:
in case fellow newbies find themselves caught between the two versions: (fast.ai 0.7.x, course/dl1) and (fast.ai 1.0.?, courses-v3/nbs/dl1). Worth the trouble, as there are goodies in the earlier course which cannot be found in the newer one. And, obviously, there’s a video series accompanying the earlier but not the later.

same problem occur when i run:
fit(lr=lr, n_cycle=n_cycle,wds=wd,cycle_len=cycle_len,use_clr=(32,10),cycle_mult=cycle_mult,_save_name = ‘language_model’)

Traceback (most recent call last):
File “E:/Mindtree_IDP/neural_language_model.py”, line 41, in
File “E:\Mindtree_IDP\lang_model_utils.py”, line 244, in train_lang_model
best_save_name = ‘language_model’
File “C:\Users\GK\AppData\Roaming\Python\Python36\site-packages\fastai-0.7.0-py3.6.egg\fastai\learner.py”, line 287, in fit
return self.fit_gen(self.model, self.data, layer_opt, n_cycle, **kwargs)
File “C:\Users\GK\AppData\Roaming\Python\Python36\site-packages\fastai-0.7.0-py3.6.egg\fastai\learner.py”, line 234, in fit_gen
swa_eval_freq=swa_eval_freq, **kwargs)
File “C:\Users\GK\AppData\Roaming\Python\Python36\site-packages\fastai-0.7.0-py3.6.egg\fastai\model.py”, line 132, in fit
loss = model_stepper.step(V(x),V(y), epoch)
File “C:\Users\GK\AppData\Roaming\Python\Python36\site-packages\fastai-0.7.0-py3.6.egg\fastai\model.py”, line 50, in step
output = self.m(*xs)
File “c:\users\gk\appdata\local\continuum\anaconda3\envs\mindtree\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “c:\users\gk\appdata\local\continuum\anaconda3\envs\mindtree\lib\site-packages\torch\nn\modules\container.py”, line 92, in forward
input = module(input)
File “c:\users\gk\appdata\local\continuum\anaconda3\envs\mindtree\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “C:\Users\GK\AppData\Roaming\Python\Python36\site-packages\fastai-0.7.0-py3.6.egg\fastai\lm_rnn.py”, line 97, in forward
raw_output, new_h = rnn(raw_output, self.hidden[l])
File “c:\users\gk\appdata\local\continuum\anaconda3\envs\mindtree\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “C:\Users\GK\AppData\Roaming\Python\Python36\site-packages\fastai-0.7.0-py3.6.egg\fastai\rnn_reg.py”, line 122, in forward
return self.module.forward(*args)
File “c:\users\gk\appdata\local\continuum\anaconda3\envs\mindtree\lib\site-packages\torch\nn\modules\rnn.py”, line 179, in forward
self.dropout, self.training, self.bidirectional, self.batch_first)
RuntimeError: shape ‘[1000000, 1]’ is invalid for input of size 2000